リポジトリ / emssr / independent_common_compiler
r2
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 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 | # -*- coding: utf-8 -*- """Common scope notebook の web server。標準機能だけで書く。 python server.py 8787 番で起動し、ブラウザを開く python server.py --port 9000 --no-browser 役目は三つ。 1. web/ の静的 file、notebooks/、assets/ の画像と動画を配る 2. 実行時のファイル(vcpu.py、toolchain.py、commonc の image…)を /runtime/ で配る。 ブラウザ側の Pyodide がこれを取り込んで lane64 を動かす 3. ウィンドウの同期。/events が Server-Sent Events、POST /state が書き込みになる ウィンドウを分けるのは表示量のためになる。lane の作業領域は交わらない(20 式 1.2)ので、 ウィンドウごとに自分の kernel を持たせても state が混ざらない。同期するのは平文と初期値だけで、 計算はそれぞれのウィンドウが自分で回す。 """ import json import os import sys import threading import time import webbrowser from http.server import SimpleHTTPRequestHandler, ThreadingHTTPServer HERE = os.path.dirname(os.path.abspath(__file__)) WEB = os.path.join(HERE, "web") NOTEBOOKS = os.path.join(HERE, "notebooks") VENDOR = os.path.join(HERE, "vendor") ASSETS = os.path.join(HERE, "assets") # notebook が貼る画像と動画 # ===== 実行時のファイル ===== # # 場所が二通りある。repository の中では Python_vCPU と builds を直に指し、 # 配布版では runtime/ に写しが揃っている。どちらでも同じ名前で配る。 def runtime_map(): local = os.path.join(HERE, "runtime") names = ("vcpu.py", "vcpu_vec.py", "lex.py", "toolchain.py", "commonc_lane64.bin", "joinrank.bin", "anf.bin", "joinrank.dic") out = {} for n in names: p = os.path.join(local, n) if os.path.exists(p): out[n] = p if len(out) == len(names): return out root = os.path.abspath(os.path.join(HERE, os.pardir, os.pardir, os.pardir)) vcpu_dir = os.path.join(root, "continuous_republic", "common_systems", "Python_vCPU") jr = os.path.join(root, "continuous_republic", "common_apps", "joinrank") fall = { "vcpu.py": os.path.join(vcpu_dir, "vcpu.py"), "vcpu_vec.py": os.path.join(vcpu_dir, "vcpu_vec.py"), "lex.py": os.path.join(vcpu_dir, "lex.py"), "toolchain.py": os.path.join(vcpu_dir, "toolchain.py"), "commonc_lane64.bin": os.path.join(root, "builds", "commonc", "commonc_lane64.bin"), "joinrank.bin": os.path.join(jr, "build", "joinrank.bin"), "anf.bin": os.path.join(jr, "build", "anf.bin"), "joinrank.dic": os.path.join(jr, "joinrank", "joinrank.dic"), } for n, p in fall.items(): if n not in out and os.path.exists(p): out[n] = p for n in ("generators.py", "scope.py", "session.py", "joint.py"): out[n] = os.path.join(HERE, "kernel", n) return out def kernel_map(): return {n: os.path.join(HERE, "kernel", n) for n in ("generators.py", "scope.py", "session.py", "joint.py")} # ===== ウィンドウの同期 ===== class Hub(object): """いま編集している平文と種を持ち、繋がっているウィンドウへ配る""" def __init__(self): self.state = {"rev": 0} self.clients = [] self.lock = threading.Lock() def snapshot(self): with self.lock: return dict(self.state) def publish(self, patch): with self.lock: self.state.update(patch) self.state["rev"] = self.state.get("rev", 0) + 1 body = ("data: %s\n\n" % json.dumps(self.state, ensure_ascii=False)).encode("utf-8") live = [] for w in self.clients: try: w.write(body) w.flush() live.append(w) except Exception: pass self.clients = live return self.state["rev"] def attach(self, w): with self.lock: self.clients.append(w) return ("data: %s\n\n" % json.dumps(self.state, ensure_ascii=False)).encode("utf-8") HUB = Hub() # 拡張子から媒体の型を引く。module の import は JavaScript の型でないと browser が断る _MEDIA = { ".js": "text/javascript", ".mjs": "text/javascript", ".json": "application/json", ".wasm": "application/wasm", ".css": "text/css", ".html": "text/html; charset=utf-8", ".woff2": "font/woff2", ".woff": "font/woff", ".ttf": "font/ttf", ".png": "image/png", ".jpg": "image/jpeg", ".jpeg": "image/jpeg", ".gif": "image/gif", ".webp": "image/webp", ".svg": "image/svg+xml", ".mp4": "video/mp4", ".webm": "video/webm", ".ogv": "video/ogg", ".mp3": "audio/mpeg", ".wav": "audio/wav", ".m4a": "audio/mp4", ".pdf": "application/pdf", ".txt": "text/plain; charset=utf-8", } def _media(path): return _MEDIA.get(os.path.splitext(path)[1].lower(), "application/octet-stream") class Handler(SimpleHTTPRequestHandler): def __init__(self, *a, **k): SimpleHTTPRequestHandler.__init__(self, *a, directory=WEB, **k) def log_message(self, fmt, *args): pass def end_headers(self): # 直した web/ がそのまま反映されるように、静的 file もキャッシュさせない if "Cache-Control" not in self._headers_buffer_names(): self.send_header("Cache-Control", "no-store") SimpleHTTPRequestHandler.end_headers(self) def _headers_buffer_names(self): return b"".join(getattr(self, "_headers_buffer", []) or []).decode("latin-1") # ---- 送る ---- def _json(self, obj): body = json.dumps(obj, ensure_ascii=False).encode("utf-8") self.send_response(200) self.send_header("Content-Type", "application/json; charset=utf-8") self.send_header("Content-Length", str(len(body))) self.send_header("Cache-Control", "no-store") self.end_headers() self.wfile.write(body) def _file(self, path, ctype): try: data = open(path, "rb").read() except OSError: self.send_error(404, "無い: %s" % os.path.basename(path)) return self.send_response(200) self.send_header("Content-Type", ctype) self.send_header("Content-Length", str(len(data))) self.send_header("Cache-Control", "no-store") self.end_headers() self.wfile.write(data) def do_GET(self): p = self.path.split("?")[0] if p == "/api/runtime": files = runtime_map() files.update(kernel_map()) missing = [n for n, q in files.items() if not os.path.exists(q)] local = os.path.isfile(os.path.join(VENDOR, "pyodide", "pyodide.js")) web = os.path.isfile(os.path.join(VENDOR, "web", "marked.umd.js")) self._json({"files": sorted(files), "missing": missing, "pyodide": "/vendor/pyodide/" if local else None, "web": "/vendor/web/" if web else None}) return if p.startswith("/runtime/"): name = p[len("/runtime/"):] files = runtime_map() files.update(kernel_map()) if name not in files: self.send_error(404, "配信の表に無い: %s" % name) return self._file(files[name], "application/octet-stream") return if p == "/api/notebooks": if not os.path.isdir(NOTEBOOKS): self._json([]) return out = [] for n in sorted(os.listdir(NOTEBOOKS)): if n.endswith(".json"): d = json.load(open(os.path.join(NOTEBOOKS, n), encoding="utf-8")) out.append({"file": n, "title": d.get("title", n)}) self._json(out) return if p.startswith("/api/notebook/"): self._file(self._nb_path(p), "application/json; charset=utf-8") return if p == "/api/state": self._json(HUB.snapshot()) return if p == "/events": self.send_response(200) self.send_header("Content-Type", "text/event-stream; charset=utf-8") self.send_header("Cache-Control", "no-store") self.send_header("Connection", "keep-alive") self.end_headers() try: self.wfile.write(HUB.attach(self.wfile)) self.wfile.flush() except Exception: return # 接続は publish が書けなくなるまで開けたままにする while True: with HUB.lock: alive = self.wfile in HUB.clients if not alive: return time.sleep(0.5) if p == "/api/assets": os.path.isdir(ASSETS) or os.makedirs(ASSETS) self._json(sorted(os.listdir(ASSETS))) return if p.startswith("/assets/"): q = os.path.join(ASSETS, os.path.basename(p[len("/assets/"):])) self._file(q, _media(q)) return if p.startswith("/vendor/"): q = os.path.join(VENDOR, p[len("/vendor/"):].replace("/", os.sep)) if not os.path.abspath(q).startswith(VENDOR): self.send_error(403) return self._file(q, _media(q)) return SimpleHTTPRequestHandler.do_GET(self) # ---- notebook の保存と削除 ---- # # 書き込める場所は notebooks/ の直下だけにする。名前は basename を取り、 # 拡張子を .json に固定するので、上の階層へは出られない。 def _nb_path(self, p): name = os.path.basename(p.split("/api/notebook/")[-1]) if not name.endswith(".json"): name += ".json" return os.path.join(NOTEBOOKS, name) def _body(self): n = int(self.headers.get("Content-Length", 0)) return json.loads(self.rfile.read(n).decode("utf-8")) def do_POST(self): p = self.path.split("?")[0] if p == "/state": try: patch = self._body() except ValueError: self.send_error(400, "JSON ではありません") return self._json({"rev": HUB.publish(patch)}) return if p.startswith("/assets/"): name = os.path.basename(p[len("/assets/"):]) if not name: self.send_error(400, "名前がありません") return n = int(self.headers.get("Content-Length", 0)) if n > 64 * 1024 * 1024: self.send_error(413, "64 MB を越えています") return os.path.isdir(ASSETS) or os.makedirs(ASSETS) with open(os.path.join(ASSETS, name), "wb") as f: f.write(self.rfile.read(n)) self._json({"path": "assets/" + name}) return if p.startswith("/api/notebook/"): try: doc = self._body() except ValueError: self.send_error(400, "JSON ではありません") return os.path.isdir(NOTEBOOKS) or os.makedirs(NOTEBOOKS) path = self._nb_path(p) with open(path, "w", encoding="utf-8", newline="\n") as f: json.dump(doc, f, ensure_ascii=False, indent=2) f.write("\n") self._json({"file": os.path.basename(path)}) return self.send_error(404) def do_DELETE(self): p = self.path.split("?")[0] if not p.startswith("/api/notebook/"): self.send_error(404) return path = self._nb_path(p) if os.path.exists(path): os.remove(path) self._json({"file": os.path.basename(path)}) def main(argv): port, open_browser = 8787, True it = iter(argv) for a in it: if a == "--port": port = int(next(it)) elif a == "--no-browser": open_browser = False else: print(__doc__) return 2 files = runtime_map() files.update(kernel_map()) missing = [n for n, p in files.items() if not os.path.exists(p)] if missing: print("配れないファイル: %s" % ", ".join(missing)) print(" setup.py を実行するか、runtime/ へコピーを置く") url = "http://127.0.0.1:%d/" % port srv = ThreadingHTTPServer(("127.0.0.1", port), Handler) srv.daemon_threads = True print("Common scope notebook %s" % url) print(" パネルを別ウィンドウで開く: %s?pane=dag ?pane=chamber ?pane=tsui ?pane=lane ?pane=joint" % url) if open_browser: threading.Timer(0.4, lambda: webbrowser.open(url)).start() try: srv.serve_forever() except KeyboardInterrupt: print("") return 0 if __name__ == "__main__": sys.exit(main(sys.argv[1:])) |