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