epoll-then 1.18 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
39
40
41
42
43
44
45
46
47
#!/usr/bin/env python3
"""
    epool-then - poll a file for a specific document, on match run a command
"""
import subprocess
import logging
import os
import sys
import argparse
import time

LOGGER = logging.getLogger(os.path.basename(__file__))

ARGP = argparse.ArgumentParser()
ARGP.add_argument('remainder', nargs=argparse.REMAINDER)
ARGP.add_argument('--file', required=True)
ARGP.add_argument('--literal', required=True)
ARGP.add_argument('--poll-sec', type=float, default=2)

def main(argp=None, argv=None):
    if argp is None:
        argp = ARGP.parse_args(argv)
    if argp.remainder and argp.remainder[0] == '--':
        argp.remainder = argp.remainder[1:]

    logging.basicConfig(
        level=logging.INFO,
    )

    while True:
        with open(argp.file) as fh:
            dat = fh.read()
        match = dat == argp.literal
        LOGGER.info('status: match=%s %s=%r', (match, argp.literal), argp.file, dat)
        if match:
            break

        time.sleep(argp.poll_sec)

    if argp.remainder:
        execve = argp.remainder
        LOGGER.info('execve=%r', execve)
        return subprocess.run(execve).returncode
    return 0

if __name__ == '__main__':
    exit(main())