#!/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())