runner.py 2.77 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
#!/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:
39
        pass
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
    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):
62
        result = call(['make', '-j10'])
63 64 65 66 67 68 69 70 71 72
        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
73 74 75 76 77
            if experiment.isdigit():
                continue
            experiment_file = os.path.join('results', experiment + '.out')
            if os.path.exists(experiment_file):
                continue
78

79
            with open(experiment_file, 'w') as of:
80 81 82 83 84 85
                with cd(experiment):
                    try:
                        p = Popen(['./' + experiment], stdout=of)
                        p.wait()
                    except Exception as e:
                        print(e)
Tobias Langer committed
86
                        print('Failed running experiment: {}'.format(experiment),
87 88
                              file=sys.stderr)
                        sys.exit(1)
89 90 91 92
            if os.stat(experiment_file).st_size == 0:
                print('Removing file after failed experiment.', file=sys.stderr)
                os.remove(experiment_file)
                shutil.rmtree(experiment)
93 94 95

if __name__ == '__main__':
    main()