#!/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: pass 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', '-j10']) 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 if experiment.isdigit(): continue experiment_file = os.path.join('results', experiment + '.out') if os.path.exists(experiment_file): continue with open(experiment_file, 'w') as of: with cd(experiment): try: p = Popen(['./' + experiment], stdout=of) p.wait() except Exception as e: print(e) print('Failed running experiment: {}'.format(experiment), file=sys.stderr) sys.exit(1) if os.stat(experiment_file).st_size == 0: print('Removing file after failed experiment.', file=sys.stderr) os.remove(experiment_file) shutil.rmtree(experiment) # 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()