forked from cztomczak/cefpython
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake_installer.py
432 lines (372 loc) · 15.5 KB
/
make_installer.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
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
# Copyright (c) 2017 CEF Python, see the Authors file.
# All rights reserved. Licensed under BSD 3-clause license.
# Project website: https://github.com/cztomczak/cefpython
"""
Create setup.py package installer.
Usage:
make_installer.py VERSION [--wheel] [--python-tag xx] [--universal]
Options:
VERSION Version number eg. 50.0
--wheel Generate wheel package.
Additional args for the wheel package:
--python-tag xx (eg. cp27 - cpython 2.7)
--universal (any python 2 or 3)
"""
from common import *
import glob
import os
import re
import shutil
import subprocess
import sys
import sysconfig
# Command line args
VERSION = ""
WHEEL = False
WHEEL_ARGS = list()
# Globals
SETUP_DIR = ""
PKG_DIR = ""
# Config
IGNORE_EXT = [".log", ".pyc", ".pdb", ]
IGNORE_DIRS = ["__pycache__"]
def main():
command_line_args()
# Make sure pyinstaller build/dist directories do not exist,
# otherwise they would be packed along with examples and thus
# increase package size significantly.
assert not os.path.exists(os.path.join(EXAMPLES_DIR,
"pyinstaller", "build"))
assert not os.path.exists(os.path.join(EXAMPLES_DIR,
"pyinstaller", "dist"))
# Setup and package directories
global SETUP_DIR, PKG_DIR
setup_dir_name = get_setup_installer_basename(VERSION, OS_POSTFIX2)
SETUP_DIR = os.path.join(BUILD_DIR, setup_dir_name)
PKG_DIR = os.path.join(SETUP_DIR, "cefpython3")
# Print src and dest for file operations
print("[make_installer.py] Src: {src}".format(src=ROOT_DIR))
print("[make_installer.py] Dst: {dst}".format(dst=SETUP_DIR))
# Make directories
if os.path.exists(SETUP_DIR):
print("[make_installer.py] Delete: {dir}"
.format(dir=SETUP_DIR.replace(ROOT_DIR, "")))
shutil.rmtree(SETUP_DIR)
os.makedirs(SETUP_DIR)
os.makedirs(os.path.join(SETUP_DIR, "examples/"))
os.makedirs(PKG_DIR)
os.makedirs(os.path.join(PKG_DIR, "examples/"))
# Copy files from tools/installer/
copy_tools_installer_files(SETUP_DIR, PKG_DIR)
# Multiple copy operations using glob patterns
copy_operations = [
(ROOT_DIR, "License"), (PKG_DIR,),
(CEF_BINARIES_LIBRARIES, "*.txt"), (PKG_DIR,),
(CEF_BINARIES_LIBRARIES, "bin/*"), (PKG_DIR,),
(CEFPYTHON_BINARY, "*"), (PKG_DIR,),
(EXAMPLES_DIR, "*"), (PKG_DIR, "examples/"),
(EXAMPLES_DIR, "*"), (SETUP_DIR, "examples/"),
]
perform_copy_operations(copy_operations)
delete_cef_sample_apps(caller_script=__file__, bin_dir=PKG_DIR)
# Linux only operations
if LINUX:
os.makedirs(os.path.join(SETUP_DIR, "examples", "kivy-select-boxes"))
copy_operations_linux = [
(LINUX_DIR, "binaries_64bit/kivy_.py"),
(SETUP_DIR, "examples/"),
(LINUX_DIR, "binaries_64bit/kivy-select-boxes/*"),
(SETUP_DIR, "examples/kivy-select-boxes/")
]
perform_copy_operations(copy_operations_linux)
# Create empty debug.log files so that package uninstalls cleanly
# in case examples or CEF tests were launched. See Issue #149.
create_empty_log_file(os.path.join(PKG_DIR, "debug.log"))
create_empty_log_file(os.path.join(PKG_DIR, "examples/debug.log"))
copy_cpp_extension_dependencies_issue359(PKG_DIR)
print("[make_installer.py] Done. Installer package created: {setup_dir}"
.format(setup_dir=SETUP_DIR))
# Optional generation of wheel package
if WHEEL:
print("[make_installer.py] Create Wheel package")
if not len(WHEEL_ARGS):
print("[make_installer.py] ERROR: you must specify flags"
" eg. --python-tag cp27 or --universal")
sys.exit(1)
command = ("\"{python}\" setup.py bdist_wheel {wheel_args}"
.format(python=sys.executable,
wheel_args=" ".join(WHEEL_ARGS)))
print("[make_installer.py] Run command: '{0}' in setup directory"
.format(command))
subprocess.check_call(command, cwd=SETUP_DIR, shell=True)
dist_dir = os.path.join(SETUP_DIR, "dist")
files = glob.glob(os.path.join(dist_dir, "*.whl"))
assert len(files) == 1
print("[make_installer.py] Done. Wheel package created: {0}"
.format(files[0]))
def command_line_args():
global VERSION, WHEEL, WHEEL_ARGS
VERSION = get_version_from_command_line_args(__file__)
if not VERSION:
print(__doc__)
sys.exit(1)
for arg in sys.argv:
if arg == VERSION:
continue
if arg == "--wheel":
WHEEL = True
continue
if WHEEL:
WHEEL_ARGS.append(arg)
if WHEEL and not len(WHEEL_ARGS):
print("ERROR: wheel requires additional args eg. --universal")
sys.exit(1)
def copy_tools_installer_files(setup_dir, pkg_dir):
variables = dict()
variables["VERSION"] = VERSION
variables["SYSCONFIG_PLATFORM"] = sysconfig.get_platform()
shutil.copy(
os.path.join(INSTALLER_DIR, "cefpython3.README.txt"),
os.path.join(setup_dir, "README.txt"))
copy_template_file(
os.path.join(INSTALLER_DIR, "cefpython3.setup.py"),
os.path.join(setup_dir, "setup.py"),
variables)
copy_template_file(
os.path.join(INSTALLER_DIR, "cefpython3.__init__.py"),
os.path.join(pkg_dir, "__init__.py"),
variables)
def copy_template_file(src, dst, variables):
"""Copy file and replaces template variables in that file."""
print("[make_installer.py] Copy_t: {src} ==> {dst}"
.format(src=short_src_path(src), dst=short_dst_path(dst)))
with open(src, "rb") as fo:
contents = fo.read().decode("utf-8")
contents = replace_template_vars(contents, variables)
with open(dst, "wb") as fo:
fo.write(contents.encode("utf-8"))
return contents
def replace_template_vars(string, dictionary):
"""Replaces template variables like {{SOME}} in the string
using the dictionary values."""
orig_string = string
for key, value in dictionary.items():
string = string.replace("{{"+key+"}}", value)
if string == orig_string:
raise Exception("Nothing to format")
if re.search(r"{{[a-zA-Z0-9_]+}}", string):
raise Exception("Not all strings were formatted")
return string
def perform_copy_operations(operations):
assert len(operations) % 2 == 0
count_ops = int(len(operations) / 2)
for op_i in range(count_ops):
# Refer to values by index
pattern = operations[op_i*2]
dst_dir = operations[op_i*2+1]
# Convert tuples to lists
pattern = list(pattern)
dst_dir = list(dst_dir)
# Join paths
pattern = os.path.join(*pattern)
dst_dir = os.path.join(*dst_dir)
dst_dir = os.path.abspath(dst_dir)
# Normalize unix slashes on Windows
pattern = pattern.replace("/", os.path.sep)
# dst_dir must be a directory
if not os.path.isdir(dst_dir):
raise Exception("Not a directory: {dst_dir}"
.format(dst_dir=dst_dir))
# Is pattern a file or a directory
if os.path.isfile(pattern):
if is_ignored_path(pattern):
raise Exception("Copy operation pattern is in ignore list:"
" {pattern}".format(pattern=pattern))
print("[make_installer.py] Copy: {file} ==> {dir}"
.format(file=short_src_path(pattern),
dir=short_dst_path(dst_dir)))
# Destination file must not exist
assert not os.path.exists(os.path.join(dst_dir,
os.path.basename(pattern)))
shutil.copy(pattern, dst_dir)
else:
# pattern is a glob pattern
base_dir = os.path.dirname(pattern)
assert base_dir
assert base_dir == os.path.abspath(base_dir)
paths = glob.glob(pattern)
if not len(paths):
raise Exception("No paths found in: {pattern}"
.format(pattern=pattern))
for path in paths:
# "path" variable contains absolute path
assert path == os.path.abspath(path)
if os.path.isfile(path):
if is_ignored_path(path):
continue
print("[make_installer.py] Copy: {file} ==> {dir}"
.format(file=short_src_path(path),
dir=short_dst_path(dst_dir)))
# Destination file must not exist
assert not os.path.exists(
os.path.join(dst_dir, os.path.basename(path)))
shutil.copy(path, dst_dir)
elif os.path.isdir(path):
if is_ignored_path(path):
continue
relative_dir = path.replace(base_dir, "")
assert relative_dir[0] == os.path.sep
relative_dir = relative_dir[1:]
perform_copy_recursively(base_dir, relative_dir, dst_dir)
else:
raise Exception("Unknown path: {path}".format(path=path))
def perform_copy_recursively(base_dir, relative_dir, new_dir):
real_dir = os.path.join(base_dir, relative_dir)
assert os.path.exists(real_dir) and os.path.isdir(real_dir)
assert os.path.exists(new_dir) and os.path.isdir(new_dir)
# Create subdirectory
new_subdir = os.path.join(new_dir, relative_dir)
if not os.path.exists(new_subdir):
print("[make_installer.py] Create: {dir}"
.format(dir=short_dst_path(new_subdir)))
os.makedirs(new_subdir)
# List directory
paths = os.listdir(real_dir)
for path in paths:
# "path" variable contains relative path
real_path = os.path.join(real_dir, path)
path = os.path.join(relative_dir, path)
if os.path.isdir(real_path):
if is_ignored_path(real_path):
continue
perform_copy_recursively(base_dir, path, new_dir)
elif os.path.isfile(real_path):
if is_ignored_path(real_path):
continue
new_file = os.path.join(new_dir, path)
new_subdir = os.path.dirname(new_file)
if os.path.exists(new_file):
raise Exception("Path aready exists: {new_file}"
.format(new_file=short_dst_path(new_file)))
print("[make_installer.py] Copy: {file} ==> {dir}"
.format(file=short_src_path(real_path),
dir=short_dst_path(new_subdir)))
shutil.copy(real_path, new_subdir)
else:
raise Exception("Unknown path: {path}".format(path=real_path))
def is_ignored_path(path):
basename = os.path.basename(path)
if basename in IGNORE_DIRS:
print("[make_installer.py] Ignore: {dir}"
.format(dir=short_src_path(path)))
return True
for ext in IGNORE_EXT:
if path.endswith(ext):
print("[make_installer.py] Ignore: {file}"
.format(file=short_src_path(path)))
return True
return False
def delete_files_by_pattern(pattern):
assert len(pattern) > 2
# Normalize unix slashes on Windows
pattern = pattern.replace("/", os.path.sep)
print("[make_installer.py] Delete: {pattern}"
.format(pattern=short_dst_path(pattern)))
files = glob.glob(pattern)
for f in files:
os.remove(f)
def create_empty_log_file(log_file):
# Normalize unix slashes on Windows
log_file = log_file.replace("/", os.path.sep)
print("[make_installer.py] Create: {file}"
.format(file=short_dst_path(log_file)))
with open(log_file, "wb") as fo:
fo.write("".encode("utf-8"))
# On Linux and Mac chmod so that for cases when package is
# installed using sudo. When wheel package is created it
# will remember file permissions set.
if LINUX or MAC:
command = "chmod 666 {file}".format(file=log_file)
print("[make_installer.py] {command}"
.format(command=command.replace(SETUP_DIR, "")))
subprocess.check_call(command, shell=True)
def copy_cpp_extension_dependencies_issue359(pkg_dir):
"""CEF Python module is written in Cython and is a Python C++
extension and depends on msvcpXX.dll. For Python 3.5 / 3.6 / 3.7
msvcp140.dll is required. See Issue #359. For Python 2.7
msvcp90.dll is required. Etc. These dependencies are not included
with Python binaries from Python.org."""
if not WINDOWS:
return
windows_dir = os.environ["SYSTEMROOT"]
if SYSTEM64:
system32 = os.path.join(windows_dir, "SysWOW64")
system64 = os.path.join(windows_dir, "System32")
else:
system32 = os.path.join(windows_dir, "")
system64 = None
if ARCH64:
system = system64
else:
system = system32
root_search_paths = []
# Need to check for .pyd files for all Python version, because
# the builder/installer work in a way that previous cefpython
# module builds for other Python versions are also included
# in the package. Thus if included, msvcpxx.dll dependency is
# required as well.
# Python 3.5 / 3.6 / 3.7
if os.path.exists(os.path.join(pkg_dir, "cefpython_py35.pyd")) \
or os.path.exists(os.path.join(pkg_dir, "cefpython_py36.pyd")) \
or os.path.exists(os.path.join(pkg_dir, "cefpython_py37.pyd")):
search_paths = [
# This is where Microsoft Visual C++ 2015 Update 3 installs
# (14.00.24212).
os.path.join(system, "msvcp140.dll"),
]
root_search_paths.append(search_paths)
# Python 3.4
if os.path.exists(os.path.join(pkg_dir, "cefpython_py34.pyd")):
search_paths = [
# 10.00.40219.325 installs here on my system.
os.path.join(system, "msvcp100.dll"),
]
root_search_paths.append(search_paths)
# Python 2.7
if os.path.exists(os.path.join(pkg_dir, "cefpython_py27.pyd")):
if ARCH32:
search_paths = [
# This runtime version is shipped with Python 2.7.14
r"c:\Windows\winsxs\x86_microsoft.vc90.crt_1fc8b3b9a1e18e3b"
r"_9.0.30729.1_none_e163563597edeada\msvcp90.dll",
]
else:
search_paths = [
# This runtime version is shipped with Python 2.7.14
r"c:\Windows\winsxs\amd64_microsoft.vc90.crt_1fc8b3b9a1e18e3b"
r"_9.0.30729.1_none_99b61f5e8371c1d4\msvcp90.dll",
]
root_search_paths.append(search_paths)
assert len(root_search_paths)
for search_paths in root_search_paths:
found = False
for path in search_paths:
if os.path.exists(path):
shutil.copy(path, pkg_dir)
found = True
if not found:
raise Exception("C++ extension dll dependency not found."
" Search paths: {0}"
.format(", ".join(search_paths)))
def short_src_path(path):
# Very long: \build\cef55_3.2883.1553.g80bd606_win32\
find = os.path.basename(CEF_BINARIES_LIBRARIES)
if len(find) > 12:
path = path.replace(find, find[:12] + "*")
path = path.replace(ROOT_DIR, "")
return path
def short_dst_path(path):
path = path.replace(SETUP_DIR, "")
return path
if __name__ == "__main__":
main()