-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdllfinder.py
281 lines (235 loc) · 9.38 KB
/
dllfinder.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#!/usr/bin/python3.3
# -*- coding: utf-8 -*-
"""dllfinder
"""
from . import _wapi
import collections
from importlib.machinery import EXTENSION_SUFFIXES
import os
import sys
from . mf3 import ModuleFinder
from . import hooks
################################
# XXX Move these into _wapi???
_buf = _wapi.create_unicode_buffer(260)
_wapi.GetWindowsDirectoryW(_buf, len(_buf))
windir = _buf.value.lower()
_wapi.GetSystemDirectoryW(_buf, len(_buf))
sysdir = _buf.value.lower()
_wapi.GetModuleFileNameW(sys.dllhandle, _buf, len(_buf))
pydll = _buf.value.lower()
def SearchPath(imagename, path=None):
pfile = _wapi.c_wchar_p()
if _wapi.SearchPathW(path,
imagename,
None,
len(_buf),
_buf,
pfile):
return _buf.value
return None
################################
class DllFinder:
def __init__(self):
# _loaded_dlls contains ALL dlls that are bound, this includes
# the loaded extension modules; maps lower case basename to
# full pathname.
self._loaded_dlls = {}
# _dlls contains the full pathname of the dlls that
# are NOT considered system dlls.
#
# The pathname is mapped to a set of modules/dlls that require
# this dll. This allows to find out WHY a certain dll has to
# be included.
self._dlls = collections.defaultdict(set)
def _add_dll(self, path):
self._dlls[path]
self.import_extension(path)
def import_extension(self, pyd, callers=None):
"""Add an extension module and scan it for dependencies.
"""
todo = {pyd} # todo contains the dlls that we have to examine
while todo:
dll = todo.pop() # get one and check it
if dll in self._loaded_dlls:
continue
for dep_dll in self.bind_image(dll):
if dep_dll in self._loaded_dlls:
continue
dll_type = self.determine_dll_type(dep_dll)
if dll_type is None:
continue
## if dll_type == "EXT":
## print("EXT", dep_dll)
## elif dll_type == "DLL":
## print("DLL", dep_dll)
todo.add(dep_dll)
self._dlls[dep_dll].add(dll)
def bind_image(self, imagename):
"""Call BindImageEx and collect all dlls that are bound.
"""
# XXX Should cache results!
path = ";".join([os.path.dirname(imagename),
os.path.dirname(sys.executable),
os.environ["PATH"]])
result = set()
@_wapi.PIMAGEHLP_STATUS_ROUTINE
def status_routine(reason, img_name, dllname, va, parameter):
if reason == _wapi.BindImportModule: # 5
assert img_name.decode("mbcs") == imagename
# imagename binds to dllname
dllname = self.search_path(dllname.decode("mbcs"), path)
result.add(dllname)
return True
# BindImageEx uses the PATH environment variable to find
# dependend dlls; set it to our changed PATH:
old_path = os.environ["PATH"]
assert isinstance(path, str)
os.environ["PATH"] = path
self._loaded_dlls[os.path.basename(imagename).lower()] = imagename
_wapi.BindImageEx(_wapi.BIND_ALL_IMAGES
| _wapi.BIND_CACHE_IMPORT_DLLS
| _wapi.BIND_NO_UPDATE,
imagename.encode("mbcs"),
None,
##path.encode("mbcs"),
None,
status_routine)
# Be a good citizen and cleanup:
os.environ["PATH"] = old_path
return result
def determine_dll_type(self, imagename):
"""determine_dll_type must be called with a full pathname.
For any dll in the Windows or System directory or any
subdirectory thereof return None, except when the dll binds to
or IS the current python dll.
Return "DLL" when the image binds to the python dll, return
None when the image is in the windows or system directory,
return "EXT" otherwise.
"""
fnm = imagename.lower()
if fnm == pydll.lower():
return "DLL"
deps = self.bind_image(imagename)
if pydll in [d.lower() for d in deps]:
return "EXT"
if fnm.startswith(windir + os.sep) or fnm.startswith(sysdir + os.sep):
return None
return "DLL"
def search_path(self, imagename, path):
"""Find an image (exe or dll) on the PATH."""
if imagename.lower() in self._loaded_dlls:
return self._loaded_dlls[imagename.lower()]
# SxS files (like msvcr90.dll or msvcr100.dll) are only found in
# the SxS directory when the PATH is NULL.
if path is not None:
found = SearchPath(imagename)
if found is not None:
return found
return SearchPath(imagename, path)
def all_dlls(self):
"""Return a set containing all dlls that are needed,
except the python dll.
"""
return {dll for dll in self._dlls
if dll.lower() != pydll.lower()}
def extension_dlls(self):
"""Return a set containing only the extension dlls that are
needed.
"""
return {dll for dll in self._dlls
if "EXT" == self.determine_dll_type(dll)}
def real_dlls(self):
"""Return a set containing only the dlls that do not bind to
the python dll.
"""
return {dll for dll in self._dlls
if "DLL" == self.determine_dll_type(dll)
and dll.lower() != pydll.lower()}
################################################################
class Scanner(ModuleFinder):
"""A ModuleFinder subclass which allows to find binary
dependencies.
"""
def __init__(self, path=None, verbose=0, excludes=[], optimize=0):
super().__init__(path, verbose, excludes, optimize)
self.dllfinder = DllFinder()
self._data_directories = {}
self._min_bundle = {}
self._import_package_later = []
self._safe_import_hook_later = []
self._boot_code = []
hooks.init_finder(self)
def add_bootcode(self, code):
"""Add some code that the exe will execute when bootstrapping."""
self._boot_code.append(code)
def set_min_bundle(self, name, value):
self._min_bundle[name] = value
def get_min_bundle(self):
return self._min_bundle
def hook(self, mod):
hookname = "hook_%s" % mod.__name__.replace(".", "_")
mth = getattr(hooks, hookname, None)
if mth:
mth(self, mod)
def _add_module(self, name, mod):
self.hook(mod)
super()._add_module(name, mod)
if hasattr(mod, "__file__") \
and mod.__file__.endswith(tuple(EXTENSION_SUFFIXES)):
callers = {self.modules[n]
for n in self._depgraph[name]
# self._depgraph can contain '-' entries!
if n in self.modules}
self._add_pyd(mod.__file__, callers)
def _add_pyd(self, name, callers):
self.dllfinder.import_extension(name, callers)
## def required_dlls(self):
## return self.dllfinder.required_dlls()
def all_dlls(self):
return self.dllfinder.all_dlls()
def real_dlls(self):
return self.dllfinder.real_dlls()
def extension_dlls(self):
return self.dllfinder.extension_dlls()
def add_datadirectory(self, name, path, recursive):
self._data_directories[name] = (path, recursive)
def add_dll(self, path):
self.dllfinder._add_dll(path)
## def report_dlls(self):
## import pprint
## pprint.pprint(set(self.dllfinder.required_dlls()))
## pprint.pprint(set(self.dllfinder.system_dlls()))
def import_package_later(self, package):
# This method can be called from hooks to add additional
# packages. It is called BEFORE a module is imported
# completely!
self._import_package_later.append(package)
def safe_import_hook_later(self, name,
caller=None,
fromlist=(),
level=0):
# This method can be called from hooks to add additional
# packages. It is called BEFORE a module is imported
# completely!
self._safe_import_hook_later.append((name, caller, fromlist, level))
def finish(self):
while self._import_package_later:
pkg = self._import_package_later.pop()
self.import_package(pkg)
while self._safe_import_hook_later:
args = self._safe_import_hook_later.pop()
name, caller, fromlist, level = args
self.safe_import_hook(name,
caller=caller,
fromlist=fromlist,
level=level)
################################################################
if __name__ == "__main__":
# test script and usage example
#
# Should we introduce an 'offical' subclass of ModuleFinder
# and DllFinder?
scanner = Scanner()
scanner.import_package("numpy")
scanner.report_dlls()