devlyn-cli 2.8.0 → 2.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,517 @@
1
+ #!/usr/bin/env python3
2
+ """Deterministic PHASE 6 final-diff gate for /devlyn:resolve."""
3
+ from __future__ import annotations
4
+
5
+ import argparse
6
+ import importlib.util
7
+ import json
8
+ import pathlib
9
+ import shutil
10
+ import subprocess
11
+ import sys
12
+ import tempfile
13
+
14
+ sys.dont_write_bytecode = True
15
+
16
+ FINDINGS_NAME = "finish-gate.findings.jsonl"
17
+ SUMMARY_NAME = "finish-gate.summary.json"
18
+ PHASE = "finish_gate"
19
+
20
+
21
+ class Malformed(Exception):
22
+ def __init__(self, message: str, file_ref: str = ".devlyn/pipeline.state.json"):
23
+ super().__init__(message)
24
+ self.file_ref = file_ref
25
+
26
+
27
+ def load_spec_verify():
28
+ helper = pathlib.Path(__file__).with_name("spec-verify-check.py")
29
+ spec = importlib.util.spec_from_file_location("devlyn_spec_verify_check", helper)
30
+ if spec is None or spec.loader is None:
31
+ raise RuntimeError(f"cannot load {helper}")
32
+ module = importlib.util.module_from_spec(spec)
33
+ spec.loader.exec_module(module)
34
+ return module
35
+
36
+
37
+ SPEC_VERIFY = load_spec_verify()
38
+
39
+
40
+ def git(work: pathlib.Path, *args: str) -> subprocess.CompletedProcess[str]:
41
+ return subprocess.run(
42
+ ["git", *args],
43
+ cwd=str(work),
44
+ capture_output=True,
45
+ text=True,
46
+ )
47
+
48
+
49
+ def git_check(work: pathlib.Path, *args: str) -> str:
50
+ proc = git(work, *args)
51
+ if proc.returncode != 0:
52
+ raise AssertionError(proc.stderr or proc.stdout or f"git {' '.join(args)} failed")
53
+ return proc.stdout.strip()
54
+
55
+
56
+ def read_state(path: pathlib.Path) -> dict:
57
+ if not path.is_file():
58
+ raise Malformed(f"{path} is missing")
59
+ try:
60
+ data = SPEC_VERIFY.loads_strict_json(path.read_text(encoding="utf-8"))
61
+ except (OSError, ValueError) as e:
62
+ raise Malformed(f"{path} is not valid JSON: {e}") from e
63
+ if not isinstance(data, dict):
64
+ raise Malformed(f"{path} must contain a JSON object")
65
+ return data
66
+
67
+
68
+ def ensure_commit(work: pathlib.Path, sha: str, label: str) -> None:
69
+ proc = git(work, "rev-parse", "--verify", "--quiet", f"{sha}^{{commit}}")
70
+ if proc.returncode != 0:
71
+ raise Malformed(f"{label} is not a valid commit: {sha!r}")
72
+
73
+
74
+ def write_findings(devlyn_dir: pathlib.Path, findings: list[dict]) -> None:
75
+ devlyn_dir.mkdir(parents=True, exist_ok=True)
76
+ with (devlyn_dir / FINDINGS_NAME).open("w", encoding="utf-8") as handle:
77
+ for finding in findings:
78
+ handle.write(json.dumps(finding) + "\n")
79
+
80
+
81
+ def write_summary(devlyn_dir: pathlib.Path, summary: dict) -> None:
82
+ devlyn_dir.mkdir(parents=True, exist_ok=True)
83
+ (devlyn_dir / SUMMARY_NAME).write_text(json.dumps(summary, sort_keys=True) + "\n", encoding="utf-8")
84
+
85
+
86
+ def make_finding(
87
+ seq: int,
88
+ rule_id: str,
89
+ message: str,
90
+ file_ref: str,
91
+ *,
92
+ status: str,
93
+ fix_hint: str,
94
+ criterion_ref: str,
95
+ ) -> dict:
96
+ return {
97
+ "id": f"FINISH-{seq:04d}",
98
+ "rule_id": rule_id,
99
+ "level": "error",
100
+ "severity": "CRITICAL",
101
+ "confidence": 1.0,
102
+ "message": message,
103
+ "file": file_ref,
104
+ "line": 1,
105
+ "phase": PHASE,
106
+ "criterion_ref": criterion_ref,
107
+ "fix_hint": fix_hint,
108
+ "blocking": True,
109
+ "status": status,
110
+ }
111
+
112
+
113
+ def malformed_finding(error: Malformed) -> dict:
114
+ return make_finding(
115
+ 1,
116
+ "scope.finish-gate-malformed",
117
+ str(error),
118
+ error.file_ref,
119
+ status="open",
120
+ criterion_ref="finish_gate/state",
121
+ fix_hint=(
122
+ "Restore a valid pipeline state and plan authorized_surface block "
123
+ "before final reporting can continue."
124
+ ),
125
+ )
126
+
127
+
128
+ def load_authorized_surface(devlyn_dir: pathlib.Path) -> list[str]:
129
+ plan_path = devlyn_dir / "plan.md"
130
+ if not plan_path.is_file():
131
+ raise Malformed(
132
+ "PHASE 6 finish gate requires .devlyn/plan.md with authorized_surface; the file is missing.",
133
+ ".devlyn/plan.md",
134
+ )
135
+ try:
136
+ plan_text = plan_path.read_text(encoding="utf-8")
137
+ except OSError as e:
138
+ raise Malformed(f"Cannot read {plan_path}: {e}", ".devlyn/plan.md") from e
139
+
140
+ _found, block = SPEC_VERIFY.extract_authorized_surface_block(plan_text)
141
+ data = None
142
+ if block is None:
143
+ parse_error = (
144
+ "plan.md must include a `<!-- devlyn:authorized-surface -->` "
145
+ "section with a fenced ```json``` authorized_surface block."
146
+ )
147
+ else:
148
+ try:
149
+ data = SPEC_VERIFY.loads_strict_json(block)
150
+ parse_error = SPEC_VERIFY.validate_authorized_surface_shape(data)
151
+ except ValueError as e:
152
+ parse_error = f"authorized_surface json block is invalid JSON: {e}"
153
+ if parse_error:
154
+ raise Malformed(f"plan.md authorized_surface is malformed: {parse_error}", ".devlyn/plan.md")
155
+ return list(data["authorized_surface"])
156
+
157
+
158
+ def cleanup_window(work: pathlib.Path, state: dict) -> set[str]:
159
+ if "cleanup" in (state.get("bypasses") or []):
160
+ return set()
161
+ phases = state.get("phases") or {}
162
+ cleanup = phases.get("cleanup") if isinstance(phases, dict) else None
163
+ if cleanup is None:
164
+ return set()
165
+ if not isinstance(cleanup, dict):
166
+ raise Malformed("phases.cleanup must be an object or null")
167
+ if cleanup.get("verdict") is None:
168
+ return set()
169
+ pre_sha = (cleanup.get("pre_sha") or "").strip()
170
+ post_sha = (cleanup.get("post_sha") or "").strip()
171
+ if not pre_sha or not post_sha:
172
+ raise Malformed("phases.cleanup with a verdict must include both pre_sha and post_sha")
173
+ ensure_commit(work, pre_sha, "phases.cleanup.pre_sha")
174
+ ensure_commit(work, post_sha, "phases.cleanup.post_sha")
175
+ proc = git(work, "diff", "--name-only", pre_sha, post_sha)
176
+ if proc.returncode != 0:
177
+ raise Malformed(f"cannot compute cleanup window: {proc.stderr.strip()}")
178
+ return {line.strip() for line in proc.stdout.splitlines() if line.strip()}
179
+
180
+
181
+ def changed_files(work: pathlib.Path, base_sha: str) -> set[str]:
182
+ proc = git(work, "diff", "--name-only", base_sha, "--")
183
+ if proc.returncode != 0:
184
+ detail = proc.stderr.strip() or proc.stdout.strip() or "git diff --name-only failed"
185
+ raise Malformed(f"cannot compute finish-gate changed files: {detail}")
186
+ return {line.strip() for line in proc.stdout.splitlines() if line.strip()}
187
+
188
+
189
+ def devlyn_relative_prefix(work: pathlib.Path, devlyn_dir: pathlib.Path) -> str:
190
+ try:
191
+ relative = devlyn_dir.resolve().relative_to(work.resolve()).as_posix().strip("/")
192
+ except ValueError as e:
193
+ raise Malformed(f"--devlyn-dir must be inside the work tree: {devlyn_dir}") from e
194
+ if not relative or relative == ".":
195
+ raise Malformed("--devlyn-dir must not be the work tree root")
196
+ return relative
197
+
198
+
199
+ def is_under_prefix(path: str, prefix: str) -> bool:
200
+ return path == prefix or path.startswith(f"{prefix}/")
201
+
202
+
203
+ def path_exists_at_base(work: pathlib.Path, base_sha: str, path: str) -> bool:
204
+ return git(work, "cat-file", "-e", f"{base_sha}:{path}").returncode == 0
205
+
206
+
207
+ def remove_worktree_path(path: pathlib.Path) -> None:
208
+ if path.is_symlink() or path.is_file():
209
+ path.unlink()
210
+ elif path.is_dir():
211
+ shutil.rmtree(path)
212
+
213
+
214
+ def revert_offender(work: pathlib.Path, base_sha: str, path: str) -> tuple[bool, str | None, bool]:
215
+ existed_at_base = path_exists_at_base(work, base_sha, path)
216
+ if existed_at_base:
217
+ proc = git(work, "checkout", base_sha, "--", path)
218
+ if proc.returncode != 0:
219
+ return (False, proc.stderr.strip() or proc.stdout.strip(), True)
220
+ return (True, None, True)
221
+
222
+ proc = git(work, "rm", "-f", "--ignore-unmatch", "--", path)
223
+ target = work / path
224
+ try:
225
+ if target.exists() or target.is_symlink():
226
+ remove_worktree_path(target)
227
+ except OSError as e:
228
+ return (False, str(e), False)
229
+ if proc.returncode != 0:
230
+ cached = git(work, "rm", "-f", "--cached", "--ignore-unmatch", "--", path)
231
+ if cached.returncode != 0:
232
+ detail = proc.stderr.strip() or cached.stderr.strip() or proc.stdout.strip()
233
+ return (False, detail, False)
234
+ return (True, None, False)
235
+
236
+
237
+ def run_gate(work: pathlib.Path, devlyn_dir: pathlib.Path) -> int:
238
+ state_path = devlyn_dir / "pipeline.state.json"
239
+ findings_path = devlyn_dir / FINDINGS_NAME
240
+ try:
241
+ state = read_state(state_path)
242
+ findings_path.unlink(missing_ok=True)
243
+ if state.get("mode") == "verify-only":
244
+ write_summary(devlyn_dir, {"mode": "verify-only", "skipped": True, "exit": 0})
245
+ return 0
246
+
247
+ base_sha = ((state.get("base_ref") or {}).get("sha") or "").strip()
248
+ if not base_sha:
249
+ raise Malformed("pipeline.state.json must include non-empty base_ref.sha")
250
+ ensure_commit(work, base_sha, "base_ref.sha")
251
+ surface = load_authorized_surface(devlyn_dir)
252
+ cleanup_paths = cleanup_window(work, state)
253
+ changed = changed_files(work, base_sha)
254
+ devlyn_prefix = devlyn_relative_prefix(work, devlyn_dir)
255
+ checked = sorted(path for path in changed if not is_under_prefix(path, devlyn_prefix))
256
+ except Malformed as e:
257
+ findings_path.unlink(missing_ok=True)
258
+ write_findings(devlyn_dir, [malformed_finding(e)])
259
+ write_summary(devlyn_dir, {"exit": 1, "malformed": str(e)})
260
+ return 1
261
+
262
+ offenders = sorted(
263
+ path for path in checked
264
+ if (
265
+ not SPEC_VERIFY.path_matches_surface(path, surface)
266
+ and path not in cleanup_paths
267
+ )
268
+ )
269
+ if not offenders:
270
+ findings_path.unlink(missing_ok=True)
271
+ write_summary(devlyn_dir, {
272
+ "mode": state.get("mode"),
273
+ "checked": len(checked),
274
+ "offenders": 0,
275
+ "exit": 0,
276
+ })
277
+ return 0
278
+
279
+ findings: list[dict] = []
280
+ failed = False
281
+ reverted = 0
282
+ revert_failed = 0
283
+ for seq, path in enumerate(offenders, start=1):
284
+ ok, detail, existed_at_base = revert_offender(work, base_sha, path)
285
+ status = "reverted" if ok else "revert-failed"
286
+ failed = failed or not ok
287
+ if ok:
288
+ reverted += 1
289
+ else:
290
+ revert_failed += 1
291
+ action = "restored to base_ref.sha" if existed_at_base else "removed because it did not exist at base_ref.sha"
292
+ message = f"Final diff touched an unaudited file outside authorized_surface and cleanup window: {path}"
293
+ if detail:
294
+ message = f"{message} ({detail})"
295
+ findings.append(make_finding(
296
+ seq,
297
+ "scope.finish-unaudited-file",
298
+ message,
299
+ path,
300
+ status=status,
301
+ criterion_ref="plan.md/authorized_surface",
302
+ fix_hint=(
303
+ "This file was outside authorized_surface plus the cleanup window "
304
+ f"and was {action}; do not ship unlicensed final-diff changes."
305
+ ),
306
+ ))
307
+ write_findings(devlyn_dir, findings)
308
+ exit_code = 2 if failed else 0
309
+ write_summary(devlyn_dir, {
310
+ "mode": state.get("mode"),
311
+ "checked": len(checked),
312
+ "offenders": len(offenders),
313
+ "reverted": reverted,
314
+ "revert_failed": revert_failed,
315
+ "exit": exit_code,
316
+ })
317
+ return exit_code
318
+
319
+
320
+ def write_text(path: pathlib.Path, text: str) -> None:
321
+ path.parent.mkdir(parents=True, exist_ok=True)
322
+ path.write_text(text, encoding="utf-8")
323
+
324
+
325
+ def write_state(devlyn: pathlib.Path, state: dict) -> None:
326
+ write_text(devlyn / "pipeline.state.json", json.dumps(state, indent=2) + "\n")
327
+
328
+
329
+ def read_findings(devlyn: pathlib.Path) -> list[dict]:
330
+ path = devlyn / FINDINGS_NAME
331
+ return [SPEC_VERIFY.loads_strict_json(line) for line in path.read_text().splitlines() if line.strip()]
332
+
333
+
334
+ def read_summary(devlyn: pathlib.Path) -> dict:
335
+ return SPEC_VERIFY.loads_strict_json((devlyn / SUMMARY_NAME).read_text(encoding="utf-8"))
336
+
337
+
338
+ def assert_summary(devlyn: pathlib.Path, expected: dict) -> None:
339
+ summary = read_summary(devlyn)
340
+ for key, value in expected.items():
341
+ assert summary.get(key) == value, summary
342
+
343
+
344
+ def make_fixture(root: pathlib.Path, name: str, *, mode: str = "full") -> tuple[pathlib.Path, pathlib.Path, str]:
345
+ work = root / name
346
+ work.mkdir()
347
+ git_check(work, "init", "-q")
348
+ write_text(work / "src" / "app.txt", "base app\n")
349
+ write_text(work / "notes.txt", "base notes\n")
350
+ write_text(work / "cleanable.txt", "base cleanable\n")
351
+ git_check(work, "add", "-A")
352
+ git_check(work, "-c", "user.email=t@t", "-c", "user.name=t", "commit", "-q", "-m", "base")
353
+ base_sha = git_check(work, "rev-parse", "HEAD")
354
+ devlyn = work / ".devlyn"
355
+ write_text(
356
+ devlyn / "plan.md",
357
+ "# PLAN\n\n<!-- devlyn:authorized-surface -->\n## Files to touch\n\n"
358
+ "- `src/app.txt` (edit): implement the requested change.\n\n"
359
+ "```json\n"
360
+ '{"authorized_surface": ["src/app.txt"]}\n'
361
+ "```\n",
362
+ )
363
+ write_state(devlyn, {
364
+ "mode": mode,
365
+ "base_ref": {"sha": base_sha},
366
+ "phases": {"cleanup": None},
367
+ })
368
+ return (work, devlyn, base_sha)
369
+
370
+
371
+ def checked_run_gate(work: pathlib.Path, devlyn: pathlib.Path) -> int:
372
+ rc = run_gate(work, devlyn)
373
+ assert not any(path.name == "__pycache__" for path in work.rglob("__pycache__"))
374
+ return rc
375
+
376
+
377
+ def self_test() -> int:
378
+ with tempfile.TemporaryDirectory() as tmp:
379
+ root = pathlib.Path(tmp)
380
+
381
+ work, devlyn, _base = make_fixture(root, "conforming")
382
+ write_text(work / "src" / "app.txt", "changed app\n")
383
+ assert checked_run_gate(work, devlyn) == 0
384
+ assert not (devlyn / FINDINGS_NAME).exists()
385
+ assert_summary(devlyn, {"mode": "full", "checked": 1, "offenders": 0, "exit": 0})
386
+
387
+ work, devlyn, _base = make_fixture(root, "tracked-mutation")
388
+ write_text(work / "notes.txt", "polluted\n")
389
+ assert checked_run_gate(work, devlyn) == 0
390
+ assert (work / "notes.txt").read_text(encoding="utf-8") == "base notes\n"
391
+ findings = read_findings(devlyn)
392
+ assert findings[0]["rule_id"] == "scope.finish-unaudited-file"
393
+ assert findings[0]["status"] == "reverted"
394
+ assert_summary(devlyn, {
395
+ "mode": "full", "checked": 1, "offenders": 1,
396
+ "reverted": 1, "revert_failed": 0, "exit": 0,
397
+ })
398
+
399
+ work, devlyn, _base = make_fixture(root, "added-file")
400
+ write_text(work / "runtime.txt", "late\n")
401
+ git_check(work, "add", "runtime.txt")
402
+ assert checked_run_gate(work, devlyn) == 0
403
+ assert not (work / "runtime.txt").exists()
404
+ assert "runtime.txt" not in git_check(work, "diff", "--name-only", _base).splitlines()
405
+ assert_summary(devlyn, {
406
+ "mode": "full", "checked": 1, "offenders": 1,
407
+ "reverted": 1, "revert_failed": 0, "exit": 0,
408
+ })
409
+
410
+ work, devlyn, _base = make_fixture(root, "devlyn-owned-tracked-mutation")
411
+ archived = devlyn / "runs" / "prior" / "pipeline.state.json"
412
+ write_text(archived, "tracked devlyn state\n")
413
+ git_check(work, "add", archived.relative_to(work).as_posix())
414
+ git_check(work, "-c", "user.email=t@t", "-c", "user.name=t", "commit", "-q", "-m", "track devlyn state")
415
+ write_text(archived, "mutated devlyn state\n")
416
+ write_text(work / "notes.txt", "polluted\n")
417
+ assert checked_run_gate(work, devlyn) == 0
418
+ assert archived.read_text(encoding="utf-8") == "mutated devlyn state\n"
419
+ assert (work / "notes.txt").read_text(encoding="utf-8") == "base notes\n"
420
+ findings = read_findings(devlyn)
421
+ assert [finding["file"] for finding in findings] == ["notes.txt"]
422
+ assert findings[0]["status"] == "reverted"
423
+ assert_summary(devlyn, {
424
+ "mode": "full", "checked": 1, "offenders": 1,
425
+ "reverted": 1, "revert_failed": 0, "exit": 0,
426
+ })
427
+
428
+ work, devlyn, base = make_fixture(root, "cleanup-window")
429
+ write_text(work / "cleanable.txt", "cleanup changed\n")
430
+ git_check(work, "add", "cleanable.txt")
431
+ git_check(work, "-c", "user.email=t@t", "-c", "user.name=t", "commit", "-q", "-m", "cleanup")
432
+ post_sha = git_check(work, "rev-parse", "HEAD")
433
+ write_state(devlyn, {
434
+ "mode": "full",
435
+ "base_ref": {"sha": base},
436
+ "phases": {"cleanup": {"verdict": "PASS", "pre_sha": base, "post_sha": post_sha}},
437
+ })
438
+ assert checked_run_gate(work, devlyn) == 0
439
+ assert not (devlyn / FINDINGS_NAME).exists()
440
+ assert_summary(devlyn, {"mode": "full", "checked": 1, "offenders": 0, "exit": 0})
441
+
442
+ work, devlyn, base = make_fixture(root, "cleanup-malformed")
443
+ write_state(devlyn, {
444
+ "mode": "full",
445
+ "base_ref": {"sha": base},
446
+ "phases": {"cleanup": {"verdict": "PASS", "pre_sha": base}},
447
+ })
448
+ assert checked_run_gate(work, devlyn) == 1
449
+ assert read_findings(devlyn)[0]["rule_id"] == "scope.finish-gate-malformed"
450
+ summary = read_summary(devlyn)
451
+ assert summary["exit"] == 1, summary
452
+ assert "phases.cleanup with a verdict" in summary["malformed"], summary
453
+
454
+ work, devlyn, _base = make_fixture(root, "verify-only", mode="verify-only")
455
+ (devlyn / "plan.md").unlink()
456
+ write_findings(devlyn, [make_finding(
457
+ 1,
458
+ "scope.finish-unaudited-file",
459
+ "stale",
460
+ "stale.txt",
461
+ status="reverted",
462
+ criterion_ref="plan.md/authorized_surface",
463
+ fix_hint="stale",
464
+ )])
465
+ write_state(devlyn, {"mode": "verify-only", "phases": {}})
466
+ assert checked_run_gate(work, devlyn) == 0
467
+ assert not (devlyn / FINDINGS_NAME).exists()
468
+ assert read_summary(devlyn) == {"exit": 0, "mode": "verify-only", "skipped": True}
469
+
470
+ work, devlyn, _base = make_fixture(root, "deleted-file")
471
+ (work / "notes.txt").unlink()
472
+ assert checked_run_gate(work, devlyn) == 0
473
+ assert (work / "notes.txt").read_text(encoding="utf-8") == "base notes\n"
474
+ assert read_findings(devlyn)[0]["status"] == "reverted"
475
+ assert_summary(devlyn, {
476
+ "mode": "full", "checked": 1, "offenders": 1,
477
+ "reverted": 1, "revert_failed": 0, "exit": 0,
478
+ })
479
+
480
+ work, devlyn, _base = make_fixture(root, "git-diff-failure")
481
+ original_git = globals()["git"]
482
+
483
+ def failing_git(work_arg: pathlib.Path, *args: str) -> subprocess.CompletedProcess[str]:
484
+ if args[:2] == ("diff", "--name-only"):
485
+ return subprocess.CompletedProcess(["git", *args], 128, "", "simulated diff failure")
486
+ return original_git(work_arg, *args)
487
+
488
+ globals()["git"] = failing_git
489
+ try:
490
+ assert checked_run_gate(work, devlyn) == 1
491
+ finding = read_findings(devlyn)[0]
492
+ assert finding["rule_id"] == "scope.finish-gate-malformed"
493
+ assert "cannot compute finish-gate changed files" in finding["message"]
494
+ summary = read_summary(devlyn)
495
+ assert summary["exit"] == 1, summary
496
+ assert "cannot compute finish-gate changed files" in summary["malformed"], summary
497
+ finally:
498
+ globals()["git"] = original_git
499
+
500
+ return 0
501
+
502
+
503
+ def main() -> int:
504
+ ap = argparse.ArgumentParser(description=__doc__)
505
+ ap.add_argument("--devlyn-dir", default=".devlyn")
506
+ ap.add_argument("--self-test", action="store_true")
507
+ args = ap.parse_args()
508
+ if args.self_test:
509
+ return self_test()
510
+ devlyn_dir = pathlib.Path(args.devlyn_dir)
511
+ if not devlyn_dir.is_absolute():
512
+ devlyn_dir = (pathlib.Path.cwd() / devlyn_dir).resolve()
513
+ return run_gate(devlyn_dir.parent, devlyn_dir)
514
+
515
+
516
+ if __name__ == "__main__":
517
+ raise SystemExit(main())
@@ -0,0 +1,44 @@
1
+ #!/usr/bin/env python3
2
+ from __future__ import annotations
3
+
4
+ import os, signal, subprocess, sys
5
+
6
+
7
+ def fail(message: str) -> int:
8
+ sys.stderr.write(f"error: {message}\n")
9
+ return 2
10
+
11
+
12
+ def main(argv: list[str]) -> int:
13
+ if len(argv) < 4:
14
+ return fail("usage: run-bounded.py <seconds> -- <cmd> [args...]")
15
+ try:
16
+ seconds = int(argv[1])
17
+ except ValueError:
18
+ return fail("seconds must be a positive integer")
19
+ if seconds <= 0:
20
+ return fail("seconds must be a positive integer")
21
+ if argv[2] != "--":
22
+ return fail("expected -- before command")
23
+
24
+ child = subprocess.Popen(argv[3:], start_new_session=True)
25
+ try:
26
+ return child.wait(timeout=seconds)
27
+ except subprocess.TimeoutExpired:
28
+ try:
29
+ os.killpg(child.pid, signal.SIGTERM)
30
+ except ProcessLookupError:
31
+ return 124
32
+ try:
33
+ child.wait(timeout=5)
34
+ except subprocess.TimeoutExpired:
35
+ try:
36
+ os.killpg(child.pid, signal.SIGKILL)
37
+ except ProcessLookupError:
38
+ pass
39
+ child.wait()
40
+ return 124
41
+
42
+
43
+ if __name__ == "__main__":
44
+ raise SystemExit(main(sys.argv))