Procházet zdrojové kódy

modifico watch para meter a base

David před 10 roky
rodič
revize
f26d9ec890
1 změnil soubory, kde provedl 77 přidání a 0 odebrání
  1. 77 0
      back/watch.py

+ 77 - 0
back/watch.py

@@ -0,0 +1,77 @@
+#!/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(INCOMING_PATH, 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()
+        d.insert_raw(file_info(out_fname))
+
+    def process_IN_MODIFY(self, event):
+        if not event.pathname.endswith(self.extension):
+            return
+        #print(event)
+        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 (type c^c to exit)' % 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)