Skip to content

Commit

Permalink
ptimize calls to install_name_tool
Browse files Browse the repository at this point in the history
  • Loading branch information
isuruf committed Mar 21, 2024
1 parent ff4eb75 commit 5cdbe66
Showing 1 changed file with 29 additions and 15 deletions.
44 changes: 29 additions & 15 deletions delocate/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
import time
import warnings
import zipfile
from collections import Counter
from datetime import datetime
from itertools import chain
from os import PathLike
from os.path import exists, isdir
from os.path import join as pjoin
Expand Down Expand Up @@ -750,6 +752,8 @@ def get_rpaths(filename: str) -> Tuple[str, ...]:
"""Return a tuple of rpaths from the library `filename`.
If `filename` is not a library then the returned tuple will be empty.
Duplicate rpaths will be returned if there are duplicate rpaths in the
Mach-O binary.
Parameters
----------
Expand Down Expand Up @@ -819,24 +823,32 @@ def add_rpath(filename: str, newpath: str, ad_hoc_sign: bool = True) -> None:


@ensure_writable
def delete_rpath(
filename: str, existing_path: str, ad_hoc_sign: bool = True
def _delete_rpaths(
filename: str, rpaths: List[str], ad_hoc_sign: bool = True
) -> None:
"""Remove rpath `newpath` from library `filename`.
Parameters
----------
filename : str
filename of library
existing_path : str
rpath to delete
rpaths : List[str]
rpaths to delete
ad_hoc_sign : {True, False}, optional
If True, sign file with ad-hoc signature
"""
_run(
["install_name_tool", "-delete_rpath", existing_path, filename],
check=True,
)
rpaths = Counter(rpaths)

Check failure on line 840 in delocate/tools.py

View workflow job for this annotation

GitHub Actions / mypy

Incompatible types in assignment (expression has type "Counter[str]", variable has type "List[str]")
n = max(rpaths.values())

Check failure on line 841 in delocate/tools.py

View workflow job for this annotation

GitHub Actions / mypy

"List[str]" has no attribute "values"
for i in range(n):
args = [
("-delete_rpath", rpath)
for (rpath, count) in rpaths.items()

Check failure on line 845 in delocate/tools.py

View workflow job for this annotation

GitHub Actions / mypy

"List[str]" has no attribute "items"
if i < count
]
_run(
["install_name_tool", *chain(*args), filename],
check=True,
)
if ad_hoc_sign:
replace_signature(filename, "-")

Expand Down Expand Up @@ -881,13 +893,15 @@ def _remove_absolute_rpaths(filename: str, ad_hoc_sign: bool = True) -> None:
ad_hoc_sign : {True, False}, optional
If True, sign file with ad-hoc signature
"""
for rpath in get_rpaths(filename):
if not _is_rpath_sanitary(rpath):
logger.info("Sanitize: Deleting rpath %r from %r", rpath, filename)
# We can run these as one command to install_name_tool if there are
# no duplicates. When there are duplicates, we need to delete them
# separately.
delete_rpath(filename, rpath, ad_hoc_sign)
rpaths_to_delete = [
rpath for rpath in get_rpaths(filename) if not _is_rpath_sanitary(rpath)
]
logger.info(
"Sanitize: Deleting rpaths %r from %r",
", ".join(rpaths_to_delete),
filename,
)
_delete_rpaths(filename, rpaths_to_delete, ad_hoc_sign)


def zip2dir(
Expand Down

0 comments on commit 5cdbe66

Please sign in to comment.