Skip to content

Instantly share code, notes, and snippets.

@makslevental
Created July 15, 2024 21:48
Show Gist options
  • Save makslevental/c2bc3d50f3fa046ec435a402623c762f to your computer and use it in GitHub Desktop.
Save makslevental/c2bc3d50f3fa046ec435a402623c762f to your computer and use it in GitHub Desktop.
auditwheel don't delete/rename sos
import os
import shutil
import stat
from os.path import isabs, abspath, basename, exists
from os.path import join as pjoin
from pathlib import Path
from auditwheel.policy import WheelPolicies
from auditwheel.repair import WHEEL_INFO_RE
from auditwheel.wheel_abi import get_wheel_elfdata
from auditwheel.wheeltools import InWheelCtx
def repair_wheel(
wheel_policy: WheelPolicies,
wheel_path: str,
abis: list[str],
lib_sdir: str,
out_dir: str,
exclude: frozenset[str],
) -> str | None:
external_refs_by_fn = get_wheel_elfdata(wheel_policy, wheel_path, exclude)[1]
# Do not repair a pure wheel, i.e. has no external refs
if not external_refs_by_fn:
return None
if not isabs(out_dir):
out_dir = abspath(out_dir)
wheel_fname = basename(wheel_path)
with InWheelCtx(wheel_path) as ctx:
ctx.out_wheel = pjoin(out_dir, wheel_fname)
match = WHEEL_INFO_RE(wheel_fname)
if not match:
raise ValueError("Failed to parse wheel file name: %s", wheel_fname)
dest_dir = match.group("name") + lib_sdir
if not exists(dest_dir):
os.mkdir(dest_dir)
# here, fn is a path to an ELF file (lib or executable) in
# the wheel, and v['libs'] contains its required libs
for fn, v in external_refs_by_fn.items():
ext_libs: dict[str, str] = v[abis[0]]["libs"]
for soname, src_path in ext_libs.items():
assert soname not in exclude
if src_path is None:
raise ValueError(
(
"Cannot repair wheel, because required "
'library "%s" could not be located'
)
% soname
)
copylib(src_path, dest_dir)
def copylib(src_path: str, dest_dir: str) -> tuple[str, str]:
src_name = os.path.basename(src_path)
new_soname = src_name
dest_path = os.path.join(dest_dir, new_soname)
if os.path.exists(dest_path):
return new_soname, dest_path
shutil.copy2(src_path, dest_path)
statinfo = os.stat(dest_path)
if not statinfo.st_mode & stat.S_IWRITE:
os.chmod(dest_path, statinfo.st_mode | stat.S_IWRITE)
return new_soname, dest_path
wheel_policy = WheelPolicies()
policy = wheel_policy.get_policy_by_name("manylinux_2_35_x86_64")
out_wheel = repair_wheel(
wheel_policy,
"/home/mlevental/dev_projects/iree_plus_amd_aie/Vitis/2023.2/aietools/tps/lnx64/dist/chess-0.0.0+deadbeef-py3-none-any.whl",
lib_sdir="libs",
abis=["manylinux_2_35_x86_64"],
out_dir=str(Path.cwd()),
exclude=frozenset(),
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment