-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathwheel_repair.py
122 lines (95 loc) · 3.73 KB
/
wheel_repair.py
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
117
118
119
120
121
122
#!/usr/bin/env python
import os
import sys
import shutil
import hashlib
import zipfile
import argparse
import tempfile
from collections import defaultdict
import pefile
from machomachomangler.pe import redll
def hash_filename(filepath, blocksize=65536):
hasher = hashlib.sha256()
with open(filepath, "rb") as afile:
buf = afile.read(blocksize)
while len(buf) > 0:
hasher.update(buf)
buf = afile.read(blocksize)
root, ext = os.path.splitext(filepath)
return f"{os.path.basename(root)}-{hasher.hexdigest()[:8]}{ext}"
def find_dll_dependencies(dll_filepath, vcpkg_bin_dir):
pe = pefile.PE(dll_filepath)
for entry in pe.DIRECTORY_ENTRY_IMPORT:
entry_name = entry.dll.decode("utf-8")
if entry_name in os.listdir(vcpkg_bin_dir):
dll_dependencies[os.path.basename(dll_filepath)].add(entry_name)
_dll_filepath = os.path.join(vcpkg_bin_dir, entry_name)
find_dll_dependencies(_dll_filepath, vcpkg_bin_dir)
def mangle_filename(old_filename, new_filename, mapping):
with open(old_filename, "rb") as f:
buf = f.read()
new_buf = redll(buf, mapping)
with open(new_filename, "wb") as f:
f.write(new_buf)
parser = argparse.ArgumentParser(
description="Vendor in external shared library dependencies of a wheel."
)
parser.add_argument("WHEEL_FILE", type=str, help="Path to wheel file")
parser.add_argument(
"-d", "--dll-dir", dest="DLL_DIR", type=str, help="Directory to find the DLLs"
)
parser.add_argument(
"-w",
"--wheel-dir",
dest="WHEEL_DIR",
type=str,
help=('Directory to store delocated wheels (default: "wheelhouse/")'),
default="wheelhouse/",
)
args = parser.parse_args()
wheel_name = os.path.basename(args.WHEEL_FILE)
package_name = wheel_name.split("-")[0]
repaired_wheel = os.path.join(args.WHEEL_DIR, wheel_name)
old_wheel_dir = tempfile.mkdtemp()
new_wheel_dir = tempfile.mkdtemp()
with zipfile.ZipFile(args.WHEEL_FILE, "r") as wheel:
wheel.extractall(old_wheel_dir)
wheel.extractall(new_wheel_dir)
pyd_path = list(filter(lambda x: x.endswith(".pyd"), wheel.namelist()))[0]
tmp_pyd_path = os.path.join(old_wheel_dir, package_name, os.path.basename(pyd_path))
# https://docs.python.org/3/library/platform.html#platform.architecture
x = "x64" if sys.maxsize > 2 ** 32 else "x86"
# set VCPKG_INSTALLATION_ROOT=C:\dev\vcpkg
dll_dir = os.path.join(
os.environ["VCPKG_INSTALLATION_ROOT"], "installed", f"{x}-windows", "bin"
)
dll_dependencies = defaultdict(set)
find_dll_dependencies(tmp_pyd_path, dll_dir)
for dll, dependencies in dll_dependencies.items():
mapping = {}
for dep in dependencies:
hashed_name = hash_filename(os.path.join(dll_dir, dep)) # already basename
mapping[dep.encode("ascii")] = hashed_name.encode("ascii")
shutil.copy(
os.path.join(dll_dir, dep),
os.path.join(new_wheel_dir, package_name, hashed_name),
)
if dll.endswith(".pyd"):
old_name = os.path.join(
old_wheel_dir, package_name, os.path.basename(tmp_pyd_path)
)
new_name = os.path.join(
new_wheel_dir, package_name, os.path.basename(tmp_pyd_path)
)
else:
old_name = os.path.join(dll_dir, dll)
hashed_name = hash_filename(os.path.join(dll_dir, dll)) # already basename
new_name = os.path.join(new_wheel_dir, package_name, hashed_name)
mangle_filename(old_name, new_name, mapping)
with zipfile.ZipFile(repaired_wheel, "w", zipfile.ZIP_DEFLATED) as new_wheel:
for root, dirs, files in os.walk(new_wheel_dir):
for file in files:
new_wheel.write(
os.path.join(root, file), os.path.join(os.path.basename(root), file)
)