-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathwheels.py
More file actions
591 lines (513 loc) · 19.3 KB
/
Copy pathwheels.py
File metadata and controls
591 lines (513 loc) · 19.3 KB
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
from __future__ import annotations
import collections
import logging
import os
import pathlib
import shutil
import sys
import tempfile
import typing
import zipfile
import elfdeps
import tomlkit
import wheel.wheelfile # type: ignore
from packaging.requirements import Requirement
from packaging.tags import Tag
from packaging.utils import (
BuildTag,
canonicalize_name,
parse_wheel_filename,
)
from packaging.version import Version
from . import (
dependencies,
external_commands,
metrics,
overrides,
packagesettings,
requirements_file,
resolver,
sbom,
sources,
)
if typing.TYPE_CHECKING:
from . import build_environment, context
logger = logging.getLogger(__name__)
FROMAGER_BUILD_SETTINGS = "fromager-build-settings"
FROMAGER_ELF_PROVIDES = "fromager-elf-provides.txt"
FROMAGER_ELF_REQUIRES = "fromager-elf-requires.txt"
FROMAGER_BUILD_REQ_PREFIX = "fromager"
def _log_existing_sboms(
req: Requirement,
dist_info_dir: pathlib.Path,
) -> None:
"""Log any existing SBOM files found in the wheel's .dist-info/sboms/ directory."""
sboms_dir = dist_info_dir / "sboms"
if not sboms_dir.is_dir():
return
sbom_files = sorted(sboms_dir.iterdir())
if sbom_files:
names = [f.name for f in sbom_files]
logger.info(
"%s: found existing SBOM files in wheel: %s",
req.name,
", ".join(names),
)
def _extra_metadata_elfdeps(
ctx: context.WorkContext,
req: Requirement,
wheel_root_dir: pathlib.Path,
dist_info_dir: pathlib.Path,
) -> typing.Iterable[elfdeps.ELFInfo]:
"""Analyze a wheel's ELF dependencies and add info files
Logs and returns library dependencies and library provides. Writes
requirements to dist-info.
"""
# mapping of required libraries to list of versions
requires: set[elfdeps.SOInfo] = set()
provides: set[elfdeps.SOInfo] = set()
runpaths: set[str] = set()
elfinfos: list[elfdeps.ELFInfo] = []
settings = elfdeps.ELFAnalyzeSettings(filter_soname=True)
for info in elfdeps.analyze_dirtree(wheel_root_dir, settings=settings):
if info.filename is not None:
relname = str(info.filename.relative_to(wheel_root_dir))
else:
relname = "n/a"
logger.debug(
f"{relname} ({info.soname}) "
f"requires {sorted(info.requires)}, "
f"provides {sorted(info.provides)}"
)
provides.update(info.provides)
requires.update(info.requires)
if info.runpath is not None:
runpaths.update(info.runpath)
elfinfos.append(info)
# Don't list provided names as requirements
requires = requires.difference(provides)
if requires:
reqmap: dict[str, list[str]] = collections.defaultdict(list)
for r in requires:
reqmap[r.soname].append(r.version)
names = sorted(
name for name in reqmap if not name.startswith(("ld-linux", "rtld"))
)
logger.info("Requires libraries: %s", ", ".join(names))
for name, versions in sorted(reqmap.items()):
logger.debug(
"Requires %s(%s)",
name,
", ".join(v for v in versions if v),
)
requires_file = dist_info_dir / FROMAGER_ELF_REQUIRES
with requires_file.open("w", encoding="utf-8") as f:
for soinfo in sorted(requires):
f.write(f"{soinfo}\n")
if provides:
names = sorted(p.soname for p in provides)
logger.info("Provides libraries: %s", ", ".join(names))
provides_file = dist_info_dir / FROMAGER_ELF_PROVIDES
with provides_file.open("w", encoding="utf-8") as f:
for soinfo in sorted(provides):
f.write(f"{soinfo}\n")
if runpaths:
logger.info("Libraries have runpath: %s", " ".join(sorted(runpaths)))
return elfinfos
def extract_info_from_wheel_file(
req: Requirement, wheel_file: pathlib.Path
) -> tuple[str, Version, BuildTag, frozenset[Tag]]:
"""Extract metadata from a wheel filename.
Returns the **verbatim** dist name (not normalized) because the
dist-info directory inside the wheel uses the original casing
(e.g. ``MarkupSafe``, not ``markupsafe``). Uses ``parse_wheel_filename``
for validation and to extract version, build tag, and platform tags.
"""
# parse_wheel_filename normalizes the dist name, however the dist-info
# directory uses the verbatim distribution name from the wheel file.
# Packages with upper case names like "MarkupSafe" are affected.
dist_name_normalized, dist_version, build_tag, wheel_tags = parse_wheel_filename(
wheel_file.name
)
dist_name = wheel_file.name.split("-", 1)[0]
if dist_name_normalized != canonicalize_name(dist_name):
# sanity check, should never fail
raise ValueError(f"{dist_name_normalized} does not match {dist_name}")
return (dist_name, dist_version, build_tag, wheel_tags)
def default_add_extra_metadata_to_wheels(
ctx: context.WorkContext,
req: Requirement,
version: Version,
extra_environ: dict[str, str],
sdist_root_dir: pathlib.Path,
dist_info_dir: pathlib.Path,
) -> dict[str, typing.Any]:
"""Default implementation returns empty dict - no extra metadata."""
return {}
@metrics.timeit(description="add extra metadata to wheels")
def add_extra_metadata_to_wheels(
*,
ctx: context.WorkContext,
req: Requirement,
version: Version,
extra_environ: dict[str, str],
sdist_root_dir: pathlib.Path,
wheel_file: pathlib.Path,
) -> pathlib.Path:
"""Unpack a wheel, inject extra metadata, and repack with a build tag.
Unpacks the wheel, validates dist-info, adds plugin metadata, build
settings, requirement files, ELF dependency info (Linux only), and
an SBOM. Repacks with ``wheel pack`` and deletes the original.
"""
pbi = ctx.package_build_info(req)
dist_name, dist_version, _, wheel_tags = extract_info_from_wheel_file(
req, wheel_file
)
dist_filename = f"{dist_name}-{dist_version}"
with tempfile.TemporaryDirectory() as dir_name:
wheel_root_dir = pathlib.Path(dir_name) / dist_filename
wheel_root_dir.mkdir()
with zipfile.ZipFile(str(wheel_file)) as zf:
for infolist in zf.filelist:
# Check for path traversal attempts
if (
os.path.isabs(infolist.filename)
or ".." in pathlib.Path(infolist.filename).parts
):
raise ValueError(f"Unsafe path in wheel: {infolist.filename}")
zf.extract(infolist, wheel_root_dir)
# the higher 16 bits store the permissions and type of file (i.e. stat.filemode)
# the lower bits of this give us the permission
permissions = infolist.external_attr >> 16 & 0o777
wheel_root_dir.joinpath(infolist.filename).chmod(permissions)
dist_info_dir = wheel_root_dir / f"{dist_filename}.dist-info"
if not dist_info_dir.is_dir():
raise ValueError(f"{wheel_file} does not contain {dist_info_dir.name}")
_log_existing_sboms(req, dist_info_dir)
data_to_add = overrides.find_and_invoke(
req.name,
"add_extra_metadata_to_wheels",
default_add_extra_metadata_to_wheels,
ctx=ctx,
req=req,
version=version,
extra_environ=extra_environ,
sdist_root_dir=sdist_root_dir,
dist_info_dir=dist_info_dir,
)
if not isinstance(data_to_add, dict):
logger.warning(
"unexpected return type from plugin add_extra_metadata_to_wheels. Expected dictionary. Will ignore"
)
data_to_add = {}
if pbi.has_config:
settings = pbi.serialize(mode="json", exclude_defaults=False)
else:
settings = {}
if data_to_add:
settings["metadata-from-plugin"] = data_to_add
build_file = dist_info_dir / FROMAGER_BUILD_SETTINGS
build_file.write_text(tomlkit.dumps(settings))
req_files = sdist_root_dir.parent.glob("*-requirements.txt")
for req_file in req_files:
shutil.copy(
req_file, dist_info_dir / f"{FROMAGER_BUILD_REQ_PREFIX}-{req_file.name}"
)
if any(tag.platform != "all" for tag in wheel_tags):
# platlib wheel
if sys.platform == "linux":
_extra_metadata_elfdeps(
ctx=ctx,
req=req,
wheel_root_dir=wheel_root_dir,
dist_info_dir=dist_info_dir,
)
else:
logger.debug(
"shared library dependency analysis not implemented for %s",
sys.platform,
)
else:
logger.debug("%s is a purelib wheel", req.name)
sbom_settings = ctx.settings.sbom_settings
if sbom_settings is not None:
sbom_doc = sbom.generate_sbom(
ctx=ctx,
req=req,
version=version,
)
sbom.write_sbom(sbom=sbom_doc, dist_info_dir=dist_info_dir)
build_tag_from_settings = pbi.build_tag(version)
build_tag = build_tag_from_settings if build_tag_from_settings else (0, "")
cmd = [
"wheel",
"pack",
str(wheel_root_dir),
"--dest-dir",
str(wheel_file.parent),
"--build-number",
f"{build_tag[0]}{build_tag[1]}",
]
external_commands.run(
cmd,
cwd=dir_name,
network_isolation=ctx.network_isolation,
)
wheel_file.unlink(missing_ok=True)
wheels = list(wheel_file.parent.glob(f"{dist_filename}-*.whl"))
if wheels:
logger.info(
f"added extra metadata and build tag {build_tag}, wheel renamed from {wheel_file.name} to {wheels[0].name}"
)
return wheels[0]
raise FileNotFoundError("Could not locate new wheels file")
def validate_wheel_filename(
req: Requirement,
version: Version,
wheel_file: pathlib.Path,
) -> None:
"""Check that wheel matches requirement name and version"""
wheel_name, wheel_version, _, _ = parse_wheel_filename(wheel_file.name)
dependencies.validate_dist_name_version(
req=req,
version=version,
what=wheel_file.name,
dist_name=wheel_name,
dist_version=wheel_version,
)
@metrics.timeit(description="build wheels")
def build_wheel(
*,
ctx: context.WorkContext,
req: Requirement,
sdist_root_dir: pathlib.Path,
version: Version,
build_env: build_environment.BuildEnvironment,
) -> pathlib.Path:
pbi = ctx.package_build_info(req)
logger.info(
f"building {ctx.variant} wheel for {req} in {sdist_root_dir} "
f"writing to {ctx.wheels_build}"
)
# add package and variant env vars, package's parallel job vars, and
# build_env's virtual env vars.
extra_environ = packagesettings.get_extra_environ(
ctx=ctx,
req=req,
sdist_root_dir=sdist_root_dir,
version=version,
build_env=build_env,
)
if pbi.build_ext_parallel:
logger.warning(
"%s: build_ext_parallel is deprecated and will be removed in a "
"future release. The parallel build feature for extensions is unsafe and "
"can cause build failures or miscompiled wheels. This option is now ignored.",
req.name,
)
overrides.find_and_invoke(
req.name,
"build_wheel",
default_build_wheel,
ctx=ctx,
build_env=build_env,
extra_environ=extra_environ,
req=req,
version=version,
sdist_root_dir=sdist_root_dir,
build_dir=pbi.build_dir(sdist_root_dir),
)
wheels = list(ctx.wheels_build.glob("*.whl"))
if len(wheels) != 1:
raise FileNotFoundError(
f"Expected 1 built wheel in {ctx.wheels_build}, got {len(wheels)}"
)
tmp_wheel_file = wheels[0]
# validate location and file name
if tmp_wheel_file.parent != ctx.wheels_build:
raise ValueError(
f"{tmp_wheel_file!r} is not in wheels build directory {ctx.wheels_build!r}"
)
validate_wheel_filename(req=req, version=version, wheel_file=tmp_wheel_file)
# add extra metadata and validate again
new_wheel_file: pathlib.Path = add_extra_metadata_to_wheels(
ctx=ctx,
req=req,
version=version,
extra_environ=extra_environ,
sdist_root_dir=sdist_root_dir,
wheel_file=tmp_wheel_file,
)
validate_wheel_filename(req=req, version=version, wheel_file=new_wheel_file)
# invalidate uv cache, so the new wheel is picked up. The step is only
# relevant for local development when a package is rebuilt multiple times
# without bumping the build tag.
ctx.uv_clean_cache(req)
return new_wheel_file
def pep517_build_wheel(
ctx: context.WorkContext,
build_env: build_environment.BuildEnvironment,
extra_environ: dict[str, str],
req: Requirement,
sdist_root_dir: pathlib.Path,
version: Version,
build_dir: pathlib.Path,
) -> pathlib.Path:
"""Use the PEP 517 API to build a wheel distribution"""
logger.debug(f"building wheel in {build_dir} with {extra_environ}")
hook_caller = dependencies.get_build_backend_hook_caller(
ctx=ctx,
req=req,
build_dir=build_dir,
override_environ=extra_environ,
build_env=build_env,
log_filename=str(sdist_root_dir.parent / "build.log"),
)
pbi = ctx.package_build_info(req)
wheel_filename = hook_caller.build_wheel(
str(ctx.wheels_build),
config_settings=pbi.config_settings,
)
logger.debug("built wheel %s", wheel_filename)
return ctx.wheels_build / wheel_filename
def default_build_wheel(
ctx: context.WorkContext,
build_env: build_environment.BuildEnvironment,
extra_environ: dict[str, str],
req: Requirement,
sdist_root_dir: pathlib.Path,
version: Version,
build_dir: pathlib.Path,
) -> pathlib.Path:
return pep517_build_wheel(
ctx=ctx,
build_env=build_env,
extra_environ=extra_environ,
req=req,
sdist_root_dir=sdist_root_dir,
version=version,
build_dir=build_dir,
)
def download_wheel(
req: Requirement,
wheel_url: str,
output_directory: pathlib.Path,
) -> pathlib.Path:
wheel_filename = output_directory / resolver.extract_filename_from_url(wheel_url)
if not wheel_filename.exists():
logger.info(f"downloading pre-built wheel {wheel_url}")
wheel_filename = _download_wheel_check(req, output_directory, wheel_url)
logger.info(f"saved wheel to {wheel_filename}")
else:
logger.info(f"have existing wheel {wheel_filename}")
return wheel_filename
def _download_wheel_check(
req: Requirement, destination_dir: pathlib.Path, wheel_url: str
) -> pathlib.Path:
wheel_filename = sources.download_url(
req=req,
destination_dir=destination_dir,
url=wheel_url,
)
# validates whether the wheel is correct or not. will raise an error in the wheel is invalid
wheel.wheelfile.WheelFile(wheel_filename)
return wheel_filename
def get_wheel_server_urls(
ctx: context.WorkContext, req: Requirement, *, cache_wheel_server_url: str | None
) -> list[str]:
pbi = ctx.package_build_info(req)
wheel_server_urls: list[str] = []
if pbi.wheel_server_url:
# use only the wheel server from settings if it is defined. Do not fallback to other URLs
wheel_server_urls.append(pbi.wheel_server_url)
else:
if ctx.wheel_server_url:
# local wheel server
wheel_server_urls.append(ctx.wheel_server_url)
if cache_wheel_server_url:
# put cache after local server so we always check local server first
wheel_server_urls.append(cache_wheel_server_url)
# XXX
# if not wheel_server_urls:
# raise ValueError("no wheel server urls configured")
return wheel_server_urls
def get_prebuilt_wheel_provider(
*,
ctx: context.WorkContext,
req: Requirement,
wheel_server_url: str,
req_type: requirements_file.RequirementType | None = None,
) -> resolver.BaseProvider:
"""Create a provider for resolving prebuilt wheels from a wheel server.
Returns a provider configured to search for wheels (not sdists) that match
the current platform.
"""
return typing.cast(
resolver.BaseProvider,
overrides.find_and_invoke(
req.name,
"get_resolver_provider",
resolver.default_resolver_provider,
ctx=ctx,
req=req,
include_sdists=False,
include_wheels=True,
sdist_server_url=wheel_server_url,
req_type=req_type,
# pre-built wheels must match platform
ignore_platform=False,
),
)
def resolve_all_prebuilt_wheels(
*,
ctx: context.WorkContext,
req: Requirement,
wheel_server_urls: list[str],
req_type: requirements_file.RequirementType | None = None,
) -> list[tuple[str, Version]]:
"""Return all matching wheel versions from the first successful server.
Tries wheel servers in order and returns all matching versions from the
first server that has any matches. Results are sorted by version (highest first).
Raises ExceptionGroup if no server has matching wheels.
"""
excs: list[Exception] = []
for url in wheel_server_urls:
try:
# Get provider for this wheel server
provider = get_prebuilt_wheel_provider(
ctx=ctx, req=req, wheel_server_url=url, req_type=req_type
)
provider.cooldown = resolver.resolve_package_cooldown(ctx, req)
# The local fromager wheel server is PEP 503-only and serves
# packages that were already resolved and vetted earlier in the
# same run. Don't fail-closed on missing upload_time there.
if ctx.wheel_server_url and url == ctx.wheel_server_url:
provider.supports_upload_time = False
# Get all matching candidates from provider
results = resolver.find_all_matching_from_provider(provider, req)
# find_all_matching_from_provider never returns empty list - raises instead
return results
except Exception as e:
excs.append(e)
raise ExceptionGroup(
f"Could not find a prebuilt wheel for {req} on {' or '.join(wheel_server_urls)}",
excs,
)
@metrics.timeit(description="resolve wheel")
def resolve_prebuilt_wheel(
*,
ctx: context.WorkContext,
req: Requirement,
wheel_server_urls: list[str],
req_type: requirements_file.RequirementType | None = None,
) -> tuple[str, Version]:
"""Return (URL, version) for the best matching wheel version.
Tries wheel servers in order and returns result from the first that succeeds.
Returns the highest matching version.
"""
results = resolve_all_prebuilt_wheels(
ctx=ctx, req=req, wheel_server_urls=wheel_server_urls, req_type=req_type
)
# Return highest version (first in sorted list)
wheel_url, version = results[0]
return str(wheel_url), version