-
Notifications
You must be signed in to change notification settings - Fork 0
/
runwsgi.py
executable file
·81 lines (71 loc) · 3.07 KB
/
runwsgi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/env python3
# Copyright (C) 2020 Oxan van Leeuwen
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import argparse
import os.path
import sys
import traceback
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
from wsgiref.simple_server import make_server
from importlib import import_module, reload
# parse arguments
parser = argparse.ArgumentParser(description='Simple WSGI server with hot reload support.')
parser.add_argument('application', help='application script or module name')
parser.add_argument('-p', '--port', default=8000, type=int, help='port to listen on')
args = parser.parse_args()
# find the directory in which the application resides, and add it to the import path
application_path_absolute = os.path.abspath(args.application)
application_dir = os.path.dirname(application_path_absolute)
application_module_name, _ = os.path.splitext(os.path.basename(application_path_absolute))
# import the application
sys.path.insert(0, application_dir)
import_module(application_module_name)
application = getattr(sys.modules[application_module_name], 'application')
# figure out all files that are imported from the application directory
reload_watches = {}
for module in sys.modules.values():
if getattr(module, '__file__', None) is None:
continue
if not module.__file__.startswith(application_dir + '/'):
continue
reload_watches[module.__file__] = module
# handler to reload files when they are changed
class ReloadHandler(FileSystemEventHandler):
def on_closed(self, event):
if event.src_path not in reload_watches:
print(f"# Ignoring change to not-loaded path {event.src_path}")
return
print(f"# Reloading {reload_watches[event.src_path].__name__} from {event.src_path} due to modification...")
try:
reload(reload_watches[event.src_path])
except Exception as e:
traceback.print_exc(file=sys.stderr)
print(f"# Failed to reload, server continues with old code!")
else:
# reset the application, in case it was reloaded
application = getattr(sys.modules[application_module_name], 'application')
wsgi_server.set_app(application)
# wire-up the reloading to the filesystem watcher
handler = ReloadHandler()
observer = Observer()
for path in reload_watches.keys():
print(f"# Watching {path} for modifications...")
observer.schedule(handler, path)
observer.start()
# and finally start the WSGI server
wsgi_server = make_server('', args.port, application)
print(f"# Listening on port {args.port} for HTTP requests...")
wsgi_server.serve_forever()