Skip to content

Commit 3b91821

Browse files
committed
feat: add option --no-copy-site-libs to skip libraries in site-packages for auditwheel repair
1 parent 48327fb commit 3b91821

File tree

2 files changed

+59
-19
lines changed

2 files changed

+59
-19
lines changed

src/auditwheel/main_repair.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,17 @@ def configure_parser(sub_parsers):
9595
help="Do not check for higher policy compatibility",
9696
default=False,
9797
)
98+
p.add_argument(
99+
"--no-copy-site-libs",
100+
dest="COPY_SITE_LIBS",
101+
action="store_false",
102+
help=(
103+
"Do not copy libraries located in the site-packages directory from "
104+
"other packages (update rpath only). The developer will need to ensure "
105+
"that the skipped libraries are listed the package install dependencies."
106+
),
107+
default=True,
108+
)
98109
p.set_defaults(func=execute)
99110

100111

@@ -170,6 +181,7 @@ def execute(args, p):
170181
update_tags=args.UPDATE_TAGS,
171182
patcher=patcher,
172183
strip=args.STRIP,
184+
copy_site_libs=args.COPY_SITE_LIBS,
173185
)
174186

175187
if out_wheel is not None:

src/auditwheel/repair.py

Lines changed: 47 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ def repair_wheel(
3838
update_tags: bool,
3939
patcher: ElfPatcher,
4040
strip: bool = False,
41+
copy_site_libs: bool = True,
4142
) -> Optional[str]:
4243

4344
external_refs_by_fn = get_wheel_elfdata(wheel_path)[1]
@@ -84,13 +85,14 @@ def repair_wheel(
8485
src_path_to_real_sonames[soname] = real_soname
8586
same_soname_libs[real_soname].add(soname)
8687

87-
for soname, src_path in tuple(ext_libs.items()):
88-
if "site-packages" in str(src_path).split(os.path.sep):
89-
try:
90-
del same_soname_libs[src_path_to_real_sonames[soname]]
91-
except KeyError:
92-
pass
93-
continue
88+
if not copy_site_libs:
89+
for soname, src_path in tuple(ext_libs.items()):
90+
if "site-packages" in str(src_path).split(os.path.sep):
91+
try:
92+
del same_soname_libs[src_path_to_real_sonames[soname]]
93+
except KeyError:
94+
pass
95+
continue
9496

9597
for real_soname, sonames in same_soname_libs.items():
9698
if len(sonames) == 0:
@@ -111,7 +113,13 @@ def repair_wheel(
111113
new_rpath = os.path.join("$ORIGIN", new_rpath)
112114
else:
113115
new_rpath = None # no new .so files are copied
114-
append_rpath_within_wheel(fn, new_rpath, ctx.name, patcher)
116+
append_rpath_within_wheel(
117+
fn,
118+
new_rpath,
119+
ctx.name,
120+
patcher,
121+
copy_site_libs=copy_site_libs,
122+
)
115123

116124
# we grafted in a bunch of libraries and modified their sonames, but
117125
# they may have internal dependencies (DT_NEEDED) on one another, so
@@ -189,7 +197,12 @@ def copylib(src_path: str, dest_dir: str, patcher: ElfPatcher) -> Tuple[str, str
189197

190198

191199
def append_rpath_within_wheel(
192-
lib_name: str, rpath: Optional[str], wheel_base_dir: str, patcher: ElfPatcher
200+
lib_name: str,
201+
rpath: Optional[str],
202+
wheel_base_dir: str,
203+
patcher: ElfPatcher,
204+
*,
205+
copy_site_libs: bool = True,
193206
) -> None:
194207
"""Add a new rpath entry to a file while preserving as many existing
195208
rpath entries as possible.
@@ -206,17 +219,20 @@ def append_rpath_within_wheel(
206219
wheel_base_dir = abspath(wheel_base_dir)
207220

208221
def is_valid_rpath(rpath: str) -> bool:
209-
return _is_valid_rpath(rpath, lib_dir, wheel_base_dir)
222+
return _is_valid_rpath(
223+
rpath, lib_dir, wheel_base_dir, copy_site_libs=copy_site_libs
224+
)
210225

211226
site_packages = os.path.join("$ORIGIN", os.path.relpath(wheel_base_dir, lib_dir))
212227

213228
old_rpaths = patcher.get_rpath(lib_name)
214229
rpaths = [rp for rp in old_rpaths.split(":") if is_valid_rpath(rp)]
215230
rpaths = [
216231
rp
217-
if "site-packages" not in rp.split(os.path.sep)
232+
if copy_site_libs or "site-packages" not in rp.split(os.path.sep)
218233
else os.path.join(
219-
site_packages, rp.rpartition("site-packages" + os.path.sep)[-1]
234+
site_packages,
235+
rp.rpartition(os.path.sep + "site-packages" + os.path.sep)[-1],
220236
)
221237
for rp in rpaths
222238
]
@@ -229,32 +245,44 @@ def is_valid_rpath(rpath: str) -> bool:
229245
patcher.set_rpath(lib_name, ":".join(rpath_set))
230246

231247

232-
def _is_valid_rpath(rpath: str, lib_dir: str, wheel_base_dir: str) -> bool:
248+
def _is_valid_rpath(
249+
rpath: str,
250+
lib_dir: str,
251+
wheel_base_dir: str,
252+
*,
253+
copy_site_libs: bool = True,
254+
) -> bool:
233255
full_rpath_entry = _resolve_rpath_tokens(rpath, lib_dir)
256+
234257
if not isabs(full_rpath_entry):
235258
logger.debug(
236259
f"rpath entry {rpath} could not be resolved to an "
237260
"absolute path -- discarding it."
238261
)
239262
return False
240-
elif not is_subdir(full_rpath_entry, wheel_base_dir):
241-
if "site-packages" in full_rpath_entry.split(os.path.sep):
263+
264+
if not is_subdir(full_rpath_entry, wheel_base_dir):
265+
if not copy_site_libs and "site-packages" in full_rpath_entry.split(
266+
os.path.sep
267+
):
242268
site_packages = os.path.join(
243269
"$ORIGIN",
244270
os.path.relpath(wheel_base_dir, lib_dir),
245271
)
246272
new_rpath = os.path.join(
247-
site_packages, rpath.rpartition("site-packages" + os.path.sep)[-1]
273+
site_packages,
274+
rpath.rpartition(os.path.sep + "site-packages" + os.path.sep)[-1],
248275
)
249276
logger.debug(f"Preserved rpath entry {rpath} as {new_rpath}")
250277
return True
278+
251279
logger.debug(
252280
f"rpath entry {rpath} points outside the wheel -- " "discarding it."
253281
)
254282
return False
255-
else:
256-
logger.debug(f"Preserved rpath entry {rpath}")
257-
return True
283+
284+
logger.debug(f"Preserved rpath entry {rpath}")
285+
return True
258286

259287

260288
def _resolve_rpath_tokens(rpath: str, lib_base_dir: str) -> str:

0 commit comments

Comments
 (0)