File: test_bundled_wheels.py

package info (click to toggle)
python-auditwheel 6.6.0%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 980 kB
  • sloc: python: 6,165; ansic: 304; cpp: 66; sh: 28; makefile: 25; f90: 12
file content (277 lines) | stat: -rw-r--r-- 9,422 bytes parent folder | download
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
from __future__ import annotations

import importlib
import json
import os
import sys
import zipfile
from argparse import Namespace
from datetime import datetime, timezone
from os.path import isabs
from pathlib import Path
from typing import Any
from unittest.mock import Mock

import pytest

from auditwheel import lddtree, main_repair
from auditwheel.architecture import Architecture
from auditwheel.libc import Libc
from auditwheel.main import main
from auditwheel.wheel_abi import NonPlatformWheelError, analyze_wheel_abi

HERE = Path(__file__).parent.resolve()


@pytest.mark.parametrize(
    ("file", "external_libs", "exclude"),
    [
        (
            "cffi-1.5.0-cp27-none-linux_x86_64.whl",
            {"libffi.so.5", "libpython2.7.so.1.0"},
            frozenset(),
        ),
        (
            "cffi-1.5.0-cp27-none-linux_x86_64.whl",
            set(),
            frozenset(["libffi.so.5", "libpython2.7.so.1.0"]),
        ),
        (
            "cffi-1.5.0-cp27-none-linux_x86_64.whl",
            {"libffi.so.5", "libpython2.7.so.1.0"},
            frozenset(["libffi.so.noexist", "libnoexist.so.*"]),
        ),
        (
            "cffi-1.5.0-cp27-none-linux_x86_64.whl",
            {"libpython2.7.so.1.0"},
            frozenset(["libffi.so.[4,5]"]),
        ),
        (
            "cffi-1.5.0-cp27-none-linux_x86_64.whl",
            {"libffi.so.5", "libpython2.7.so.1.0"},
            frozenset(["libffi.so.[6,7]"]),
        ),
        (
            "cffi-1.5.0-cp27-none-linux_x86_64.whl",
            {"libpython2.7.so.1.0"},
            frozenset([f"{HERE}/*"]),
        ),
        (
            "cffi-1.5.0-cp27-none-linux_x86_64.whl",
            {"libpython2.7.so.1.0"},
            frozenset(["libffi.so.*"]),
        ),
        ("cffi-1.5.0-cp27-none-linux_x86_64.whl", set(), frozenset(["*"])),
        (
            "python_snappy-0.5.2-pp260-pypy_41-linux_x86_64.whl",
            {"libsnappy.so.1"},
            frozenset(),
        ),
    ],
)
def test_analyze_wheel_abi(file, external_libs, exclude):
    # If exclude libs contain path, LD_LIBRARY_PATH need to be modified to find the libs
    # `lddtree.load_ld_paths` needs to be reloaded for it's `lru_cache`-ed.
    modify_ld_library_path = any(isabs(e) for e in exclude)

    with pytest.MonkeyPatch.context() as cp:
        if modify_ld_library_path:
            cp.setenv("LD_LIBRARY_PATH", f"{HERE}")
            importlib.reload(lddtree)

        winfo = analyze_wheel_abi(
            Libc.GLIBC,
            Architecture.x86_64,
            HERE / file,
            exclude,
            disable_isa_ext_check=False,
            allow_graft=True,
        )
        assert set(winfo.external_refs["manylinux_2_5_x86_64"].libs) == external_libs, (
            f"{HERE}, {exclude}, {os.environ}"
        )

    if modify_ld_library_path:
        importlib.reload(lddtree)


def test_analyze_wheel_abi_pyfpe():
    winfo = analyze_wheel_abi(
        Libc.GLIBC,
        Architecture.x86_64,
        HERE / "fpewheel-0.0.0-cp35-cp35m-linux_x86_64.whl",
        frozenset(),
        disable_isa_ext_check=False,
        allow_graft=True,
    )
    # for external symbols, it could get manylinux1
    assert winfo.sym_policy.name == "manylinux_2_5_x86_64"
    # but for having the pyfpe reference, it gets just linux
    assert winfo.pyfpe_policy.name == "linux_x86_64"
    assert winfo.overall_policy.name == "linux_x86_64"


def test_show_wheel_abi_pyfpe(monkeypatch, capsys):
    wheel = str(HERE / "fpewheel-0.0.0-cp35-cp35m-linux_x86_64.whl")
    monkeypatch.setattr(sys, "platform", "linux")
    monkeypatch.setattr(Architecture, "detect", lambda: Architecture.x86_64)
    monkeypatch.setattr(sys, "argv", ["auditwheel", "show", wheel])
    assert main() == 0
    captured = capsys.readouterr()
    assert "This wheel uses the PyFPE_jbuf function" in captured.out


def test_analyze_wheel_abi_bad_architecture():
    with pytest.raises(NonPlatformWheelError):
        analyze_wheel_abi(
            Libc.GLIBC,
            Architecture.aarch64,
            HERE / "fpewheel-0.0.0-cp35-cp35m-linux_x86_64.whl",
            frozenset(),
            disable_isa_ext_check=False,
            allow_graft=True,
        )


def test_analyze_wheel_abi_static_exe(caplog):
    result = analyze_wheel_abi(
        None,
        None,
        HERE
        / "patchelf-0.17.2.1-py2.py3-none-manylinux_2_5_x86_64.manylinux1_x86_64.musllinux_1_1_x86_64.whl",  # noqa: E501
        frozenset(),
        disable_isa_ext_check=False,
        allow_graft=False,
    )
    assert "setting architecture to x86_64" in caplog.text
    assert "couldn't detect wheel libc, defaulting to" in caplog.text
    assert result.policies.architecture == Architecture.x86_64
    if Libc.detect() == Libc.MUSL:
        assert result.policies.libc == Libc.MUSL
        assert result.overall_policy.name.startswith("musllinux_1_")
    else:
        assert result.policies.libc == Libc.GLIBC
        assert result.overall_policy.name == "manylinux_2_5_x86_64"


