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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
|
"""Verify the contents of the built sdist and wheel."""
from __future__ import annotations
import contextlib
import fnmatch
import os
import pathlib
import re
import shutil
import subprocess
import sys
import tarfile
import tempfile
import typing as t
import zipfile
from ansible.release import __version__
def collect_sdist_files(complete_file_list: list[str]) -> list[str]:
"""Return a list of files which should be present in the sdist."""
ignore_patterns = (
'.azure-pipelines/*',
'.cherry_picker.toml',
'.git*',
'.mailmap',
'changelogs/README.md',
'changelogs/config.yaml',
'changelogs/fragments/*',
'hacking/*',
)
sdist_files = [path for path in complete_file_list if not any(fnmatch.fnmatch(path, ignore) for ignore in ignore_patterns)]
egg_info = (
'PKG-INFO',
'SOURCES.txt',
'dependency_links.txt',
'entry_points.txt',
'not-zip-safe',
'requires.txt',
'top_level.txt',
)
sdist_files.append('PKG-INFO')
sdist_files.extend(f'lib/ansible_core.egg-info/{name}' for name in egg_info)
return sdist_files
def collect_wheel_files(complete_file_list: list[str]) -> list[str]:
"""Return a list of files which should be present in the wheel."""
wheel_files = []
for path in complete_file_list:
if path.startswith('lib/ansible/'):
prefix = 'lib'
elif path.startswith('test/lib/ansible_test/'):
prefix = 'test/lib'
else:
continue
wheel_files.append(os.path.relpath(path, prefix))
dist_info = (
'COPYING',
'METADATA',
'RECORD',
'WHEEL',
'entry_points.txt',
'top_level.txt',
)
wheel_files.append(f'ansible_core-{__version__}.data/scripts/ansible-test')
wheel_files.extend(f'ansible_core-{__version__}.dist-info/{name}' for name in dist_info)
return wheel_files
@contextlib.contextmanager
def clean_repository(complete_file_list: list[str]) -> t.Generator[str, None, None]:
"""Copy the files to a temporary directory and yield the path."""
directories = sorted(set(os.path.dirname(path) for path in complete_file_list))
directories.remove('')
with tempfile.TemporaryDirectory() as temp_dir:
for directory in directories:
os.makedirs(os.path.join(temp_dir, directory))
for path in complete_file_list:
shutil.copy2(path, os.path.join(temp_dir, path), follow_symlinks=False)
yield temp_dir
def build(source_dir: str, tmp_dir: str) -> tuple[pathlib.Path, pathlib.Path]:
"""Create a sdist and wheel."""
create = subprocess.run( # pylint: disable=subprocess-run-check
[sys.executable, '-m', 'build', '--outdir', tmp_dir],
stdin=subprocess.DEVNULL,
capture_output=True,
text=True,
cwd=source_dir,
)
if create.returncode != 0:
raise RuntimeError(f'build failed:\n{create.stderr}\n{create.stdout}')
tmp_dir_files = list(pathlib.Path(tmp_dir).iterdir())
if len(tmp_dir_files) != 2:
raise RuntimeError(f'build resulted in {len(tmp_dir_files)} items instead of 2')
sdist_path = [path for path in tmp_dir_files if path.suffix == '.gz'][0]
wheel_path = [path for path in tmp_dir_files if path.suffix == '.whl'][0]
return sdist_path, wheel_path
def list_sdist(path: pathlib.Path) -> list[str]:
"""Return a list of the files in the sdist."""
item: tarfile.TarInfo
with tarfile.open(path) as sdist:
paths = ['/'.join(pathlib.Path(item.path).parts[1:]) for item in sdist.getmembers() if not item.isdir()]
return paths
def list_wheel(path: pathlib.Path) -> list[str]:
"""Return a list of the files in the wheel."""
with zipfile.ZipFile(path) as wheel:
paths = [item.filename for item in wheel.filelist if not item.is_dir()]
return paths
def check_files(source: str, expected: list[str], actual: list[str]) -> list[str]:
"""Verify the expected files exist and no extra files exist."""
missing = sorted(set(expected) - set(actual))
extra = sorted(set(actual) - set(expected))
errors = (
[f'{path}: missing from {source}' for path in missing] +
[f'{path}: unexpected in {source}' for path in extra]
)
return errors
def main() -> None:
"""Main program entry point."""
complete_file_list = sys.argv[1:] or sys.stdin.read().splitlines()
python_version = '.'.join(map(str, sys.version_info[:2]))
python_min = os.environ['ANSIBLE_TEST_MIN_PYTHON']
python_max = os.environ['ANSIBLE_TEST_MAX_PYTHON']
if python_version == python_min:
use_upper_setuptools_version = False
elif python_version == python_max:
use_upper_setuptools_version = True
else:
raise RuntimeError(f'Python version {python_version} is neither the minimum {python_min} or the maximum {python_max}.')
errors = check_build(complete_file_list, use_upper_setuptools_version)
for error in errors:
print(error)
def set_setuptools_version(repo_dir: str, use_upper_version: bool) -> str:
pyproject_toml = pathlib.Path(repo_dir) / 'pyproject.toml'
current = pyproject_toml.read_text()
pattern = re.compile(r'^(?P<begin>requires = \["setuptools >= )(?P<lower>[^,]+)(?P<middle>, <= )(?P<upper>[^"]+)(?P<end>".*)$', re.MULTILINE)
match = pattern.search(current)
if not match:
raise RuntimeError(f"Unable to find the 'requires' entry in: {pyproject_toml}")
lower_version = match.group('lower')
upper_version = match.group('upper')
requested_version = upper_version if use_upper_version else lower_version
updated = pattern.sub(fr'\g<begin>{requested_version}\g<middle>{requested_version}\g<end>', current)
if current == updated:
raise RuntimeError("Failed to set the setuptools version.")
pyproject_toml.write_text(updated)
return requested_version
def check_build(complete_file_list: list[str], use_upper_setuptools_version: bool) -> list[str]:
errors: list[str] = []
complete_file_list = list(complete_file_list) # avoid mutation of input
# Limit visible files to those reported by ansible-test.
# This avoids including files which are not committed to git.
with clean_repository(complete_file_list) as clean_repo_dir:
setuptools_version = set_setuptools_version(clean_repo_dir, use_upper_setuptools_version)
if __version__.endswith('.dev0'):
# Make sure a changelog exists for this version when testing from devel.
# When testing from a stable branch the changelog will already exist.
major_minor_version = '.'.join(__version__.split('.')[:2])
changelog_path = f'changelogs/CHANGELOG-v{major_minor_version}.rst'
pathlib.Path(clean_repo_dir, changelog_path).touch()
complete_file_list.append(changelog_path)
expected_sdist_files = collect_sdist_files(complete_file_list)
expected_wheel_files = collect_wheel_files(complete_file_list)
with tempfile.TemporaryDirectory() as tmp_dir:
sdist_path, wheel_path = build(clean_repo_dir, tmp_dir)
actual_sdist_files = list_sdist(sdist_path)
actual_wheel_files = list_wheel(wheel_path)
errors.extend(check_files('sdist', expected_sdist_files, actual_sdist_files))
errors.extend(check_files('wheel', expected_wheel_files, actual_wheel_files))
errors = [f'{msg} ({setuptools_version})' for msg in errors]
return errors
if __name__ == '__main__':
main()
|