| 1 |
""" |
| 2 |
Generates a layout of Python for Windows from a build. |
| 3 |
|
| 4 |
See python make_layout.py --help for usage. |
| 5 |
""" |
| 6 |
|
| 7 |
__author__ = "Steve Dower <[email protected]>" |
| 8 |
__version__ = "3.8" |
| 9 |
|
| 10 |
import argparse |
| 11 |
import json |
| 12 |
import os |
| 13 |
import shutil |
| 14 |
import sys |
| 15 |
import tempfile |
| 16 |
import zipfile |
| 17 |
|
| 18 |
from pathlib import Path |
| 19 |
|
| 20 |
if __name__ == "__main__": |
| 21 |
# Started directly, so enable relative imports |
| 22 |
__path__ = [str(Path(__file__).resolve().parent)] |
| 23 |
|
| 24 |
from .support.appxmanifest import * |
| 25 |
from .support.catalog import * |
| 26 |
from .support.constants import * |
| 27 |
from .support.filesets import * |
| 28 |
from .support.logging import * |
| 29 |
from .support.options import * |
| 30 |
from .support.pip import * |
| 31 |
from .support.props import * |
| 32 |
from .support.pymanager import * |
| 33 |
from .support.nuspec import * |
| 34 |
|
| 35 |
TEST_PYDS_ONLY = FileStemSet("xxlimited", "xxlimited_35", "_ctypes_test", "_test*") |
| 36 |
TEST_DLLS_ONLY = set() |
| 37 |
TEST_DIRS_ONLY = FileNameSet("test", "tests") |
| 38 |
|
| 39 |
IDLE_DIRS_ONLY = FileNameSet("idlelib") |
| 40 |
|
| 41 |
TCLTK_PYDS_ONLY = FileStemSet("_tkinter") |
| 42 |
TCLTK_DLLS_ONLY = FileStemSet("tcl*", "tk*", "zlib1") |
| 43 |
TCLTK_DIRS_ONLY = FileNameSet("tkinter", "turtledemo") |
| 44 |
TCLTK_FILES_ONLY = FileNameSet("turtle.py") |
| 45 |
|
| 46 |
VENV_DIRS_ONLY = FileNameSet("venv", "ensurepip") |
| 47 |
|
| 48 |
EXCLUDE_FROM_DLLS = FileStemSet("python*", "pyshellext", "vcruntime*") |
| 49 |
EXCLUDE_FROM_LIB = FileNameSet("*.pyc", "__pycache__", "*.pickle") |
| 50 |
EXCLUDE_FROM_PACKAGED_LIB = FileNameSet("readme.txt") |
| 51 |
EXCLUDE_FROM_COMPILE = FileNameSet("badsyntax_*", "bad_*") |
| 52 |
EXCLUDE_FROM_CATALOG = FileSuffixSet(".exe", ".pyd", ".dll") |
| 53 |
|
| 54 |
REQUIRED_DLLS = FileStemSet("libcrypto*", "libssl*", "libffi*") |
| 55 |
|
| 56 |
PY_FILES = FileSuffixSet(".py") |
| 57 |
PYC_FILES = FileSuffixSet(".pyc") |
| 58 |
CAT_FILES = FileSuffixSet(".cat") |
| 59 |
CDF_FILES = FileSuffixSet(".cdf") |
| 60 |
|
| 61 |
DATA_DIRS = FileNameSet("data") |
| 62 |
|
| 63 |
TOOLS_DIRS = FileNameSet("scripts", "i18n", "parser") |
| 64 |
TOOLS_FILES = FileSuffixSet(".py", ".pyw", ".txt") |
| 65 |
|
| 66 |
|
| 67 |
def copy_if_modified(src, dest): |
| 68 |
try: |
| 69 |
dest_stat = os.stat(dest) |
| 70 |
except FileNotFoundError: |
| 71 |
do_copy = True |
| 72 |
else: |
| 73 |
src_stat = os.stat(src) |
| 74 |
do_copy = ( |
| 75 |
src_stat.st_mtime != dest_stat.st_mtime |
| 76 |
or src_stat.st_size != dest_stat.st_size |
| 77 |
) |
| 78 |
|
| 79 |
if do_copy: |
| 80 |
try: |
| 81 |
shutil.copy2(src, dest) |
| 82 |
except FileNotFoundError: |
| 83 |
raise FileNotFoundError(src) from None |
| 84 |
|
| 85 |
|
| 86 |
def get_lib_layout(ns): |
| 87 |
def _c(f): |
| 88 |
if f in EXCLUDE_FROM_LIB: |
| 89 |
return False |
| 90 |
if f.is_dir(): |
| 91 |
if f in TEST_DIRS_ONLY: |
| 92 |
return ns.include_tests |
| 93 |
if f in TCLTK_DIRS_ONLY: |
| 94 |
return ns.include_tcltk |
| 95 |
if f in IDLE_DIRS_ONLY: |
| 96 |
return ns.include_idle |
| 97 |
if f in VENV_DIRS_ONLY: |
| 98 |
return ns.include_venv |
| 99 |
else: |
| 100 |
if f in TCLTK_FILES_ONLY: |
| 101 |
return ns.include_tcltk |
| 102 |
return True |
| 103 |
|
| 104 |
for dest, src in rglob(ns.source / "Lib", "**/*", _c): |
| 105 |
yield dest, src |
| 106 |
|
| 107 |
|
| 108 |
def get_tcltk_lib(ns): |
| 109 |
if not ns.include_tcltk: |
| 110 |
return |
| 111 |
|
| 112 |
tcl_lib = os.getenv("TCL_LIBRARY") |
| 113 |
if not tcl_lib or not os.path.isdir(tcl_lib): |
| 114 |
try: |
| 115 |
with open(ns.build / "TCL_LIBRARY.env", "r", encoding="utf-8-sig") as f: |
| 116 |
tcl_lib = f.read().strip() |
| 117 |
except FileNotFoundError: |
| 118 |
pass |
| 119 |
if not tcl_lib or not os.path.isdir(tcl_lib): |
| 120 |
log_warning("Failed to find TCL_LIBRARY") |
| 121 |
return |
| 122 |
|
| 123 |
for dest, src in rglob(Path(tcl_lib).parent, "**/*"): |
| 124 |
yield "tcl/{}".format(dest), src |
| 125 |
|
| 126 |
|
| 127 |
def get_layout(ns): |
| 128 |
def in_build(f, dest="", new_name=None, no_lib=False): |
| 129 |
n, _, x = f.rpartition(".") |
| 130 |
n = new_name or n |
| 131 |
src = ns.build / f |
| 132 |
if ns.debug and src not in REQUIRED_DLLS: |
| 133 |
if not "_d." in src.name: |
| 134 |
src = src.parent / (src.stem + "_d" + src.suffix) |
| 135 |
if "_d." not in f: |
| 136 |
n += "_d" |
| 137 |
f = n + "." + x |
| 138 |
yield dest + n + "." + x, src |
| 139 |
if ns.include_symbols: |
| 140 |
pdb = src.with_suffix(".pdb") |
| 141 |
if pdb.is_file(): |
| 142 |
yield dest + n + ".pdb", pdb |
| 143 |
if ns.include_dev and not no_lib: |
| 144 |
lib = src.with_suffix(".lib") |
| 145 |
if lib.is_file(): |
| 146 |
yield "libs/" + n + ".lib", lib |
| 147 |
|
| 148 |
source = "python.exe" |
| 149 |
sourcew = "pythonw.exe" |
| 150 |
alias = [ |
| 151 |
"python", |
| 152 |
"python{}".format(VER_MAJOR) if ns.include_alias3 else "", |
| 153 |
"python{}".format(VER_DOT) if ns.include_alias3x else "", |
| 154 |
] |
| 155 |
aliasw = [ |
| 156 |
"pythonw", |
| 157 |
"pythonw{}".format(VER_MAJOR) if ns.include_alias3 else "", |
| 158 |
"pythonw{}".format(VER_DOT) if ns.include_alias3x else "", |
| 159 |
] |
| 160 |
if ns.include_appxmanifest: |
| 161 |
source = "python_uwp.exe" |
| 162 |
sourcew = "pythonw_uwp.exe" |
| 163 |
elif ns.include_freethreaded: |
| 164 |
source = "python{}t.exe".format(VER_DOT) |
| 165 |
sourcew = "pythonw{}t.exe".format(VER_DOT) |
| 166 |
if not ns.include_alias: |
| 167 |
alias = [] |
| 168 |
aliasw = [] |
| 169 |
alias.extend([ |
| 170 |
"python{}t".format(VER_DOT), |
| 171 |
"python{}t".format(VER_MAJOR) if ns.include_alias3 else None, |
| 172 |
]) |
| 173 |
aliasw.extend([ |
| 174 |
"pythonw{}t".format(VER_DOT), |
| 175 |
"pythonw{}t".format(VER_MAJOR) if ns.include_alias3 else None, |
| 176 |
]) |
| 177 |
|
| 178 |
for a in filter(None, alias): |
| 179 |
yield from in_build(source, new_name=a) |
| 180 |
for a in filter(None, aliasw): |
| 181 |
yield from in_build(sourcew, new_name=a) |
| 182 |
|
| 183 |
if ns.include_freethreaded: |
| 184 |
yield from in_build(FREETHREADED_PYTHON_DLL_NAME) |
| 185 |
else: |
| 186 |
yield from in_build(PYTHON_DLL_NAME) |
| 187 |
|
| 188 |
if ns.include_launchers and ns.include_appxmanifest: |
| 189 |
if ns.include_pip: |
| 190 |
yield from in_build("python_uwp.exe", new_name="pip{}".format(VER_DOT)) |
| 191 |
if ns.include_idle: |
| 192 |
yield from in_build("pythonw_uwp.exe", new_name="idle{}".format(VER_DOT)) |
| 193 |
|
| 194 |
if ns.include_stable: |
| 195 |
if ns.include_freethreaded: |
| 196 |
yield from in_build(FREETHREADED_PYTHON_STABLE_DLL_NAME) |
| 197 |
else: |
| 198 |
yield from in_build(PYTHON_STABLE_DLL_NAME) |
| 199 |
|
| 200 |
found_any = False |
| 201 |
for dest, src in rglob(ns.build, "vcruntime*.dll"): |
| 202 |
found_any = True |
| 203 |
yield dest, src |
| 204 |
if not found_any: |
| 205 |
log_error("Failed to locate vcruntime DLL in the build.") |
| 206 |
|
| 207 |
yield "LICENSE.txt", ns.build / "LICENSE.txt" |
| 208 |
|
| 209 |
dest = "" if ns.flat_dlls else "DLLs/" |
| 210 |
|
| 211 |
for _, src in rglob(ns.build, "*.pyd"): |
| 212 |
if ns.include_freethreaded: |
| 213 |
if not src.match("*.cp*t-win*.pyd"): |
| 214 |
continue |
| 215 |
if bool(src.match("*_d.cp*.pyd")) != bool(ns.debug): |
| 216 |
continue |
| 217 |
else: |
| 218 |
if src.match("*.cp*t-win*.pyd"): |
| 219 |
continue |
| 220 |
if bool(src.match("*_d.pyd")) != bool(ns.debug): |
| 221 |
continue |
| 222 |
if src in TEST_PYDS_ONLY and not ns.include_tests: |
| 223 |
continue |
| 224 |
if src in TCLTK_PYDS_ONLY and not ns.include_tcltk: |
| 225 |
continue |
| 226 |
yield from in_build(src.name, dest=dest, no_lib=True) |
| 227 |
|
| 228 |
for _, src in rglob(ns.build, "*.dll"): |
| 229 |
if src.stem.endswith("_d") != bool(ns.debug) and src not in REQUIRED_DLLS: |
| 230 |
continue |
| 231 |
if src in EXCLUDE_FROM_DLLS: |
| 232 |
continue |
| 233 |
if src in TEST_DLLS_ONLY and not ns.include_tests: |
| 234 |
continue |
| 235 |
if src in TCLTK_DLLS_ONLY and not ns.include_tcltk: |
| 236 |
continue |
| 237 |
yield from in_build(src.name, dest=dest, no_lib=True) |
| 238 |
|
| 239 |
if ns.zip_lib: |
| 240 |
zip_name = PYTHON_ZIP_NAME |
| 241 |
yield zip_name, ns.temp / zip_name |
| 242 |
else: |
| 243 |
for dest, src in get_lib_layout(ns): |
| 244 |
yield "Lib/{}".format(dest), src |
| 245 |
|
| 246 |
if ns.include_venv: |
| 247 |
if ns.include_freethreaded: |
| 248 |
yield from in_build("venvlaunchert.exe", "Lib/venv/scripts/nt/") |
| 249 |
yield from in_build("venvwlaunchert.exe", "Lib/venv/scripts/nt/") |
| 250 |
elif (VER_MAJOR, VER_MINOR) > (3, 12): |
| 251 |
yield from in_build("venvlauncher.exe", "Lib/venv/scripts/nt/") |
| 252 |
yield from in_build("venvwlauncher.exe", "Lib/venv/scripts/nt/") |
| 253 |
else: |
| 254 |
# Older versions of venv expected the scripts to be named 'python' |
| 255 |
# and they were renamed at this stage. We need to replicate that |
| 256 |
# when packaging older versions. |
| 257 |
yield from in_build("venvlauncher.exe", "Lib/venv/scripts/nt/", "python") |
| 258 |
yield from in_build("venvwlauncher.exe", "Lib/venv/scripts/nt/", "pythonw") |
| 259 |
|
| 260 |
if ns.include_tools: |
| 261 |
|
| 262 |
def _c(d): |
| 263 |
if d.is_dir(): |
| 264 |
return d in TOOLS_DIRS |
| 265 |
return d in TOOLS_FILES |
| 266 |
|
| 267 |
for dest, src in rglob(ns.source / "Tools", "**/*", _c): |
| 268 |
yield "Tools/{}".format(dest), src |
| 269 |
|
| 270 |
if ns.include_underpth: |
| 271 |
yield PYTHON_PTH_NAME, ns.temp / PYTHON_PTH_NAME |
| 272 |
|
| 273 |
if ns.include_dev: |
| 274 |
for dest, src in rglob(ns.source / "Include", "**/*.h"): |
| 275 |
yield "include/{}".format(dest), src |
| 276 |
# Support for layout of new and old releases. |
| 277 |
pc = ns.source / "PC" |
| 278 |
if (pc / "pyconfig.h.in").is_file(): |
| 279 |
yield "include/pyconfig.h", ns.build / "pyconfig.h" |
| 280 |
else: |
| 281 |
yield "include/pyconfig.h", pc / "pyconfig.h" |
| 282 |
|
| 283 |
for dest, src in get_tcltk_lib(ns): |
| 284 |
yield dest, src |
| 285 |
|
| 286 |
if ns.include_pip: |
| 287 |
for dest, src in get_pip_layout(ns): |
| 288 |
if not isinstance(src, tuple) and ( |
| 289 |
src in EXCLUDE_FROM_LIB or src in EXCLUDE_FROM_PACKAGED_LIB |
| 290 |
): |
| 291 |
continue |
| 292 |
yield dest, src |
| 293 |
|
| 294 |
if ns.include_chm: |
| 295 |
for dest, src in rglob(ns.doc_build / "htmlhelp", PYTHON_CHM_NAME): |
| 296 |
yield "Doc/{}".format(dest), src |
| 297 |
|
| 298 |
if ns.include_html_doc: |
| 299 |
for dest, src in rglob(ns.doc_build / "html", "**/*"): |
| 300 |
yield "Doc/html/{}".format(dest), src |
| 301 |
|
| 302 |
if ns.include_props: |
| 303 |
for dest, src in get_props_layout(ns): |
| 304 |
yield dest, src |
| 305 |
|
| 306 |
if ns.include_nuspec: |
| 307 |
for dest, src in get_nuspec_layout(ns): |
| 308 |
yield dest, src |
| 309 |
|
| 310 |
for dest, src in get_appx_layout(ns): |
| 311 |
yield dest, src |
| 312 |
|
| 313 |
if ns.include_cat: |
| 314 |
if ns.flat_dlls: |
| 315 |
yield ns.include_cat.name, ns.include_cat |
| 316 |
else: |
| 317 |
yield "DLLs/{}".format(ns.include_cat.name), ns.include_cat |
| 318 |
|
| 319 |
if ns.include_install_json or ns.include_install_embed_json or ns.include_install_test_json: |
| 320 |
yield "__install__.json", ns.temp / "__install__.json" |
| 321 |
|
| 322 |
|
| 323 |
def _compile_one_py(src, dest, name, optimize, checked=True): |
| 324 |
import py_compile |
| 325 |
|
| 326 |
if dest is not None: |
| 327 |
dest = str(dest) |
| 328 |
|
| 329 |
mode = ( |
| 330 |
py_compile.PycInvalidationMode.CHECKED_HASH |
| 331 |
if checked |
| 332 |
else py_compile.PycInvalidationMode.UNCHECKED_HASH |
| 333 |
) |
| 334 |
|
| 335 |
try: |
| 336 |
return Path( |
| 337 |
py_compile.compile( |
| 338 |
str(src), |
| 339 |
dest, |
| 340 |
str(name), |
| 341 |
doraise=True, |
| 342 |
optimize=optimize, |
| 343 |
invalidation_mode=mode, |
| 344 |
) |
| 345 |
) |
| 346 |
except py_compile.PyCompileError: |
| 347 |
log_warning("Failed to compile {}", src) |
| 348 |
return None |
| 349 |
|
| 350 |
|
| 351 |
# name argument added to address bpo-37641 |
| 352 |
def _py_temp_compile(src, name, ns, dest_dir=None, checked=True): |
| 353 |
if not ns.precompile or src not in PY_FILES or src.parent in DATA_DIRS: |
| 354 |
return None |
| 355 |
dest = (dest_dir or ns.temp) / (src.stem + ".pyc") |
| 356 |
return _compile_one_py(src, dest, name, optimize=2, checked=checked) |
| 357 |
|
| 358 |
|
| 359 |
def _write_to_zip(zf, dest, src, ns, checked=True): |
| 360 |
pyc = _py_temp_compile(src, dest, ns, checked=checked) |
| 361 |
if pyc: |
| 362 |
try: |
| 363 |
zf.write(str(pyc), dest.with_suffix(".pyc")) |
| 364 |
finally: |
| 365 |
try: |
| 366 |
pyc.unlink() |
| 367 |
except: |
| 368 |
log_exception("Failed to delete {}", pyc) |
| 369 |
return |
| 370 |
|
| 371 |
zf.write(str(src), str(dest)) |
| 372 |
|
| 373 |
|
| 374 |
def generate_source_files(ns): |
| 375 |
if ns.zip_lib: |
| 376 |
zip_name = PYTHON_ZIP_NAME |
| 377 |
zip_path = ns.temp / zip_name |
| 378 |
if zip_path.is_file(): |
| 379 |
zip_path.unlink() |
| 380 |
elif zip_path.is_dir(): |
| 381 |
log_error( |
| 382 |
"Cannot create zip file because a directory exists by the same name" |
| 383 |
) |
| 384 |
return |
| 385 |
log_info("Generating {} in {}", zip_name, ns.temp) |
| 386 |
ns.temp.mkdir(parents=True, exist_ok=True) |
| 387 |
with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED) as zf: |
| 388 |
for dest, src in get_lib_layout(ns): |
| 389 |
_write_to_zip(zf, dest, src, ns, checked=False) |
| 390 |
|
| 391 |
if ns.include_underpth: |
| 392 |
log_info("Generating {} in {}", PYTHON_PTH_NAME, ns.temp) |
| 393 |
ns.temp.mkdir(parents=True, exist_ok=True) |
| 394 |
with open(ns.temp / PYTHON_PTH_NAME, "w", encoding="utf-8") as f: |
| 395 |
if ns.zip_lib: |
| 396 |
print(PYTHON_ZIP_NAME, file=f) |
| 397 |
if ns.include_pip: |
| 398 |
print("packages", file=f) |
| 399 |
else: |
| 400 |
print("Lib", file=f) |
| 401 |
print("Lib/site-packages", file=f) |
| 402 |
if not ns.flat_dlls: |
| 403 |
print("DLLs", file=f) |
| 404 |
print(".", file=f) |
| 405 |
print(file=f) |
| 406 |
print("# Uncomment to run site.main() automatically", file=f) |
| 407 |
print("#import site", file=f) |
| 408 |
|
| 409 |
if ns.include_pip: |
| 410 |
log_info("Extracting pip") |
| 411 |
extract_pip_files(ns) |
| 412 |
|
| 413 |
if ns.include_install_json: |
| 414 |
log_info("Generating __install__.json in {}", ns.temp) |
| 415 |
ns.temp.mkdir(parents=True, exist_ok=True) |
| 416 |
with open(ns.temp / "__install__.json", "w", encoding="utf-8") as f: |
| 417 |
json.dump(calculate_install_json(ns), f, indent=2) |
| 418 |
elif ns.include_install_embed_json: |
| 419 |
log_info("Generating embeddable __install__.json in {}", ns.temp) |
| 420 |
ns.temp.mkdir(parents=True, exist_ok=True) |
| 421 |
with open(ns.temp / "__install__.json", "w", encoding="utf-8") as f: |
| 422 |
json.dump(calculate_install_json(ns, for_embed=True), f, indent=2) |
| 423 |
elif ns.include_install_test_json: |
| 424 |
log_info("Generating test __install__.json in {}", ns.temp) |
| 425 |
ns.temp.mkdir(parents=True, exist_ok=True) |
| 426 |
with open(ns.temp / "__install__.json", "w", encoding="utf-8") as f: |
| 427 |
json.dump(calculate_install_json(ns, for_test=True), f, indent=2) |
| 428 |
|
| 429 |
|
| 430 |
def _create_zip_file(ns): |
| 431 |
if not ns.zip: |
| 432 |
return None |
| 433 |
|
| 434 |
if ns.zip.is_file(): |
| 435 |
try: |
| 436 |
ns.zip.unlink() |
| 437 |
except OSError: |
| 438 |
log_exception("Unable to remove {}", ns.zip) |
| 439 |
sys.exit(8) |
| 440 |
elif ns.zip.is_dir(): |
| 441 |
log_error("Cannot create ZIP file because {} is a directory", ns.zip) |
| 442 |
sys.exit(8) |
| 443 |
|
| 444 |
ns.zip.parent.mkdir(parents=True, exist_ok=True) |
| 445 |
return zipfile.ZipFile(ns.zip, "w", zipfile.ZIP_DEFLATED) |
| 446 |
|
| 447 |
|
| 448 |
def copy_files(files, ns): |
| 449 |
if ns.copy: |
| 450 |
ns.copy.mkdir(parents=True, exist_ok=True) |
| 451 |
|
| 452 |
try: |
| 453 |
total = len(files) |
| 454 |
except TypeError: |
| 455 |
total = None |
| 456 |
count = 0 |
| 457 |
|
| 458 |
zip_file = _create_zip_file(ns) |
| 459 |
try: |
| 460 |
need_compile = [] |
| 461 |
in_catalog = [] |
| 462 |
|
| 463 |
for dest, src in files: |
| 464 |
count += 1 |
| 465 |
if count % 10 == 0: |
| 466 |
if total: |
| 467 |
log_info("Processed {:>4} of {} files", count, total) |
| 468 |
else: |
| 469 |
log_info("Processed {} files", count) |
| 470 |
log_debug("Processing {!s}", src) |
| 471 |
|
| 472 |
if isinstance(src, tuple): |
| 473 |
src, content = src |
| 474 |
if ns.copy: |
| 475 |
log_debug("Copy {} -> {}", src, ns.copy / dest) |
| 476 |
(ns.copy / dest).parent.mkdir(parents=True, exist_ok=True) |
| 477 |
with open(ns.copy / dest, "wb") as f: |
| 478 |
f.write(content) |
| 479 |
if ns.zip: |
| 480 |
log_debug("Zip {} into {}", src, ns.zip) |
| 481 |
zip_file.writestr(str(dest), content) |
| 482 |
continue |
| 483 |
|
| 484 |
if ( |
| 485 |
ns.precompile |
| 486 |
and src in PY_FILES |
| 487 |
and src not in EXCLUDE_FROM_COMPILE |
| 488 |
and src.parent not in DATA_DIRS |
| 489 |
and os.path.normcase(str(dest)).startswith(os.path.normcase("Lib")) |
| 490 |
): |
| 491 |
if ns.copy: |
| 492 |
need_compile.append((dest, ns.copy / dest)) |
| 493 |
else: |
| 494 |
(ns.temp / "Lib" / dest).parent.mkdir(parents=True, exist_ok=True) |
| 495 |
copy_if_modified(src, ns.temp / "Lib" / dest) |
| 496 |
need_compile.append((dest, ns.temp / "Lib" / dest)) |
| 497 |
|
| 498 |
if src not in EXCLUDE_FROM_CATALOG: |
| 499 |
in_catalog.append((src.name, src)) |
| 500 |
|
| 501 |
if ns.copy: |
| 502 |
log_debug("Copy {} -> {}", src, ns.copy / dest) |
| 503 |
(ns.copy / dest).parent.mkdir(parents=True, exist_ok=True) |
| 504 |
try: |
| 505 |
copy_if_modified(src, ns.copy / dest) |
| 506 |
except shutil.SameFileError: |
| 507 |
pass |
| 508 |
|
| 509 |
if ns.zip: |
| 510 |
log_debug("Zip {} into {}", src, ns.zip) |
| 511 |
zip_file.write(src, str(dest)) |
| 512 |
|
| 513 |
if need_compile: |
| 514 |
for dest, src in need_compile: |
| 515 |
compiled = [ |
| 516 |
_compile_one_py(src, None, dest, optimize=0), |
| 517 |
_compile_one_py(src, None, dest, optimize=1), |
| 518 |
_compile_one_py(src, None, dest, optimize=2), |
| 519 |
] |
| 520 |
for c in compiled: |
| 521 |
if not c: |
| 522 |
continue |
| 523 |
cdest = Path(dest).parent / Path(c).relative_to(src.parent) |
| 524 |
if ns.zip: |
| 525 |
log_debug("Zip {} into {}", c, ns.zip) |
| 526 |
zip_file.write(c, str(cdest)) |
| 527 |
in_catalog.append((cdest.name, cdest)) |
| 528 |
|
| 529 |
if ns.catalog: |
| 530 |
# Just write out the CDF now. Compilation and signing is |
| 531 |
# an extra step |
| 532 |
log_info("Generating {}", ns.catalog) |
| 533 |
ns.catalog.parent.mkdir(parents=True, exist_ok=True) |
| 534 |
write_catalog(ns.catalog, in_catalog) |
| 535 |
|
| 536 |
finally: |
| 537 |
if zip_file: |
| 538 |
zip_file.close() |
| 539 |
|
| 540 |
|
| 541 |
def main(): |
| 542 |
parser = argparse.ArgumentParser() |
| 543 |
parser.add_argument("-v", help="Increase verbosity", action="count") |
| 544 |
parser.add_argument( |
| 545 |
"-s", |
| 546 |
"--source", |
| 547 |
metavar="dir", |
| 548 |
help="The directory containing the repository root", |
| 549 |
type=Path, |
| 550 |
default=None, |
| 551 |
) |
| 552 |
parser.add_argument( |
| 553 |
"-b", "--build", metavar="dir", help="Specify the build directory", type=Path |
| 554 |
) |
| 555 |
parser.add_argument( |
| 556 |
"--arch", |
| 557 |
metavar="architecture", |
| 558 |
help="Specify the target architecture", |
| 559 |
type=str, |
| 560 |
default=None, |
| 561 |
) |
| 562 |
parser.add_argument( |
| 563 |
"--doc-build", |
| 564 |
metavar="dir", |
| 565 |
help="Specify the docs build directory", |
| 566 |
type=Path, |
| 567 |
default=None, |
| 568 |
) |
| 569 |
parser.add_argument( |
| 570 |
"--copy", |
| 571 |
metavar="directory", |
| 572 |
help="The name of the directory to copy an extracted layout to", |
| 573 |
type=Path, |
| 574 |
default=None, |
| 575 |
) |
| 576 |
parser.add_argument( |
| 577 |
"--zip", |
| 578 |
metavar="file", |
| 579 |
help="The ZIP file to write all files to", |
| 580 |
type=Path, |
| 581 |
default=None, |
| 582 |
) |
| 583 |
parser.add_argument( |
| 584 |
"--catalog", |
| 585 |
metavar="file", |
| 586 |
help="The CDF file to write catalog entries to", |
| 587 |
type=Path, |
| 588 |
default=None, |
| 589 |
) |
| 590 |
parser.add_argument( |
| 591 |
"--log", |
| 592 |
metavar="file", |
| 593 |
help="Write all operations to the specified file", |
| 594 |
type=Path, |
| 595 |
default=None, |
| 596 |
) |
| 597 |
parser.add_argument( |
| 598 |
"-t", |
| 599 |
"--temp", |
| 600 |
metavar="file", |
| 601 |
help="A temporary working directory", |
| 602 |
type=Path, |
| 603 |
default=None, |
| 604 |
) |
| 605 |
parser.add_argument( |
| 606 |
"-d", "--debug", help="Include debug build", action="store_true" |
| 607 |
) |
| 608 |
parser.add_argument( |
| 609 |
"-p", |
| 610 |
"--precompile", |
| 611 |
help="Include .pyc files instead of .py", |
| 612 |
action="store_true", |
| 613 |
) |
| 614 |
parser.add_argument( |
| 615 |
"-z", "--zip-lib", help="Include library in a ZIP file", action="store_true" |
| 616 |
) |
| 617 |
parser.add_argument( |
| 618 |
"--flat-dlls", help="Does not create a DLLs directory", action="store_true" |
| 619 |
) |
| 620 |
parser.add_argument( |
| 621 |
"-a", |
| 622 |
"--include-all", |
| 623 |
help="Include all optional components", |
| 624 |
action="store_true", |
| 625 |
) |
| 626 |
parser.add_argument( |
| 627 |
"--include-cat", |
| 628 |
metavar="file", |
| 629 |
help="Specify the catalog file to include", |
| 630 |
type=Path, |
| 631 |
default=None, |
| 632 |
) |
| 633 |
for opt, help in get_argparse_options(): |
| 634 |
parser.add_argument(opt, help=help, action="store_true") |
| 635 |
|
| 636 |
ns = parser.parse_args() |
| 637 |
update_presets(ns) |
| 638 |
|
| 639 |
ns.source = ns.source or (Path(__file__).resolve().parent.parent.parent) |
| 640 |
ns.build = ns.build or Path(sys.executable).parent |
| 641 |
ns.doc_build = ns.doc_build or (ns.source / "Doc" / "build") |
| 642 |
if ns.copy and not ns.copy.is_absolute(): |
| 643 |
ns.copy = (Path.cwd() / ns.copy).resolve() |
| 644 |
if not ns.temp: |
| 645 |
# Put temp on a Dev Drive for speed if we're copying to one. |
| 646 |
# If not, the regular temp dir will have to do. |
| 647 |
if ns.copy and getattr(os.path, "isdevdrive", lambda d: False)(ns.copy): |
| 648 |
ns.temp = ns.copy.with_name(ns.copy.name + "_temp") |
| 649 |
else: |
| 650 |
ns.temp = Path(tempfile.mkdtemp()) |
| 651 |
if not ns.source.is_absolute(): |
| 652 |
ns.source = (Path.cwd() / ns.source).resolve() |
| 653 |
if not ns.build.is_absolute(): |
| 654 |
ns.build = (Path.cwd() / ns.build).resolve() |
| 655 |
if not ns.temp.is_absolute(): |
| 656 |
ns.temp = (Path.cwd() / ns.temp).resolve() |
| 657 |
if not ns.doc_build.is_absolute(): |
| 658 |
ns.doc_build = (Path.cwd() / ns.doc_build).resolve() |
| 659 |
if ns.include_cat and not ns.include_cat.is_absolute(): |
| 660 |
ns.include_cat = (Path.cwd() / ns.include_cat).resolve() |
| 661 |
if ns.zip and not ns.zip.is_absolute(): |
| 662 |
ns.zip = (Path.cwd() / ns.zip).resolve() |
| 663 |
if ns.catalog and not ns.catalog.is_absolute(): |
| 664 |
ns.catalog = (Path.cwd() / ns.catalog).resolve() |
| 665 |
|
| 666 |
configure_logger(ns) |
| 667 |
|
| 668 |
if not ns.arch: |
| 669 |
from .support.arch import calculate_from_build_dir |
| 670 |
ns.arch = calculate_from_build_dir(ns.build) |
| 671 |
|
| 672 |
expect = f"{VER_MAJOR}.{VER_MINOR}.{VER_MICRO}{VER_SUFFIX}" |
| 673 |
actual = check_patchlevel_version(ns.source) |
| 674 |
if actual and actual != expect: |
| 675 |
log_error(f"Inferred version {expect} does not match {actual} from patchlevel.h. " |
| 676 |
"You should set %PYTHONINCLUDE% or %PYTHON_HEXVERSION% before launching.") |
| 677 |
return 5 |
| 678 |
|
| 679 |
log_info( |
| 680 |
"""OPTIONS |
| 681 |
Source: {ns.source} |
| 682 |
Build: {ns.build} |
| 683 |
Temp: {ns.temp} |
| 684 |
Arch: {ns.arch} |
| 685 |
|
| 686 |
Copy to: {ns.copy} |
| 687 |
Zip to: {ns.zip} |
| 688 |
Catalog: {ns.catalog}""", |
| 689 |
ns=ns, |
| 690 |
) |
| 691 |
|
| 692 |
if ns.arch not in ("win32", "amd64", "arm32", "arm64"): |
| 693 |
log_error("--arch is not a valid value (win32, amd64, arm32, arm64)") |
| 694 |
return 4 |
| 695 |
if ns.arch == "arm32": |
| 696 |
for n in ("include_idle", "include_tcltk"): |
| 697 |
if getattr(ns, n): |
| 698 |
log_warning(f"Disabling --{n.replace('_', '-')} on unsupported platform") |
| 699 |
setattr(ns, n, False) |
| 700 |
|
| 701 |
if ns.include_idle and not ns.include_tcltk: |
| 702 |
log_warning("Assuming --include-tcltk to support --include-idle") |
| 703 |
ns.include_tcltk = True |
| 704 |
|
| 705 |
if not (ns.include_alias or ns.include_alias3 or ns.include_alias3x): |
| 706 |
if ns.include_freethreaded: |
| 707 |
ns.include_alias3x = True |
| 708 |
else: |
| 709 |
ns.include_alias = True |
| 710 |
|
| 711 |
try: |
| 712 |
generate_source_files(ns) |
| 713 |
files = list(get_layout(ns)) |
| 714 |
copy_files(files, ns) |
| 715 |
except KeyboardInterrupt: |
| 716 |
log_info("Interrupted by Ctrl+C") |
| 717 |
return 3 |
| 718 |
except SystemExit: |
| 719 |
raise |
| 720 |
except: |
| 721 |
log_exception("Unhandled error") |
| 722 |
|
| 723 |
if error_was_logged(): |
| 724 |
log_error("Errors occurred.") |
| 725 |
return 1 |
| 726 |
|
| 727 |
|
| 728 |
if __name__ == "__main__": |
| 729 |
sys.exit(int(main() or 0)) |