#!/usr/bin/env run.sh # : run gitstats # : out gitstats-serve """ Show gitstats for the omnirepo. Generates gitstats HTML output in _/var, then starts a webserver to serve that directory. """ import http.server import os import pathlib import subprocess import sys def main() -> None: """Serve gitstats on localhost:8000. Raises: ValueError: if CODEROOT is not set """ if "test" in sys.argv: # test that gitstats in available, and exit sys.exit(subprocess.run(["which", "gitstats"], check=False).returncode) root = os.getenv("CODEROOT") if root is None: msg = "CODEROOT is not set" raise ValueError(msg) omni_dir = pathlib.Path(root) out_dir = omni_dir / "_" / "var" / "gitstats" subprocess.check_call([ "gitstats", str(omni_dir), str(out_dir), ]) addr = ("", 8000) handler = http.server.SimpleHTTPRequestHandler pwd = pathlib.Path.cwd() os.chdir(out_dir) with http.server.HTTPServer(addr, handler) as httpd: host, port = httpd.socket.getsockname()[:2] sys.stdout.write(f"serving gitstats on http://{host}:{port}\n") sys.stdout.flush() try: httpd.serve_forever() except KeyboardInterrupt: sys.stdout.write("\nstopping httpd\n") os.chdir(pwd)