13a07806f920275e8348e74389f2cd59dac5ddb4
[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 self.options.verbose:
30                 print 'dbg: TestMapper/',type,'name=',name,'& pattern=',map_pattern
31             if utils.match (name,map_pattern):
32                 utils.header("TestMapper/%s : applying rules '%s' on %s"%(type,map_pattern,name))
33                 for (k,v) in rename_dict.iteritems():
34                     # apply : separator
35                     path=k.split(':')
36                     # step down but last step in path
37                     o=obj
38                     for step in path[:-1]:
39                         if not o.has_key(step):
40                             o[step]={}
41                             utils.header ("WARNING : created step %s in path %s on %s %s"%(
42                                     step,path,type,name))
43                         o=o[step]
44                     # last step is the one for side-effect
45                     step=path[-1]
46                     if not o.has_key(step):
47                         utils.header ("WARNING : inserting key %s for path %s on %s %s"%(
48                                 step,path,type,name))
49                     # apply formatting if '%s' found in the value
50                     if v.find('%s')>=0:
51                         v=v%obj[k]
52                     print("TestMapper, rewriting %s: %s into %s"%(name,k,v))
53                     o[step]=v
54                 # only apply first rule
55                 return
56
57     def node_names (self):
58         result=[]
59         for plc in self.plcs:
60             for site in plc['sites']:
61                 for node in site['nodes']:
62                     result.append(node['node_fields']['hostname'])
63         return result
64
65     def map (self,mapper):
66
67         plc_maps  = mapper.get('plc',[])
68         node_maps = mapper.get('node',[])
69
70         for plc in self.plcs:
71             name=TestMapper.plc_name(plc)
72             self.apply_first_map ('plc',name,plc,plc_maps)
73
74             for site in plc['sites']:
75                 for node in site['nodes']:
76                     nodename = TestMapper.node_name(node)
77                     self.apply_first_map('node',nodename,node,node_maps)
78
79         return self.plcs