Revision 0b1c84fc gen-rspec.py

View differences:

gen-rspec.py
1 1
#!/usr/bin/env python
2 2
import xml.etree.ElementTree as ET
3

  
3 4
try:
4 5
    from StringIO import StringIO
5 6
except ImportError:
......
8 9
from argparse import ArgumentParser
9 10
import sys
10 11

  
12
from const import AVAILABLE, COMPONENT_ID, COMPONENT_NAME, FOOTER_TEMPLATE, \
13
    HARDWARE_TYPE, HEADER_TEMPLATE, LOCATION, NAME, NODE, NODE_TEMPLATE, NOW, \
14
    TRUE, X, Y, Z
15

  
11 16

  
12 17
def matches(name, filters, nodes):
13 18
    if len(nodes) > 0:
......
30 35
    return False
31 36

  
32 37

  
38
def fetch_nodes(testbed, n, use_hardware, filters, nodes, hardware_types,
39
                dump_file, df):
40
    omni = ["omni", "-V3", "listresources", "-a", testbed, "--error",
41
            "--tostdout"]
42

  
43
    xml = check_output(omni)
44

  
45
    # parse the xml file removing the namespaces from tag names
46
    xml_file = StringIO(xml.decode('utf-8'))
47
    it = ET.iterparse(xml_file)
48
    for _, el in it:
49
        el.tag = el.tag.split('}', 1)[1]  # strip all namespaces
50
    e = it.root
51

  
52
    t = __import__('%stemplates' % testbed, globals(), locals(),
53
                   [NODE_TEMPLATE, HEADER_TEMPLATE, FOOTER_TEMPLATE])
54
    h = 40
55
    w = 120
56
    xs = 100
57
    ys = 100
58

  
59
    xml = ""
60

  
61
    for node in e.findall(NODE):
62
        name = node.get(COMPONENT_NAME)
63
        available = node.find(AVAILABLE).get(NOW).lower() == TRUE
64
        has_position = X in node.find(LOCATION).attrib
65
        component_id = node.get(COMPONENT_ID)
66
        if has_position:
67
            x = float(node.find(LOCATION).get(X))
68
            y = float(node.find(LOCATION).get(Y))
69
            z = float(node.find(LOCATION).get(Z))
70
        else:
71
            x = 0
72
            y = 0
73
            z = 0
74
        row = (n - 1) / 8
75
        col = (n - 1) % 8
76
        good_node = False
77
        if use_hardware:
78
            hardware_type_nodes = node.findall(HARDWARE_TYPE)
79
            hardware_types = [ht.get(NAME) for ht in hardware_type_nodes]
80
            if available and matches_hardware(hardware, hardware_types):
81
                good_node = True
82
        else:
83
            if available and matches(name, filters, nodes):
84
                good_node = True
85

  
86
        if good_node:
87
            domain = component_id.split("+")[1]
88
            hostname = "{}.{}".format(name, domain)
89
            xml += t.node_template % \
90
                   (name, component_id, xs + col * w, ys + row * h)
91
            if dump_file != "":
92
                df.write("%s\n" % name)
93
            n += 1
94
    return n, xml
95

  
96

  
33 97
parser = ArgumentParser()
34 98
parser.add_argument("-t", "--testbed", dest="testbed",
35
                    default="twist", action="store", metavar="TESTBED",
36
                    help="Testbed to use [default: %(default)s]")
99
                    default="wall1", action="store", metavar="TESTBED",
100
                    help="Comma separated list of testbeds to use [default: "
101
                         "%(default)s]")
37 102
parser.add_argument("-f", "--filter", dest="filter",
38 103
                    default="", action="store", metavar="List of filters",
39 104
                    help="Comma separated list of node prefixes [default: %("
......
57 122
    print("Cannot use the --filter and the --hardware options together")
58 123
    sys.exit(1)
59 124

  
60
testbed = args.testbed
125
testbeds = args.testbed.split(",")
61 126
use_hardware = False
62 127
if args.filter:
63 128
    filters = args.filter.split(",")
......
77 142

  
78 143
dump_file = args.dump
79 144

  
80
omni = ["omni", "-V3", "listresources", "-a", testbed, "--error", "--tostdout"]
81

  
82
xml = check_output(omni)
83

  
84
# parse the xml file removing the namespaces from tag names
85
xml_file = StringIO(xml)
86
it = ET.iterparse(xml_file)
87
for _, el in it:
88
    el.tag = el.tag.split('}', 1)[1]  # strip all namespaces
89
e = it.root
90

  
91

  
92
t = __import__('%stemplates' % testbed, globals(), locals(),
93
               ['node_template', 'header_template', 'footer_template'])
94

  
95
h = 40
96
w = 120
97
xs = 100
98
ys = 100
145
t = __import__('%stemplates' % testbeds[0], globals(), locals(),
146
               [NODE_TEMPLATE, HEADER_TEMPLATE, FOOTER_TEMPLATE])
99 147

  
148
df = None
100 149
if dump_file != "":
101 150
    df = open(dump_file, "w")
102 151

  
103
n = 0
104 152
print(t.header_template)
105
for node in e.findall('node'):
106
    name = node.get('component_name')
107
    available = node.find('available').get('now').lower() == "true"
108
    has_position = 'x' in node.find('location').attrib
109
    component_id = node.get('component_id')
110
    if has_position:
111
        x = float(node.find('location').get('x'))
112
        y = float(node.find('location').get('y'))
113
        z = float(node.find('location').get('z'))
114
    else:
115
        x = 0
116
        y = 0
117
        z = 0
118
    row = (n - 1) / 8
119
    col = (n - 1) % 8
120
    good_node = False
121
    if use_hardware:
122
        hardware_type_nodes = node.findall("hardware_type")
123
        hardware_types = [ht.get('name') for ht in hardware_type_nodes]
124
        if available and matches_hardware(hardware, hardware_types):
125
            good_node = True
126
    else:
127
        if available and matches(name, filters, nodes):
128
            good_node = True
129

  
130
    if good_node:
131
        print(t.node_template %
132
              (name, component_id, xs + col * w, ys + row * h))
133
        if dump_file != "":
134
            df.write("%s\n" % name)
135
        n += 1
136
    # sys.stdout.write("Node: %s (%savailable) (%s)" %
137
    #                  (name, "" if available else "not", component_id))
138
    # if has_position:
139
    #     sys.stdout.write(" Position x=%f, y=%f, z=%f" % (x, y, z))
140
    # sys.stdout.write("\n")
153

  
154
n = 0
155
for testbed in testbeds:
156
    n, xml = fetch_nodes(testbed, n, use_hardware, filters, nodes, hardware,
157
                         dump_file, df)
158
    print(xml)
159

  
141 160
print(t.footer_template)
142 161

  
143 162
if dump_file != "":

Also available in: Unified diff