Skip to content

Commit f942693

Browse files
committed
Merge branch 'fast' of github.com:oraluben/auditwheel into fast
2 parents 72120a6 + 701d938 commit f942693

27 files changed

+546
-613
lines changed

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ enable_error_code = ["ignore-without-code", "redundant-expr", "truthy-bool"]
1515
extra_checks = true
1616
strict = false
1717
strict_equality = true
18-
warn_unused_configs = true
18+
warn_redundant_casts = true
1919
warn_unreachable = false
20+
warn_unused_configs = true
2021
warn_unused_ignores = true
2122

2223
[[tool.mypy.overrides]]

src/auditwheel/condatools.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,27 @@
44

55
from __future__ import annotations
66

7-
import os
7+
from pathlib import Path
88

99
from .tmpdirs import InTemporaryDirectory
1010
from .tools import tarbz2todir
1111

1212

1313
class InCondaPkg(InTemporaryDirectory):
14-
def __init__(self, in_conda_pkg: str) -> None:
14+
def __init__(self, in_conda_pkg: Path) -> None:
1515
"""Initialize in-conda-package context manager"""
16-
self.in_conda_pkg = os.path.abspath(in_conda_pkg)
16+
self.in_conda_pkg = in_conda_pkg.absolute()
1717
super().__init__()
1818

19-
def __enter__(self) -> str:
19+
def __enter__(self) -> Path:
2020
tarbz2todir(self.in_conda_pkg, self.name)
2121
return super().__enter__()
2222

2323

2424
class InCondaPkgCtx(InCondaPkg):
25-
def __init__(self, in_conda_pkg: str) -> None:
25+
def __init__(self, in_conda_pkg: Path) -> None:
2626
super().__init__(in_conda_pkg)
27-
self.path: str | None = None
27+
self.path: Path | None = None
2828

2929
def __enter__(self): # type: ignore[no-untyped-def]
3030
self.path = super().__enter__()
@@ -34,6 +34,6 @@ def iter_files(self) -> list[str]:
3434
if self.path is None:
3535
msg = "This function should be called from context manager"
3636
raise ValueError(msg)
37-
files = os.path.join(self.path, "info", "files")
37+
files = self.path / "info" / "files"
3838
with open(files) as f:
3939
return [line.strip() for line in f.readlines()]

src/auditwheel/elfutils.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from __future__ import annotations
22

33
from collections.abc import Iterable, Iterator
4-
from os.path import basename
54
from pathlib import Path
65

76
from elftools.common.exceptions import ELFError
@@ -10,7 +9,7 @@
109
from .lddtree import parse_ld_paths
1110

1211

13-
def elf_read_dt_needed(fn: str) -> list[str]:
12+
def elf_read_dt_needed(fn: Path) -> list[str]:
1413
needed = []
1514
with open(fn, "rb") as f:
1615
elf = ELFFile(f)
@@ -26,13 +25,13 @@ def elf_read_dt_needed(fn: str) -> list[str]:
2625
return needed
2726

2827

29-
def elf_file_filter(paths: Iterable[str]) -> Iterator[tuple[str, ELFFile]]:
28+
def elf_file_filter(paths: Iterable[Path]) -> Iterator[tuple[Path, ELFFile]]:
3029
"""Filter through an iterator of filenames and load up only ELF
3130
files
3231
"""
3332

3433
for path in paths:
35-
if path.endswith(".py"):
34+
if path.name.endswith(".py"):
3635
continue
3736
else:
3837
try:
@@ -86,8 +85,8 @@ def elf_references_PyFPE_jbuf(elf: ELFFile) -> bool:
8685
return False
8786

8887

