From c6e548b42c97d0b609ab69cfb414439d1d07e3f9 Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Tue, 3 May 2016 23:19:39 +0000 Subject: [PATCH] Fire inotify events on manifest changes --- server/https_server.py | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/server/https_server.py b/server/https_server.py index 3fa1ec7..b518a0f 100755 --- a/server/https_server.py +++ b/server/https_server.py @@ -2,8 +2,11 @@ import argparse import os +import pyinotify from gevent import pywsgi import ssl +import sys +import threading parser = argparse.ArgumentParser(description='iconograph https_server') @@ -41,7 +44,15 @@ parser.add_argument( FLAGS = parser.parse_args() -class ImageRequestHandler(object): +class INotifyHandler(pyinotify.ProcessEvent): + def process_IN_MOVED_TO(self, event): + if event.name != 'manifest.json': + return + image_type = os.path.basename(event.path) + print('new manifest: %r' % image_type) + + +class HTTPRequestHandler(object): _MIME_TYPES = { '.iso': 'application/octet-stream', @@ -89,15 +100,19 @@ class ImageRequestHandler(object): return -class ImageServer(object): +class Server(object): def __init__(self, listen_host, listen_port, server_key, server_cert, ca_cert, image_path): - self._handler = ImageRequestHandler(image_path) + wm = pyinotify.WatchManager() + inotify_handler = INotifyHandler() + self._notifier = pyinotify.Notifier(wm, inotify_handler) + wm.add_watch(image_path, pyinotify.IN_MOVED_TO, rec=True, auto_add=True) + http_handler = HTTPRequestHandler(image_path) self._httpd = pywsgi.WSGIServer( (listen_host, listen_port), - self._handler, + http_handler, keyfile=server_key, certfile=server_cert, ca_certs=ca_cert, @@ -105,11 +120,14 @@ class ImageServer(object): ssl_version=ssl.PROTOCOL_TLSv1_2) def Serve(self): + self._notify_thread = threading.Thread(target=self._notifier.loop) + self._notify_thread.daemon = True + self._notify_thread.start() self._httpd.serve_forever() def main(): - server = ImageServer( + server = Server( FLAGS.listen_host, FLAGS.listen_port, FLAGS.server_key,