X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=system%2FTestMapper.py;h=f07ed1b0b3674bc941266b0c49907daaefa03ace;hb=457d31694f7b0f60e2a6fea230e9a3572b9d2b78;hp=f7933bad7430b9e9aa84368626563f0a23812642;hpb=eb4d7e9f9e7f77d6f2e49aab70dd715a43708230;p=tests.git diff --git a/system/TestMapper.py b/system/TestMapper.py index f7933ba..f07ed1b 100644 --- a/system/TestMapper.py +++ b/system/TestMapper.py @@ -1,83 +1,87 @@ # -# Thierry Parmentelat - INRIA Sophia Antipolis +# Thierry Parmentelat +# Copyright (C) 2010 INRIA +# # # mapper class # # this works on a spec as defined in a config file -# and allows to remap various fields, typically to another testbox -# see an example in config_onelab_testbox32.py +# and allows to remap various fields on the local substrate # import utils class TestMapper: - def __init__ (self,plcs,options): - self.plcs=plcs - self.options=options + def __init__(self,plcs,options): + self.plcs = plcs + self.options = options @staticmethod - def plc_name (plc): + def plc_name(plc): return plc['name'] @staticmethod - def node_name (node): - return node['node_fields']['hostname'] + def node_name(node): + return node['name'] + + def node_names(self): + result = [] + for plc in self.plcs: + for site in plc['sites']: + for node in site['nodes']: + result.append(node['name']) + return result - def apply_first_map (self, type, name, obj, maplist): + def apply_first_map(self, type, name, obj, maplist): for (map_pattern,rename_dict) in maplist: if utils.match (name,map_pattern): - utils.header("TestMapper/%s : applying rules '%s' on %s"%(type,map_pattern,name)) - for (k,v) in rename_dict.iteritems(): + if self.options.verbose: + utils.header("TestMapper/{} : applying rules '{}' on {}"\ + .format(type, map_pattern, name)) + for (k,v) in rename_dict.items(): # apply : separator - path=k.split(':') + path = k.split(':') # step down but last step in path - o=obj + o = obj for step in path[:-1]: - if not o.has_key(step): - o[step]={} - utils.header ("WARNING : created step %s in path %s on %s %s"%( - step,path,type,name)) - o=o[step] + if step not in o: + o[step] = {} + if self.options.verbose: + utils.header ("WARNING : created step {} in path {} on {} {}"\ + .format(step,path,type,name)) + o = o[step] # last step is the one for side-effect - step=path[-1] - if not o.has_key(step): - utils.header ("WARNING : inserting key %s for path %s on %s %s"%( - step,path,type,name)) + step = path[-1] + if self.options.verbose: + if step not in o: + utils.header ("WARNING : inserting key {} for path {} on {} {}"\ + .format(step, path, type, name)) # apply formatting if '%s' found in the value - if v.find('%s')>=0: - v=v%obj[k] - utils.header("TestMapper, rewriting %s: %s into %s"%(name,k,v)) - o[step]=v + if v is None: + if self.options.verbose: print("TestMapper WARNING - None value - ignored, key=",k) + continue + if v.find('%s') >= 0: + v = v % obj[k] + if self.options.verbose: + print(("TestMapper, rewriting {}: {} into {}"\ + .format(name, k, v))) + o[step] = v # only apply first rule return - def node_names (self): - result=[] - for plc in self.plcs: - for site in plc['sites']: - for node in site['nodes']: - result.append(node['node_fields']['hostname']) - return result - - def map (self,mapper): + def map(self, mapper): - try: - plc_maps = mapper['plc'] - except: - plc_maps = [] - try: - node_maps = mapper['node'] - except: - node_maps = [] + plc_maps = mapper.get('plc',[]) + node_maps = mapper.get('node',[]) for plc in self.plcs: - name=TestMapper.plc_name(plc) - self.apply_first_map ('plc',name,plc,plc_maps) + name = TestMapper.plc_name(plc) + self.apply_first_map('plc', name, plc, plc_maps) for site in plc['sites']: for node in site['nodes']: nodename = TestMapper.node_name(node) - self.apply_first_map('node',nodename,node,node_maps) + self.apply_first_map('node', nodename, node, node_maps) return self.plcs