Skip to content
Snippets Groups Projects
scheduler.py 2.29 KiB
Newer Older
Boris Feld's avatar
Boris Feld committed
"""
Usage:
    python scheduler.py TASKS

The TASKS file is a text file containing one task per line:

RUN 4.7.2
RUN 666 -b "time_commit"

The format is the following: `TASK_TYPE ARGUMENTS`.
The supported TASK_TYPE are:
    * RUN: launch asv on the ARGUMENTS. Accepts all arguments that asv run
      accepts.

Once a TASK is done, the scheduler will remove it from the the task file. You
can edit this file, please use an editor that detects when the tasks file
changed since you start editing it (at least nano does it).
"""


import sys
import time
import shlex
import subprocess

from os.path import dirname, join, abspath

SCRIPT = abspath(join(dirname(__file__), "main-run.sh"))
Boris Feld's avatar
Boris Feld committed


def read_next_scheduling(task_file_path):
    with open(task_file_path, "r") as f:
        first_line = f.readline().strip()

    return first_line


def run(task):
    splitted = shlex.split(task)
    args = [SCRIPT] + splitted

    try:
        print("RUNNING %r" % args)
        subprocess.check_call(args)
    except subprocess.CalledProcessError as e:
        print("\nTask %r failed with error code %r" % (task, e.returncode))
        # XXX find a way to report an error without removing a task


def remove_task(task, task_file_path):
    """ Remove the first line of the scheduling TASK if it match passed task
    """
    with open(task_file_path, "r") as f:
        lines = f.readlines()

    if lines[0].strip() == task:
        lines = lines[1:]

        # XXX We should instead write to a temporary file and move it
        with open(task_file_path, "w") as f:
            f.writelines(lines)

        print("Removed task %r" % task)
    else:
        print("Don't remove non-matching first task %r" % task)


def main(task_file_path):
    while True:
        next_task = read_next_scheduling(task_file_path)

        if next_task:

            # Try splitting it
            splitted = next_task.split(" ", 1)
            assert len(splitted) == 2
            task_type, task = splitted

            # We only support RUN type right now
            assert task_type == "RUN"

            print("=" * 80)
            print("Got task %r" % task)
            run(task)
            print("\n"*2)
            remove_task(next_task, task_file_path)
            print("=" * 80)

        time.sleep(1)


if __name__ == "__main__":
    main(sys.argv[1])