{{ message }}
forked from cztomczak/cefpython
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhook-cefpython3.py
More file actions
320 lines (275 loc) · 12 KB
/
Copy pathhook-cefpython3.py
File metadata and controls
320 lines (275 loc) · 12 KB
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
"""
This is PyInstaller hook file for CEF Python. This file
helps PyInstaller find CEF Python dependencies that are
required to run final executable.
See PyInstaller docs for hooks:
https://pyinstaller.readthedocs.io/en/stable/hooks.html
"""
import glob
import os
import platform
import re
import sys
import PyInstaller
from PyInstaller.utils.hooks import is_module_satisfies, get_package_paths
from PyInstaller.compat import is_win, is_darwin, is_linux
from PyInstaller import log as logging
try:
# PyInstaller >= 4.0 doesn't support Python 2.7
from PyInstaller.compat import is_py2
except ImportError:
is_py2 = None
# Constants
CEFPYTHON_MIN_VERSION = "57.0"
PYINSTALLER_MIN_VERSION = "6.0"
# Makes assumption that using "python.exe" and not "pyinstaller.exe"
# TODO: use this code to work cross-platform:
# > from PyInstaller.utils.hooks import get_package_paths
# > get_package_paths("cefpython3")
CEFPYTHON3_DIR = get_package_paths("cefpython3")[1]
CYTHON_MODULE_EXT = ".pyd" if is_win else ".so"
MAC_HELPER_APP_NAMES = [
"cefpython Helper.app",
"cefpython Helper (Alerts).app",
"cefpython Helper (GPU).app",
"cefpython Helper (Plugin).app",
"cefpython Helper (Renderer).app",
]
# Globals
logger = logging.getLogger(__name__)
# Functions
def check_platforms():
if not is_win and not is_darwin and not is_linux:
raise SystemExit("Error: Currently only Windows, Linux and Darwin "
"platforms are supported, see Issue #135.")
def check_pyinstaller_version():
"""Using is_module_satisfies() for pyinstaller fails when
installed using 'pip install develop.zip' command
(PyInstaller Issue #2802)."""
# Example version string for dev version of pyinstaller:
# > 3.3.dev0+g5dc9557c
version = PyInstaller.__version__
match = re.search(r"^\d+\.\d+(\.\d+)?", version)
parsed = tuple(int(part) for part in match.group(0).split("."))
minimum = tuple(int(part) for part in PYINSTALLER_MIN_VERSION.split("."))
if parsed < minimum:
raise SystemExit("Error: pyinstaller %s or higher is required"
% PYINSTALLER_MIN_VERSION)
def check_cefpython3_version():
if not is_module_satisfies("cefpython3 >= %s" % CEFPYTHON_MIN_VERSION):
raise SystemExit("Error: cefpython3 %s or higher is required"
% CEFPYTHON_MIN_VERSION)
def get_cefpython_modules():
"""Get all cefpython Cython modules in the cefpython3 package.
It returns a list of names without file extension. Eg.
'cefpython_py27'. """
pyds = glob.glob(os.path.join(CEFPYTHON3_DIR,
"cefpython_py*" + CYTHON_MODULE_EXT))
assert pyds, "Missing cefpython3 Cython module"
modules = []
for path in pyds:
filename = os.path.basename(path)
mod = filename.replace(CYTHON_MODULE_EXT, "")
modules.append(mod)
return modules
def get_excluded_cefpython_modules():
"""CEF Python package includes Cython modules for various Python
versions. When using Python 2.7 pyinstaller should not
bundle modules for eg. Python 3.6, otherwise it will
cause to include Python 3 dll dependencies. Returns a list
of fully qualified names eg. 'cefpython3.cefpython_py27'."""
pyver = "".join(map(str, sys.version_info[:2]))
pyver_string = "py%s" % pyver
modules = get_cefpython_modules()
excluded = []
for mod in modules:
if pyver_string in mod:
continue
excluded.append("cefpython3.%s" % mod)
logger.info("Exclude cefpython3 module: %s" % excluded[-1])
return excluded
def get_macos_framework_binaries():
"""Return the CEF framework Mach-O files with bundle-relative targets."""
framework_name = "Chromium Embedded Framework.framework"
framework_path = os.path.join(CEFPYTHON3_DIR, framework_name)
main_binary = os.path.join(
framework_path, "Chromium Embedded Framework")
assert os.path.isfile(main_binary), \
"CEF framework binary not found: {}".format(main_binary)
ret = [(main_binary, framework_name)]
libraries_path = os.path.join(framework_path, "Libraries")
assert os.path.isdir(libraries_path), \
"CEF framework Libraries directory not found: {}".format(
libraries_path)
for filename in sorted(os.listdir(libraries_path)):
if os.path.splitext(filename)[1] not in (".dylib", ".so"):
continue
source = os.path.join(libraries_path, filename)
if os.path.isfile(source):
ret.append((source, os.path.join(framework_name, "Libraries")))
return ret
def get_macos_helper_binaries():
"""Return all helper Mach-O files with their bundle-relative targets."""
ret = []
for app_name in MAC_HELPER_APP_NAMES:
executable_name = os.path.splitext(app_name)[0]
executable = os.path.join(
CEFPYTHON3_DIR, app_name, "Contents", "MacOS", executable_name)
assert os.path.isfile(executable), \
"Missing CEF helper executable: {}".format(executable)
destination = os.path.join(app_name, "Contents", "MacOS")
ret.append((executable, destination))
return ret
def get_cefpython3_datas():
"""Returning almost all of cefpython binaries as DATAS (see exception
below), because pyinstaller does strange things and fails if these are
returned as BINARIES. It first updates manifest in .dll files:
>> Updating manifest in chrome_elf.dll
And then because of that it fails to load the library:
>> hsrc = win32api.LoadLibraryEx(filename, 0, LOAD_LIBRARY_AS_DATAFILE)
>> pywintypes.error: (5, 'LoadLibraryEx', 'Access is denied.')
It is not required for pyinstaller to modify in any way
CEF binaries or to look for its dependencies. CEF binaries
does not have any external dependencies like MSVCR or similar.
The .pak .dat and .bin files cannot be marked as BINARIES
as pyinstaller would fail to find binary depdendencies on
these files.
The subprocess executable on Windows/Linux and the five helper executables
inside macOS app bundles are passed to PyInstaller as BINARIES so it can
collect dependencies and preserve executable handling.
DATAS are in format: tuple(full_path, dest_subdir).
"""
ret = list()
if is_win:
cefdatadir = "."
elif is_darwin or is_linux:
cefdatadir = "."
else:
assert False, "Unsupported system {}".format(platform.system())
# Binaries, licenses and readmes in the cefpython3/ directory
for filename in os.listdir(CEFPYTHON3_DIR):
# Ignore Cython modules which are already handled by
# pyinstaller automatically.
if filename[:-len(CYTHON_MODULE_EXT)] in get_cefpython_modules():
continue
# CEF binaries and datas
extension = os.path.splitext(filename)[1]
if extension in \
[".exe", ".dll", ".pak", ".dat", ".bin", ".txt", ".so", ".plist"] \
or filename.lower().startswith("license"):
logger.info("Include cefpython3 data: {}".format(filename))
ret.append((os.path.join(CEFPYTHON3_DIR, filename), cefdatadir))
if is_darwin:
# Preserve all non-Mach-O framework files. In addition to Resources,
# CEF 147 has runtime-loaded files under Libraries (for example the
# Vulkan ICD JSON), so dependency analysis of the main binary alone is
# insufficient. Framework Mach-O files are collected as BINARIES.
framework_name = "Chromium Embedded Framework.framework"
framework_path = os.path.join(CEFPYTHON3_DIR, framework_name)
framework_binaries = {
os.path.normpath(source)
for source, _ in get_macos_framework_binaries()
}
for path, dirs, files in os.walk(framework_path):
dirs[:] = [name for name in dirs if name != "_CodeSignature"]
for filename in files:
absolute_file_path = os.path.join(path, filename)
if os.path.normpath(absolute_file_path) in framework_binaries:
continue
dest_path = os.path.relpath(path, CEFPYTHON3_DIR)
ret.append((absolute_file_path, dest_path))
logger.info("Include cefpython3 data: {}/{}".format(
dest_path, filename))
# Preserve the complete helper bundle structure. The Mach-O in each
# Contents/MacOS directory is collected separately as a BINARY below;
# Info.plist and any other bundle resources remain DATA files at their
# original relative paths. Source signatures are omitted because
# PyInstaller mutates/re-signs collected Mach-O files.
for app_name in MAC_HELPER_APP_NAMES:
app_path = os.path.join(CEFPYTHON3_DIR, app_name)
assert os.path.isdir(app_path), \
"{} not found in cefpython3".format(app_name)
executable = os.path.join(
app_path, "Contents", "MacOS",
os.path.splitext(app_name)[0])
for path, dirs, files in os.walk(app_path):
dirs[:] = [name for name in dirs if name != "_CodeSignature"]
for filename in files:
absolute_file_path = os.path.join(path, filename)
if absolute_file_path == executable:
continue
dest_path = os.path.relpath(path, CEFPYTHON3_DIR)
ret.append((absolute_file_path, dest_path))
logger.info("Include cefpython3 data: {}/{}".format(
dest_path, filename))
elif is_win or is_linux:
# The .pak files in cefpython3/locales/ directory
locales_dir = os.path.join(CEFPYTHON3_DIR, "locales")
assert os.path.exists(locales_dir), \
"locales/ dir not found in cefpython3"
for filename in os.listdir(locales_dir):
logger.info("Include cefpython3 data: {}/{}".format(
os.path.basename(locales_dir), filename))
ret.append((os.path.join(locales_dir, filename),
os.path.join(cefdatadir, "locales")))
# Optional .so/.dll files in cefpython3/swiftshader/ directory
swiftshader_dir = os.path.join(CEFPYTHON3_DIR, "swiftshader")
if os.path.isdir(swiftshader_dir):
for filename in os.listdir(swiftshader_dir):
logger.info("Include cefpython3 data: {}/{}".format(
os.path.basename(swiftshader_dir), filename))
ret.append((os.path.join(swiftshader_dir, filename),
os.path.join(cefdatadir, "swiftshader")))
return ret
# ----------------------------------------------------------------------------
# Main
# ----------------------------------------------------------------------------
# Checks
check_platforms()
check_pyinstaller_version()
check_cefpython3_version()
# Info
logger.info("CEF Python package directory: %s" % CEFPYTHON3_DIR)
# Hidden imports.
# PyInstaller has no way on detecting imports made by Cython
# modules, so all pure Python imports made in cefpython .pyx
# files need to be manually entered here.
# TODO: Write a tool script that would find such imports in
# .pyx files automatically.
hiddenimports = [
"codecs",
"copy",
"datetime",
"inspect",
"json",
"os",
"platform",
"random",
"re",
"sys",
"time",
"traceback",
"types",
"urllib",
"weakref",
]
if is_py2:
hiddenimports += [
"urlparse",
]
# Excluded modules
excludedimports = get_excluded_cefpython_modules()
# Include binaries requiring PyInstaller dependency analysis.
if is_darwin:
binaries = get_macos_framework_binaries() + get_macos_helper_binaries()
elif is_linux:
binaries = [(os.path.join(CEFPYTHON3_DIR, "subprocess"), ".")]
elif is_win:
binaries = [(os.path.join(CEFPYTHON3_DIR, "subprocess.exe"), ".")]
else:
binaries = []
# Include datas
datas = get_cefpython3_datas()
# Notify pyinstaller.spec code that this hook was executed
# and that it succeeded.
os.environ["PYINSTALLER_CEFPYTHON3_HOOK_SUCCEEDED"] = "1"
You can’t perform that action at this time.
