Skip to content

Commit 5276a7b

Browse files
authored
Support to wildcard a path with --exclude (#528)
1 parent 64197a9 commit 5276a7b

File tree

3 files changed

+39
-6
lines changed

3 files changed

+39
-6
lines changed

src/auditwheel/lddtree.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,7 @@ def lddtree(
386386
libs: list[str] = []
387387
rpaths: list[str] = []
388388
runpaths: list[str] = []
389+
_excluded_libs: set[str] = set()
389390
for segment in elf.iter_segments():
390391
if segment.header.p_type != "PT_DYNAMIC":
391392
continue
@@ -396,8 +397,11 @@ def lddtree(
396397
elif t.entry.d_tag == "DT_RUNPATH":
397398
runpaths = parse_ld_paths(t.runpath, path=path, root=root)
398399
elif t.entry.d_tag == "DT_NEEDED":
399-
if any(fnmatch(t.needed, e) for e in exclude):
400+
if t.needed in _excluded_libs or any(
401+
fnmatch(t.needed, e) for e in exclude
402+
):
400403
log.info("Excluding %s", t.needed)
404+
_excluded_libs.add(t.needed)
401405
else:
402406
libs.append(t.needed)
403407
if runpaths:
@@ -416,7 +420,6 @@ def lddtree(
416420
log.debug(" ldpaths[runpath] = %s", runpaths)
417421
ret["rpath"] = rpaths
418422
ret["runpath"] = runpaths
419-
ret["needed"] = libs
420423

421424
# Search for the libs this ELF uses.
422425
all_ldpaths: list[str] | None = None
@@ -434,6 +437,12 @@ def lddtree(
434437
+ ldpaths["interp"]
435438
)
436439
realpath, fullpath = find_lib(elf, lib, all_ldpaths, root)
440+
if lib in _excluded_libs or (
441+
realpath is not None and any(fnmatch(realpath, e) for e in exclude)
442+
):
443+
log.info("Excluding %s", realpath)
444+
_excluded_libs.add(lib)
445+
continue
437446
_all_libs[lib] = {
438447
"realpath": realpath,
439448
"path": fullpath,
@@ -453,4 +462,6 @@ def lddtree(
453462

454463
del elf
455464

465+
ret["needed"] = [lib for lib in libs if lib not in _excluded_libs]
466+
456467
return ret

tests/integration/libffi.so.5

5.87 KB
Binary file not shown.

tests/integration/test_bundled_wheels.py

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
from __future__ import annotations
22

3+
import importlib
4+
import os
35
import platform
46
import subprocess
57
import sys
68
import zipfile
79
from argparse import Namespace
810
from datetime import datetime, timezone
11+
from os.path import isabs
912
from pathlib import Path
1013
from unittest import mock
1114
from unittest.mock import Mock
1215

1316
import pytest
1417

15-
from auditwheel import main_repair
18+
from auditwheel import lddtree, main_repair
1619
from auditwheel.libc import Libc
1720
from auditwheel.policy import WheelPolicies
1821
from auditwheel.wheel_abi import analyze_wheel_abi
@@ -40,6 +43,11 @@
4043
{"libffi.so.5"},
4144
frozenset(["libffi.so.[6,7]"]),
4245
),
46+
(
47+
"cffi-1.5.0-cp27-none-linux_x86_64.whl",
48+
set(),
49+
frozenset([f"{HERE}/*"]),
50+
),
4351
("cffi-1.5.0-cp27-none-linux_x86_64.whl", set(), frozenset(["libffi.so.*"])),
4452
("cffi-1.5.0-cp27-none-linux_x86_64.whl", set(), frozenset(["*"])),
4553
(
@@ -50,9 +58,23 @@
5058
],
5159
)
5260
def test_analyze_wheel_abi(file, external_libs, exclude):
53-
wheel_policies = WheelPolicies(libc=Libc.GLIBC, arch="x86_64")
54-
winfo = analyze_wheel_abi(wheel_policies, str(HERE / file), exclude)
55-
assert set(winfo.external_refs["manylinux_2_5_x86_64"]["libs"]) == external_libs
61+
# If exclude libs contain path, LD_LIBRARY_PATH need to be modified to find the libs
62+
# `lddtree.load_ld_paths` needs to be reloaded for it's `lru_cache`-ed.
63+
modify_ld_library_path = any(isabs(e) for e in exclude)
64+
65+
with pytest.MonkeyPatch.context() as cp:
66+
if modify_ld_library_path:
67+
cp.setenv("LD_LIBRARY_PATH", f"{HERE}")
68+
importlib.reload(lddtree)
69+
70+
wheel_policies = WheelPolicies(libc=Libc.GLIBC, arch="x86_64")
71+
winfo = analyze_wheel_abi(wheel_policies, str(HERE / file), exclude)
72+
assert (
73+
set(winfo.external_refs["manylinux_2_5_x86_64"]["libs"]) == external_libs
74+
), f"{HERE}, {exclude}, {os.environ}"
75+
76+
if modify_ld_library_path:
77+
importlib.reload(lddtree)
5678

5779

5880
def test_analyze_wheel_abi_pyfpe():

0 commit comments

Comments
 (0)