#!/usr/bin/env python3 # # Usage: # ./autocompile.py path ext1 # # Blocks monitoring |path| and its subdirectories for modifications on # files ending with suffix |extk|. Run |cmd| each time a modification # is detected. |cmd| is optional and defaults to 'make'. import subprocess import sys import os import pyinotify from db import db,file_info from constants import RAW_PATH,INCOMING_PATH from subprocess import Popen,PIPE class OnWriteHandler(pyinotify.ProcessEvent): def my_init(self, cwd, extension, cmd): self.cwd = cwd self.extension = extension self.cmd = cmd def _run_cmd(self,path): print('==> Modification detected') d=db() out_fname=os.path.join(RAW_PATH, os.path.basename(path).replace('flv','mp4')) in_fname=os.path.join(self.cwd, path) if os.path.isfile(out_fname): #exists return process=[ 'ffmpeg', '-loglevel', 'error', '-i', in_fname, '-c', 'copy', '-movflags', '+faststart', '-f','mp4', out_fname ] print(" ".join(process)) proc=Popen(process, universal_newlines=True) proc.wait() rc = proc.returncode if rc != 0: print("Error?") return d.insert_raw(file_info(out_fname)) def process_IN_CLOSE_WRITE(self, event): if not event.pathname.endswith(self.extension): return print(event.mask, event.maskname, event.pathname) self._run_cmd(event.pathname) def auto_compile(path, extension, cmd): wm = pyinotify.WatchManager() handler = OnWriteHandler(cwd=path, extension=extension, cmd=cmd) notifier = pyinotify.Notifier(wm, default_proc_fun=handler) wm.add_watch(path, pyinotify.ALL_EVENTS, rec=True, auto_add=True) print('==> Start monitoring %s' % path) notifier.loop() if __name__ == '__main__': if len(sys.argv) < 3: print ("Command line error: missing argument(s).") sys.exit(1) # Required arguments path = sys.argv[1] extension = sys.argv[2] # Optional argument cmd = 'ls' if len(sys.argv) == 4: cmd = sys.argv[3] # Blocks monitoring auto_compile(path, extension, cmd)