Skip to content

Instantly share code, notes, and snippets.

@umar-ahmed
Created January 4, 2017 00:17
Show Gist options
  • Select an option

  • Save umar-ahmed/219ff3e352441cf918967896343808f9 to your computer and use it in GitHub Desktop.

Select an option

Save umar-ahmed/219ff3e352441cf918967896343808f9 to your computer and use it in GitHub Desktop.

Revisions

  1. umar-ahmed created this gist Jan 4, 2017.
    43 changes: 43 additions & 0 deletions watcher.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@
    import sys
    import time
    from watchdog.observers import Observer
    from watchdog.events import FileSystemEventHandler
    import os
    import zipfile


    def zip(src, dst):
    zf = zipfile.ZipFile("%s.xd" % (dst), "w", zipfile.ZIP_DEFLATED)
    abs_src = os.path.abspath(src)
    for dirname, subdirs, files in os.walk(src):
    for filename in files:
    absname = os.path.abspath(os.path.join(dirname, filename))
    arcname = absname[len(abs_src) + 1:]
    zf.write(absname, arcname)
    zf.close()


    class ChangeHandler(FileSystemEventHandler):
    """ EventHandler for file system """
    def on_modified(self, event):
    print("File changed. Regenerating XD File...", end='')
    zip('files', 'test')
    print("COMPLETED!")


    if __name__ == "__main__":

    # Use the current directory if one is not provided
    path = sys.argv[1] if len(sys.argv) > 1 else '.'

    event_handler = ChangeHandler()

    observer = Observer()
    observer.schedule(event_handler, path, recursive=True)
    observer.start()
    try:
    while True:
    time.sleep(1)
    except KeyboardInterrupt:
    observer.stop()
    observer.join()