Created
May 16, 2014 09:25
-
-
Save ntotani/a24bfdfc3029466bdc87 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import time | |
from socket import * | |
from watchdog.events import FileSystemEventHandler | |
from watchdog.observers import Observer | |
CMD_PORT = 6010 | |
FILE_PORT = 6020 | |
BUFFER = 1024 | |
HOST = '0.0.0.0' # change if other device | |
BASE_DIR = os.path.abspath(os.path.dirname(__file__)) | |
RESOURCE_DIR = BASE_DIR + '/res' | |
class ResourceHandler(FileSystemEventHandler): | |
def on_created(self, event): | |
on_resource(event) | |
def on_modified(self, event): | |
on_resource(event) | |
def on_resource(event): | |
if event.is_directory: | |
return | |
upload(event.src_path) | |
reload() | |
def upload(src_path): | |
filename = os.path.relpath(src_path, BASE_DIR) | |
mtime = os.path.getmtime(src_path) | |
json = '{"filename":"/%s", "lastmodifytime":"%d"}' % (filename, mtime) | |
soc = socket(AF_INET, SOCK_STREAM) | |
soc.connect((HOST, FILE_PORT)) | |
soc.send('%03d %s' % (len(json), json)) | |
fp = open(src_path, 'rb') | |
data = fp.read(BUFFER) | |
while (data): | |
soc.send(data) | |
data = fp.read(BUFFER) | |
fp.close() | |
soc.close() | |
def reload(): | |
soc = socket(AF_INET, SOCK_STREAM) | |
soc.connect((HOST, CMD_PORT)) | |
soc.send('sendrequest {"cmd":"reload","modulefiles":[]}\n') | |
soc.send('exit\n') | |
soc.close() | |
if __name__ == "__main__": | |
obs = Observer() | |
obs.schedule(ResourceHandler(), RESOURCE_DIR, recursive=True) | |
obs.start() | |
try: | |
while True: | |
time.sleep(1) | |
except KeyboardInterrupt: | |
obs.stop() | |
obs.join() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment