canibuild 0.1.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.
- canibuild/__init__.py +3 -0
- canibuild/__main__.py +5 -0
- canibuild/check.py +391 -0
- canibuild/cli.py +163 -0
- canibuild/fixes.py +163 -0
- canibuild/lockfiles.py +576 -0
- canibuild/model.py +102 -0
- canibuild/py.typed +0 -0
- canibuild/pypi.py +196 -0
- canibuild/report.py +145 -0
- canibuild/rules.py +858 -0
- canibuild/sdist.py +199 -0
- canibuild/target.py +282 -0
- canibuild-0.1.0.dist-info/METADATA +207 -0
- canibuild-0.1.0.dist-info/RECORD +18 -0
- canibuild-0.1.0.dist-info/WHEEL +4 -0
- canibuild-0.1.0.dist-info/entry_points.txt +2 -0
- canibuild-0.1.0.dist-info/licenses/LICENSE +21 -0
canibuild/__init__.py
ADDED
canibuild/__main__.py
ADDED
canibuild/check.py
ADDED
|
@@ -0,0 +1,391 @@
|
|
|
1
|
+
"""Check one lockfile against one target using PyPI metadata and build rules."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import threading
|
|
6
|
+
from collections.abc import Callable, Sequence
|
|
7
|
+
from concurrent.futures import ThreadPoolExecutor
|
|
8
|
+
from dataclasses import dataclass, replace
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
|
|
11
|
+
from packaging.specifiers import InvalidSpecifier, SpecifierSet
|
|
12
|
+
from packaging.version import InvalidVersion, Version
|
|
13
|
+
|
|
14
|
+
from . import lockfiles, rules
|
|
15
|
+
from .fixes import fixes_for
|
|
16
|
+
from .model import Artifact, Finding, Pin, Report, Severity
|
|
17
|
+
from .pypi import FetchError, FileInfo, Index
|
|
18
|
+
from .sdist import SdistError, inspect_sdist
|
|
19
|
+
from .target import Target, wheel_is_compatible
|
|
20
|
+
|
|
21
|
+
DEFAULT_TOOLS: dict[str, tuple[str, ...]] = {
|
|
22
|
+
"uv.lock": ("uv",),
|
|
23
|
+
"pylock": ("pip", "uv pip"),
|
|
24
|
+
"requirements": ("pip", "uv pip"),
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def _admits(info: FileInfo, target: Target) -> bool:
|
|
29
|
+
if not info.requires_python:
|
|
30
|
+
return True
|
|
31
|
+
try:
|
|
32
|
+
return SpecifierSet(info.requires_python).contains(target.python_full, prereleases=True)
|
|
33
|
+
except InvalidSpecifier:
|
|
34
|
+
return True
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def _release_admits(files: Sequence[FileInfo], target: Target) -> bool:
|
|
38
|
+
return any(_admits(item, target) for item in files)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def _satisfies(specifier: str | None, version: Version) -> bool:
|
|
42
|
+
if specifier is None:
|
|
43
|
+
return True
|
|
44
|
+
try:
|
|
45
|
+
return SpecifierSet(specifier).contains(version, prereleases=True)
|
|
46
|
+
except InvalidSpecifier:
|
|
47
|
+
return True
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def _latest(index: Index, pin: Pin, target: Target) -> Version | None:
|
|
51
|
+
for version in index.versions(pin.name):
|
|
52
|
+
if not _satisfies(pin.specifier, version):
|
|
53
|
+
continue
|
|
54
|
+
files = index.release_files(pin.name, str(version))
|
|
55
|
+
if _release_admits(files, target):
|
|
56
|
+
return version
|
|
57
|
+
return None
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def _upgrade(
|
|
61
|
+
index: Index,
|
|
62
|
+
pin: Pin,
|
|
63
|
+
target: Target,
|
|
64
|
+
sdist_is_clean: Callable[[str, FileInfo], bool] | None = None,
|
|
65
|
+
) -> tuple[str, str] | None:
|
|
66
|
+
"""Return the oldest newer release that installs cleanly, and why.
|
|
67
|
+
|
|
68
|
+
The reason is "wheel" when that release has a wheel for the target, or "sdist" when
|
|
69
|
+
it would still build from source but its sdist shows none of the known problems.
|
|
70
|
+
"""
|
|
71
|
+
if pin.version is None:
|
|
72
|
+
return None
|
|
73
|
+
try:
|
|
74
|
+
pinned = Version(pin.version)
|
|
75
|
+
except InvalidVersion:
|
|
76
|
+
return None
|
|
77
|
+
tags = target.tags()
|
|
78
|
+
newer = sorted(version for version in index.versions(pin.name) if version > pinned)
|
|
79
|
+
sdist_checks = 0
|
|
80
|
+
for version in newer[:60]:
|
|
81
|
+
try:
|
|
82
|
+
files = index.release_files(pin.name, str(version))
|
|
83
|
+
except (FetchError, OSError, ValueError):
|
|
84
|
+
continue
|
|
85
|
+
usable = [item for item in files if not item.yanked]
|
|
86
|
+
if any(
|
|
87
|
+
item.packagetype == "bdist_wheel" and wheel_is_compatible(item.filename, tags)
|
|
88
|
+
for item in usable
|
|
89
|
+
):
|
|
90
|
+
return str(version), "wheel"
|
|
91
|
+
sdists = [item for item in usable if item.packagetype == "sdist"]
|
|
92
|
+
if sdist_is_clean is not None and sdists and sdist_checks < 5:
|
|
93
|
+
sdist_checks += 1
|
|
94
|
+
if sdist_is_clean(str(version), sdists[0]):
|
|
95
|
+
return str(version), "sdist"
|
|
96
|
+
return None
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
class _SharedVersions:
|
|
100
|
+
def __init__(self, index: Index, target: Target) -> None:
|
|
101
|
+
self._index = index
|
|
102
|
+
self._target = target
|
|
103
|
+
self._lock = threading.Lock()
|
|
104
|
+
self._cache: dict[str, tuple[Version, ...]] = {}
|
|
105
|
+
|
|
106
|
+
def get(self, name: str) -> tuple[Version, ...]:
|
|
107
|
+
with self._lock:
|
|
108
|
+
cached = self._cache.get(name)
|
|
109
|
+
if cached is not None:
|
|
110
|
+
return cached
|
|
111
|
+
try:
|
|
112
|
+
versions = tuple(
|
|
113
|
+
version
|
|
114
|
+
for version in self._index.versions(name)
|
|
115
|
+
if _release_admits(self._index.release_files(name, str(version)), self._target)
|
|
116
|
+
)
|
|
117
|
+
except (FetchError, OSError, ValueError):
|
|
118
|
+
versions = ()
|
|
119
|
+
with self._lock:
|
|
120
|
+
self._cache[name] = versions
|
|
121
|
+
return versions
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
@dataclass(frozen=True)
|
|
125
|
+
class _PinResult:
|
|
126
|
+
checked: int = 0
|
|
127
|
+
builds: int = 0
|
|
128
|
+
skipped: tuple[str, ...] = ()
|
|
129
|
+
findings: tuple[Finding, ...] = ()
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
def _with_fixes(
|
|
133
|
+
findings: list[Finding],
|
|
134
|
+
pin: Pin,
|
|
135
|
+
*,
|
|
136
|
+
upgrade_to: tuple[str, str] | None,
|
|
137
|
+
tools: Sequence[str],
|
|
138
|
+
note: str | None,
|
|
139
|
+
lockfile: str = "requirements.txt",
|
|
140
|
+
) -> list[Finding]:
|
|
141
|
+
result: list[Finding] = []
|
|
142
|
+
for finding in findings:
|
|
143
|
+
evidence = finding.evidence
|
|
144
|
+
if note is not None:
|
|
145
|
+
evidence = evidence + (note,)
|
|
146
|
+
if finding.severity.rank > Severity.INFO.rank:
|
|
147
|
+
fixes = fixes_for(
|
|
148
|
+
finding,
|
|
149
|
+
upgrade_to=upgrade_to[0] if upgrade_to else None,
|
|
150
|
+
upgrade_reason=upgrade_to[1] if upgrade_to else "wheel",
|
|
151
|
+
tools=tools,
|
|
152
|
+
lockfile=lockfile,
|
|
153
|
+
)
|
|
154
|
+
result.append(replace(finding, evidence=evidence, fixes=fixes))
|
|
155
|
+
else:
|
|
156
|
+
result.append(replace(finding, evidence=evidence))
|
|
157
|
+
return result
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
def _check_pin(
|
|
161
|
+
pin: Pin,
|
|
162
|
+
*,
|
|
163
|
+
target: Target,
|
|
164
|
+
index: Index,
|
|
165
|
+
tools: Sequence[str],
|
|
166
|
+
shared: _SharedVersions,
|
|
167
|
+
note: str | None = None,
|
|
168
|
+
lockfile: str = "requirements.txt",
|
|
169
|
+
) -> _PinResult:
|
|
170
|
+
tags = target.tags()
|
|
171
|
+
release: list[FileInfo] | None = None
|
|
172
|
+
|
|
173
|
+
def release_files() -> list[FileInfo]:
|
|
174
|
+
nonlocal release
|
|
175
|
+
if release is None:
|
|
176
|
+
assert pin.version is not None
|
|
177
|
+
release = index.release_files(pin.name, pin.version)
|
|
178
|
+
return release
|
|
179
|
+
|
|
180
|
+
if pin.wheels is None and not release_files():
|
|
181
|
+
return _PinResult(skipped=(f"{pin.name} {pin.version}: not found on PyPI, not checked",))
|
|
182
|
+
|
|
183
|
+
if pin.wheels is None:
|
|
184
|
+
wheels = [
|
|
185
|
+
item.filename
|
|
186
|
+
for item in release_files()
|
|
187
|
+
if item.packagetype == "bdist_wheel" and not item.yanked
|
|
188
|
+
]
|
|
189
|
+
else:
|
|
190
|
+
wheels = [artifact.filename for artifact in pin.wheels]
|
|
191
|
+
|
|
192
|
+
if any(wheel_is_compatible(filename, tags) for filename in wheels):
|
|
193
|
+
return _PinResult(checked=1)
|
|
194
|
+
|
|
195
|
+
if pin.sdist is not None:
|
|
196
|
+
sdist: Artifact | None = pin.sdist
|
|
197
|
+
else:
|
|
198
|
+
sdists = [
|
|
199
|
+
item for item in release_files() if item.packagetype == "sdist" and not item.yanked
|
|
200
|
+
]
|
|
201
|
+
sdist = Artifact(filename=sdists[0].filename, url=sdists[0].url) if sdists else None
|
|
202
|
+
|
|
203
|
+
if sdist is None:
|
|
204
|
+
finding = Finding(
|
|
205
|
+
package=pin.name,
|
|
206
|
+
version=pin.version,
|
|
207
|
+
severity=Severity.ERROR,
|
|
208
|
+
rule="no-installable-file",
|
|
209
|
+
message=f"no wheel for {target.describe()} and no sdist",
|
|
210
|
+
)
|
|
211
|
+
upgrade_to = _upgrade(index, pin, target)
|
|
212
|
+
return _PinResult(
|
|
213
|
+
findings=tuple(
|
|
214
|
+
_with_fixes(
|
|
215
|
+
[finding],
|
|
216
|
+
pin,
|
|
217
|
+
upgrade_to=upgrade_to,
|
|
218
|
+
tools=tools,
|
|
219
|
+
note=note,
|
|
220
|
+
lockfile=lockfile,
|
|
221
|
+
)
|
|
222
|
+
)
|
|
223
|
+
)
|
|
224
|
+
|
|
225
|
+
def analyze(url: str, filename: str, version: str | None) -> list[Finding]:
|
|
226
|
+
info = inspect_sdist(index.download(url, filename), filename)
|
|
227
|
+
return rules.analyze(
|
|
228
|
+
info,
|
|
229
|
+
package=pin.name,
|
|
230
|
+
version=version,
|
|
231
|
+
python=target.python,
|
|
232
|
+
setuptools_versions=shared.get("setuptools"),
|
|
233
|
+
numpy_versions=shared.get("numpy"),
|
|
234
|
+
tool_versions={
|
|
235
|
+
tool: shared.get(tool) for tool in rules.BUILD_TOOLS_NEEDING_PKG_RESOURCES
|
|
236
|
+
},
|
|
237
|
+
)
|
|
238
|
+
|
|
239
|
+
def sdist_is_clean(version: str, item: FileInfo) -> bool:
|
|
240
|
+
try:
|
|
241
|
+
later = analyze(item.url, item.filename, version)
|
|
242
|
+
except (FetchError, OSError, SdistError, ValueError):
|
|
243
|
+
return False
|
|
244
|
+
return not any(finding.severity.rank > Severity.INFO.rank for finding in later)
|
|
245
|
+
|
|
246
|
+
if sdist.url is None:
|
|
247
|
+
raise SdistError(f"{sdist.filename}: no download URL")
|
|
248
|
+
findings = analyze(sdist.url, sdist.filename, pin.version)
|
|
249
|
+
if not any(finding.severity.rank > Severity.INFO.rank for finding in findings):
|
|
250
|
+
findings.append(
|
|
251
|
+
Finding(
|
|
252
|
+
package=pin.name,
|
|
253
|
+
version=pin.version,
|
|
254
|
+
severity=Severity.INFO,
|
|
255
|
+
rule="builds-from-source",
|
|
256
|
+
message=(f"no wheel for {target.describe()}; pip/uv will build it from the sdist"),
|
|
257
|
+
)
|
|
258
|
+
)
|
|
259
|
+
upgrade_to = None
|
|
260
|
+
if any(finding.severity.rank > Severity.INFO.rank for finding in findings):
|
|
261
|
+
upgrade_to = _upgrade(index, pin, target, sdist_is_clean)
|
|
262
|
+
return _PinResult(
|
|
263
|
+
builds=1,
|
|
264
|
+
findings=tuple(
|
|
265
|
+
_with_fixes(
|
|
266
|
+
findings,
|
|
267
|
+
pin,
|
|
268
|
+
upgrade_to=upgrade_to,
|
|
269
|
+
tools=tools,
|
|
270
|
+
note=note,
|
|
271
|
+
lockfile=lockfile,
|
|
272
|
+
)
|
|
273
|
+
),
|
|
274
|
+
)
|
|
275
|
+
|
|
276
|
+
|
|
277
|
+
def _effective_pins(
|
|
278
|
+
pins: Sequence[Pin], index: Index, target: Target
|
|
279
|
+
) -> tuple[list[tuple[Pin, str | None]], list[str], list[Finding]]:
|
|
280
|
+
effective: list[tuple[Pin, str | None]] = []
|
|
281
|
+
skipped: list[str] = []
|
|
282
|
+
findings: list[Finding] = []
|
|
283
|
+
seen: set[tuple[str, str | None]] = set()
|
|
284
|
+
for pin in pins:
|
|
285
|
+
key = (pin.key, pin.version)
|
|
286
|
+
if key in seen:
|
|
287
|
+
continue
|
|
288
|
+
seen.add(key)
|
|
289
|
+
if pin.source in {"editable", "virtual"}:
|
|
290
|
+
continue # the project itself
|
|
291
|
+
if pin.source != "registry":
|
|
292
|
+
skipped.append(f"{pin.name}: {pin.source} source, not checked")
|
|
293
|
+
continue
|
|
294
|
+
if pin.version is not None and "+" in pin.version:
|
|
295
|
+
skipped.append(
|
|
296
|
+
f"{pin.name} {pin.version}: local version from another index, not checked"
|
|
297
|
+
)
|
|
298
|
+
continue
|
|
299
|
+
if pin.version is not None:
|
|
300
|
+
effective.append((pin, None))
|
|
301
|
+
continue
|
|
302
|
+
try:
|
|
303
|
+
version = _latest(index, pin, target)
|
|
304
|
+
except (FetchError, OSError, ValueError) as exc:
|
|
305
|
+
findings.append(
|
|
306
|
+
Finding(
|
|
307
|
+
package=pin.name,
|
|
308
|
+
version=None,
|
|
309
|
+
severity=Severity.WARNING,
|
|
310
|
+
rule="not-checked",
|
|
311
|
+
message=str(exc),
|
|
312
|
+
)
|
|
313
|
+
)
|
|
314
|
+
continue
|
|
315
|
+
if version is None:
|
|
316
|
+
skipped.append(f"{pin.name}: no version satisfies the specifier")
|
|
317
|
+
continue
|
|
318
|
+
effective.append(
|
|
319
|
+
(
|
|
320
|
+
replace(
|
|
321
|
+
pin,
|
|
322
|
+
version=str(version),
|
|
323
|
+
specifier=None,
|
|
324
|
+
wheels=None,
|
|
325
|
+
sdist=None,
|
|
326
|
+
),
|
|
327
|
+
f"{pin.name} is not pinned; checked the newest matching release, {version}",
|
|
328
|
+
)
|
|
329
|
+
)
|
|
330
|
+
return effective, skipped, findings
|
|
331
|
+
|
|
332
|
+
|
|
333
|
+
def check(
|
|
334
|
+
path: Path,
|
|
335
|
+
target: Target,
|
|
336
|
+
index: Index,
|
|
337
|
+
*,
|
|
338
|
+
tools: Sequence[str] | None = None,
|
|
339
|
+
jobs: int = 8,
|
|
340
|
+
) -> Report:
|
|
341
|
+
"""Check every registry pin in a lockfile for the given target."""
|
|
342
|
+
|
|
343
|
+
pins = lockfiles.load(path, target.marker_env())
|
|
344
|
+
if tools is None:
|
|
345
|
+
tools = DEFAULT_TOOLS.get(lockfiles.detect_format(path), ("pip", "uv pip"))
|
|
346
|
+
shared = _SharedVersions(index, target)
|
|
347
|
+
effective, skipped, findings = _effective_pins(pins, index, target)
|
|
348
|
+
|
|
349
|
+
def run(item: tuple[Pin, str | None]) -> _PinResult:
|
|
350
|
+
pin, note = item
|
|
351
|
+
try:
|
|
352
|
+
return _check_pin(
|
|
353
|
+
pin,
|
|
354
|
+
target=target,
|
|
355
|
+
index=index,
|
|
356
|
+
tools=tools,
|
|
357
|
+
shared=shared,
|
|
358
|
+
note=note,
|
|
359
|
+
lockfile=Path(path).name,
|
|
360
|
+
)
|
|
361
|
+
except (FetchError, OSError, SdistError, ValueError) as exc:
|
|
362
|
+
return _PinResult(
|
|
363
|
+
findings=(
|
|
364
|
+
Finding(
|
|
365
|
+
package=pin.name,
|
|
366
|
+
version=pin.version,
|
|
367
|
+
severity=Severity.WARNING,
|
|
368
|
+
rule="not-checked",
|
|
369
|
+
message=str(exc),
|
|
370
|
+
),
|
|
371
|
+
)
|
|
372
|
+
)
|
|
373
|
+
|
|
374
|
+
results: list[_PinResult] = []
|
|
375
|
+
if effective:
|
|
376
|
+
with ThreadPoolExecutor(max_workers=max(1, jobs)) as pool:
|
|
377
|
+
results = list(pool.map(run, effective))
|
|
378
|
+
|
|
379
|
+
report = Report(
|
|
380
|
+
lockfile=str(path),
|
|
381
|
+
target=target.describe(),
|
|
382
|
+
skipped=list(skipped),
|
|
383
|
+
findings=list(findings),
|
|
384
|
+
)
|
|
385
|
+
for result in results:
|
|
386
|
+
report.checked += result.checked
|
|
387
|
+
report.builds_from_source += result.builds
|
|
388
|
+
report.skipped.extend(result.skipped)
|
|
389
|
+
report.findings.extend(result.findings)
|
|
390
|
+
report.findings.sort(key=lambda finding: (-finding.severity.rank, finding.package.lower()))
|
|
391
|
+
return report
|
canibuild/cli.py
ADDED
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
"""Command-line interface for canibuild."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import argparse
|
|
6
|
+
import sys
|
|
7
|
+
from collections.abc import Sequence
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
|
|
10
|
+
from packaging.specifiers import InvalidSpecifier, SpecifierSet
|
|
11
|
+
|
|
12
|
+
from . import __version__, lockfiles
|
|
13
|
+
from .check import check
|
|
14
|
+
from .model import Report, Severity
|
|
15
|
+
from .pypi import Index, default_cache_dir
|
|
16
|
+
from .report import render_json, render_text
|
|
17
|
+
from .target import Target
|
|
18
|
+
|
|
19
|
+
_TOOL_NAMES = {"uv": "uv", "uv-pip": "uv pip", "pip": "pip", "poetry": "poetry"}
|
|
20
|
+
_ALL_TOOLS = ("uv", "uv pip", "pip", "poetry")
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def _parser() -> argparse.ArgumentParser:
|
|
24
|
+
parser = argparse.ArgumentParser(
|
|
25
|
+
prog="canibuild",
|
|
26
|
+
description="Check a lockfile for dependencies that will fail to build.",
|
|
27
|
+
)
|
|
28
|
+
parser.add_argument("lockfile", nargs="?", metavar="LOCKFILE")
|
|
29
|
+
parser.add_argument("--python", metavar="X.Y")
|
|
30
|
+
parser.add_argument("--platform", metavar="P")
|
|
31
|
+
parser.add_argument("--format", choices=["text", "json"], default="text")
|
|
32
|
+
parser.add_argument("--fail-on", choices=["error", "warning", "never"], default="error")
|
|
33
|
+
parser.add_argument(
|
|
34
|
+
"--tool",
|
|
35
|
+
action="append",
|
|
36
|
+
choices=["uv", "uv-pip", "pip", "poetry", "all"],
|
|
37
|
+
help="tool to write fixes for (repeatable)",
|
|
38
|
+
)
|
|
39
|
+
parser.add_argument("--cache-dir", metavar="DIR")
|
|
40
|
+
parser.add_argument("--no-cache", action="store_true")
|
|
41
|
+
parser.add_argument("--jobs", type=int, default=8, metavar="N")
|
|
42
|
+
parser.add_argument("-v", "--verbose", action="store_true")
|
|
43
|
+
parser.add_argument("--version", action="version", version=f"canibuild {__version__}")
|
|
44
|
+
return parser
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def _tools(values: Sequence[str] | None) -> tuple[str, ...] | None:
|
|
48
|
+
if not values:
|
|
49
|
+
return None
|
|
50
|
+
result: list[str] = []
|
|
51
|
+
for value in values:
|
|
52
|
+
names = _ALL_TOOLS if value == "all" else (_TOOL_NAMES[value],)
|
|
53
|
+
for name in names:
|
|
54
|
+
if name not in result:
|
|
55
|
+
result.append(name)
|
|
56
|
+
return tuple(result)
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def _failed(report: Report, fail_on: str) -> bool:
|
|
60
|
+
if fail_on == "never":
|
|
61
|
+
return False
|
|
62
|
+
threshold = Severity.ERROR if fail_on == "error" else Severity.WARNING
|
|
63
|
+
return any(finding.severity.rank >= threshold.rank for finding in report.findings)
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def _within_requires_python(path: Path, target: Target, *, explicit: bool) -> Target:
|
|
67
|
+
"""Keep the target inside the lockfile's requires-python.
|
|
68
|
+
|
|
69
|
+
uv only records wheels for the Python versions a project supports, so checking
|
|
70
|
+
another version would report every compiled package as a source build.
|
|
71
|
+
"""
|
|
72
|
+
spec_text = lockfiles.requires_python(path)
|
|
73
|
+
if not spec_text:
|
|
74
|
+
return target
|
|
75
|
+
try:
|
|
76
|
+
spec = SpecifierSet(spec_text)
|
|
77
|
+
except InvalidSpecifier:
|
|
78
|
+
return target
|
|
79
|
+
if spec.contains(target.python_full, prereleases=True):
|
|
80
|
+
return target
|
|
81
|
+
if explicit:
|
|
82
|
+
print(
|
|
83
|
+
f"canibuild: warning: Python {target.python_full} is outside this lockfile's "
|
|
84
|
+
f"requires-python ({spec_text})",
|
|
85
|
+
file=sys.stderr,
|
|
86
|
+
)
|
|
87
|
+
return target
|
|
88
|
+
for minor in range(8, 20):
|
|
89
|
+
if spec.contains(f"3.{minor}.0", prereleases=True):
|
|
90
|
+
platform = None if target.platform == "current" else target.platform
|
|
91
|
+
chosen = Target.parse(f"3.{minor}", platform)
|
|
92
|
+
print(
|
|
93
|
+
f"canibuild: checking for Python 3.{minor}, the oldest version allowed by "
|
|
94
|
+
f"requires-python ({spec_text}); pass --python to choose another",
|
|
95
|
+
file=sys.stderr,
|
|
96
|
+
)
|
|
97
|
+
return chosen
|
|
98
|
+
return target
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def main(argv: Sequence[str] | None = None) -> int:
|
|
102
|
+
"""Run the command-line interface and return an exit code."""
|
|
103
|
+
|
|
104
|
+
args_list = list(sys.argv[1:] if argv is None else argv)
|
|
105
|
+
if args_list and args_list[0] == "check":
|
|
106
|
+
args_list = args_list[1:]
|
|
107
|
+
parser = _parser()
|
|
108
|
+
try:
|
|
109
|
+
args = parser.parse_args(args_list)
|
|
110
|
+
except SystemExit as exc:
|
|
111
|
+
return int(exc.code) if isinstance(exc.code, int) else 0
|
|
112
|
+
|
|
113
|
+
if not args.lockfile:
|
|
114
|
+
parser.print_usage(sys.stderr)
|
|
115
|
+
print(
|
|
116
|
+
"canibuild: error: the following arguments are required: LOCKFILE",
|
|
117
|
+
file=sys.stderr,
|
|
118
|
+
)
|
|
119
|
+
return 2
|
|
120
|
+
path = Path(args.lockfile)
|
|
121
|
+
if not path.is_file():
|
|
122
|
+
print(f"canibuild: {path}: no such file", file=sys.stderr)
|
|
123
|
+
return 2
|
|
124
|
+
|
|
125
|
+
try:
|
|
126
|
+
target = Target.parse(args.python, args.platform)
|
|
127
|
+
target = _within_requires_python(path, target, explicit=args.python is not None)
|
|
128
|
+
except ValueError as exc:
|
|
129
|
+
print(f"canibuild: {exc}", file=sys.stderr)
|
|
130
|
+
return 2
|
|
131
|
+
except lockfiles.LockfileError as exc:
|
|
132
|
+
print(f"canibuild: {exc}", file=sys.stderr)
|
|
133
|
+
return 2
|
|
134
|
+
|
|
135
|
+
if args.jobs < 1:
|
|
136
|
+
print("canibuild: --jobs must be at least 1", file=sys.stderr)
|
|
137
|
+
return 2
|
|
138
|
+
|
|
139
|
+
if args.no_cache:
|
|
140
|
+
cache_dir = None
|
|
141
|
+
elif args.cache_dir is not None:
|
|
142
|
+
cache_dir = Path(args.cache_dir)
|
|
143
|
+
else:
|
|
144
|
+
cache_dir = default_cache_dir()
|
|
145
|
+
index = Index(cache_dir=cache_dir)
|
|
146
|
+
|
|
147
|
+
try:
|
|
148
|
+
report = check(
|
|
149
|
+
path,
|
|
150
|
+
target,
|
|
151
|
+
index,
|
|
152
|
+
tools=_tools(args.tool),
|
|
153
|
+
jobs=args.jobs,
|
|
154
|
+
)
|
|
155
|
+
except lockfiles.LockfileError as exc:
|
|
156
|
+
print(f"canibuild: {exc}", file=sys.stderr)
|
|
157
|
+
return 2
|
|
158
|
+
|
|
159
|
+
if args.format == "json":
|
|
160
|
+
print(render_json(report))
|
|
161
|
+
else:
|
|
162
|
+
print(render_text(report, verbose=args.verbose))
|
|
163
|
+
return 1 if _failed(report, args.fail_on) else 0
|