test_scheduler.py 910 Bytes
Newer Older
lwc-tester committed
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 39 40 41 42 43
#!/usr/bin/env python3
import sys
import time
import fcntl
import subprocess


def file_pop_line(path):
    try:
        with open(path, 'rt+') as q:
            fcntl.lockf(q, fcntl.LOCK_EX)
            first = q.readline()
            if first == '':
                return None
            rest = q.read()
            q.seek(0)
            q.write(rest)
            q.truncate()
        return first
    except FileNotFoundError:
        return None


def main(argv):
    test_queue = argv[1]

    while 1:
        cmd = file_pop_line(test_queue).strip()

        if cmd is None:
            time.sleep(5)
        else:
            print()
            print("Executing %s" % cmd)
            p = subprocess.Popen(['bash', '-c', cmd])
            p.wait()
            print()
            print("Return code is %d" % p.returncode)
            print()


if __name__ == '__main__':
    sys.exit(main(sys.argv))