-
Notifications
You must be signed in to change notification settings - Fork 12
/
serve.py
executable file
·53 lines (41 loc) · 1.62 KB
/
serve.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
#!/usr/bin/env python3
from __future__ import print_function
import subprocess
import sys
import time
import os
try:
from http.server import SimpleHTTPRequestHandler
from http.server import HTTPServer
except ImportError:
from SimpleHTTPServer import SimpleHTTPRequestHandler
from BaseHTTPServer import HTTPServer
outdir = sys.argv[1]
reldir = os.path.relpath('.', outdir)
os.chdir(outdir)
try:
from watchdog.events import PatternMatchingEventHandler
from watchdog.observers import Observer
class MakeHandler(PatternMatchingEventHandler):
def __init__(self, interval):
PatternMatchingEventHandler.__init__(self, patterns=[os.path.join('*.res', '*'), '*.md'], ignore_patterns=[os.path.join(reldir, outdir, '*'), os.path.join('*', 'LICENSE.md'), os.path.join('*', 'README.md'), os.path.join('*', '.git', '*')], ignore_directories=True, case_sensitive=False)
self.last = time.time()
self.interval = interval
def on_any_event(self, event):
cur = time.time()
if cur < self.last + self.interval:
return
print('rebuilding')
subprocess.call(['make', '-C', reldir, 'website'], stdout=open('/dev/null'), stderr=subprocess.STDOUT)
self.last = cur
observer = Observer()
observer.schedule(MakeHandler(interval=1), reldir, recursive=True)
observer.start()
except ImportError:
pass
print('serving "{}" at http://localhost:8080/'.format(outdir))
httpd = HTTPServer(('localhost', 8080), SimpleHTTPRequestHandler)
try:
httpd.serve_forever()
except KeyboardInterrupt:
print('shutting down')