aboutsummaryrefslogtreecommitdiff
path: root/web.py
blob: 0bbebb78f867f25f84ea0b383eaf4f7216dfde99 (plain) (blame)
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
import json
import os
import queue
import re
import threading
import time
from typing import Any

from flask import (
    Flask,
    abort,
    send_from_directory,
    stream_template,
)


def get_project(project: str) -> dict[str, Any]:
    with open(f"./projects/{project}.json", "r") as p:
        return json.loads(p.read())


def get_run(project: str, run: int) -> str:
    with open(f"./stdout/public/{project}/{run}", "r") as r:
        return r.read()


def stream_run(project: str, run: int):
    while True:
        with open(f"./stdout/public/{project}/{run}", "r") as r:
            txt = r.read()
            yield txt
            if "--MSCI_EXIT_" in txt:
                break
        time.sleep(0.5)


def _projects(q: queue.Queue[tuple[str, dict[str, Any]] | None]):
    if os.path.isdir("./projects"):
        _projects = os.listdir("./projects")
        for _p in _projects:
            loaded = get_project(_p.replace(".json", ""))
            if not loaded["hidden"]:
                q.put((_p.replace(".json", ""), loaded))
    else:
        os.mkdir("./projects")
    q.put(None)


def projects():
    q: queue.Queue[tuple[str, dict[str, Any]] | None] = queue.Queue()
    threading.Thread(target=_projects, args=(q,), daemon=True).start()
    while True:
        pr = q.get()
        if pr is None:
            break
        yield pr


def _project_runs(project: str, q: queue.Queue[dict[str, Any] | None]):
    pr_stdout = f"./stdout/public/{project}"
    if os.path.isdir(pr_stdout):
        _runs = sorted(os.listdir(pr_stdout), key=int)
        for _r in _runs:
            run_str = get_run(project, int(_r))
            run_date = re.search(r"--MSCI_DATE\((.*?)\)--", run_str)
            run_status = (
                True
                if "--MSCI_EXIT_SUCCESS--" in run_str
                else False if "--MSCI_EXIT_FAILURE--" in run_str else None
            )
            run_data = {
                "number": int(_r),
                "date": run_date.group(1) if run_date else None,
                "status": run_status,
            }
            q.put(run_data)
    q.put(None)


def project_runs(project: str):
    q: queue.Queue[dict[str, Any] | None] = queue.Queue()
    threading.Thread(
        target=_project_runs,
        args=(
            project,
            q,
        ),
        daemon=True,
    ).start()
    while True:
        pr = q.get()
        if pr is None:
            break
        yield pr


def project_exists(name: str):
    return (
        os.path.isfile(f"./projects/{name}.json")
        and get_project(name)["hidden"] == False
    )


def run_exists(project: str, run: int):
    if project_exists(project):
        if not os.path.isfile(f"./stdout/public/{project}/{run}"):
            return False
        return True
    return False


app = Flask(__name__)


@app.route("/favicon.ico")
def favicon():
    return send_from_directory(
        app.root_path,
        "favicon.ico",
        mimetype="image/vnd.microsoft.icon",
    )


@app.route("/", methods=["GET"])
def home():
    return stream_template("home.html", projects=projects())


@app.route("/<string:project>", methods=["GET"])
def project(project: str):
    if project_exists(project):
        return stream_template(
            "project.html",
            project=get_project(project),
            project_path=project,
            runlist=project_runs(project),
        )
    abort(404)


@app.route("/<string:project>/<int:run>", methods=["GET"])
def run(project: str, run: int):
    if run_exists(project, run):
        return stream_template(
            "run.html",
            project=get_project(project),
            project_path=project,
            run=stream_run(project, run),
            run_number=run,
        )
    abort(404)