cupel-cli 0.3.0__py3-none-any.whl

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.
cupel.py ADDED
@@ -0,0 +1,130 @@
1
+ # ABOUTME: The `cupel` console script of the Python package (ADR-125): downloads this platform's release binary on
2
+ # ABOUTME: first run, checks it against the release's SHA256SUMS, caches it, and runs it with the caller's arguments.
3
+ """Verification suite that grades what a coding agent produced. Runs the cupel release binary for this platform."""
4
+
5
+ from __future__ import annotations
6
+
7
+ import hashlib
8
+ import io
9
+ import os
10
+ import platform
11
+ import subprocess
12
+ import sys
13
+ import tarfile
14
+ import tempfile
15
+ import urllib.error
16
+ import urllib.request
17
+ import zipfile
18
+
19
+ # The release this package runs.
20
+ __version__ = "0.3.0"
21
+
22
+ RELEASES = "https://github.com/tysont/cupel/releases"
23
+
24
+ # Each release target's triple and archive format, by `<os>-<arch>` in Rust's spelling, as
25
+ # dist/targets.toml lists them.
26
+ TARGETS: dict[str, list[str]] = {
27
+ "linux-x86_64": ["x86_64-unknown-linux-musl", "tar.gz"],
28
+ "linux-aarch64": ["aarch64-unknown-linux-musl", "tar.gz"],
29
+ "macos-x86_64": ["x86_64-apple-darwin", "tar.gz"],
30
+ "macos-aarch64": ["aarch64-apple-darwin", "tar.gz"],
31
+ "windows-x86_64": ["x86_64-pc-windows-msvc", "zip"],
32
+ }
33
+
34
+ OS = {"Linux": "linux", "Darwin": "macos", "Windows": "windows"}
35
+ ARCH = {"x86_64": "x86_64", "AMD64": "x86_64", "aarch64": "aarch64", "arm64": "aarch64", "ARM64": "aarch64"}
36
+
37
+
38
+ class InstallError(Exception):
39
+ """The release binary could not be downloaded or checked."""
40
+
41
+
42
+ def target() -> list[str]:
43
+ system, machine = platform.system(), platform.machine()
44
+ found = TARGETS.get(f"{OS.get(system)}-{ARCH.get(machine)}")
45
+ if found is None:
46
+ raise InstallError(f"no release for {system} {machine}; see {RELEASES}")
47
+ return found
48
+
49
+
50
+ def cache_dir() -> str:
51
+ """Where downloaded binaries are kept: CUPEL_CACHE_DIR, else the user's cache directory."""
52
+ if os.environ.get("CUPEL_CACHE_DIR"):
53
+ return os.environ["CUPEL_CACHE_DIR"]
54
+ if os.name == "nt":
55
+ return os.path.join(os.environ.get("LOCALAPPDATA", os.path.expanduser("~")), "cupel")
56
+ return os.path.join(os.environ.get("XDG_CACHE_HOME", os.path.expanduser("~/.cache")), "cupel")
57
+
58
+
59
+ def download(url: str) -> bytes:
60
+ try:
61
+ with urllib.request.urlopen(url) as response:
62
+ body: bytes = response.read()
63
+ return body
64
+ except urllib.error.HTTPError as error:
65
+ raise InstallError(f"could not download {url}: HTTP {error.code}") from error
66
+ except urllib.error.URLError as error:
67
+ raise InstallError(f"could not download {url}: {error.reason}") from error
68
+
69
+
70
+ def listed_digest(sums: str, archive: str) -> str:
71
+ """The digest SHA256SUMS gives `archive`, in hex."""
72
+ for line in sums.splitlines():
73
+ fields = line.split()
74
+ if len(fields) == 2 and fields[1].lstrip("*") == archive:
75
+ return fields[0]
76
+ raise InstallError(f"SHA256SUMS lists no {archive}")
77
+
78
+
79
+ def extract(data: bytes, archive_format: str, into: str) -> None:
80
+ if archive_format == "zip":
81
+ with zipfile.ZipFile(io.BytesIO(data)) as archive:
82
+ archive.extractall(into)
83
+ return
84
+ with tarfile.open(fileobj=io.BytesIO(data), mode="r:gz") as archive:
85
+ if hasattr(tarfile, "data_filter"):
86
+ archive.extractall(into, filter="data")
87
+ else:
88
+ archive.extractall(into)
89
+
90
+
91
+ def install(path: str, triple: str, archive_format: str) -> None:
92
+ """Downloads the release archive for `triple`, checks it, and moves its binary to `path`."""
93
+ base = f"{os.environ.get('CUPEL_DOWNLOAD_URL') or RELEASES}/download/v{__version__}"
94
+ archive = f"cupel-{__version__}-{triple}.{archive_format}"
95
+ listed = listed_digest(download(f"{base}/SHA256SUMS").decode("utf-8"), archive)
96
+ print(f"cupel: downloading {base}/{archive}", file=sys.stderr, flush=True)
97
+ data = download(f"{base}/{archive}")
98
+ actual = hashlib.sha256(data).hexdigest()
99
+ if actual != listed:
100
+ raise InstallError(f"checksum mismatch for {archive}: SHA256SUMS has {listed}, the download is {actual}")
101
+ with tempfile.TemporaryDirectory(prefix=".download-", dir=os.path.dirname(path)) as staging:
102
+ extract(data, archive_format, staging)
103
+ os.replace(os.path.join(staging, f"cupel-{__version__}-{triple}", os.path.basename(path)), path)
104
+
105
+
106
+ def binary() -> str:
107
+ """The cached release binary, downloaded first if it is not there."""
108
+ triple, archive_format = target()
109
+ name = "cupel.exe" if os.name == "nt" else "cupel"
110
+ path = os.path.join(cache_dir(), __version__, triple, name)
111
+ if not os.path.exists(path):
112
+ os.makedirs(os.path.dirname(path), exist_ok=True)
113
+ install(path, triple, archive_format)
114
+ return path
115
+
116
+
117
+ def main() -> None:
118
+ try:
119
+ path = binary()
120
+ except (InstallError, OSError) as error:
121
+ # Exit 3, Cupel's environment code: the binary could not run, so nothing was graded.
122
+ print(f"cupel: {error}", file=sys.stderr)
123
+ sys.exit(3)
124
+ if os.name == "nt":
125
+ sys.exit(subprocess.call([path, *sys.argv[1:]]))
126
+ os.execv(path, [path, *sys.argv[1:]])
127
+
128
+
129
+ if __name__ == "__main__":
130
+ main()
@@ -0,0 +1,7 @@
1
+ Metadata-Version: 2.4
2
+ Name: cupel-cli
3
+ Version: 0.3.0
4
+ Summary: Verification suite that grades what a coding agent produced. Runs the cupel release binary for this platform.
5
+ Requires-Python: >=3.9
6
+ License-Expression: BSD-3-Clause
7
+ Project-URL: Homepage, https://github.com/tysont/cupel
@@ -0,0 +1,5 @@
1
+ cupel.py,sha256=_dFMYnoRgPG2TUnd-7k4qWhp_vI5HQr3JtivEDPGAgc,5057
2
+ cupel_cli-0.3.0.dist-info/entry_points.txt,sha256=ziaB9rYT3wykLBUjYq3RgKwr7ZJRXisLDHWRRE0LbGs,36
3
+ cupel_cli-0.3.0.dist-info/WHEEL,sha256=G2gURzTEtmeR8nrdXUJfNiB3VYVxigPQ-bEQujpNiNs,82
4
+ cupel_cli-0.3.0.dist-info/METADATA,sha256=pE73aOyN1EgxHqikAoYXMnRDSmHoTeUOhwRMIGV5gmE,283
5
+ cupel_cli-0.3.0.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: flit 3.12.0
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -0,0 +1,3 @@
1
+ [console_scripts]
2
+ cupel=cupel:main
3
+