diff --git a/runner.py b/runner.py new file mode 100755 index 0000000..d088d9b --- /dev/null +++ b/runner.py @@ -0,0 +1,102 @@ +#!/usr/bin/env python3 + +import argparse +import shutil +from subprocess import call, Popen, PIPE +import sys +import os + +def query_yes_no(question, default=None): + """ + Queries the user for a decision. + """ + + if default is None: + prompt = ' [y/n]' + elif default.lower() == 'yes': + prompt = ' [Y/n]' + elif default.lower() == 'no': + prompt = ' [y/N]' + else: + raise ValueError('Invalid default answer {}'.format(default)) + + while True: + print(question, prompt, end='') + choice = input().lower() + if 'yes'.find(choice) == 0: + return True + elif 'no'.find(choice) == 0: + return False + +def create_dir(filename): + """ + Create the directory denoted by filename, if it doesn't exists. Ask for its + removal if it exists. + """ + try: + os.makedirs(filename) + except FileExistsError: + if not query_yes_no('Folder exists, remove?', default='yes'): + return False + shutil.rmtree(filename) + os.makedirs(filename) + return True + +class cd: + """Context manager for changing the current working directory""" + def __init__(self, newPath): + self.newPath = os.path.expanduser(newPath) + + def __enter__(self): + self.savedPath = os.getcwd() + os.chdir(self.newPath) + + def __exit__(self, etype, value, traceback): + os.chdir(self.savedPath) + +def main(): + parser = argparse.ArgumentParser('Build and run experiments.') + parser.add_argument('path', type=str, default='.', nargs='?', + help='Base path of the experiments') + + args = parser.parse_args() + + with cd(args.path): + result = call(['make']) + if result is not 0 and result is not 2: + print('Failed building the experiments!', file=sys.stderr) + sys.exit(1) + + if not create_dir('results'): + sys.exit(1) + + for experiment in os.listdir(): + if experiment == 'results' or not os.path.isdir(experiment): + continue + + with open(os.path.join('results', experiment + '.out'), 'w') as of: + with cd(experiment): + try: + p = Popen(['./' + experiment], stdout=of) + p.wait() + except Exception as e: + print(e) + print('Failed running experiment: {}', experiment, + file=sys.stderr) + sys.exit(1) + + + # with cd(experiment): + # try: + # p = Popen(['./' + experiment], stdout=PIPE) + # out, err = p.communicate() + # except Exception as e: + # print(e) + # print('Failed running experiment: {}', experiment, + # file=sys.stderr) + # sys.exit(1) + # with open(os.path.join('results', experiment + '.out'), 'w') as of: + # of.write(str(out)) + +if __name__ == '__main__': + main()