@pytest.mark.parametrize(
    "timestamp",
    [
        (0, 315532800),  # zip timestamp starts 1980-01-01, not 1970-01-01
        (315532799, 315532800),  # zip timestamp starts 1980-01-01, not 1970-01-01
        (315532801, 315532800),  # zip timestamp round odd seconds down to even seconds
        (315532802, 315532802),
        (650203201, 650203200),  # zip timestamp round odd seconds down to even seconds
    ],
)
def test_wheel_source_date_epoch(timestamp, tmp_path, monkeypatch):
    wheel_path = HERE / "arch-wheels/musllinux_1_2/testsimple-0.0.1-cp312-cp312-linux_x86_64.whl"
    wheel_output_path = tmp_path / "out"
    args = Namespace(
        LIB_SDIR=".libs",
        ONLY_PLAT=False,
        PLAT="auto",
        STRIP=False,
        UPDATE_TAGS=True,
        WHEEL_DIR=wheel_output_path,
        WHEEL_FILE=[wheel_path],
        EXCLUDE=[],
        DISABLE_ISA_EXT_CHECK=False,
        ZIP_COMPRESSION_LEVEL=6,
        cmd="repair",
        func=Mock(),
        prog="auditwheel",
        verbose=1,
    )

    monkeypatch.setenv("SOURCE_DATE_EPOCH", str(timestamp[0]))
    main_repair.execute(args, Mock())
    output_wheel, *_ = list(wheel_output_path.glob("*.whl"))
    with zipfile.ZipFile(output_wheel) as wheel_file:
        for file in wheel_file.infolist():
            file_date_time = datetime(*file.date_time, tzinfo=timezone.utc)
            assert file_date_time.timestamp() == timestamp[1]


def test_libpython(tmp_path, caplog):
    wheel = HERE / "python_mscl-67.0.1.0-cp313-cp313-manylinux2014_aarch64.whl"
    args = Namespace(
        LIB_SDIR=".libs",
        ONLY_PLAT=False,
        PLAT="auto",
        STRIP=False,
        UPDATE_TAGS=True,
        WHEEL_DIR=tmp_path,
        WHEEL_FILE=[wheel],
        EXCLUDE=[],
        DISABLE_ISA_EXT_CHECK=False,
        ZIP_COMPRESSION_LEVEL=6,
        cmd="repair",
        func=Mock(),
        prog="auditwheel",
        verbose=0,
    )
    main_repair.execute(args, Mock())
    assert "Removing libpython3.13.so.1.0 dependency from python_mscl/_mscl.so" in caplog.text
    assert tuple(path.name for path in tmp_path.glob("*.whl")) == (
        "python_mscl-67.0.1.0-cp313-cp313-manylinux2014_aarch64.manylinux_2_31_aarch64.whl",
    )


def test_main_lddtree(
    monkeypatch: pytest.MonkeyPatch,
    tmp_path: Path,
    caplog: pytest.LogCaptureFixture,
) -> None:
    wheel_path = (
        HERE
        / "patchelf-0.17.2.1-py2.py3-none-manylinux_2_5_x86_64.manylinux1_x86_64.musllinux_1_1_x86_64.whl"  # noqa: E501
    )
    patchelf_path = tmp_path / "patchelf-0.17.2.1.data/scripts/patchelf"
    with zipfile.ZipFile(wheel_path) as f:
        f.extract(str(patchelf_path.relative_to(tmp_path)), tmp_path)
    patchelf_path = patchelf_path.resolve(strict=True)

    monkeypatch.setattr(sys, "platform", "linux")
    monkeypatch.setattr(Architecture, "detect", lambda: Architecture.x86_64)
    monkeypatch.setattr(sys, "argv", ["auditwheel", "lddtree", str(patchelf_path)])
    assert main() == 0
    assert len(caplog.messages) == 1
    actual_json = json.loads(caplog.messages[0])
    expected_json: Any = {
        "interpreter": None,
        "libc": None,
        "path": str(patchelf_path),
        "realpath": str(patchelf_path),
        "platform": {
            "_elf_osabi": "ELFOSABI_SYSV",
            "_elf_class": 64,
            "_elf_little_endian": True,
            "_elf_machine": "EM_X86_64",
            "_base_arch": "<Architecture.x86_64: 'x86_64'>",
            "_ext_arch": None,
            "_error_msg": None,
        },
        "needed": [],
        "rpath": [],
        "runpath": [],
        "libraries": {},
    }
    assert expected_json == actual_json


def test_weak_symbols_not_blacklisted() -> None:
    # https://github.com/pypa/auditwheel/issues/663
    # the cryptography wheel overall policy was misclassified as manylinux_2_24_x86_64
    # in auditwheel 6.5.1 because it uses the undefined weak symbol '__cxa_thread_atexit_impl'
    result = analyze_wheel_abi(
        None,
        None,
        HERE / "cryptography-46.0.3-cp38-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl",
        frozenset(),
        disable_isa_ext_check=False,
        allow_graft=False,
    )
    assert result.policies.libc == Libc.GLIBC
    assert result.policies.architecture == Architecture.x86_64
    assert result.overall_policy.name == "manylinux_2_17_x86_64"