d60cf05376230f4be992cf9d097f3a7cd7f925d7
[tests.git] / system / TestMapper.py
1 #
2 # Thierry Parmentelat - INRIA Sophia Antipolis 
3 #
4 # mapper class
5
6 # this works on a spec as defined in a config file
7 # and allows to remap various fields, typically to another testbox 
8 # see an example in config_onelab_testbox32.py
9
10
11 import utils
12
13 class TestMapper:
14
15     def __init__ (self,plcs,options):
16         self.plcs=plcs
17         self.options=options
18
19     @staticmethod
20     def plc_name (plc):
21         return plc['name']
22
23     @staticmethod
24     def node_name (node):
25         return node['node_fields']['hostname']
26
27     def apply_first_map (self, type, name, obj, maplist):
28         for (map_pattern,rename_dict) in maplist:
29             if utils.match (name,map_pattern):
30                 utils.header("TestMapper/%s : applying rules '%s' on %s"%(type,map_pattern,name))
31                 for (k,v) in rename_dict.iteritems():
32                     # apply : separator
33                     path=k.split(':')
34                     # step down but last step in path
35                     o=obj
36                     for step in path[:-1]:
37                         if not o.has_key(step):
38                             o[step]={}
39                             utils.header ("WARNING : created step %s in path %s on %s %s"%(
40                                     step,path,type,name))
41                         o=o[step]
42                     # last step is the one for side-effect
43                     step=path[-1]
44                     if not o.has_key(step):
45                         utils.header ("WARNING : inserting key %s for path %s on %s %s"%(
46                                 step,path,type,name))
47                     # apply formatting if '%s' found in the value
48                     if v.find('%s')>=0:
49                         v=v%obj[k]
50                     print("TestMapper, rewriting %s: %s into %s"%(name,k,v))
51                     o[step]=v
52                 # only apply first rule
53                 return
54
55     def node_names (self):
56         result=[]
57         for plc in self.plcs:
58             for site in plc['sites']:
59                 for node in site['nodes']:
60                     result.append(node['node_fields']['hostname'])
61         return result
62
63     def map (self,mapper):
64
65         try:
66             plc_maps = mapper['plc']
67         except:
68             plc_maps = []
69         try:
70             node_maps = mapper['node']
71         except:
72             node_maps = []
73
74         for plc in self.plcs:
75             name=TestMapper.plc_name(plc)
76             self.apply_first_map ('plc',name,plc,plc_maps)
77
78             for site in plc['sites']:
79                 for node in site['nodes']:
80                     nodename = TestMapper.node_name(node)
81                     self.apply_first_map('node',nodename,node,node_maps)
82
83         return self.plcs