mkrun 0.2.0__tar.gz → 0.3.0__tar.gz
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.
- {mkrun-0.2.0 → mkrun-0.3.0}/PKG-INFO +29 -1
- {mkrun-0.2.0 → mkrun-0.3.0}/README.md +28 -0
- {mkrun-0.2.0 → mkrun-0.3.0}/pyproject.toml +7 -1
- {mkrun-0.2.0 → mkrun-0.3.0}/src/make/__init__.py +17 -3
- {mkrun-0.2.0 → mkrun-0.3.0}/src/make/env.py +8 -1
- {mkrun-0.2.0 → mkrun-0.3.0}/src/make/errors.py +4 -0
- mkrun-0.3.0/src/make/http.py +126 -0
- mkrun-0.3.0/src/make/poll.py +76 -0
- mkrun-0.3.0/src/make/proc.py +92 -0
- {mkrun-0.2.0 → mkrun-0.3.0}/src/make/sh.py +31 -10
- {mkrun-0.2.0 → mkrun-0.3.0}/tests/conftest.py +15 -0
- mkrun-0.3.0/tests/test_http.py +78 -0
- mkrun-0.3.0/tests/test_poll.py +38 -0
- mkrun-0.3.0/tests/test_proc.py +37 -0
- {mkrun-0.2.0 → mkrun-0.3.0}/tests/test_sh.py +21 -0
- {mkrun-0.2.0 → mkrun-0.3.0}/tests/test_sources.py +4 -0
- {mkrun-0.2.0 → mkrun-0.3.0}/.gitignore +0 -0
- {mkrun-0.2.0 → mkrun-0.3.0}/LICENSE-APACHE +0 -0
- {mkrun-0.2.0 → mkrun-0.3.0}/LICENSE-MIT +0 -0
- {mkrun-0.2.0 → mkrun-0.3.0}/src/make/__main__.py +0 -0
- {mkrun-0.2.0 → mkrun-0.3.0}/src/make/bootstrap.py +0 -0
- {mkrun-0.2.0 → mkrun-0.3.0}/src/make/cli.py +0 -0
- {mkrun-0.2.0 → mkrun-0.3.0}/src/make/completions.py +0 -0
- {mkrun-0.2.0 → mkrun-0.3.0}/src/make/config.py +0 -0
- {mkrun-0.2.0 → mkrun-0.3.0}/src/make/context.py +0 -0
- {mkrun-0.2.0 → mkrun-0.3.0}/src/make/discovery.py +0 -0
- {mkrun-0.2.0 → mkrun-0.3.0}/src/make/fs.py +0 -0
- {mkrun-0.2.0 → mkrun-0.3.0}/src/make/params.py +0 -0
- {mkrun-0.2.0 → mkrun-0.3.0}/src/make/runner.py +0 -0
- {mkrun-0.2.0 → mkrun-0.3.0}/src/make/tasks.py +0 -0
- {mkrun-0.2.0 → mkrun-0.3.0}/src/make/testing.py +0 -0
- {mkrun-0.2.0 → mkrun-0.3.0}/tests/helpers.py +0 -0
- {mkrun-0.2.0 → mkrun-0.3.0}/tests/test_cli.py +0 -0
- {mkrun-0.2.0 → mkrun-0.3.0}/tests/test_config.py +0 -0
- {mkrun-0.2.0 → mkrun-0.3.0}/tests/test_env.py +0 -0
- {mkrun-0.2.0 → mkrun-0.3.0}/tests/test_fs.py +0 -0
- {mkrun-0.2.0 → mkrun-0.3.0}/tests/test_overrides.py +0 -0
- {mkrun-0.2.0 → mkrun-0.3.0}/tests/test_params.py +0 -0
- {mkrun-0.2.0 → mkrun-0.3.0}/tests/test_runner.py +0 -0
- {mkrun-0.2.0 → mkrun-0.3.0}/tests/test_startup.py +0 -0
- {mkrun-0.2.0 → mkrun-0.3.0}/tests/test_tasks.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: mkrun
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.3.0
|
|
4
4
|
Summary: A command runner whose tasks are Python functions -- shareable as versioned packages.
|
|
5
5
|
Project-URL: Homepage, https://github.com/optersoft/make
|
|
6
6
|
Project-URL: Source, https://github.com/optersoft/make
|
|
@@ -177,6 +177,34 @@ is data and cannot become syntax. Every one of these honours `--dry-run`; give
|
|
|
177
177
|
`sh.out(..., dry="...")` or `sh.ok(..., dry=False)` a stand-in when the value
|
|
178
178
|
steers later logic.
|
|
179
179
|
|
|
180
|
+
Watchers that exit the moment stdin closes (tailwindcss `--watch`) take
|
|
181
|
+
`sh.background(..., hold_stdin=True)` — a stdin that never reaches EOF, without
|
|
182
|
+
the `tail -f /dev/null |` shell wrapper.
|
|
183
|
+
|
|
184
|
+
### Waiting, HTTP and processes
|
|
185
|
+
|
|
186
|
+
The three loops every task file used to hand-roll in bash — poll-until-ready,
|
|
187
|
+
`curl -w '%{http_code}'`, and `lsof | kill`:
|
|
188
|
+
|
|
189
|
+
```python
|
|
190
|
+
from make import poll, http, proc
|
|
191
|
+
|
|
192
|
+
pid = poll(lambda: sh.out("pidof", "-s", pkg, check=False, dry="4711"),
|
|
193
|
+
timeout=10, message=f"{pkg} to start") # returns the truthy value
|
|
194
|
+
|
|
195
|
+
http.get(url).status # 200, 404, ... or 0 when unreachable
|
|
196
|
+
http.ok(url) # bool; a bad status is a result, not an exception
|
|
197
|
+
http.wait("http://localhost:8002/", timeout=60) # raises WaitTimeout if never up
|
|
198
|
+
|
|
199
|
+
proc.port_pids(8080) # who is LISTENING on :8080
|
|
200
|
+
proc.reap_port(8080) # terminate them; -> the pids signalled
|
|
201
|
+
proc.reap("dx serve.*8002") # pgrep -f pattern; anchor it to YOUR thing
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
All of them honour `--dry-run`: `poll()` returns its `dry=` stand-in without
|
|
205
|
+
looping, `http` answers with `dry_status=`, and `proc` reports what it would
|
|
206
|
+
signal without killing anything.
|
|
207
|
+
|
|
180
208
|
### Changing files
|
|
181
209
|
|
|
182
210
|
A dry run that suppresses every command but still writes files looks safe and
|
|
@@ -153,6 +153,34 @@ is data and cannot become syntax. Every one of these honours `--dry-run`; give
|
|
|
153
153
|
`sh.out(..., dry="...")` or `sh.ok(..., dry=False)` a stand-in when the value
|
|
154
154
|
steers later logic.
|
|
155
155
|
|
|
156
|
+
Watchers that exit the moment stdin closes (tailwindcss `--watch`) take
|
|
157
|
+
`sh.background(..., hold_stdin=True)` — a stdin that never reaches EOF, without
|
|
158
|
+
the `tail -f /dev/null |` shell wrapper.
|
|
159
|
+
|
|
160
|
+
### Waiting, HTTP and processes
|
|
161
|
+
|
|
162
|
+
The three loops every task file used to hand-roll in bash — poll-until-ready,
|
|
163
|
+
`curl -w '%{http_code}'`, and `lsof | kill`:
|
|
164
|
+
|
|
165
|
+
```python
|
|
166
|
+
from make import poll, http, proc
|
|
167
|
+
|
|
168
|
+
pid = poll(lambda: sh.out("pidof", "-s", pkg, check=False, dry="4711"),
|
|
169
|
+
timeout=10, message=f"{pkg} to start") # returns the truthy value
|
|
170
|
+
|
|
171
|
+
http.get(url).status # 200, 404, ... or 0 when unreachable
|
|
172
|
+
http.ok(url) # bool; a bad status is a result, not an exception
|
|
173
|
+
http.wait("http://localhost:8002/", timeout=60) # raises WaitTimeout if never up
|
|
174
|
+
|
|
175
|
+
proc.port_pids(8080) # who is LISTENING on :8080
|
|
176
|
+
proc.reap_port(8080) # terminate them; -> the pids signalled
|
|
177
|
+
proc.reap("dx serve.*8002") # pgrep -f pattern; anchor it to YOUR thing
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
All of them honour `--dry-run`: `poll()` returns its `dry=` stand-in without
|
|
181
|
+
looping, `http` answers with `dry_status=`, and `proc` reports what it would
|
|
182
|
+
signal without killing anything.
|
|
183
|
+
|
|
156
184
|
### Changing files
|
|
157
185
|
|
|
158
186
|
A dry run that suppresses every command but still writes files looks safe and
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "mkrun"
|
|
3
|
-
version = "0.
|
|
3
|
+
version = "0.3.0"
|
|
4
4
|
description = "A command runner whose tasks are Python functions -- shareable as versioned packages."
|
|
5
5
|
readme = "README.md"
|
|
6
6
|
requires-python = ">=3.11"
|
|
@@ -120,3 +120,9 @@ ignore = ["E501", "B008", "RUF012", "SIM117", "RUF022"]
|
|
|
120
120
|
# file, so `tests/**` alone would not reach the optersoft package.
|
|
121
121
|
"tests/**" = ["F811", "B006"]
|
|
122
122
|
"optersoft/tests/**" = ["F811", "B006"]
|
|
123
|
+
# The examples are verbatim copies of files that live in other repositories,
|
|
124
|
+
# where `make` is an ordinary third-party import. Here `src` marks it
|
|
125
|
+
# first-party, so isort wants a different order in the copy than in the
|
|
126
|
+
# original -- and the original is the one that has to be right. Keeping them
|
|
127
|
+
# byte-identical is worth more than import grouping in a file nobody imports.
|
|
128
|
+
"optersoft/examples/**" = ["I001"]
|
|
@@ -17,12 +17,22 @@ distributable as versioned packages instead of a directory someone `git clone`d.
|
|
|
17
17
|
|
|
18
18
|
from __future__ import annotations
|
|
19
19
|
|
|
20
|
-
__version__ = "0.
|
|
20
|
+
__version__ = "0.3.0"
|
|
21
21
|
|
|
22
|
-
from . import config, env, fs
|
|
22
|
+
from . import config, env, fs, http, proc
|
|
23
23
|
from .context import Context, confirm, ctx, echo, info, note, paint, path, step, warn
|
|
24
|
-
from .errors import
|
|
24
|
+
from .errors import (
|
|
25
|
+
Aborted,
|
|
26
|
+
CommandFailed,
|
|
27
|
+
ConfigError,
|
|
28
|
+
MakeError,
|
|
29
|
+
TaskError,
|
|
30
|
+
ToolMissing,
|
|
31
|
+
UsageError,
|
|
32
|
+
WaitTimeout,
|
|
33
|
+
)
|
|
25
34
|
from .params import Arg, arg
|
|
35
|
+
from .poll import poll
|
|
26
36
|
from .runner import invoke
|
|
27
37
|
from .sh import Result, sh
|
|
28
38
|
from .tasks import Group, Task, group, registry, task
|
|
@@ -40,6 +50,9 @@ __all__ = [
|
|
|
40
50
|
"fs",
|
|
41
51
|
"config",
|
|
42
52
|
"arg",
|
|
53
|
+
"poll",
|
|
54
|
+
"http",
|
|
55
|
+
"proc",
|
|
43
56
|
# output
|
|
44
57
|
"step",
|
|
45
58
|
"info",
|
|
@@ -63,6 +76,7 @@ __all__ = [
|
|
|
63
76
|
"ToolMissing",
|
|
64
77
|
"CommandFailed",
|
|
65
78
|
"Aborted",
|
|
79
|
+
"WaitTimeout",
|
|
66
80
|
]
|
|
67
81
|
|
|
68
82
|
|
|
@@ -49,7 +49,14 @@ _INTERPOLATION = re.compile(r"\$\{([A-Za-z_][A-Za-z0-9_]*)\}|\$([A-Za-z_][A-Za-z
|
|
|
49
49
|
#: `~/.just` entry is deliberate migration support: if you are coming from
|
|
50
50
|
#: `just`, your credentials already live there, and a tool that demands you move
|
|
51
51
|
#: your secrets before it will run is a tool nobody adopts.
|
|
52
|
-
|
|
52
|
+
#:
|
|
53
|
+
#: `MAKE_CONFIG_DIR` replaces the whole list -- the hermeticity valve. A test
|
|
54
|
+
#: harness (including this repo's own) must not inherit the developer's real
|
|
55
|
+
#: `~/.make/sources.toml` and secrets through a spawned `mk`.
|
|
56
|
+
_configured = os.environ.get("MAKE_CONFIG_DIR")
|
|
57
|
+
CONFIG_DIRS = (
|
|
58
|
+
(Path(_configured).expanduser(),) if _configured else (Path.home() / ".make", Path.home() / ".just")
|
|
59
|
+
)
|
|
53
60
|
|
|
54
61
|
|
|
55
62
|
def config_dir() -> Path:
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
"""HTTP checks without curl.
|
|
2
|
+
|
|
3
|
+
Three repos shelled out to `curl -sS -o /dev/null -w '%{http_code}'` and then
|
|
4
|
+
parsed the printed code back out of a string. The request is one stdlib call;
|
|
5
|
+
what the shell versions were really carrying was the *conventions* -- never
|
|
6
|
+
raise on a bad status, report an unreachable host as code 0, honour
|
|
7
|
+
`--dry-run`. Those live here now:
|
|
8
|
+
|
|
9
|
+
from make import http
|
|
10
|
+
|
|
11
|
+
http.get(url).status # 200, 404, ... or 0 when unreachable
|
|
12
|
+
http.ok(url) # bool: 2xx/3xx
|
|
13
|
+
http.wait("http://localhost:8002/", timeout=60) # poll until it answers
|
|
14
|
+
|
|
15
|
+
A non-2xx status is a *result*, not an exception -- smoke tests want to compare
|
|
16
|
+
it, and a deploy wants to name it in its own error. Only `wait()` raises, and
|
|
17
|
+
only on timeout.
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
from __future__ import annotations
|
|
21
|
+
|
|
22
|
+
from dataclasses import dataclass, field
|
|
23
|
+
|
|
24
|
+
from .context import current, echo, paint
|
|
25
|
+
from .poll import poll as _poll
|
|
26
|
+
|
|
27
|
+
__all__ = ["Response", "get", "ok", "request", "status", "wait"]
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
@dataclass(frozen=True)
|
|
31
|
+
class Response:
|
|
32
|
+
"""Outcome of one request. `status` 0 means the host never answered."""
|
|
33
|
+
|
|
34
|
+
url: str
|
|
35
|
+
status: int
|
|
36
|
+
body: str = ""
|
|
37
|
+
headers: dict[str, str] = field(default_factory=dict)
|
|
38
|
+
"""Response headers, keys lower-cased."""
|
|
39
|
+
skipped: bool = False
|
|
40
|
+
"""True when the request was not made because of `--dry-run`."""
|
|
41
|
+
|
|
42
|
+
@property
|
|
43
|
+
def ok(self) -> bool:
|
|
44
|
+
return 200 <= self.status < 400
|
|
45
|
+
|
|
46
|
+
def __bool__(self) -> bool:
|
|
47
|
+
return self.ok
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def request(
|
|
51
|
+
url: str,
|
|
52
|
+
*,
|
|
53
|
+
method: str = "GET",
|
|
54
|
+
timeout: float = 20.0,
|
|
55
|
+
headers: dict[str, str] | None = None,
|
|
56
|
+
data: bytes | str | None = None,
|
|
57
|
+
read_body: bool = True,
|
|
58
|
+
echo_cmd: bool = True,
|
|
59
|
+
dry_status: int = 200,
|
|
60
|
+
dry_body: str = "",
|
|
61
|
+
) -> Response:
|
|
62
|
+
"""Perform one request, returning a `Response` whatever happens.
|
|
63
|
+
|
|
64
|
+
Give `dry_status=` a stand-in when the status steers later logic, for the
|
|
65
|
+
same reason `sh.out(dry=...)` exists.
|
|
66
|
+
"""
|
|
67
|
+
context = current()
|
|
68
|
+
if echo_cmd and (not context.quiet or context.dry_run):
|
|
69
|
+
prefix = paint("[dry-run] ", "yellow") if context.dry_run else ""
|
|
70
|
+
echo(prefix + paint("~ ", "cyan", "bold") + paint(f"{method} {url}", "cyan"))
|
|
71
|
+
if context.dry_run:
|
|
72
|
+
return Response(url=url, status=dry_status, body=dry_body, skipped=True)
|
|
73
|
+
|
|
74
|
+
import urllib.error
|
|
75
|
+
import urllib.request
|
|
76
|
+
|
|
77
|
+
payload = data.encode() if isinstance(data, str) else data
|
|
78
|
+
req = urllib.request.Request(url, method=method, headers=headers or {}, data=payload)
|
|
79
|
+
try:
|
|
80
|
+
with urllib.request.urlopen(req, timeout=timeout) as answer:
|
|
81
|
+
body = answer.read().decode(errors="replace") if read_body else ""
|
|
82
|
+
heads = {k.lower(): v for k, v in answer.getheaders()}
|
|
83
|
+
return Response(url=url, status=answer.status, body=body, headers=heads)
|
|
84
|
+
except urllib.error.HTTPError as exc:
|
|
85
|
+
body = exc.read().decode(errors="replace") if read_body else ""
|
|
86
|
+
heads = {k.lower(): v for k, v in (exc.headers or {}).items()}
|
|
87
|
+
return Response(url=url, status=exc.code, body=body, headers=heads)
|
|
88
|
+
except (urllib.error.URLError, TimeoutError, ConnectionError, OSError):
|
|
89
|
+
return Response(url=url, status=0)
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def get(url: str, **kwargs: object) -> Response:
|
|
93
|
+
return request(url, **kwargs) # type: ignore[arg-type]
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def status(url: str, **kwargs: object) -> int:
|
|
97
|
+
return request(url, **kwargs).status # type: ignore[arg-type]
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def ok(url: str, **kwargs: object) -> bool:
|
|
101
|
+
kwargs.setdefault("echo_cmd", False)
|
|
102
|
+
return request(url, **kwargs).ok # type: ignore[arg-type]
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def wait(
|
|
106
|
+
url: str,
|
|
107
|
+
*,
|
|
108
|
+
timeout: float = 60.0,
|
|
109
|
+
interval: float = 1.0,
|
|
110
|
+
request_timeout: float = 5.0,
|
|
111
|
+
message: str | None = None,
|
|
112
|
+
) -> Response:
|
|
113
|
+
"""Poll `url` until it answers with a 2xx/3xx; raise `WaitTimeout` otherwise."""
|
|
114
|
+
|
|
115
|
+
def ready() -> Response | None:
|
|
116
|
+
answer = request(url, timeout=request_timeout, read_body=False, echo_cmd=False)
|
|
117
|
+
return answer if answer.ok else None
|
|
118
|
+
|
|
119
|
+
result: Response = _poll(
|
|
120
|
+
ready,
|
|
121
|
+
timeout=timeout,
|
|
122
|
+
interval=interval,
|
|
123
|
+
message=message or url,
|
|
124
|
+
dry=Response(url=url, status=200, skipped=True),
|
|
125
|
+
)
|
|
126
|
+
return result
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
"""Waiting for something to become true.
|
|
2
|
+
|
|
3
|
+
Every repo in the fleet had its own spelling of the same loop: bash
|
|
4
|
+
`until curl -sf ...; do sleep 1; done`, bash `for _ in $(seq 1 60)`, bash
|
|
5
|
+
`until getprop sys.boot_completed`, and a Python `while` with a deadline. Each
|
|
6
|
+
one re-decided the timeout, the interval, what to print, and what a dry run
|
|
7
|
+
should pretend -- and two of them could hang forever.
|
|
8
|
+
|
|
9
|
+
from make import poll, sh
|
|
10
|
+
|
|
11
|
+
pid = poll(lambda: sh.out("pidof", "-s", pkg, check=False, dry="4711"),
|
|
12
|
+
timeout=10, message=f"{pkg} to start")
|
|
13
|
+
|
|
14
|
+
`poll()` returns the first truthy value `check` produces, so the condition and
|
|
15
|
+
the answer are one expression. On timeout it raises `WaitTimeout`, naming what
|
|
16
|
+
it was waiting for.
|
|
17
|
+
|
|
18
|
+
Under `--dry-run` the loop does not run at all and `dry` is returned: the
|
|
19
|
+
commands inside `check` are suppressed, so looping on their stand-in answers
|
|
20
|
+
could never terminate.
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
from __future__ import annotations
|
|
24
|
+
|
|
25
|
+
import time
|
|
26
|
+
from collections.abc import Callable
|
|
27
|
+
from typing import Any, TypeVar
|
|
28
|
+
|
|
29
|
+
from .context import current, echo, paint
|
|
30
|
+
from .errors import WaitTimeout
|
|
31
|
+
|
|
32
|
+
__all__ = ["poll"]
|
|
33
|
+
|
|
34
|
+
T = TypeVar("T")
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def poll(
|
|
38
|
+
check: Callable[[], T],
|
|
39
|
+
*,
|
|
40
|
+
timeout: float = 60.0,
|
|
41
|
+
interval: float = 1.0,
|
|
42
|
+
message: str | None = None,
|
|
43
|
+
dry: Any = True,
|
|
44
|
+
) -> T:
|
|
45
|
+
"""Call `check` until it returns a truthy value, then return that value.
|
|
46
|
+
|
|
47
|
+
Args:
|
|
48
|
+
check: called repeatedly; the first truthy return ends the wait.
|
|
49
|
+
timeout: seconds before giving up with `WaitTimeout`.
|
|
50
|
+
interval: seconds between attempts.
|
|
51
|
+
message: what is being waited for -- used in output and in the error.
|
|
52
|
+
dry: the stand-in returned immediately under `--dry-run`.
|
|
53
|
+
"""
|
|
54
|
+
context = current()
|
|
55
|
+
label = message or getattr(check, "__name__", "condition")
|
|
56
|
+
if label == "<lambda>":
|
|
57
|
+
label = "condition"
|
|
58
|
+
|
|
59
|
+
if context.dry_run:
|
|
60
|
+
echo(paint("[dry-run] ", "yellow") + paint(f"wait for {label}", "cyan"))
|
|
61
|
+
return dry
|
|
62
|
+
|
|
63
|
+
if not context.quiet:
|
|
64
|
+
echo(paint("~ ", "cyan", "bold") + paint(f"waiting for {label} (up to {timeout:.0f}s)", "cyan"))
|
|
65
|
+
|
|
66
|
+
deadline = time.monotonic() + timeout
|
|
67
|
+
while True:
|
|
68
|
+
value = check()
|
|
69
|
+
if value:
|
|
70
|
+
return value
|
|
71
|
+
if time.monotonic() >= deadline:
|
|
72
|
+
raise WaitTimeout(
|
|
73
|
+
f"timed out after {timeout:.0f}s waiting for {label}",
|
|
74
|
+
hint="raise timeout= if it genuinely needs longer",
|
|
75
|
+
)
|
|
76
|
+
time.sleep(interval)
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
"""Finding and reaping processes, with the discipline built in.
|
|
2
|
+
|
|
3
|
+
`lsof`/`pgrep`/`pkill` calls were scattered through the fleet's task files, and
|
|
4
|
+
each call site had to re-learn the same three rules: match on the *listening*
|
|
5
|
+
socket, not every connection to the port; scope `pkill -f` patterns tightly,
|
|
6
|
+
because an unscoped one once matched its own caller; and never kill anything
|
|
7
|
+
under `--dry-run`. One implementation, so the rules cannot be re-forgotten:
|
|
8
|
+
|
|
9
|
+
from make import proc
|
|
10
|
+
|
|
11
|
+
proc.port_pids(8080) # who is LISTENING on :8080
|
|
12
|
+
proc.reap_port(8080) # terminate them; -> pids it signalled
|
|
13
|
+
proc.reap("dx serve.*drive-web") # pattern reap; anchor it to YOUR thing
|
|
14
|
+
|
|
15
|
+
Everything goes through `sh`, so `make.testing.record()` sees these calls and
|
|
16
|
+
`--dry-run` reports instead of killing.
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
from __future__ import annotations
|
|
20
|
+
|
|
21
|
+
from collections.abc import Sequence
|
|
22
|
+
|
|
23
|
+
from .context import current, note
|
|
24
|
+
from .sh import sh
|
|
25
|
+
|
|
26
|
+
__all__ = ["file_pids", "pattern_pids", "port_pids", "reap", "reap_port"]
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def _pids(lines: Sequence[str]) -> list[int]:
|
|
30
|
+
found: list[int] = []
|
|
31
|
+
for line in lines:
|
|
32
|
+
token = line.split()[0] if line.split() else ""
|
|
33
|
+
if token.isdigit():
|
|
34
|
+
found.append(int(token))
|
|
35
|
+
return found
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def port_pids(port: int, *, dry: Sequence[str] = ()) -> list[int]:
|
|
39
|
+
"""PIDs *listening* on a TCP port -- not everyone connected to it."""
|
|
40
|
+
lines = sh.lines("lsof", "-t", f"-iTCP:{port}", "-sTCP:LISTEN", check=False, echo_cmd=False, dry=dry)
|
|
41
|
+
return _pids(lines)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def file_pids(*paths: object, dry: Sequence[str] = ()) -> list[int]:
|
|
45
|
+
"""PIDs holding any of these files open -- WAL locks, log holders.
|
|
46
|
+
|
|
47
|
+
The reap that actually matters for a database: an orphan can hold the lock
|
|
48
|
+
from no port at all, invisible to a port-scoped reap.
|
|
49
|
+
"""
|
|
50
|
+
if not paths:
|
|
51
|
+
return []
|
|
52
|
+
lines = sh.lines("lsof", "-t", "--", *paths, check=False, echo_cmd=False, dry=dry)
|
|
53
|
+
return sorted(set(_pids(lines)))
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def pattern_pids(pattern: str, *, dry: Sequence[str] = ()) -> list[int]:
|
|
57
|
+
"""PIDs whose full command line matches `pattern` (a `pgrep -f` regex).
|
|
58
|
+
|
|
59
|
+
Anchor the pattern to something only your target carries -- a port, a
|
|
60
|
+
profile directory, an AVD name. A bare program name matches other people's
|
|
61
|
+
processes, and once matched a task's own parent shell.
|
|
62
|
+
"""
|
|
63
|
+
lines = sh.lines("pgrep", "-f", pattern, check=False, echo_cmd=False, dry=dry)
|
|
64
|
+
return _pids(lines)
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def _kill(pids: list[int], *, force: bool, label: str) -> list[int]:
|
|
68
|
+
if not pids:
|
|
69
|
+
return []
|
|
70
|
+
if current().dry_run:
|
|
71
|
+
note(f"would signal {label}: {', '.join(map(str, pids))}")
|
|
72
|
+
return []
|
|
73
|
+
for pid in pids:
|
|
74
|
+
sh("kill", "-9" if force else "-15", str(pid), check=False, echo_cmd=False)
|
|
75
|
+
note(f"signalled {label}: {', '.join(map(str, pids))}")
|
|
76
|
+
return pids
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def reap_port(port: int, *, force: bool = False, dry: Sequence[str] = ()) -> list[int]:
|
|
80
|
+
"""Terminate whatever is listening on a port. Returns the PIDs signalled.
|
|
81
|
+
|
|
82
|
+
An empty answer is not an error -- "nothing to reap" is the normal case.
|
|
83
|
+
"""
|
|
84
|
+
return _kill(port_pids(port, dry=dry), force=force, label=f":{port}")
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def reap(pattern: str, *, force: bool = False, dry: Sequence[str] = ()) -> list[int]:
|
|
88
|
+
"""Terminate every process matching a `pgrep -f` pattern. Returns the PIDs.
|
|
89
|
+
|
|
90
|
+
The pattern rules from `pattern_pids` apply doubly here: this one kills.
|
|
91
|
+
"""
|
|
92
|
+
return _kill(pattern_pids(pattern, dry=dry), force=force, label=pattern)
|
|
@@ -234,8 +234,16 @@ class _Sh:
|
|
|
234
234
|
cwd: str | Path | None = None,
|
|
235
235
|
env: Mapping[str, str] | None = None,
|
|
236
236
|
log: str | Path | None = None,
|
|
237
|
+
hold_stdin: bool = False,
|
|
237
238
|
) -> subprocess.Popen[Any] | None:
|
|
238
|
-
"""Spawn a detached child (dev servers, watchers). None under --dry-run.
|
|
239
|
+
"""Spawn a detached child (dev servers, watchers). None under --dry-run.
|
|
240
|
+
|
|
241
|
+
`hold_stdin=True` gives the child a stdin that never reaches EOF, and
|
|
242
|
+
survives this process exiting: the child itself inherits the write end
|
|
243
|
+
of the pipe it reads from. Some watchers (tailwindcss `--watch`) exit
|
|
244
|
+
the moment stdin closes -- the workaround used to be a shell wrapper
|
|
245
|
+
piping `tail -f /dev/null` into them.
|
|
246
|
+
"""
|
|
239
247
|
argv = _stringify(args)
|
|
240
248
|
display = shlex.join(argv)
|
|
241
249
|
if current().dry_run:
|
|
@@ -248,15 +256,28 @@ class _Sh:
|
|
|
248
256
|
stream = open(log, "ab") # noqa: SIM115 - owned by the child
|
|
249
257
|
else:
|
|
250
258
|
stream = subprocess.DEVNULL
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
stdin=
|
|
258
|
-
|
|
259
|
-
|
|
259
|
+
stdin: int = subprocess.DEVNULL
|
|
260
|
+
pass_fds: tuple[int, ...] = ()
|
|
261
|
+
held: tuple[int, int] | None = None
|
|
262
|
+
if hold_stdin:
|
|
263
|
+
held = os.pipe()
|
|
264
|
+
os.set_inheritable(held[1], True)
|
|
265
|
+
stdin, pass_fds = held[0], (held[1],)
|
|
266
|
+
try:
|
|
267
|
+
return subprocess.Popen(
|
|
268
|
+
argv,
|
|
269
|
+
cwd=str(cwd) if cwd else None,
|
|
270
|
+
env=_merged_env(env),
|
|
271
|
+
stdout=stream,
|
|
272
|
+
stderr=subprocess.STDOUT,
|
|
273
|
+
stdin=stdin,
|
|
274
|
+
pass_fds=pass_fds,
|
|
275
|
+
start_new_session=True,
|
|
276
|
+
)
|
|
277
|
+
finally:
|
|
278
|
+
if held is not None:
|
|
279
|
+
os.close(held[0])
|
|
280
|
+
os.close(held[1])
|
|
260
281
|
|
|
261
282
|
def replace_process(self, *args: Any, env: Mapping[str, str] | None = None) -> None:
|
|
262
283
|
"""`exec` into a command, replacing this process. Never returns."""
|
|
@@ -10,6 +10,21 @@ from make.tasks import Registry
|
|
|
10
10
|
from make.tasks import registry as global_registry
|
|
11
11
|
|
|
12
12
|
|
|
13
|
+
@pytest.fixture(autouse=True)
|
|
14
|
+
def hermetic_config_dirs(tmp_path: Path, monkeypatch: pytest.MonkeyPatch):
|
|
15
|
+
"""No test reads the developer's real ~/.make or ~/.just.
|
|
16
|
+
|
|
17
|
+
A real `~/.make/sources.toml` redirecting packages to sibling checkouts is
|
|
18
|
+
exactly what this tool documents -- and its relative paths resolve against
|
|
19
|
+
each test's tmp project, failing (or worse, quietly succeeding) there.
|
|
20
|
+
Tests that need config layers monkeypatch CONFIG_DIRS themselves, on top.
|
|
21
|
+
"""
|
|
22
|
+
from make import env as env_module
|
|
23
|
+
|
|
24
|
+
monkeypatch.setattr(env_module, "CONFIG_DIRS", (tmp_path / "make-home", tmp_path / "just-home"))
|
|
25
|
+
yield
|
|
26
|
+
|
|
27
|
+
|
|
13
28
|
@pytest.fixture(autouse=True)
|
|
14
29
|
def clean_registry():
|
|
15
30
|
"""Each test starts with an empty registry, and gives back what was there.
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
"""`http` carries the conventions the curl-in-a-string versions carried by hand."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import threading
|
|
6
|
+
from collections.abc import Iterator
|
|
7
|
+
from http.server import BaseHTTPRequestHandler, HTTPServer
|
|
8
|
+
|
|
9
|
+
import pytest
|
|
10
|
+
|
|
11
|
+
from make import http
|
|
12
|
+
from make.testing import context
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class _Handler(BaseHTTPRequestHandler):
|
|
16
|
+
def do_GET(self) -> None: # the http.server API's spelling
|
|
17
|
+
code = 404 if self.path == "/missing" else 200
|
|
18
|
+
body = b"hello"
|
|
19
|
+
self.send_response(code)
|
|
20
|
+
self.send_header("Content-Length", str(len(body)))
|
|
21
|
+
self.end_headers()
|
|
22
|
+
self.wfile.write(body)
|
|
23
|
+
|
|
24
|
+
def log_message(self, *args: object) -> None: # keep pytest output clean
|
|
25
|
+
pass
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
@pytest.fixture(scope="module")
|
|
29
|
+
def server() -> Iterator[str]:
|
|
30
|
+
httpd = HTTPServer(("127.0.0.1", 0), _Handler)
|
|
31
|
+
thread = threading.Thread(target=httpd.serve_forever, daemon=True)
|
|
32
|
+
thread.start()
|
|
33
|
+
yield f"http://127.0.0.1:{httpd.server_address[1]}"
|
|
34
|
+
httpd.shutdown()
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def test_get_returns_status_and_body(server: str):
|
|
38
|
+
with context():
|
|
39
|
+
answer = http.get(f"{server}/")
|
|
40
|
+
assert answer.status == 200
|
|
41
|
+
assert answer.ok
|
|
42
|
+
assert answer.body == "hello"
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def test_a_404_is_a_result_not_an_exception(server: str):
|
|
46
|
+
with context():
|
|
47
|
+
answer = http.get(f"{server}/missing")
|
|
48
|
+
assert answer.status == 404
|
|
49
|
+
assert not answer.ok
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def test_an_unreachable_host_is_status_zero():
|
|
53
|
+
with context():
|
|
54
|
+
# A reserved port on localhost nothing listens on.
|
|
55
|
+
answer = http.get("http://127.0.0.1:1/", timeout=0.5)
|
|
56
|
+
assert answer.status == 0
|
|
57
|
+
assert not answer
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def test_dry_run_makes_no_request():
|
|
61
|
+
with context(dry_run=True):
|
|
62
|
+
answer = http.get("http://127.0.0.1:1/", dry_status=204)
|
|
63
|
+
assert answer.skipped
|
|
64
|
+
assert answer.status == 204
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def test_wait_polls_until_the_url_answers(server: str):
|
|
68
|
+
with context():
|
|
69
|
+
answer = http.wait(f"{server}/", timeout=5, interval=0)
|
|
70
|
+
assert answer.ok
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def test_wait_raises_on_timeout():
|
|
74
|
+
from make.errors import WaitTimeout
|
|
75
|
+
|
|
76
|
+
with context():
|
|
77
|
+
with pytest.raises(WaitTimeout):
|
|
78
|
+
http.wait("http://127.0.0.1:1/", timeout=0, interval=0, request_timeout=0.2)
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"""`poll()` is the one loop the fleet used to write four different ways."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import pytest
|
|
6
|
+
|
|
7
|
+
from make import poll
|
|
8
|
+
from make.errors import WaitTimeout
|
|
9
|
+
from make.testing import context
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def test_returns_the_first_truthy_value_not_just_true():
|
|
13
|
+
answers = iter(["", "", "4711"])
|
|
14
|
+
with context():
|
|
15
|
+
assert poll(lambda: next(answers), timeout=5, interval=0) == "4711"
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def test_times_out_with_the_label_in_the_message():
|
|
19
|
+
with context():
|
|
20
|
+
with pytest.raises(WaitTimeout, match="the app to start"):
|
|
21
|
+
poll(lambda: False, timeout=0, interval=0, message="the app to start")
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def test_dry_run_never_calls_the_check():
|
|
25
|
+
calls: list[int] = []
|
|
26
|
+
|
|
27
|
+
def check() -> bool:
|
|
28
|
+
calls.append(1)
|
|
29
|
+
return False
|
|
30
|
+
|
|
31
|
+
with context(dry_run=True):
|
|
32
|
+
assert poll(check, timeout=5, dry="stand-in") == "stand-in"
|
|
33
|
+
assert calls == []
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def test_zero_timeout_still_tries_once():
|
|
37
|
+
with context():
|
|
38
|
+
assert poll(lambda: 42, timeout=0, interval=0) == 42
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"""`proc` centralises the lsof/pgrep/kill discipline the task files repeated."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from make import proc
|
|
6
|
+
from make.testing import context, record
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def test_port_pids_asks_for_listeners_only():
|
|
10
|
+
with record(responses={"lsof": "123\n456\n"}) as rec:
|
|
11
|
+
assert proc.port_pids(8080) == [123, 456]
|
|
12
|
+
assert rec.saw("-iTCP:8080")
|
|
13
|
+
assert rec.saw("-sTCP:LISTEN")
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def test_reap_port_signals_each_listener():
|
|
17
|
+
with record(responses={"lsof": "123\n"}) as rec:
|
|
18
|
+
assert proc.reap_port(8080) == [123]
|
|
19
|
+
assert rec.saw("kill", "-15", "123")
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def test_reap_force_uses_sigkill():
|
|
23
|
+
with record(responses={"pgrep": "99\n"}) as rec:
|
|
24
|
+
assert proc.reap("dx serve.*port 8002", force=True) == [99]
|
|
25
|
+
assert rec.saw("kill", "-9", "99")
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def test_nothing_matching_is_the_normal_case_not_an_error():
|
|
29
|
+
with record() as rec:
|
|
30
|
+
assert proc.reap_port(8080) == []
|
|
31
|
+
assert not rec.saw("kill")
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def test_dry_run_finds_but_never_kills():
|
|
35
|
+
with context(dry_run=True), record() as rec:
|
|
36
|
+
assert proc.reap_port(8080, dry=["123"]) == []
|
|
37
|
+
assert rec.commands == []
|
|
@@ -78,6 +78,27 @@ def test_missing_executable_is_a_tool_error_not_a_traceback(monkeypatch):
|
|
|
78
78
|
sh("definitely-not-a-real-binary")
|
|
79
79
|
|
|
80
80
|
|
|
81
|
+
def test_background_hold_stdin_keeps_a_watcher_alive():
|
|
82
|
+
"""`cat` exits on stdin EOF -- exactly like tailwindcss --watch."""
|
|
83
|
+
import time
|
|
84
|
+
|
|
85
|
+
with context():
|
|
86
|
+
plain = sh.background("cat", log="/dev/null")
|
|
87
|
+
held = sh.background("cat", log="/dev/null", hold_stdin=True)
|
|
88
|
+
assert plain is not None and held is not None
|
|
89
|
+
try:
|
|
90
|
+
deadline = time.monotonic() + 5
|
|
91
|
+
while plain.poll() is None and time.monotonic() < deadline:
|
|
92
|
+
time.sleep(0.05)
|
|
93
|
+
assert plain.poll() is not None # DEVNULL stdin: immediate EOF, cat exits
|
|
94
|
+
time.sleep(0.2)
|
|
95
|
+
assert held.poll() is None # held pipe: no EOF, cat stays up
|
|
96
|
+
finally:
|
|
97
|
+
held.kill()
|
|
98
|
+
if plain.poll() is None:
|
|
99
|
+
plain.kill()
|
|
100
|
+
|
|
101
|
+
|
|
81
102
|
def test_context_env_reaches_the_child():
|
|
82
103
|
captured = {}
|
|
83
104
|
|
|
@@ -104,6 +104,10 @@ def run_make(project: Path, *args: str) -> subprocess.CompletedProcess[str]:
|
|
|
104
104
|
environment = {k: v for k, v in os.environ.items() if k != "_MAKE_BOOTSTRAPPED"}
|
|
105
105
|
environment.pop("VIRTUAL_ENV", None)
|
|
106
106
|
environment["NO_COLOR"] = "1"
|
|
107
|
+
# Hermetic: the developer's real ~/.make/sources.toml must not redirect
|
|
108
|
+
# this test's packages -- its relative paths would resolve against the
|
|
109
|
+
# tmp project and fail, or worse, silently succeed.
|
|
110
|
+
environment["MAKE_CONFIG_DIR"] = str(project / "home")
|
|
107
111
|
return subprocess.run(
|
|
108
112
|
[sys.executable, "-m", "make", *args],
|
|
109
113
|
cwd=project,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|