summaryrefslogtreecommitdiff
path: root/Omni/Dev/GitStats.py
blob: 8a6328da6f75367c6983f2df17a5b220affc20a4 (plain)
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
#!/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)