Skip to content

Commit 73f77c2

Browse files
WOLIKIMCHENGroot
andauthored
feat(scripts): add Python check-prerequisites PoC (#3302)
* feat(scripts): add Python check-prerequisites PoC * fix(scripts): address check-prerequisites parity feedback * test(scripts): label PowerShell prerequisite parity cases --------- Co-authored-by: root <kinsonnee@gmail.com>
1 parent b8d27e4 commit 73f77c2

3 files changed

Lines changed: 756 additions & 0 deletions

File tree

Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
#!/usr/bin/env python3
2+
"""Consolidated prerequisite checking script."""
3+
4+
from __future__ import annotations
5+
6+
import json
7+
import sys
8+
from dataclasses import dataclass
9+
from pathlib import Path
10+
11+
try:
12+
from common import FeaturePaths, format_speckit_command, get_feature_paths
13+
except ImportError: # pragma: no cover - direct execution from unusual cwd
14+
sys.path.insert(0, str(Path(__file__).resolve().parent))
15+
from common import FeaturePaths, format_speckit_command, get_feature_paths
16+
17+
18+
def _json_line(payload: object) -> str:
19+
return json.dumps(payload, ensure_ascii=False, separators=(",", ":")) + "\n"
20+
21+
22+
HELP_TEXT = """Usage: check_prerequisites.py [OPTIONS]
23+
24+
Consolidated prerequisite checking for Spec-Driven Development workflow.
25+
26+
OPTIONS:
27+
--json Output in JSON format
28+
--require-tasks Require tasks.md to exist (for implementation phase)
29+
--include-tasks Include tasks.md in AVAILABLE_DOCS list
30+
--paths-only Only output path variables (no prerequisite validation)
31+
--help, -h Show this help message
32+
33+
EXAMPLES:
34+
# Check task prerequisites (plan.md required)
35+
./check_prerequisites.py --json
36+
37+
# Check implementation prerequisites (plan.md + tasks.md required)
38+
./check_prerequisites.py --json --require-tasks --include-tasks
39+
40+
# Get feature paths only (no validation)
41+
./check_prerequisites.py --paths-only
42+
43+
"""
44+
45+
46+
@dataclass(frozen=True)
47+
class Args:
48+
json_mode: bool = False
49+
require_tasks: bool = False
50+
include_tasks: bool = False
51+
paths_only: bool = False
52+
53+
54+
def _parse_args(argv: list[str]) -> Args:
55+
json_mode = False
56+
require_tasks = False
57+
include_tasks = False
58+
paths_only = False
59+
60+
for arg in argv:
61+
if arg == "--json":
62+
json_mode = True
63+
elif arg == "--require-tasks":
64+
require_tasks = True
65+
elif arg == "--include-tasks":
66+
include_tasks = True
67+
elif arg == "--paths-only":
68+
paths_only = True
69+
elif arg in {"--help", "-h"}:
70+
sys.stdout.write(HELP_TEXT)
71+
raise SystemExit(0)
72+
else:
73+
print(
74+
f"ERROR: Unknown option '{arg}'. Use --help for usage information.",
75+
file=sys.stderr,
76+
)
77+
raise SystemExit(1)
78+
79+
return Args(
80+
json_mode=json_mode,
81+
require_tasks=require_tasks,
82+
include_tasks=include_tasks,
83+
paths_only=paths_only,
84+
)
85+
86+
87+
def _dir_has_entries(path: Path) -> bool:
88+
try:
89+
return path.is_dir() and any(path.iterdir())
90+
except OSError:
91+
return False
92+
93+
94+
def _available_docs(paths: FeaturePaths, include_tasks: bool) -> list[str]:
95+
docs: list[str] = []
96+
if paths.research.is_file():
97+
docs.append("research.md")
98+
if paths.data_model.is_file():
99+
docs.append("data-model.md")
100+
if _dir_has_entries(paths.contracts_dir):
101+
docs.append("contracts/")
102+
if paths.quickstart.is_file():
103+
docs.append("quickstart.md")
104+
if include_tasks and paths.tasks.is_file():
105+
docs.append("tasks.md")
106+
return docs
107+
108+
109+
def _print_paths_only(paths: FeaturePaths, json_mode: bool) -> None:
110+
if json_mode:
111+
sys.stdout.write(
112+
_json_line(
113+
{
114+
"REPO_ROOT": str(paths.repo_root),
115+
"BRANCH": paths.current_branch,
116+
"FEATURE_DIR": str(paths.feature_dir),
117+
"FEATURE_SPEC": str(paths.feature_spec),
118+
"IMPL_PLAN": str(paths.impl_plan),
119+
"TASKS": str(paths.tasks),
120+
}
121+
)
122+
)
123+
return
124+
125+
print(f"REPO_ROOT: {paths.repo_root}")
126+
print(f"BRANCH: {paths.current_branch}")
127+
print(f"FEATURE_DIR: {paths.feature_dir}")
128+
print(f"FEATURE_SPEC: {paths.feature_spec}")
129+
print(f"IMPL_PLAN: {paths.impl_plan}")
130+
print(f"TASKS: {paths.tasks}")
131+
132+
133+
def _check_file(path: Path, description: str) -> None:
134+
marker = "✓" if path.is_file() else "✗"
135+
print(f" {marker} {description}")
136+
137+
138+
def _check_dir(path: Path, description: str) -> None:
139+
marker = "✓" if _dir_has_entries(path) else "✗"
140+
print(f" {marker} {description}")
141+
142+
143+
def _print_text_results(paths: FeaturePaths, include_tasks: bool) -> None:
144+
print(f"FEATURE_DIR:{paths.feature_dir}")
145+
print("AVAILABLE_DOCS:")
146+
_check_file(paths.research, "research.md")
147+
_check_file(paths.data_model, "data-model.md")
148+
_check_dir(paths.contracts_dir, "contracts/")
149+
_check_file(paths.quickstart, "quickstart.md")
150+
if include_tasks:
151+
_check_file(paths.tasks, "tasks.md")
152+
153+
154+
def main(argv: list[str] | None = None) -> int:
155+
args = _parse_args(list(argv if argv is not None else sys.argv[1:]))
156+
157+
try:
158+
paths = get_feature_paths(
159+
no_persist=args.paths_only,
160+
script_file=Path(__file__),
161+
)
162+
except SystemExit as exc:
163+
if exc.code == 0:
164+
return 0
165+
print("ERROR: Failed to resolve feature paths", file=sys.stderr)
166+
return int(exc.code) if isinstance(exc.code, int) else 1
167+
168+
if args.paths_only:
169+
_print_paths_only(paths, args.json_mode)
170+
return 0
171+
172+
if not paths.feature_dir.is_dir():
173+
print(f"ERROR: Feature directory not found: {paths.feature_dir}", file=sys.stderr)
174+
print(
175+
f"Run {format_speckit_command('specify', paths.repo_root)} first to create the feature structure.",
176+
file=sys.stderr,
177+
)
178+
return 1
179+
180+
if not paths.impl_plan.is_file():
181+
print(f"ERROR: plan.md not found in {paths.feature_dir}", file=sys.stderr)
182+
print(
183+
f"Run {format_speckit_command('plan', paths.repo_root)} first to create the implementation plan.",
184+
file=sys.stderr,
185+
)
186+
return 1
187+
188+
if args.require_tasks and not paths.tasks.is_file():
189+
print(f"ERROR: tasks.md not found in {paths.feature_dir}", file=sys.stderr)
190+
print(
191+
f"Run {format_speckit_command('tasks', paths.repo_root)} first to create the task list.",
192+
file=sys.stderr,
193+
)
194+
return 1
195+
196+
docs = _available_docs(paths, args.include_tasks)
197+
if args.json_mode:
198+
sys.stdout.write(
199+
_json_line({"FEATURE_DIR": str(paths.feature_dir), "AVAILABLE_DOCS": docs})
200+
)
201+
else:
202+
_print_text_results(paths, args.include_tasks)
203+
return 0
204+
205+
206+
if __name__ == "__main__":
207+
raise SystemExit(main())

0 commit comments

Comments
 (0)