Statistics
| Branch: | Revision:

iof-tools / gen-rspec.py @ 2141d5e7

History | View | Annotate | Download (4.31 KB)

1
#!/usr/bin/env python
2
import xml.etree.ElementTree as ET
3
try:
4
    from StringIO import StringIO
5
except ImportError:
6
    from io import StringIO
7
from subprocess import check_output
8
from argparse import ArgumentParser
9
import sys
10

    
11

    
12
def matches(name, filters, nodes):
13
    if len(nodes) > 0:
14
        return name in nodes
15
    if len(filters) == 0:
16
        return True
17
    for f in filters:
18
        if name.startswith(f):
19
            return True
20
    return False
21

    
22

    
23
def matches_hardware(hardware_filter, hardware_types):
24
    if len(hardware_filter) == 0:
25
        return True
26
    for f in hardware_filter:
27
        for h in hardware_types:
28
            if h.startswith(f):
29
                return True
30
    return False
31

    
32

    
33
parser = ArgumentParser()
34
parser.add_argument("-t", "--testbed", dest="testbed",
35
                    default="twist", action="store", metavar="TESTBED",
36
                    help="Testbed to use [default: %(default)s]")
37
parser.add_argument("-f", "--filter", dest="filter",
38
                    default="", action="store", metavar="List of filters",
39
                    help="Comma separated list of node prefixes [default: %("
40
                         "default)s]")
41
parser.add_argument("-w", "--hardware", dest="hardware",
42
                    default="", action="store", metavar="List of hardware",
43
                    help="Comma separated list of hardware types [default: %("
44
                         "default)s]")
45
parser.add_argument("-n", "--nodes", dest="nodes",
46
                    default="", action="store", metavar="List of nodes",
47
                    help="Comma separated list of nodes [default: %("
48
                         "default)s] This argument has precedence over "
49
                         "--filter")
50
parser.add_argument("-d", "--dump", dest="dump",
51
                    default="", action="store", metavar="FILENAME",
52
                    help="Output file where to store the list of available "
53
                         "nodes. If not specified, no file will be written")
54
args = parser.parse_args()
55

    
56
if args.filter and args.hardware:
57
    print("Cannot use the --filter and the --hardware options together")
58
    sys.exit(1)
59

    
60
testbed = args.testbed
61
use_hardware = False
62
if args.filter:
63
    filters = args.filter.split(",")
64
else:
65
    filters = []
66

    
67
if args.nodes:
68
    nodes = args.nodes.split(",")
69
else:
70
    nodes = []
71

    
72
if args.hardware:
73
    use_hardware = True
74
    hardware = args.hardware.split(",")
75
else:
76
    hardware = []
77

    
78
dump_file = args.dump
79

    
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
99

    
100
if dump_file != "":
101
    df = open(dump_file, "w")
102

    
103
n = 0
104
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")
141
print(t.footer_template)
142

    
143
if dump_file != "":
144
    df.close()