-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
validate.py
175 lines (146 loc) · 5.25 KB
/
validate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
from __future__ import annotations
import argparse
import configparser
import os.path
import re
import subprocess
import sys
import tempfile
import zipfile
from collections.abc import Mapping
from typing import NamedTuple
from packaging.tags import Tag
from packaging.utils import parse_wheel_filename
from packaging.version import Version
PYTHONS = ((3, 11), (3, 12), (3, 13))
DIST_INFO_RE = re.compile(r"^[^/]+.dist-info/[^/]+$")
class Info(NamedTuple):
validate_extras: str | None
validate_incorrect_missing_deps: tuple[str, ...]
validate_skip_imports: tuple[str, ...]
@classmethod
def from_dct(cls, dct: Mapping[str, str]) -> Info:
return cls(
validate_extras=dct.get("validate_extras") or None,
validate_incorrect_missing_deps=tuple(
dct.get("validate_incorrect_missing_deps", "").split()
),
validate_skip_imports=tuple(dct.get("validate_skip_imports", "").split()),
)
def _parse_cp_tag(s: str) -> tuple[int, int]:
return int(s[2]), int(s[3:])
def _py_exe(major: int, minor: int) -> str:
return f"python{major}.{minor}"
def _pythons_to_check(tags: frozenset[Tag]) -> tuple[str, ...]:
ret: set[str] = set()
for tag in tags:
if tag.abi == "abi3" and tag.interpreter.startswith("cp"):
min_py = _parse_cp_tag(tag.interpreter)
ret.update(_py_exe(*py) for py in PYTHONS if py >= min_py)
elif tag.interpreter.startswith("cp"):
ret.add(_py_exe(*_parse_cp_tag(tag.interpreter)))
elif tag.interpreter == "py2":
continue
elif tag.interpreter == "py3":
ret.update(_py_exe(*py) for py in PYTHONS)
else:
raise AssertionError(f"unexpected tag: {tag}")
if not ret:
raise AssertionError(f"no interpreters found for {tags}")
else:
return tuple(sorted(ret))
def _top_imports(whl: str) -> list[str]:
with zipfile.ZipFile(whl) as zipf:
dist_info_names = {
os.path.basename(name): name
for name in zipf.namelist()
if DIST_INFO_RE.match(name)
}
if "RECORD" in dist_info_names:
with zipf.open(dist_info_names["RECORD"]) as f:
pkgs = {}
for line_b in f:
fname = line_b.decode().split(",")[0]
if fname.endswith("/__init__.py"):
pkgs[fname.split("/")[0]] = 1
elif "/" not in fname and fname.endswith((".so", ".py")):
pkgs[fname.split(".")[0]] = 1
return list(pkgs)
else:
raise NotImplementedError("need RECORD")
def _validate(
*,
python: str,
filename: str,
info: Info,
index_url: str,
) -> None:
print(f"validating {python}: {filename}")
with tempfile.TemporaryDirectory() as tmpdir:
print("creating env")
venv = os.path.join(tmpdir, "venv")
py = os.path.join(venv, "bin", "python")
subprocess.check_call(
(
sys.executable,
"-mvirtualenv",
"--no-periodic-update",
"--pip=embed",
"--setuptools=embed",
"--wheel=embed",
"--quiet",
f"--python={python}",
venv,
)
)
print("=> installing")
if info.validate_extras is not None:
install_target = f"{filename}[{info.validate_extras}]"
else:
install_target = filename
subprocess.check_call(
(
py,
"-mpip",
"install",
"--quiet",
"--no-cache-dir",
"--disable-pip-version-check",
"--only-binary=:all:",
f"--index-url={index_url}",
# allow just-built wheels to count too
f"--find-links={os.path.dirname(filename)}",
install_target,
*info.validate_incorrect_missing_deps,
)
)
print("=> importing")
for s in _top_imports(filename):
if s not in info.validate_skip_imports:
subprocess.check_call((py, "-c", f"__import__({s!r})"))
def main() -> int:
parser = argparse.ArgumentParser()
parser.add_argument("--index-url", required=True)
parser.add_argument("--dist", default="dist")
parser.add_argument("--packages-ini", default="packages.ini")
args = parser.parse_args()
cfg = configparser.ConfigParser()
if not cfg.read(args.packages_ini):
raise SystemExit(f"{args.packages_ini}: not found")
packages = {}
for k in cfg.sections():
pkg, _, version_s = k.partition("==")
packages[(pkg, Version(version_s))] = Info.from_dct(cfg[k])
for filename in sorted(os.listdir(args.dist)):
name, version, _, wheel_tags = parse_wheel_filename(filename)
info = packages[(name, version)]
for python in _pythons_to_check(wheel_tags):
_validate(
python=python,
filename=os.path.join(args.dist, filename),
info=info,
index_url=args.index_url,
)
return 0
if __name__ == "__main__":
raise SystemExit(main())