# 需要导入模块: from fabric import api [as 别名]
# 或者: from fabric.api import lcd [as 别名]
def __run(name, **kwargs):
"""
Run an experiment.
:param name: experiment name
: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)
"""
set_logging(kwargs.get('loglevel'))
path = kwargs['path']
check_structure(path, remove=True)
with settings(hide(*HIDDEN_ALL), warn_only=True):
for sim in ["without", "with"]:
sim_path = join(path, "{}-malicious".format(sim))
data, results = join(sim_path, 'data'), join(sim_path, 'results')
# the Makefile is at experiment's root ('path')
logger.debug(" > Running simulation {} the malicious mote...".format(sim))
task = kwargs.get('task', "run")
with lcd(sim_path):
output = local("make run TASK={}".format(task), capture=True)
remove_files(sim_path, '.{}'.format(task))
error, interrupt, error_buffer = False, False, []
for line in output.split('\n'):
if line.strip().startswith("FATAL") or line.strip().startswith("ERROR"):
error, interrupt = True, True
elif line.strip().startswith("INFO"):
error = False
if len(error_buffer) > 0:
logger.error('Cooja error:\n' + '\n'.join(error_buffer))
error_buffer = []
if error:
error_buffer.append(line)
if interrupt:
logger.warn("Cooja failed to execute ; 'run' interrupted (no parsing done)")
raise Exception("Cooja failed to execute")
# once the execution is over, gather the screenshots into a single GIF and keep the first and
# the last screenshots ; move these to the results folder
logger.debug(" > Gathering screenshots in an animated GIF...")
with lcd(data):
local('convert -delay 10 -loop 0 network*.png wsn-{}-malicious.gif'.format(sim), capture=True)
network_images = {int(fn.split('.')[0].split('_')[-1]): fn for fn in listdir(data)
if fn.startswith('network_')}
move_files(data, results, 'wsn-{}-malicious.gif'.format(sim))
if len(network_images) > 0:
net_start_old = network_images[min(network_images.keys())]
net_start, ext = splitext(net_start_old)
net_start_new = 'wsn-{}-malicious_start{}'.format(sim, ext)
net_end_old = network_images[max(network_images.keys())]
net_end, ext = splitext(net_end_old)
net_end_new = 'wsn-{}-malicious_end{}'.format(sim, ext)
move_files(data, results, (net_start_old, net_start_new), (net_end_old, net_end_new))
remove_files(data, *network_images.values())
# then start the parsing functions to derive more results
parsing_chain(sim_path, logger)
move_files(sim_path, results, 'COOJA.log')
# finally, generate the PDF report
generate_report(path, REPORT_THEME)
return "Both Cooja executions succeeded"