| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import importlib |
| 4 |
import json |
| 5 |
import os |
| 6 |
import sys |
| 7 |
import zipfile |
| 8 |
from argparse import Namespace |
| 9 |
from datetime import datetime, timezone |
| 10 |
from os.path import isabs |
| 11 |
from pathlib import Path |
| 12 |
from typing import Any |
| 13 |
from unittest.mock import Mock |
| 14 |
|
| 15 |
import pytest |
| 16 |
|
| 17 |
from auditwheel import lddtree, main_repair |
| 18 |
from auditwheel.architecture import Architecture |
| 19 |
from auditwheel.libc import Libc |
| 20 |
from auditwheel.main import main |
| 21 |
from auditwheel.wheel_abi import NonPlatformWheelError, analyze_wheel_abi |
| 22 |
|
| 23 |
HERE = Path(__file__).parent.resolve() |
| 24 |
|
| 25 |
|
| 26 |
@pytest.mark.parametrize( |
| 27 |
("file", "external_libs", "exclude"), |
| 28 |
[ |
| 29 |
( |
| 30 |
"cffi-1.5.0-cp27-none-linux_x86_64.whl", |
| 31 |
{"libffi.so.5", "libpython2.7.so.1.0"}, |
| 32 |
frozenset(), |
| 33 |
), |
| 34 |
( |
| 35 |
"cffi-1.5.0-cp27-none-linux_x86_64.whl", |
| 36 |
set(), |
| 37 |
frozenset(["libffi.so.5", "libpython2.7.so.1.0"]), |
| 38 |
), |
| 39 |
( |
| 40 |
"cffi-1.5.0-cp27-none-linux_x86_64.whl", |
| 41 |
{"libffi.so.5", "libpython2.7.so.1.0"}, |
| 42 |
frozenset(["libffi.so.noexist", "libnoexist.so.*"]), |
| 43 |
), |
| 44 |
( |
| 45 |
"cffi-1.5.0-cp27-none-linux_x86_64.whl", |
| 46 |
{"libpython2.7.so.1.0"}, |
| 47 |
frozenset(["libffi.so.[4,5]"]), |
| 48 |
), |
| 49 |
( |
| 50 |
"cffi-1.5.0-cp27-none-linux_x86_64.whl", |
| 51 |
{"libffi.so.5", "libpython2.7.so.1.0"}, |
| 52 |
frozenset(["libffi.so.[6,7]"]), |
| 53 |
), |
| 54 |
( |
| 55 |
"cffi-1.5.0-cp27-none-linux_x86_64.whl", |
| 56 |
{"libpython2.7.so.1.0"}, |
| 57 |
frozenset([f"{HERE}/*"]), |
| 58 |
), |
| 59 |
( |
| 60 |
"cffi-1.5.0-cp27-none-linux_x86_64.whl", |
| 61 |
{"libpython2.7.so.1.0"}, |
| 62 |
frozenset(["libffi.so.*"]), |
| 63 |
), |
| 64 |
("cffi-1.5.0-cp27-none-linux_x86_64.whl", set(), frozenset(["*"])), |
| 65 |
( |
| 66 |
"python_snappy-0.5.2-pp260-pypy_41-linux_x86_64.whl", |
| 67 |
{"libsnappy.so.1"}, |
| 68 |
frozenset(), |
| 69 |
), |
| 70 |
], |
| 71 |
) |
| 72 |
def test_analyze_wheel_abi(file, external_libs, exclude): |
| 73 |
# If exclude libs contain path, LD_LIBRARY_PATH need to be modified to find the libs |
| 74 |
# `lddtree.load_ld_paths` needs to be reloaded for it's `lru_cache`-ed. |
| 75 |
modify_ld_library_path = any(isabs(e) for e in exclude) |
| 76 |
|
| 77 |
with pytest.MonkeyPatch.context() as cp: |
| 78 |
if modify_ld_library_path: |
| 79 |
cp.setenv("LD_LIBRARY_PATH", f"{HERE}") |
| 80 |
importlib.reload(lddtree) |
| 81 |
|
| 82 |
winfo = analyze_wheel_abi( |
| 83 |
Libc.GLIBC, |
| 84 |
Architecture.x86_64, |
| 85 |
HERE / file, |
| 86 |
exclude, |
| 87 |
disable_isa_ext_check=False, |
| 88 |
allow_graft=True, |
| 89 |
) |
| 90 |
assert set(winfo.external_refs["manylinux_2_5_x86_64"].libs) == external_libs, ( |
| 91 |
f"{HERE}, {exclude}, {os.environ}" |
| 92 |
) |
| 93 |
|
| 94 |
if modify_ld_library_path: |
| 95 |
importlib.reload(lddtree) |
| 96 |
|
| 97 |
|
| 98 |
def test_analyze_wheel_abi_pyfpe(): |
| 99 |
winfo = analyze_wheel_abi( |
| 100 |
Libc.GLIBC, |
| 101 |
Architecture.x86_64, |
| 102 |
HERE / "fpewheel-0.0.0-cp35-cp35m-linux_x86_64.whl", |
| 103 |
frozenset(), |
| 104 |
disable_isa_ext_check=False, |
| 105 |
allow_graft=True, |
| 106 |
) |
| 107 |
# for external symbols, it could get manylinux1 |
| 108 |
assert winfo.sym_policy.name == "manylinux_2_5_x86_64" |
| 109 |
# but for having the pyfpe reference, it gets just linux |
| 110 |
assert winfo.pyfpe_policy.name == "linux_x86_64" |
| 111 |
assert winfo.overall_policy.name == "linux_x86_64" |
| 112 |
|
| 113 |
|
| 114 |
def test_show_wheel_abi_pyfpe(monkeypatch, capsys): |
| 115 |
wheel = str(HERE / "fpewheel-0.0.0-cp35-cp35m-linux_x86_64.whl") |
| 116 |
monkeypatch.setattr(sys, "platform", "linux") |
| 117 |
monkeypatch.setattr(Architecture, "detect", lambda: Architecture.x86_64) |
| 118 |
monkeypatch.setattr(sys, "argv", ["auditwheel", "show", wheel]) |
| 119 |
assert main() == 0 |
| 120 |
captured = capsys.readouterr() |
| 121 |
assert "This wheel uses the PyFPE_jbuf function" in captured.out |
| 122 |
|
| 123 |
|
| 124 |
def test_analyze_wheel_abi_bad_architecture(): |
| 125 |
with pytest.raises(NonPlatformWheelError): |
| 126 |
analyze_wheel_abi( |
| 127 |
Libc.GLIBC, |
| 128 |
Architecture.aarch64, |
| 129 |
HERE / "fpewheel-0.0.0-cp35-cp35m-linux_x86_64.whl", |
| 130 |
frozenset(), |
| 131 |
disable_isa_ext_check=False, |
| 132 |
allow_graft=True, |
| 133 |
) |
| 134 |
|
| 135 |
|
| 136 |
def test_analyze_wheel_abi_static_exe(caplog): |
| 137 |
result = analyze_wheel_abi( |
| 138 |
None, |
| 139 |
None, |
| 140 |
HERE |
| 141 |
/ "patchelf-0.17.2.1-py2.py3-none-manylinux_2_5_x86_64.manylinux1_x86_64.musllinux_1_1_x86_64.whl", # noqa: E501 |
| 142 |
frozenset(), |
| 143 |
disable_isa_ext_check=False, |
| 144 |
allow_graft=False, |
| 145 |
) |
| 146 |
assert "setting architecture to x86_64" in caplog.text |
| 147 |
assert "couldn't detect wheel libc, defaulting to" in caplog.text |
| 148 |
assert result.policies.architecture == Architecture.x86_64 |
| 149 |
if Libc.detect() == Libc.MUSL: |
| 150 |
assert result.policies.libc == Libc.MUSL |
| 151 |
assert result.overall_policy.name.startswith("musllinux_1_") |
| 152 |
else: |
| 153 |
assert result.policies.libc == Libc.GLIBC |
| 154 |
assert result.overall_policy.name == "manylinux_2_5_x86_64" |
| 155 |
|
| 156 |
|
| 157 |
@pytest.mark.parametrize( |
| 158 |
"timestamp", |
| 159 |
[ |
| 160 |
(0, 315532800), # zip timestamp starts 1980-01-01, not 1970-01-01 |
| 161 |
(315532799, 315532800), # zip timestamp starts 1980-01-01, not 1970-01-01 |
| 162 |
(315532801, 315532800), # zip timestamp round odd seconds down to even seconds |
| 163 |
(315532802, 315532802), |
| 164 |
(650203201, 650203200), # zip timestamp round odd seconds down to even seconds |
| 165 |
], |
| 166 |
) |
| 167 |
def test_wheel_source_date_epoch(timestamp, tmp_path, monkeypatch): |
| 168 |
wheel_path = HERE / "arch-wheels/musllinux_1_2/testsimple-0.0.1-cp312-cp312-linux_x86_64.whl" |
| 169 |
wheel_output_path = tmp_path / "out" |
| 170 |
args = Namespace( |
| 171 |
LIB_SDIR=".libs", |
| 172 |
ONLY_PLAT=False, |
| 173 |
PLAT="auto", |
| 174 |
STRIP=False, |
| 175 |
UPDATE_TAGS=True, |
| 176 |
WHEEL_DIR=wheel_output_path, |
| 177 |
WHEEL_FILE=[wheel_path], |
| 178 |
EXCLUDE=[], |
| 179 |
LDPATHS=None, |
| 180 |
DISABLE_ISA_EXT_CHECK=False, |
| 181 |
ZIP_COMPRESSION_LEVEL=6, |
| 182 |
cmd="repair", |
| 183 |
func=Mock(), |
| 184 |
prog="auditwheel", |
| 185 |
verbose=1, |
| 186 |
) |
| 187 |
|
| 188 |
monkeypatch.setenv("SOURCE_DATE_EPOCH", str(timestamp[0])) |
| 189 |
main_repair.execute(args, Mock()) |
| 190 |
output_wheel, *_ = list(wheel_output_path.glob("*.whl")) |
| 191 |
with zipfile.ZipFile(output_wheel) as wheel_file: |
| 192 |
for file in wheel_file.infolist(): |
| 193 |
file_date_time = datetime(*file.date_time, tzinfo=timezone.utc) |
| 194 |
assert file_date_time.timestamp() == timestamp[1] |
| 195 |
|
| 196 |
|
| 197 |
def test_libpython(tmp_path, caplog): |
| 198 |
wheel = HERE / "python_mscl-67.0.1.0-cp313-cp313-manylinux2014_aarch64.whl" |
| 199 |
args = Namespace( |
| 200 |
LIB_SDIR=".libs", |
| 201 |
ONLY_PLAT=False, |
| 202 |
PLAT="auto", |
| 203 |
STRIP=False, |
| 204 |
UPDATE_TAGS=True, |
| 205 |
WHEEL_DIR=tmp_path, |
| 206 |
WHEEL_FILE=[wheel], |
| 207 |
EXCLUDE=[], |
| 208 |
LDPATHS=None, |
| 209 |
DISABLE_ISA_EXT_CHECK=False, |
| 210 |
ZIP_COMPRESSION_LEVEL=6, |
| 211 |
cmd="repair", |
| 212 |
func=Mock(), |
| 213 |
prog="auditwheel", |
| 214 |
verbose=0, |
| 215 |
) |
| 216 |
main_repair.execute(args, Mock()) |
| 217 |
assert "Removing libpython3.13.so.1.0 dependency from python_mscl/_mscl.so" in caplog.text |
| 218 |
assert tuple(path.name for path in tmp_path.glob("*.whl")) == ( |
| 219 |
"python_mscl-67.0.1.0-cp313-cp313-manylinux2014_aarch64.manylinux_2_31_aarch64.whl", |
| 220 |
) |
| 221 |
|
| 222 |
|
| 223 |
def test_main_lddtree( |
| 224 |
monkeypatch: pytest.MonkeyPatch, |
| 225 |
tmp_path: Path, |
| 226 |
caplog: pytest.LogCaptureFixture, |
| 227 |
) -> None: |
| 228 |
wheel_path = ( |
| 229 |
HERE |
| 230 |
/ "patchelf-0.17.2.1-py2.py3-none-manylinux_2_5_x86_64.manylinux1_x86_64.musllinux_1_1_x86_64.whl" # noqa: E501 |
| 231 |
) |
| 232 |
patchelf_path = tmp_path / "patchelf-0.17.2.1.data/scripts/patchelf" |
| 233 |
with zipfile.ZipFile(wheel_path) as f: |
| 234 |
f.extract(str(patchelf_path.relative_to(tmp_path)), tmp_path) |
| 235 |
patchelf_path = patchelf_path.resolve(strict=True) |
| 236 |
|
| 237 |
monkeypatch.setattr(sys, "platform", "linux") |
| 238 |
monkeypatch.setattr(Architecture, "detect", lambda: Architecture.x86_64) |
| 239 |
monkeypatch.setattr(sys, "argv", ["auditwheel", "lddtree", str(patchelf_path)]) |
| 240 |
assert main() == 0 |
| 241 |
assert len(caplog.messages) == 1 |
| 242 |
actual_json = json.loads(caplog.messages[0]) |
| 243 |
expected_json: Any = { |
| 244 |
"interpreter": None, |
| 245 |
"libc": None, |
| 246 |
"path": str(patchelf_path), |
| 247 |
"realpath": str(patchelf_path), |
| 248 |
"platform": { |
| 249 |
"_elf_osabi": "ELFOSABI_SYSV", |
| 250 |
"_elf_class": 64, |
| 251 |
"_elf_little_endian": True, |
| 252 |
"_elf_machine": "EM_X86_64", |
| 253 |
"_base_arch": "<Architecture.x86_64: 'x86_64'>", |
| 254 |
"_ext_arch": None, |
| 255 |
"_error_msg": None, |
| 256 |
}, |
| 257 |
"needed": [], |
| 258 |
"rpath": [], |
| 259 |
"runpath": [], |
| 260 |
"libraries": {}, |
| 261 |
} |
| 262 |
assert expected_json == actual_json |
| 263 |
|
| 264 |
|
| 265 |
def test_weak_symbols_not_blacklisted() -> None: |
| 266 |
# https://github.com/pypa/auditwheel/issues/663 |
| 267 |
# the cryptography wheel overall policy was misclassified as manylinux_2_24_x86_64 |
| 268 |
# in auditwheel 6.5.1 because it uses the undefined weak symbol '__cxa_thread_atexit_impl' |
| 269 |
result = analyze_wheel_abi( |
| 270 |
None, |
| 271 |
None, |
| 272 |
HERE / "cryptography-46.0.3-cp38-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", |
| 273 |
frozenset(), |
| 274 |
disable_isa_ext_check=False, |
| 275 |
allow_graft=False, |
| 276 |
) |
| 277 |
assert result.policies.libc == Libc.GLIBC |
| 278 |
assert result.policies.architecture == Architecture.x86_64 |
| 279 |
assert result.overall_policy.name == "manylinux_2_17_x86_64" |