1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
|
#!/usr/bin/env python3
import argparse
import os
import shutil
import subprocess
import sys
import tempfile
from pathlib import Path
def main():
if sys.platform.startswith("linux"):
os_ = "linux"
elif sys.platform.startswith("darwin"):
os_ = "macos"
elif sys.platform.startswith("win32"):
os_ = "windows"
else:
raise NotImplementedError(
f"sys.platform '{sys.platform}' is not supported yet."
)
p = argparse.ArgumentParser(
description="Convert wheel to be independent of python implementation and ABI"
)
p.set_defaults(prog=Path(sys.argv[0]).name)
p.add_argument("WHEEL_FILE", help="Path to wheel file.")
p.add_argument(
"-w",
"--wheel-dir",
dest="WHEEL_DIR",
help=('Directory to store delocated wheels (default: "wheelhouse/")'),
default="wheelhouse/",
)
args = p.parse_args()
file = Path(args.WHEEL_FILE).resolve(strict=True)
wheelhouse = Path(args.WHEEL_DIR).resolve()
wheelhouse.mkdir(parents=True, exist_ok=True)
with tempfile.TemporaryDirectory() as tmpdir_:
tmpdir = Path(tmpdir_)
# use the platform specific repair tool first
if os_ == "linux":
# use path from cibuildwheel which allows auditwheel to create
# rtree.libs/libspatialindex-*.so.*
cibw_lib_path = "/project/rtree/lib"
if os.environ.get("LD_LIBRARY_PATH"): # append path
os.environ["LD_LIBRARY_PATH"] += f"{os.pathsep}{cibw_lib_path}"
else:
os.environ["LD_LIBRARY_PATH"] = cibw_lib_path
subprocess.run(
["auditwheel", "repair", "-w", str(tmpdir), str(file)], check=True
)
elif os_ == "macos":
subprocess.run(
[
"delocate-wheel",
# "--require-archs",
# "arm64,x86_64",
"-w",
str(tmpdir),
str(file),
],
check=True,
)
elif os_ == "windows":
# no specific tool, just copy
shutil.copyfile(file, tmpdir / file.name)
(file,) = tmpdir.glob("*.whl")
# make this a py3 wheel
subprocess.run(
[
"wheel",
"tags",
"--python-tag",
"py3",
"--abi-tag",
"none",
"--remove",
str(file),
],
check=True,
)
(file,) = tmpdir.glob("*.whl")
# unpack
subprocess.run(["wheel", "unpack", file.name], cwd=tmpdir, check=True)
for unpackdir in tmpdir.iterdir():
if unpackdir.is_dir():
break
else:
raise RuntimeError("subdirectory not found")
if os_ == "linux":
# This is auditwheel's libs, which needs post-processing
libs_dir = unpackdir / "rtree.libs"
lsidx_list = list(libs_dir.glob("libspatialindex*.so*"))
assert len(lsidx_list) == 1, list(libs_dir.iterdir())
lsidx = lsidx_list[0]
subprocess.run(["patchelf", "--set-rpath", "$ORIGIN", lsidx], check=True)
# remove duplicated dir
lib_dir = unpackdir / "rtree" / "lib"
shutil.rmtree(lib_dir)
# re-pack
subprocess.run(["wheel", "pack", str(unpackdir.name)], cwd=tmpdir, check=True)
files = list(tmpdir.glob("*.whl"))
assert len(files) == 1, files
file = files[0]
file.rename(wheelhouse / file.name)
if __name__ == "__main__":
main()
|