File size: 3,083 Bytes
07a91a1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Simple task runner for common project workflows."""

from __future__ import annotations

import argparse
import os
import subprocess
import sys
import time


def run_cmd(cmd: list[str]) -> int:
    print(">", " ".join(cmd))
    return subprocess.call(cmd)


def task_install() -> int:
    return run_cmd([sys.executable, "-m", "pip", "install", "-r", "requirements.txt"])


def task_run() -> int:
    return run_cmd([sys.executable, "-m", "uvicorn", "api.main:app", "--host", "0.0.0.0", "--port", "8000"])


def task_space() -> int:
    return run_cmd([sys.executable, "app.py"])


def task_smoke() -> int:
    return run_cmd([sys.executable, "smoke_test.py"])


def task_compile() -> int:
    return run_cmd([sys.executable, "-m", "compileall", "."])


def task_docker_up() -> int:
    return run_cmd(["docker", "compose", "up", "--build"])


def task_docker_down() -> int:
    return run_cmd(["docker", "compose", "down"])


def task_hf_upload(repo_id: str, token: str, private: bool) -> int:
    cmd = [sys.executable, "upload_to_hf.py", "--repo-id", repo_id, "--token", token]
    if private:
        cmd.append("--private")
    return run_cmd(cmd)


def task_serve_smoke() -> int:
    """Start API, run smoke test, then stop API."""
    env = os.environ.copy()
    server_cmd = [sys.executable, "-m", "uvicorn", "api.main:app", "--host", "127.0.0.1", "--port", "8000"]
    print(">", " ".join(server_cmd))
    server = subprocess.Popen(server_cmd, env=env)
    try:
        time.sleep(3)
        return run_cmd([sys.executable, "smoke_test.py"])
    finally:
        server.terminate()
        try:
            server.wait(timeout=10)
        except subprocess.TimeoutExpired:
            server.kill()


def main():
    parser = argparse.ArgumentParser(description="Project task runner")
    sub = parser.add_subparsers(dest="task", required=True)

    sub.add_parser("install")
    sub.add_parser("run")
    sub.add_parser("space")
    sub.add_parser("smoke")
    sub.add_parser("compile")
    sub.add_parser("docker-up")
    sub.add_parser("docker-down")
    sub.add_parser("serve-smoke")
    hf = sub.add_parser("hf-upload")
    hf.add_argument("--repo-id", required=True)
    hf.add_argument("--token", required=True)
    hf.add_argument("--private", action="store_true")

    args = parser.parse_args()

    if args.task == "install":
        code = task_install()
    elif args.task == "run":
        code = task_run()
    elif args.task == "space":
        code = task_space()
    elif args.task == "smoke":
        code = task_smoke()
    elif args.task == "compile":
        code = task_compile()
    elif args.task == "docker-up":
        code = task_docker_up()
    elif args.task == "docker-down":
        code = task_docker_down()
    elif args.task == "serve-smoke":
        code = task_serve_smoke()
    else:
        code = task_hf_upload(args.repo_id, args.token, args.private)

    raise SystemExit(code)


if __name__ == "__main__":
    main()