def make_all(exp_file, **kwargs): """ Make a campaign of experiments. :param exp_file: experiments JSON filename or basename (absolute or relative path ; if no path provided, the JSON file is searched in the experiments folder) """ global reuse_bin_path console = kwargs.get('console') clean_all(exp_file, silent=True) if console is None else console.do_clean_all(exp_file, silent=True) experiments = get_experiments(exp_file) sim_json, motes = None, None # if a simulation named 'BASE' is present, use it as a template simulation for all the other simulations if 'BASE' in experiments.keys(): experiments['BASE']['silent'] = True sim_json = dict(experiments['BASE']['simulation']) motes = generate_motes(defaults=DEFAULTS, **validated_parameters(experiments['BASE'])) del experiments['BASE'] for name, params in sorted(experiments.items(), key=lambda x: x[0]): params['campaign'] = splitext(basename(exp_file))[0] if sim_json is not None: params.setdefault('simulation', {}) for k, v in sim_json.items(): if k not in params['simulation'].keys(): params['simulation'][k] = v params['motes'] = motes make(name, ask=False, **params) if console is None else console.do_make(name, ask=False, **params)
def make_all(exp_file, **kwargs): """ Make a campaign of experiments. :param exp_file: experiments JSON filename or basename (absolute or relative path ; if no path provided, the JSON file is searched in the experiments folder) """ global reuse_bin_path console = kwargs.get('console') clean_all(exp_file, silent=True) if console is None else console.do_clean_all( exp_file, silent=True) experiments = get_experiments(exp_file, silent=True) sim_json, motes = None, None # if a simulation named 'BASE' is present, use it as a template simulation for all the other simulations if 'BASE' in experiments.keys(): experiments['BASE']['silent'] = True sim_json = dict(experiments['BASE']['simulation']) motes = generate_motes(defaults=DEFAULTS, **validated_parameters(experiments['BASE'])) del experiments['BASE'] for name, params in sorted(experiments.items(), key=lambda x: x[0]): params['campaign'] = splitext(basename(exp_file))[0] if sim_json is not None: params.setdefault('simulation', {}) for k, v in sim_json.items(): if k not in params['simulation'].keys(): params['simulation'][k] = v params['motes'] = motes make(name, ask=False, ** params) if console is None else console.do_make( name, ask=False, **params)
def __make(name, ask=True, **kwargs): """ Make a new experiment. :param name: experiment name (or path to the experiment, if expanded in the 'command' decorator) :param ask: ask confirmation :param path: expanded path of the experiment (dynamically filled in through 'command' decorator with 'expand' :param kwargs: simulation keyword arguments (see the documentation for more information) """ global reuse_bin_path path = kwargs['path'] logger.debug(" > Validating parameters...") params = validated_parameters(kwargs) ext_lib = params.get("ext_lib") if ext_lib and not exists(ext_lib): logger.error("External library does not exist !") logger.critical("Make aborded.") return False logger.debug(" > Creating simulation...") # create experiment's directories check_structure(path, create=True, remove=True) templates = get_path(path, 'templates', create=True) get_path(templates, 'motes', create=True) # select the right malicious mote template and duplicate the simulation file copy_files((TEMPLATES_FOLDER, 'experiment'), templates, ('motes/{}.c'.format(params["mtype_root"]), 'motes/root.c'), ('motes/{}.c'.format(params["mtype_sensor"]), 'motes/sensor.c'), ('motes/{}.c'.format(params["mtype_malicious"]), 'motes/malicious.c'), 'motes/Makefile', 'Makefile', 'simulation.csc', 'script.js') # create experiment's files from templates then clean the templates folder replacements = render_templates(path, **params) remove_folder(templates) # now, write the config file without the list of motes del params['motes'] write_config(path, params) # now compile with settings(hide(*HIDDEN_ALL), warn_only=True): with_malicious = join(path, 'with-malicious', 'motes') without_malicious = join(path, 'without-malicious', 'motes') contiki = join(with_malicious, 'contiki') contiki_rpl = join(contiki, 'core', 'net', 'rpl') # copy a reduced version of Contiki where the debug flags can be set for RPL files set in DEBUG_FILES copy_folder(CONTIKI_FOLDER, with_malicious, includes=get_contiki_includes(params["target"], params["malicious_target"])) apply_debug_flags(contiki_rpl, debug=['NONE', 'PRINT'][params["debug"]]) with lcd(with_malicious): # first, compile root and sensor mote types croot, csensor = 'root.{}'.format(params["target"]), 'sensor.{}'.format(params["target"]) if reuse_bin_path is None or reuse_bin_path == with_malicious: logger.debug(" > Making '{}'...".format(croot)) stderr(local)("make root CONTIKI={}".format(contiki), capture=True) logger.debug(" > Making '{}'...".format(csensor)) stderr(local)("make sensor CONTIKI={}".format(contiki), capture=True) # here, files are moved ; otherwise, 'make clean' would also remove *.z1 move_files(with_malicious, without_malicious, croot, csensor) # after compiling, clean artifacts local('make clean') remove_files(with_malicious, 'root.c', 'sensor.c') else: copy_files(reuse_bin_path, without_malicious, croot, csensor) # second, handle the malicious mote compilation malicious = 'malicious.{}'.format(params["malicious_target"]) if ext_lib is not None: remove_folder(contiki_rpl) copy_folder(ext_lib, contiki_rpl) apply_replacements(contiki_rpl, replacements) logger.debug(" > Making '{}'...".format(malicious)) stderr(local)("make malicious CONTIKI={} TARGET={}" .format(contiki, params["malicious_target"]), capture=True) # temporary move compiled malicious mote, clean the compilation artifacts, move the malicious mote back # from the temporary location and copy compiled root and sensor motes move_files(with_malicious, without_malicious, malicious) local('make clean') move_files(without_malicious, with_malicious, malicious) copy_files(without_malicious, with_malicious, croot, csensor) # finally, remove compilation sources remove_files(with_malicious, 'malicious.c') remove_folder(contiki)
def __make(name, ask=True, **kwargs): """ Make a new experiment. :param name: experiment name (or path to the experiment, if expanded in the 'command' decorator) :param ask: ask confirmation :param path: expanded path of the experiment (dynamically filled in through 'command' decorator with 'expand' :param kwargs: simulation keyword arguments (see the documentation for more information) """ global reuse_bin_path set_logging(kwargs.get('loglevel')) path = kwargs['path'] logger.debug(" > Validating parameters...") params = validated_parameters(kwargs) ext_lib = params.get("ext_lib") if ext_lib and not exists(ext_lib): logger.error("External library does not exist !") logger.critical("Make aborded.") return False logger.debug(" > Creating simulation...") # create experiment's directories check_structure(path, create=True, remove=True) templates = get_path(path, 'templates', create=True) get_path(templates, 'motes', create=True) # select the right malicious mote template and duplicate the simulation file copy_files( (TEMPLATES_FOLDER, 'experiment'), templates, ('motes/{}.c'.format(params["mtype_root"]), 'motes/root.c'), ('motes/{}.c'.format(params["mtype_sensor"]), 'motes/sensor.c'), ('motes/{}.c'.format(params["mtype_malicious"]), 'motes/malicious.c'), 'motes/Makefile', 'Makefile', 'simulation.csc', 'script.js') # create experiment's files from templates then clean the templates folder replacements = render_templates(path, **params) remove_folder(templates) # now, write the config file without the list of motes del params['motes'] write_config(path, params) # now compile with settings(hide(*HIDDEN_ALL), warn_only=True): with_malicious = join(path, 'with-malicious', 'motes') without_malicious = join(path, 'without-malicious', 'motes') contiki = join(with_malicious, split(CONTIKI_FOLDER)[-1]) contiki_rpl = join(contiki, 'core', 'net', 'rpl') # copy a reduced version of Contiki where the debug flags can be set for RPL files set in DEBUG_FILES copy_folder(CONTIKI_FOLDER, with_malicious, includes=get_contiki_includes(params["target"], params["malicious_target"])) apply_debug_flags(contiki_rpl, debug=['NONE', 'PRINT'][params["debug"]]) with lcd(with_malicious): # first, compile root and sensor mote types croot, csensor = 'root.{}'.format( params["target"]), 'sensor.{}'.format(params["target"]) if reuse_bin_path is None or reuse_bin_path == with_malicious: logger.debug(" > Making '{}'...".format(croot)) stderr(local)("make root CONTIKI={}".format(contiki), capture=True) logger.debug(" > Making '{}'...".format(csensor)) stderr(local)("make sensor CONTIKI={}".format(contiki), capture=True) # here, files are moved ; otherwise, 'make clean' would also remove *.z1 move_files(with_malicious, without_malicious, croot, csensor) # after compiling, clean artifacts local('make clean') remove_files(with_malicious, 'root.c', 'sensor.c') else: copy_files(reuse_bin_path, without_malicious, croot, csensor) # second, handle the malicious mote compilation malicious = 'malicious.{}'.format(params["malicious_target"]) if ext_lib is not None: remove_folder(contiki_rpl) copy_folder(ext_lib, contiki_rpl) apply_replacements(contiki_rpl, replacements) logger.debug(" > Making '{}'...".format(malicious)) stderr(local)("make malicious CONTIKI={} TARGET={}".format( contiki, params["malicious_target"]), capture=True) # temporary move compiled malicious mote, clean the compilation artifacts, move the malicious mote back # from the temporary location and copy compiled root and sensor motes move_files(with_malicious, without_malicious, malicious) local('make clean') move_files(without_malicious, with_malicious, malicious) copy_files(without_malicious, with_malicious, croot, csensor) # finally, remove compilation sources remove_files(with_malicious, 'malicious.c') remove_folder(contiki)