-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_contents.py
209 lines (166 loc) · 7.21 KB
/
test_contents.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import csv
import os
import re
import tarfile
from collections import defaultdict
from functools import partial
from pathlib import Path
from textwrap import indent
from zipfile import ZipFile
IGNORE_GIT = "\\.git.*|.*\\.gitignore|.*\\.patch"
def get_sdist_files(file, name):
file = Path(file)
with tarfile.open(file) as tar:
return {str(Path(f.name).relative_to(name)): f.size for f in tar.getmembers()}
def get_wheel_files(file, name):
file = Path(file)
with ZipFile(file) as zip:
return {
t[0]: (int(t[2]) if t[2] else None) for t in # strip the sha256
csv.reader(zip.read(name + ".dist-info/RECORD").decode("ascii").splitlines())
}
def get_ogdf_files(base):
infos = {}
for root, dirs, files in os.walk(base):
infos.update({
str(path.relative_to(base)): path.stat().st_size
for path in (Path(root, f) for f in files)
})
return infos
def partition(d, ps):
r = defaultdict(dict)
for k, v in d.items():
for p in ps:
if k.startswith(p):
r[p][k[len(p):]] = v
break
else:
r[""][k] = v
return r
def diff_dicts(a, b):
a, b = set(a.items()), set(b.items())
return dict(sorted(a - b)), dict(sorted(b - a))
def diff_dict_keys(a, b):
A, B = set(a.keys()), set(b.keys())
return {k: a[k] for k in sorted(A - B)}, {k: b[k] for k in sorted(B - A)}
issues = 0
def check_diff(tag, actual, expected, ign_a="", ign_e="", exp_a=[], win=False):
global issues
print("\tChecking", tag)
if win:
actual, expected = diff_dict_keys(actual, expected)
else:
actual, expected = diff_dicts(actual, expected)
for e in exp_a:
if e not in actual:
print("\tMissing file %s in %s!" % (e, tag))
issues += 1
sup = {k: v for k, v in actual.items() if not re.fullmatch(ign_a, k) and k not in exp_a}
mis = {k: v for k, v in expected.items() if not re.fullmatch(ign_e, k)}
if sup or mis:
print(("\tMismatch for %s!\n" % tag) + indent("Superfluous:\n%s\nMissing:\n%s" % (sup, mis), "\t\t"))
issues += 1
def ignore(*ps):
return "|".join(p.replace(".", "\\.") for p in ps)
LICENSES = [
"ogdf/LICENSE.txt",
"ogdf/LICENSE_GPL_v2.txt",
"ogdf/LICENSE_GPL_v3.txt",
"ogdf/include/ogdf/lib/minisat/LICENSE",
]
def check_wheel(wheelp, ogdfp, name, tag):
name_esc = ignore(name)
headers, others = {}, {}
for k, v in ogdfp["include/"].items():
if re.match(".*\\.(h|hpp|inc)", k):
headers[k] = v
else:
others[k] = v
# _cur is the install location for the current platform (UNIX), _oth for the other (Windows)
incl_cur, incl_oth = wheelp[name + ".data/data/include/"], wheelp["ogdf_wheel/install/include/"]
exam_cur, exam_oth = wheelp[name + ".data/data/share/doc/libogdf/examples/"], wheelp["ogdf_wheel/install/share/doc/libogdf/examples/"]
check = check_diff
if "win" in tag:
incl_cur, incl_oth = incl_oth, incl_cur
exam_cur, exam_oth = exam_oth, exam_cur
check = partial(check_diff, win=True)
check("wheel includes [cur]", incl_cur, headers, exp_a=["ogdf/basic/internal/config_autogen.h"])
check("wheel includes [oth]", incl_oth, {})
check("wheel examples [cur]", exam_cur, ogdfp["doc/examples/"], ign_e=IGNORE_GIT + "|.*\\.dox")
check("wheel examples [oth]", exam_oth, {})
check("not installed wheel includes", others,
{'coin/Readme.txt': 641,
'ogdf/lib/.clang-tidy': 109,
'ogdf/lib/minisat/LICENSE': 1142,
'ogdf/lib/minisat/doc/ReleaseNotes-2.2.0.txt': 3418,
'ogdf/geometric/README.md': 321})
ign_meta = f"{name_esc}\\.dist-info/(METADATA|RECORD|WHEEL)|{name_esc}\\.data/data/lib/cmake/.*\.cmake"
exp_lic = [name + ".dist-info/licenses/" + f for f in LICENSES] + ['ogdf_wheel/__init__.py']
if "win" in tag:
check("wheel install [win]", wheelp["ogdf_wheel/install/"], {},
ign_a="lib/cmake/.*\.cmake|lib/(COIN|OGDF)\.lib",
exp_a=["bin/OGDF.dll"])
check("wheel rest [win]", wheelp[""], {}, ign_a=ign_meta, exp_a=exp_lic)
elif "macos" in tag:
check("wheel rest [macos]", wheelp[""], {},
ign_a=ign_meta,
exp_a=[name + ".data/data/lib/libOGDF.dylib", name + ".data/data/lib/libCOIN.dylib", *exp_lic])
else:
check("wheel rest [linux]", wheelp[""], {},
ign_a=ign_meta,
exp_a=[name + ".data/data/lib/libOGDF.so", name + ".data/data/lib/libCOIN.so", *exp_lic])
def check_sdist(sdistp, ogdff):
check_diff("sdist ogdf", sdistp["ogdf/"], ogdff,
ign_e=IGNORE_GIT)
check_diff("sdist rest", sdistp[""], {},
ign_a=IGNORE_GIT + "|test_[a-z_]+\\.py",
exp_a=['PKG-INFO', 'hatch_build.py', 'pyproject.toml', 'src/ogdf_wheel/__init__.py', 'README.md'])
def dump_data(dumpdir, files, partitions, name):
import json
with open(dumpdir / name + "_files.csv", "w", newline="") as csvfile:
csv.writer(csvfile).writerows(files.items())
with open(dumpdir / name + "_files.json", "w") as jsonfile:
json.dump(partitions, jsonfile)
if __name__ == "__main__":
import click
@click.command()
@click.option('--dist', type=click.Path(exists=True, file_okay=False), default=Path("dist"))
@click.option('--ogdf', type=click.Path(exists=True, file_okay=False), default=Path("ogdf"))
@click.option('--dump', type=click.Path(file_okay=False))
def main(dist, ogdf, dump):
dist = Path(dist)
ogdf = Path(ogdf)
dump = Path(dump) if dump else dump
sdists = list(dist.glob("*.tar.gz"))
if len(sdists) != 1:
raise click.Abort("Didn't find exactly one source dist (.tar.gz) in %s: %s" % (dist, sdists))
name = sdists[0].name.rsplit(".", 2)[0]
ogdff = get_ogdf_files(ogdf)
ogdfp = partition(ogdff, ["include/", "doc/examples/", "src/", "test/"])
if dump: dump_data(dump, ogdff, ogdfp, "ogdf")
checks = 0
for sdist in dist.glob("*.tar.gz"):
print("Checking", sdist)
checks += 1
sdistf = get_sdist_files(sdist, name)
sdistp = partition(sdistf, ["ogdf/"])
if dump: dump_data(dump, sdistf, sdistp, "sdist-%s" % sdist.name)
check_sdist(sdistp, ogdff)
for wheel in dist.glob("*.whl"):
print("Checking", wheel)
checks += 1
wheelf = get_wheel_files(wheel, name)
wheelp = partition(wheelf, [
name + ".data/data/include/", name + ".data/data/share/doc/libogdf/examples/",
"ogdf_wheel/install/include/", "ogdf_wheel/install/share/doc/libogdf/examples/", "ogdf_wheel/install/"])
if dump: dump_data(dump, wheelf, wheelp, "wheel-%s" % wheel.name)
check_wheel(wheelp, ogdfp, name, wheel.stem)
if issues:
print("There were %s issue(s)!" % issues)
click.get_current_context().exit(1)
elif not checks:
print("No checks were run! Does the dist directory exist?")
click.get_current_context().exit(1)
else:
print("Everything looks good!")
main()