devlyn-cli 2.8.1 → 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,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))