89-
def elf_is_python_extension(fn: str, elf: ELFFile) -> tuple[bool, int | None]:
90-
modname = basename(fn).split(".", 1)[0]
88+
def elf_is_python_extension(fn: Path, elf: ELFFile) -> tuple[bool, int | None]:
89+
modname = fn.name.split(".", 1)[0]
9190
module_init_f = {
9291
"init" + modname: 2,
9392
"PyInit_" + modname: 3,
@@ -109,7 +108,7 @@ def elf_is_python_extension(fn: str, elf: ELFFile) -> tuple[bool, int | None]:
109108
return False, None
110109

111110

112-
def elf_read_rpaths(fn: str) -> dict[str, list[str]]:
111+
def elf_read_rpaths(fn: Path) -> dict[str, list[str]]:
113112
result: dict[str, list[str]] = {"rpaths": [], "runpaths": []}
114113

115114
with open(fn, "rb") as f:
@@ -120,9 +119,9 @@ def elf_read_rpaths(fn: str) -> dict[str, list[str]]:
120119

121120
for t in section.iter_tags():
122121
if t.entry.d_tag == "DT_RPATH":
123-
result["rpaths"] = parse_ld_paths(t.rpath, root="/", path=fn)
122+
result["rpaths"] = parse_ld_paths(t.rpath, root="/", path=str(fn))
124123
elif t.entry.d_tag == "DT_RUNPATH":
125-
result["runpaths"] = parse_ld_paths(t.runpath, root="/", path=fn)
124+
result["runpaths"] = parse_ld_paths(t.runpath, root="/", path=str(fn))
126125

127126
return result
128127

@@ -137,7 +136,7 @@ def is_subdir(path: str | Path | None, directory: str | Path) -> bool:
137136
return directory in path.parents
138137

139138

140-
def get_undefined_symbols(path: str) -> set[str]:
139+
def get_undefined_symbols(path: Path) -> set[str]:
141140
undef_symbols = set()
142141
with open(path, "rb") as f:
143142
elf = ELFFile(f)
@@ -151,14 +150,14 @@ def get_undefined_symbols(path: str) -> set[str]:
151150

152151

153152
def filter_undefined_symbols(
154-
path: str, symbols: dict[str, list[str]]
153+
path: Path, symbols: dict[str, frozenset[str]]
155154
) -> dict[str, list[str]]:
156155
if not symbols:
157156
return {}
158157
undef_symbols = set("*") | get_undefined_symbols(path)
159158
result = {}
160159
for lib, sym_list in symbols.items():
161-
intersection = set(sym_list) & undef_symbols
160+
intersection = sym_list & undef_symbols
162161
if intersection:
163162
result[lib] = sorted(intersection)
164163
return result

src/auditwheel/genericpkgctx.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
from __future__ import annotations
22

3+
from pathlib import Path
4+
35
from .condatools import InCondaPkgCtx
46
from .wheeltools import InWheelCtx
57

68

79
def InGenericPkgCtx(
8-
in_path: str, out_path: str | None = None
10+
in_path: Path, out_path: Path | None = None
911
) -> InWheelCtx | InCondaPkgCtx:
1012
"""Factory that returns a InWheelCtx or InCondaPkgCtx
1113
context manager depending on the file extension
1214
"""
13-
if in_path.endswith(".whl"):
15+
if in_path.name.endswith(".whl"):
1416
return InWheelCtx(in_path, out_path)
15-
if in_path.endswith(".tar.bz2"):
17+
if in_path.name.endswith(".tar.bz2"):
1618
if out_path is not None:
1719
raise NotImplementedError()
1820
return InCondaPkgCtx(in_path)

src/auditwheel/json.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,29 @@
33
import dataclasses
44
import json
55
from enum import Enum
6+
from pathlib import PurePath
67
from typing import Any
78

89

9-
def _encode_value(value: Any) -> Any:
10-
if dataclasses.is_dataclass(value) and not isinstance(value, type):
11-
return dataclasses.asdict(value)
12-
if isinstance(value, frozenset):
13-
return sorted(value)
14-
if isinstance(value, Enum):
15-
return repr(value)
16-
msg = f"object of type {value.__class__.__name__!r} can't be encoded to JSON"
17-
raise TypeError(msg)
10+
class _CustomEncoder(json.JSONEncoder):
11+
def default(self, value: Any) -> Any:
12+
if dataclasses.is_dataclass(value) and not isinstance(value, type):
13+
as_dict = dataclasses.asdict(value)
14+
as_dict.pop("policy", None) # don't dump full policy in logs
15+
return as_dict
16+
if isinstance(value, frozenset):
17+
return sorted(value)
18+
if isinstance(value, Enum):
19+
return repr(value)
20+
if isinstance(value, PurePath):
21+
return str(value)
22+
return super().default(value)
23+
24+
def encode(self, o: Any) -> str:
25+
if isinstance(o, dict):
26+
o = {str(k): v for k, v in o.items()}
27+
return super().encode(o)
1828

1929

2030
def dumps(obj: Any) -> str:
21-
return json.dumps(obj, indent=4, default=_encode_value)
31+
return json.dumps(obj, indent=4, cls=_CustomEncoder)

src/auditwheel/lddtree.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def extended_architecture(self) -> Architecture | None:
7272
class DynamicLibrary:
7373
soname: str
7474
path: str | None
75-
realpath: str | None
75+
realpath: Path | None
7676
platform: Platform | None = None
7777
needed: frozenset[str] = frozenset()
7878

@@ -81,7 +81,7 @@ class DynamicLibrary:
8181
class DynamicExecutable:
8282
interpreter: str | None
8383
path: str
84-
realpath: str
84+
realpath: Path
8585
platform: Platform
8686
needed: frozenset[str]
8787
rpath: tuple[str, ...]
@@ -354,7 +354,7 @@ def load_ld_paths(root: str = "/", prefix: str = "") -> dict[str, list[str]]:
354354

355355
def find_lib(
356356
platform: Platform, lib: str, ldpaths: list[str], root: str = "/"
357-
) -> tuple[str | None, str | None]:
357+
) -> tuple[Path | None, str | None]:
358358
"""Try to locate a ``lib`` that is compatible to ``elf`` in the given
359359
``ldpaths``
360360
@@ -376,9 +376,9 @@ def find_lib(
376376

377377
for ldpath in ldpaths:
378378
path = os.path.join(ldpath, lib)
379-
target = readlink(path, root, prefixed=True)
379+
target = Path(readlink(path, root, prefixed=True))
380380

381-
if os.path.exists(target):
381+
if target.exists():
382382
with open(target, "rb") as f:
383383
libelf = ELFFile(f)
384384
if platform.is_compatible(_get_platform(libelf)):
@@ -388,7 +388,7 @@ def find_lib(
388388

389389

390390
def ldd(
391-
path: str,
391+
path: Path,
392392
root: str = "/",
393393
prefix: str = "",
394394
ldpaths: dict[str, list[str]] | None = None,
@@ -481,9 +481,9 @@ def ldd(
481481

482482
for t in segment.iter_tags():
483483
if t.entry.d_tag == "DT_RPATH":
484-
rpaths = parse_ld_paths(t.rpath, path=path, root=root)
484+
rpaths = parse_ld_paths(t.rpath, path=str(path), root=root)
485485
elif t.entry.d_tag == "DT_RUNPATH":
486-
runpaths = parse_ld_paths(t.runpath, path=path, root=root)
486+
runpaths = parse_ld_paths(t.runpath, path=str(path), root=root)
487487
elif t.entry.d_tag == "DT_NEEDED":
488488
needed.add(t.needed)
489489
if runpaths:
@@ -525,7 +525,7 @@ def ldd(
525525
_excluded_libs.add(soname)
526526
continue
527527
realpath, fullpath = find_lib(platform, soname, all_ldpaths, root)
528-
if realpath is not None and any(fnmatch(realpath, e) for e in exclude):
528+
if realpath is not None and any(fnmatch(str(realpath), e) for e in exclude):
529529
log.info("Excluding %s", realpath)
530530
_excluded_libs.add(soname)
531531
continue
@@ -544,12 +544,15 @@ def ldd(
544544
if interpreter is not None:
545545
soname = os.path.basename(interpreter)
546546
_all_libs[soname] = DynamicLibrary(
547-
soname, interpreter, readlink(interpreter, root, prefixed=True), platform
547+
soname,
548+
interpreter,
549+
Path(readlink(interpreter, root, prefixed=True)),
550+
platform,
548551
)
549552

550553
return DynamicExecutable(
551554
interpreter,
552-
path if display is None else display,
555+
str(path) if display is None else display,
553556
path,
554557
platform,
555558
frozenset(needed - _excluded_libs),

0 commit comments

Comments
 (0)