-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdo
More file actions
executable file
·153 lines (117 loc) · 3.73 KB
/
Copy pathdo
File metadata and controls
executable file
·153 lines (117 loc) · 3.73 KB
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
#!/usr/bin/env python3
"""Project task runner.
Usage:
./do <command>
Commands:
setup Install all dev dependencies
build Build for release
test Run all tests
lint Run all linters
format Run all formatters
coverage Run all coverage tools
presubmit Run setup, formatters, linters, tests, coverage, then ci
ci Run all presubmit checks as CI would (temporary commit)
"""
import json
import os
import re
import signal
import subprocess
import sys
ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
CONFIG_FILE = os.path.join(ROOT_DIR, ".workflow.jsonc")
PRESUBMIT_TIMEOUT_SECONDS = 10 * 60
def load_config():
"""Read and parse .workflow.jsonc, stripping // comments."""
if not os.path.exists(CONFIG_FILE):
return {}
try:
with open(CONFIG_FILE, "r", encoding="utf-8") as f:
raw = f.read()
stripped = re.sub(r"//[^\n]*", "", raw)
return json.loads(stripped)
except Exception:
return {}
def run(cmd, **kwargs):
"""Run a command and return the result."""
print(f" -> {' '.join(cmd) if isinstance(cmd, list) else cmd}")
result = subprocess.run(cmd, cwd=ROOT_DIR, shell=isinstance(cmd, str), **kwargs)
return result.returncode
def cmd_setup():
"""Install all dev dependencies."""
print("setup: no dev dependencies configured")
return 1
def cmd_build():
"""Build for release."""
print("build: no build configured")
return 1
def cmd_test():
"""Run all tests."""
return run([sys.executable, "-m", "pytest", "tests/", "-v"])
def cmd_lint():
"""Run all linters."""
print("lint: no linters configured")
return 1
def cmd_format():
"""Run all formatters."""
print("format: no formatters configured")
return 1
def cmd_coverage():
"""Run tests with coverage reporting."""
return run([
sys.executable, "-m", "pytest", "tests/", "-v",
"--cov", "--cov-report=term-missing",
])
def cmd_ci():
"""Run CI checks via .tools/ci.py."""
ci_script = os.path.join(ROOT_DIR, ".tools", "ci.py")
if not os.path.exists(ci_script):
print("ci: .tools/ci.py not found")
return 1
return run([sys.executable, ci_script])
def cmd_presubmit():
"""Run all presubmit checks with a timeout."""
checks = [
("setup", cmd_setup), # Setup dependencies so presubmit passes in a clean checkout.
("format", cmd_format),
("lint", cmd_lint),
("build", cmd_build), # Verify build release works as part of presubmit.
("test", cmd_test),
("coverage", cmd_coverage),
]
def _timeout_handler(signum, frame):
print(f"\nPresubmit timed out after {PRESUBMIT_TIMEOUT_SECONDS // 60} minutes.")
sys.exit(1)
if hasattr(signal, "SIGALRM"):
signal.signal(signal.SIGALRM, _timeout_handler)
signal.alarm(PRESUBMIT_TIMEOUT_SECONDS)
failed = []
for name, fn in checks:
print(f"\n== {name} ==")
rc = fn()
if rc != 0:
failed.append(name)
if hasattr(signal, "SIGALRM"):
signal.alarm(0)
if failed:
print(f"\nFAILED: {', '.join(failed)}")
return 1
print("\nAll presubmit checks passed. Running ci...")
return cmd_ci()
def main():
commands = {
"setup": cmd_setup,
"build": cmd_build,
"test": cmd_test,
"lint": cmd_lint,
"format": cmd_format,
"coverage": cmd_coverage,
"presubmit": cmd_presubmit,
"ci": cmd_ci,
}
if len(sys.argv) < 2 or sys.argv[1] not in commands:
print(f"Usage: ./do <{'|'.join(commands.keys())}>")
sys.exit(1)
sys.exit(commands[sys.argv[1]]())
if __name__ == "__main__":
main()