87ec6c481c36587b4572aeafbb3fb4881fb03f0c
[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 re
12 import utils
13
14 class TestMapper:
15
16     def __init__ (self,plcs,options):
17         self.plcs=plcs
18         self.options=options
19
20     @staticmethod
21     def match (name,key):
22         key=key.replace("*",".*")
23         return re.compile(key).match(name)
24
25     @staticmethod
26     def plc_name (plc):
27         return plc['name']
28
29     @staticmethod
30     def node_name (node):
31         return node['node_fields']['hostname']
32
33     def apply_first_map (self, type, name, obj, maplist):
34         for (map_pattern,rename_dict) in maplist:
35             if TestMapper.match (name,map_pattern):
36                 utils.header("TestMapper/%s : applying match key %s on plc %s"%(type,map_pattern,name))
37                 for (k,v) in rename_dict.iteritems():
38                     # apply : separator
39                     path=k.split(':')
40                     # step down but last step in path
41                     o=obj
42                     for step in path[:-1]:
43                         if not o.has_key(step):
44                             o[step]={}
45                             utils.header ("WARNING : created step %s in path %s on %s %s"%(
46                                     step,path,type,name))
47                         o=o[step]
48                     # last step is the one for side-effect
49                     step=path[-1]
50                     if not o.has_key(step):
51                         utils.header ("WARNING : inserting key %s for path %s on %s %s"%(
52                                 step,path,type,name))
53                     # apply formatting if '%s' found in the value
54                     if v.find('%s')>=0:
55                         v=v%obj[k]
56                     if self.options.verbose:
57                         utils.header("mapping %s->%s towards %s"%(name,k,v))
58                     o[step]=v
59                 # only apply first rule
60                 return
61
62     def node_names (self):
63         result=[]
64         for plc in self.plcs:
65             for site in plc['sites']:
66                 for node in site['nodes']:
67                     result.append(node['node_fields']['hostname'])
68         return result
69
70     def map (self,mapper):
71
72         try:
73             plc_maps = mapper['plc']
74         except:
75             plc_maps = []
76         try:
77             node_maps = mapper['node']
78         except:
79             node_maps = []
80
81         for plc in self.plcs:
82             name=TestMapper.plc_name(plc)
83             self.apply_first_map ('plc',name,plc,plc_maps)
84
85             for site in plc['sites']:
86                 for node in site['nodes']:
87                     nodename = TestMapper.node_name(node)
88                     self.apply_first_map('node',nodename,node,node_maps)
89
90         return self.plcs