This repository has been archived by the owner on Aug 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathserve.py
205 lines (167 loc) · 6.22 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
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
"""
Start local development server
"""
import argparse
import logging
import shlex
import subprocess
import webbrowser
from contextlib import suppress
from http.server import HTTPServer, SimpleHTTPRequestHandler
from pathlib import Path
from ssl import wrap_socket
from tempfile import NamedTemporaryFile
from threading import Thread
from livereload.server import LogFormatter, Server
from watchdog.observers import Observer
from watchdog.tricks import ShellCommandTrick
import build
PARCEL_CLI = "./node_modules/.bin/parcel"
BUNDLER_COMMAND = f"{PARCEL_CLI} watch --target default --no-hmr src/*.html"
LIVERELOAD_DELAY = 0.1
ROOT_DIR = "dist/"
PATHS_TO_WATCH = {
"build.py": ["thematiques", "index"],
"construction/*.py": ["thematiques", "index"],
"construction/directives/*.py": ["thematiques", "index"],
"contenus/actualites/*.toml": ["index"],
"contenus/config/*.md": ["index"],
"contenus/conseils/*.md": ["index"],
"contenus/meta/*.md": ["thematiques", "index"],
"contenus/questions/*.md": ["index"],
"contenus/réponses/*.md": ["index"],
"contenus/statuts/*.md": ["index"],
"contenus/suivi/*.md": ["index"],
"contenus/thematiques/*.md": ["thematiques", "index"],
"templates/index.html": ["index"],
"templates/thematique.html": ["thematiques"],
}
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("--address", default="0.0.0.0")
parser.add_argument("--port", type=int, default=None)
parser.add_argument("--ssl", action="store_true")
parser.add_argument("--ssl-cert", default="cert.pem")
parser.add_argument("--ssl-key", default="key.pem")
parser.add_argument("--open", action="store_true")
parser.add_argument("--watch", action="store_true")
return parser.parse_args()
def serve(address, port, open_, watch, ssl, ssl_cert, ssl_key, bundler_watch_filename):
if ssl:
return serve_https(
address=args.address,
port=args.port or 8443,
open_=args.open,
watch=args.watch,
ssl_cert=args.ssl_cert,
ssl_key=args.ssl_key,
)
else:
return serve_http(
address=args.address,
port=args.port or 5500,
open_=args.open,
watch=args.watch,
bundler_watch_filename=bundler_watch_filename,
)
class CustomServer(Server):
"""
Custom server with logger that decodes bytes in logs
"""
def _setup_logging(self):
super()._setup_logging()
logger = logging.getLogger("livereload")
formatter = self.BytesFormatter()
for handler in logger.handlers:
handler.setFormatter(formatter)
class BytesFormatter(LogFormatter):
def format(self, record):
if isinstance(record.msg, bytes):
with suppress(UnicodeDecodeError):
record.msg = record.msg.decode("utf-8")
return super().format(record)
def serve_http(address, port, open_, watch, bundler_watch_filename):
server = CustomServer()
if watch:
def build_targets(targets):
def callback():
for target in targets:
if target == "index":
build.index()
if target == "thematiques":
build.thematiques()
return callback
for path, targets in PATHS_TO_WATCH.items():
server.watch(path, build_targets(targets), delay="forever")
server.watch(bundler_watch_filename, delay=LIVERELOAD_DELAY)
if open_:
open_address = '127.0.0.1' if address == '0.0.0.0' else address
webbrowser.open(f"http://{open_address}:{port}")
server.serve(
host=address,
port=port,
root=ROOT_DIR,
)
def serve_https(address, port, open_, watch, ssl_cert, ssl_key):
class MyHTTPRequestHandler(SimpleHTTPRequestHandler):
def __init__(self, *args, **kwargs):
super().__init__(*args, directory=ROOT_DIR, **kwargs)
def log_request(self, *args, **kwargs):
pass
class BuildEventHandler(ShellCommandTrick):
def __init__(self, targets):
super().__init__(
shell_command="python3 build.py " + " ".join(targets),
wait_for_process=True,
drop_during_process=True,
)
def on_any_event(self, event):
if event.event_type == "modified" and not event.is_directory:
super().on_any_event(event)
if watch:
observer = Observer()
for path, targets in PATHS_TO_WATCH.items():
directory = Path(path).parts[0]
handler = BuildEventHandler(targets)
observer.schedule(handler, directory, recursive=True)
observer.start()
print(f"Listening on https://{address}:{port}/")
if open_:
open_address = '127.0.0.1' if address == '0.0.0.0' else address
webbrowser.open(f"https://{open_address}:{port}")
logging.getLogger()
httpd = HTTPServer((address, port), MyHTTPRequestHandler)
httpd.socket = wrap_socket(
httpd.socket, certfile=ssl_cert, keyfile=ssl_key, server_side=True
)
httpd.serve_forever()
class BundlerThread(Thread):
def __init__(self, watch_file):
super().__init__()
self.watch_file = watch_file
self.daemon = True
def run(self):
proc = subprocess.Popen(shlex.split(BUNDLER_COMMAND), stdout=subprocess.PIPE)
while True:
for line_bytes in proc.stdout:
line = line_bytes.decode("utf-8")
print(line)
if line.startswith("✨ Built in"):
self.trigger_livereload()
def trigger_livereload(self):
self.watch_file.truncate(0)
if __name__ == "__main__":
args = parse_args()
with NamedTemporaryFile(delete=True) as bundler_watch_file:
bundler_thread = BundlerThread(watch_file=bundler_watch_file)
bundler_thread.start()
serve(
address=args.address,
port=args.port,
open_=args.open,
watch=args.watch,
ssl=args.ssl,
ssl_cert=args.ssl_cert,
ssl_key=args.ssl_key,
bundler_watch_filename=bundler_watch_file.name,
)