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