Steve Dower | 0cd6391 | 2018-12-10 18:52:57 -0800 | [diff] [blame] | 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 <steve.dower@python.org>" |
| 8 | __version__ = "3.8" |
| 9 | |
| 10 | import argparse |
| 11 | import functools |
| 12 | import os |
| 13 | import re |
| 14 | import shutil |
| 15 | import subprocess |
| 16 | import sys |
| 17 | import tempfile |
| 18 | import zipfile |
| 19 | |
| 20 | from pathlib import Path |
| 21 | |
| 22 | if __name__ == "__main__": |
| 23 | # Started directly, so enable relative imports |
| 24 | __path__ = [str(Path(__file__).resolve().parent)] |
| 25 | |
| 26 | from .support.appxmanifest import * |
| 27 | from .support.catalog import * |
| 28 | from .support.constants import * |
| 29 | from .support.filesets import * |
| 30 | from .support.logging import * |
| 31 | from .support.options import * |
| 32 | from .support.pip import * |
| 33 | from .support.props import * |
Steve Dower | 21a92f8 | 2019-06-14 08:29:20 -0700 | [diff] [blame] | 34 | from .support.nuspec import * |
Steve Dower | 0cd6391 | 2018-12-10 18:52:57 -0800 | [diff] [blame] | 35 | |
| 36 | BDIST_WININST_FILES_ONLY = FileNameSet("wininst-*", "bdist_wininst.py") |
| 37 | BDIST_WININST_STUB = "PC/layout/support/distutils.command.bdist_wininst.py" |
| 38 | |
| 39 | TEST_PYDS_ONLY = FileStemSet("xxlimited", "_ctypes_test", "_test*") |
| 40 | TEST_DIRS_ONLY = FileNameSet("test", "tests") |
| 41 | |
| 42 | IDLE_DIRS_ONLY = FileNameSet("idlelib") |
| 43 | |
| 44 | TCLTK_PYDS_ONLY = FileStemSet("tcl*", "tk*", "_tkinter") |
| 45 | TCLTK_DIRS_ONLY = FileNameSet("tkinter", "turtledemo") |
| 46 | TCLTK_FILES_ONLY = FileNameSet("turtle.py") |
| 47 | |
| 48 | VENV_DIRS_ONLY = FileNameSet("venv", "ensurepip") |
| 49 | |
Steve Dower | 59c2aa2 | 2018-12-27 12:44:25 -0800 | [diff] [blame] | 50 | EXCLUDE_FROM_PYDS = FileStemSet("python*", "pyshellext", "vcruntime*") |
Steve Dower | 0cd6391 | 2018-12-10 18:52:57 -0800 | [diff] [blame] | 51 | EXCLUDE_FROM_LIB = FileNameSet("*.pyc", "__pycache__", "*.pickle") |
| 52 | EXCLUDE_FROM_PACKAGED_LIB = FileNameSet("readme.txt") |
| 53 | EXCLUDE_FROM_COMPILE = FileNameSet("badsyntax_*", "bad_*") |
| 54 | EXCLUDE_FROM_CATALOG = FileSuffixSet(".exe", ".pyd", ".dll") |
| 55 | |
Paul Monson | 32119e1 | 2019-03-29 16:30:10 -0700 | [diff] [blame] | 56 | REQUIRED_DLLS = FileStemSet("libcrypto*", "libssl*", "libffi*") |
Steve Dower | 0cd6391 | 2018-12-10 18:52:57 -0800 | [diff] [blame] | 57 | |
| 58 | LIB2TO3_GRAMMAR_FILES = FileNameSet("Grammar.txt", "PatternGrammar.txt") |
| 59 | |
| 60 | PY_FILES = FileSuffixSet(".py") |
| 61 | PYC_FILES = FileSuffixSet(".pyc") |
| 62 | CAT_FILES = FileSuffixSet(".cat") |
| 63 | CDF_FILES = FileSuffixSet(".cdf") |
| 64 | |
| 65 | DATA_DIRS = FileNameSet("data") |
| 66 | |
| 67 | TOOLS_DIRS = FileNameSet("scripts", "i18n", "pynche", "demo", "parser") |
| 68 | TOOLS_FILES = FileSuffixSet(".py", ".pyw", ".txt") |
| 69 | |
Steve Dower | 21a92f8 | 2019-06-14 08:29:20 -0700 | [diff] [blame] | 70 | |
Paul Monson | f4e5661 | 2019-04-12 09:55:57 -0700 | [diff] [blame] | 71 | def copy_if_modified(src, dest): |
| 72 | try: |
| 73 | dest_stat = os.stat(dest) |
| 74 | except FileNotFoundError: |
| 75 | do_copy = True |
| 76 | else: |
| 77 | src_stat = os.stat(src) |
Steve Dower | 21a92f8 | 2019-06-14 08:29:20 -0700 | [diff] [blame] | 78 | do_copy = ( |
| 79 | src_stat.st_mtime != dest_stat.st_mtime |
| 80 | or src_stat.st_size != dest_stat.st_size |
| 81 | ) |
Paul Monson | f4e5661 | 2019-04-12 09:55:57 -0700 | [diff] [blame] | 82 | |
| 83 | if do_copy: |
| 84 | shutil.copy2(src, dest) |
Steve Dower | 0cd6391 | 2018-12-10 18:52:57 -0800 | [diff] [blame] | 85 | |
Steve Dower | 21a92f8 | 2019-06-14 08:29:20 -0700 | [diff] [blame] | 86 | |
Steve Dower | 0cd6391 | 2018-12-10 18:52:57 -0800 | [diff] [blame] | 87 | def get_lib_layout(ns): |
| 88 | def _c(f): |
| 89 | if f in EXCLUDE_FROM_LIB: |
| 90 | return False |
| 91 | if f.is_dir(): |
| 92 | if f in TEST_DIRS_ONLY: |
| 93 | return ns.include_tests |
| 94 | if f in TCLTK_DIRS_ONLY: |
| 95 | return ns.include_tcltk |
| 96 | if f in IDLE_DIRS_ONLY: |
| 97 | return ns.include_idle |
| 98 | if f in VENV_DIRS_ONLY: |
| 99 | return ns.include_venv |
| 100 | else: |
| 101 | if f in TCLTK_FILES_ONLY: |
| 102 | return ns.include_tcltk |
| 103 | if f in BDIST_WININST_FILES_ONLY: |
| 104 | return ns.include_bdist_wininst |
| 105 | return True |
| 106 | |
| 107 | for dest, src in rglob(ns.source / "Lib", "**/*", _c): |
| 108 | yield dest, src |
| 109 | |
| 110 | if not ns.include_bdist_wininst: |
| 111 | src = ns.source / BDIST_WININST_STUB |
| 112 | yield Path("distutils/command/bdist_wininst.py"), src |
| 113 | |
| 114 | |
| 115 | def get_tcltk_lib(ns): |
| 116 | if not ns.include_tcltk: |
| 117 | return |
| 118 | |
| 119 | tcl_lib = os.getenv("TCL_LIBRARY") |
| 120 | if not tcl_lib or not os.path.isdir(tcl_lib): |
| 121 | try: |
| 122 | with open(ns.build / "TCL_LIBRARY.env", "r", encoding="utf-8-sig") as f: |
| 123 | tcl_lib = f.read().strip() |
| 124 | except FileNotFoundError: |
| 125 | pass |
| 126 | if not tcl_lib or not os.path.isdir(tcl_lib): |
Steve Dower | 21a92f8 | 2019-06-14 08:29:20 -0700 | [diff] [blame] | 127 | log_warning("Failed to find TCL_LIBRARY") |
Steve Dower | 0cd6391 | 2018-12-10 18:52:57 -0800 | [diff] [blame] | 128 | return |
| 129 | |
| 130 | for dest, src in rglob(Path(tcl_lib).parent, "**/*"): |
| 131 | yield "tcl/{}".format(dest), src |
| 132 | |
| 133 | |
| 134 | def get_layout(ns): |
| 135 | def in_build(f, dest="", new_name=None): |
| 136 | n, _, x = f.rpartition(".") |
| 137 | n = new_name or n |
| 138 | src = ns.build / f |
| 139 | if ns.debug and src not in REQUIRED_DLLS: |
| 140 | if not src.stem.endswith("_d"): |
| 141 | src = src.parent / (src.stem + "_d" + src.suffix) |
| 142 | if not n.endswith("_d"): |
| 143 | n += "_d" |
| 144 | f = n + "." + x |
| 145 | yield dest + n + "." + x, src |
| 146 | if ns.include_symbols: |
| 147 | pdb = src.with_suffix(".pdb") |
| 148 | if pdb.is_file(): |
| 149 | yield dest + n + ".pdb", pdb |
| 150 | if ns.include_dev: |
| 151 | lib = src.with_suffix(".lib") |
| 152 | if lib.is_file(): |
| 153 | yield "libs/" + n + ".lib", lib |
| 154 | |
| 155 | if ns.include_appxmanifest: |
Steve Dower | 1fab9cb | 2019-08-07 10:49:40 -0700 | [diff] [blame] | 156 | yield from in_build("python_uwp.exe", new_name="python{}".format(VER_DOT)) |
| 157 | yield from in_build("pythonw_uwp.exe", new_name="pythonw{}".format(VER_DOT)) |
| 158 | # For backwards compatibility, but we don't reference these ourselves. |
Steve Dower | 0cd6391 | 2018-12-10 18:52:57 -0800 | [diff] [blame] | 159 | yield from in_build("python_uwp.exe", new_name="python") |
| 160 | yield from in_build("pythonw_uwp.exe", new_name="pythonw") |
| 161 | else: |
| 162 | yield from in_build("python.exe", new_name="python") |
| 163 | yield from in_build("pythonw.exe", new_name="pythonw") |
| 164 | |
| 165 | yield from in_build(PYTHON_DLL_NAME) |
| 166 | |
| 167 | if ns.include_launchers and ns.include_appxmanifest: |
| 168 | if ns.include_pip: |
Steve Dower | 1fab9cb | 2019-08-07 10:49:40 -0700 | [diff] [blame] | 169 | yield from in_build("python_uwp.exe", new_name="pip{}".format(VER_DOT)) |
Steve Dower | 0cd6391 | 2018-12-10 18:52:57 -0800 | [diff] [blame] | 170 | if ns.include_idle: |
Steve Dower | 1fab9cb | 2019-08-07 10:49:40 -0700 | [diff] [blame] | 171 | yield from in_build("pythonw_uwp.exe", new_name="idle{}".format(VER_DOT)) |
Steve Dower | 0cd6391 | 2018-12-10 18:52:57 -0800 | [diff] [blame] | 172 | |
| 173 | if ns.include_stable: |
| 174 | yield from in_build(PYTHON_STABLE_DLL_NAME) |
| 175 | |
| 176 | for dest, src in rglob(ns.build, "vcruntime*.dll"): |
| 177 | yield dest, src |
| 178 | |
Steve Dower | 21a92f8 | 2019-06-14 08:29:20 -0700 | [diff] [blame] | 179 | yield "LICENSE.txt", ns.build / "LICENSE.txt" |
Steve Dower | 28f6cb3 | 2019-01-22 10:49:52 -0800 | [diff] [blame] | 180 | |
Steve Dower | 0cd6391 | 2018-12-10 18:52:57 -0800 | [diff] [blame] | 181 | for dest, src in rglob(ns.build, ("*.pyd", "*.dll")): |
| 182 | if src.stem.endswith("_d") != bool(ns.debug) and src not in REQUIRED_DLLS: |
| 183 | continue |
| 184 | if src in EXCLUDE_FROM_PYDS: |
| 185 | continue |
| 186 | if src in TEST_PYDS_ONLY and not ns.include_tests: |
| 187 | continue |
| 188 | if src in TCLTK_PYDS_ONLY and not ns.include_tcltk: |
| 189 | continue |
| 190 | |
| 191 | yield from in_build(src.name, dest="" if ns.flat_dlls else "DLLs/") |
| 192 | |
| 193 | if ns.zip_lib: |
| 194 | zip_name = PYTHON_ZIP_NAME |
| 195 | yield zip_name, ns.temp / zip_name |
| 196 | else: |
| 197 | for dest, src in get_lib_layout(ns): |
| 198 | yield "Lib/{}".format(dest), src |
| 199 | |
| 200 | if ns.include_venv: |
| 201 | yield from in_build("venvlauncher.exe", "Lib/venv/scripts/nt/", "python") |
| 202 | yield from in_build("venvwlauncher.exe", "Lib/venv/scripts/nt/", "pythonw") |
| 203 | |
| 204 | if ns.include_tools: |
| 205 | |
| 206 | def _c(d): |
| 207 | if d.is_dir(): |
| 208 | return d in TOOLS_DIRS |
| 209 | return d in TOOLS_FILES |
| 210 | |
| 211 | for dest, src in rglob(ns.source / "Tools", "**/*", _c): |
| 212 | yield "Tools/{}".format(dest), src |
| 213 | |
| 214 | if ns.include_underpth: |
| 215 | yield PYTHON_PTH_NAME, ns.temp / PYTHON_PTH_NAME |
| 216 | |
| 217 | if ns.include_dev: |
| 218 | |
Zackery Spytz | edb172a | 2019-10-28 11:03:27 -0600 | [diff] [blame] | 219 | for dest, src in rglob(ns.source / "Include", "**/*.h"): |
Steve Dower | 0cd6391 | 2018-12-10 18:52:57 -0800 | [diff] [blame] | 220 | yield "include/{}".format(dest), src |
| 221 | src = ns.source / "PC" / "pyconfig.h" |
| 222 | yield "include/pyconfig.h", src |
| 223 | |
| 224 | for dest, src in get_tcltk_lib(ns): |
| 225 | yield dest, src |
| 226 | |
| 227 | if ns.include_pip: |
Steve Dower | 21a92f8 | 2019-06-14 08:29:20 -0700 | [diff] [blame] | 228 | for dest, src in get_pip_layout(ns): |
Steve Dower | 123536f | 2019-07-24 15:13:22 -0700 | [diff] [blame] | 229 | if not isinstance(src, tuple) and ( |
Steve Dower | 21a92f8 | 2019-06-14 08:29:20 -0700 | [diff] [blame] | 230 | src in EXCLUDE_FROM_LIB or src in EXCLUDE_FROM_PACKAGED_LIB |
| 231 | ): |
| 232 | continue |
| 233 | yield dest, src |
Steve Dower | 0cd6391 | 2018-12-10 18:52:57 -0800 | [diff] [blame] | 234 | |
| 235 | if ns.include_chm: |
| 236 | for dest, src in rglob(ns.doc_build / "htmlhelp", PYTHON_CHM_NAME): |
| 237 | yield "Doc/{}".format(dest), src |
| 238 | |
| 239 | if ns.include_html_doc: |
| 240 | for dest, src in rglob(ns.doc_build / "html", "**/*"): |
| 241 | yield "Doc/html/{}".format(dest), src |
| 242 | |
| 243 | if ns.include_props: |
| 244 | for dest, src in get_props_layout(ns): |
| 245 | yield dest, src |
| 246 | |
Steve Dower | 21a92f8 | 2019-06-14 08:29:20 -0700 | [diff] [blame] | 247 | if ns.include_nuspec: |
| 248 | for dest, src in get_nuspec_layout(ns): |
| 249 | yield dest, src |
| 250 | |
Steve Dower | 0cd6391 | 2018-12-10 18:52:57 -0800 | [diff] [blame] | 251 | for dest, src in get_appx_layout(ns): |
| 252 | yield dest, src |
| 253 | |
| 254 | if ns.include_cat: |
| 255 | if ns.flat_dlls: |
| 256 | yield ns.include_cat.name, ns.include_cat |
| 257 | else: |
| 258 | yield "DLLs/{}".format(ns.include_cat.name), ns.include_cat |
| 259 | |
| 260 | |
Steve Dower | 872bd2b | 2019-01-08 02:38:01 -0800 | [diff] [blame] | 261 | def _compile_one_py(src, dest, name, optimize, checked=True): |
Steve Dower | 0cd6391 | 2018-12-10 18:52:57 -0800 | [diff] [blame] | 262 | import py_compile |
| 263 | |
| 264 | if dest is not None: |
| 265 | dest = str(dest) |
| 266 | |
Steve Dower | 872bd2b | 2019-01-08 02:38:01 -0800 | [diff] [blame] | 267 | mode = ( |
| 268 | py_compile.PycInvalidationMode.CHECKED_HASH |
| 269 | if checked |
| 270 | else py_compile.PycInvalidationMode.UNCHECKED_HASH |
| 271 | ) |
| 272 | |
Steve Dower | 0cd6391 | 2018-12-10 18:52:57 -0800 | [diff] [blame] | 273 | try: |
| 274 | return Path( |
| 275 | py_compile.compile( |
| 276 | str(src), |
| 277 | dest, |
| 278 | str(name), |
| 279 | doraise=True, |
| 280 | optimize=optimize, |
Steve Dower | 872bd2b | 2019-01-08 02:38:01 -0800 | [diff] [blame] | 281 | invalidation_mode=mode, |
Steve Dower | 0cd6391 | 2018-12-10 18:52:57 -0800 | [diff] [blame] | 282 | ) |
| 283 | ) |
| 284 | except py_compile.PyCompileError: |
| 285 | log_warning("Failed to compile {}", src) |
| 286 | return None |
| 287 | |
Bill Collins | c4cda43 | 2019-07-25 22:36:58 +0100 | [diff] [blame] | 288 | # name argument added to address bpo-37641 |
| 289 | def _py_temp_compile(src, name, ns, dest_dir=None, checked=True): |
Steve Dower | 0cd6391 | 2018-12-10 18:52:57 -0800 | [diff] [blame] | 290 | if not ns.precompile or src not in PY_FILES or src.parent in DATA_DIRS: |
| 291 | return None |
Bill Collins | c4cda43 | 2019-07-25 22:36:58 +0100 | [diff] [blame] | 292 | dest = (dest_dir or ns.temp) / (src.stem + ".pyc") |
Steve Dower | 21a92f8 | 2019-06-14 08:29:20 -0700 | [diff] [blame] | 293 | return _compile_one_py( |
Bill Collins | c4cda43 | 2019-07-25 22:36:58 +0100 | [diff] [blame] | 294 | src, dest, name, optimize=2, checked=checked |
Steve Dower | 21a92f8 | 2019-06-14 08:29:20 -0700 | [diff] [blame] | 295 | ) |
Steve Dower | 0cd6391 | 2018-12-10 18:52:57 -0800 | [diff] [blame] | 296 | |
| 297 | |
Steve Dower | 872bd2b | 2019-01-08 02:38:01 -0800 | [diff] [blame] | 298 | def _write_to_zip(zf, dest, src, ns, checked=True): |
Bill Collins | c4cda43 | 2019-07-25 22:36:58 +0100 | [diff] [blame] | 299 | pyc = _py_temp_compile(src, dest, ns, checked=checked) |
Steve Dower | 0cd6391 | 2018-12-10 18:52:57 -0800 | [diff] [blame] | 300 | if pyc: |
| 301 | try: |
| 302 | zf.write(str(pyc), dest.with_suffix(".pyc")) |
| 303 | finally: |
| 304 | try: |
| 305 | pyc.unlink() |
| 306 | except: |
| 307 | log_exception("Failed to delete {}", pyc) |
| 308 | return |
| 309 | |
| 310 | if src in LIB2TO3_GRAMMAR_FILES: |
| 311 | from lib2to3.pgen2.driver import load_grammar |
| 312 | |
| 313 | tmp = ns.temp / src.name |
| 314 | try: |
| 315 | shutil.copy(src, tmp) |
| 316 | load_grammar(str(tmp)) |
| 317 | for f in ns.temp.glob(src.stem + "*.pickle"): |
| 318 | zf.write(str(f), str(dest.parent / f.name)) |
| 319 | try: |
| 320 | f.unlink() |
| 321 | except: |
| 322 | log_exception("Failed to delete {}", f) |
| 323 | except: |
| 324 | log_exception("Failed to compile {}", src) |
| 325 | finally: |
| 326 | try: |
| 327 | tmp.unlink() |
| 328 | except: |
| 329 | log_exception("Failed to delete {}", tmp) |
| 330 | |
| 331 | zf.write(str(src), str(dest)) |
| 332 | |
| 333 | |
| 334 | def generate_source_files(ns): |
| 335 | if ns.zip_lib: |
| 336 | zip_name = PYTHON_ZIP_NAME |
| 337 | zip_path = ns.temp / zip_name |
| 338 | if zip_path.is_file(): |
| 339 | zip_path.unlink() |
| 340 | elif zip_path.is_dir(): |
| 341 | log_error( |
| 342 | "Cannot create zip file because a directory exists by the same name" |
| 343 | ) |
| 344 | return |
| 345 | log_info("Generating {} in {}", zip_name, ns.temp) |
| 346 | ns.temp.mkdir(parents=True, exist_ok=True) |
| 347 | with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED) as zf: |
| 348 | for dest, src in get_lib_layout(ns): |
Steve Dower | 872bd2b | 2019-01-08 02:38:01 -0800 | [diff] [blame] | 349 | _write_to_zip(zf, dest, src, ns, checked=False) |
Steve Dower | 0cd6391 | 2018-12-10 18:52:57 -0800 | [diff] [blame] | 350 | |
| 351 | if ns.include_underpth: |
| 352 | log_info("Generating {} in {}", PYTHON_PTH_NAME, ns.temp) |
| 353 | ns.temp.mkdir(parents=True, exist_ok=True) |
| 354 | with open(ns.temp / PYTHON_PTH_NAME, "w", encoding="utf-8") as f: |
| 355 | if ns.zip_lib: |
| 356 | print(PYTHON_ZIP_NAME, file=f) |
| 357 | if ns.include_pip: |
| 358 | print("packages", file=f) |
| 359 | else: |
| 360 | print("Lib", file=f) |
| 361 | print("Lib/site-packages", file=f) |
| 362 | if not ns.flat_dlls: |
| 363 | print("DLLs", file=f) |
| 364 | print(".", file=f) |
| 365 | print(file=f) |
| 366 | print("# Uncomment to run site.main() automatically", file=f) |
| 367 | print("#import site", file=f) |
| 368 | |
Steve Dower | 0cd6391 | 2018-12-10 18:52:57 -0800 | [diff] [blame] | 369 | if ns.include_pip: |
Steve Dower | 21a92f8 | 2019-06-14 08:29:20 -0700 | [diff] [blame] | 370 | log_info("Extracting pip") |
| 371 | extract_pip_files(ns) |
Steve Dower | 0cd6391 | 2018-12-10 18:52:57 -0800 | [diff] [blame] | 372 | |
| 373 | |
| 374 | def _create_zip_file(ns): |
| 375 | if not ns.zip: |
| 376 | return None |
| 377 | |
| 378 | if ns.zip.is_file(): |
| 379 | try: |
| 380 | ns.zip.unlink() |
| 381 | except OSError: |
| 382 | log_exception("Unable to remove {}", ns.zip) |
| 383 | sys.exit(8) |
| 384 | elif ns.zip.is_dir(): |
| 385 | log_error("Cannot create ZIP file because {} is a directory", ns.zip) |
| 386 | sys.exit(8) |
| 387 | |
| 388 | ns.zip.parent.mkdir(parents=True, exist_ok=True) |
| 389 | return zipfile.ZipFile(ns.zip, "w", zipfile.ZIP_DEFLATED) |
| 390 | |
| 391 | |
| 392 | def copy_files(files, ns): |
| 393 | if ns.copy: |
| 394 | ns.copy.mkdir(parents=True, exist_ok=True) |
| 395 | |
| 396 | try: |
| 397 | total = len(files) |
| 398 | except TypeError: |
| 399 | total = None |
| 400 | count = 0 |
| 401 | |
| 402 | zip_file = _create_zip_file(ns) |
| 403 | try: |
| 404 | need_compile = [] |
| 405 | in_catalog = [] |
| 406 | |
| 407 | for dest, src in files: |
| 408 | count += 1 |
| 409 | if count % 10 == 0: |
| 410 | if total: |
| 411 | log_info("Processed {:>4} of {} files", count, total) |
| 412 | else: |
| 413 | log_info("Processed {} files", count) |
| 414 | log_debug("Processing {!s}", src) |
| 415 | |
Steve Dower | 21a92f8 | 2019-06-14 08:29:20 -0700 | [diff] [blame] | 416 | if isinstance(src, tuple): |
| 417 | src, content = src |
| 418 | if ns.copy: |
| 419 | log_debug("Copy {} -> {}", src, ns.copy / dest) |
| 420 | (ns.copy / dest).parent.mkdir(parents=True, exist_ok=True) |
| 421 | with open(ns.copy / dest, "wb") as f: |
| 422 | f.write(content) |
| 423 | if ns.zip: |
| 424 | log_debug("Zip {} into {}", src, ns.zip) |
| 425 | zip_file.writestr(str(dest), content) |
| 426 | continue |
| 427 | |
Steve Dower | 0cd6391 | 2018-12-10 18:52:57 -0800 | [diff] [blame] | 428 | if ( |
| 429 | ns.precompile |
| 430 | and src in PY_FILES |
| 431 | and src not in EXCLUDE_FROM_COMPILE |
| 432 | and src.parent not in DATA_DIRS |
| 433 | and os.path.normcase(str(dest)).startswith(os.path.normcase("Lib")) |
| 434 | ): |
| 435 | if ns.copy: |
| 436 | need_compile.append((dest, ns.copy / dest)) |
| 437 | else: |
| 438 | (ns.temp / "Lib" / dest).parent.mkdir(parents=True, exist_ok=True) |
Paul Monson | f4e5661 | 2019-04-12 09:55:57 -0700 | [diff] [blame] | 439 | copy_if_modified(src, ns.temp / "Lib" / dest) |
Steve Dower | 0cd6391 | 2018-12-10 18:52:57 -0800 | [diff] [blame] | 440 | need_compile.append((dest, ns.temp / "Lib" / dest)) |
| 441 | |
| 442 | if src not in EXCLUDE_FROM_CATALOG: |
| 443 | in_catalog.append((src.name, src)) |
| 444 | |
| 445 | if ns.copy: |
| 446 | log_debug("Copy {} -> {}", src, ns.copy / dest) |
| 447 | (ns.copy / dest).parent.mkdir(parents=True, exist_ok=True) |
| 448 | try: |
Paul Monson | f4e5661 | 2019-04-12 09:55:57 -0700 | [diff] [blame] | 449 | copy_if_modified(src, ns.copy / dest) |
Steve Dower | 0cd6391 | 2018-12-10 18:52:57 -0800 | [diff] [blame] | 450 | except shutil.SameFileError: |
| 451 | pass |
| 452 | |
| 453 | if ns.zip: |
| 454 | log_debug("Zip {} into {}", src, ns.zip) |
| 455 | zip_file.write(src, str(dest)) |
| 456 | |
| 457 | if need_compile: |
| 458 | for dest, src in need_compile: |
| 459 | compiled = [ |
| 460 | _compile_one_py(src, None, dest, optimize=0), |
| 461 | _compile_one_py(src, None, dest, optimize=1), |
| 462 | _compile_one_py(src, None, dest, optimize=2), |
| 463 | ] |
| 464 | for c in compiled: |
| 465 | if not c: |
| 466 | continue |
| 467 | cdest = Path(dest).parent / Path(c).relative_to(src.parent) |
| 468 | if ns.zip: |
| 469 | log_debug("Zip {} into {}", c, ns.zip) |
| 470 | zip_file.write(c, str(cdest)) |
| 471 | in_catalog.append((cdest.name, cdest)) |
| 472 | |
| 473 | if ns.catalog: |
| 474 | # Just write out the CDF now. Compilation and signing is |
| 475 | # an extra step |
| 476 | log_info("Generating {}", ns.catalog) |
| 477 | ns.catalog.parent.mkdir(parents=True, exist_ok=True) |
| 478 | write_catalog(ns.catalog, in_catalog) |
| 479 | |
| 480 | finally: |
| 481 | if zip_file: |
| 482 | zip_file.close() |
| 483 | |
| 484 | |
| 485 | def main(): |
| 486 | parser = argparse.ArgumentParser() |
| 487 | parser.add_argument("-v", help="Increase verbosity", action="count") |
| 488 | parser.add_argument( |
| 489 | "-s", |
| 490 | "--source", |
| 491 | metavar="dir", |
| 492 | help="The directory containing the repository root", |
| 493 | type=Path, |
| 494 | default=None, |
| 495 | ) |
| 496 | parser.add_argument( |
| 497 | "-b", "--build", metavar="dir", help="Specify the build directory", type=Path |
| 498 | ) |
| 499 | parser.add_argument( |
| 500 | "--doc-build", |
| 501 | metavar="dir", |
| 502 | help="Specify the docs build directory", |
| 503 | type=Path, |
| 504 | default=None, |
| 505 | ) |
| 506 | parser.add_argument( |
| 507 | "--copy", |
| 508 | metavar="directory", |
| 509 | help="The name of the directory to copy an extracted layout to", |
| 510 | type=Path, |
| 511 | default=None, |
| 512 | ) |
| 513 | parser.add_argument( |
| 514 | "--zip", |
| 515 | metavar="file", |
| 516 | help="The ZIP file to write all files to", |
| 517 | type=Path, |
| 518 | default=None, |
| 519 | ) |
| 520 | parser.add_argument( |
| 521 | "--catalog", |
| 522 | metavar="file", |
| 523 | help="The CDF file to write catalog entries to", |
| 524 | type=Path, |
| 525 | default=None, |
| 526 | ) |
| 527 | parser.add_argument( |
| 528 | "--log", |
| 529 | metavar="file", |
| 530 | help="Write all operations to the specified file", |
| 531 | type=Path, |
| 532 | default=None, |
| 533 | ) |
| 534 | parser.add_argument( |
| 535 | "-t", |
| 536 | "--temp", |
| 537 | metavar="file", |
| 538 | help="A temporary working directory", |
| 539 | type=Path, |
| 540 | default=None, |
| 541 | ) |
| 542 | parser.add_argument( |
| 543 | "-d", "--debug", help="Include debug build", action="store_true" |
| 544 | ) |
| 545 | parser.add_argument( |
| 546 | "-p", |
| 547 | "--precompile", |
| 548 | help="Include .pyc files instead of .py", |
| 549 | action="store_true", |
| 550 | ) |
| 551 | parser.add_argument( |
| 552 | "-z", "--zip-lib", help="Include library in a ZIP file", action="store_true" |
| 553 | ) |
| 554 | parser.add_argument( |
| 555 | "--flat-dlls", help="Does not create a DLLs directory", action="store_true" |
| 556 | ) |
| 557 | parser.add_argument( |
| 558 | "-a", |
| 559 | "--include-all", |
| 560 | help="Include all optional components", |
| 561 | action="store_true", |
| 562 | ) |
| 563 | parser.add_argument( |
| 564 | "--include-cat", |
| 565 | metavar="file", |
| 566 | help="Specify the catalog file to include", |
| 567 | type=Path, |
| 568 | default=None, |
| 569 | ) |
| 570 | for opt, help in get_argparse_options(): |
| 571 | parser.add_argument(opt, help=help, action="store_true") |
| 572 | |
| 573 | ns = parser.parse_args() |
| 574 | update_presets(ns) |
| 575 | |
| 576 | ns.source = ns.source or (Path(__file__).resolve().parent.parent.parent) |
| 577 | ns.build = ns.build or Path(sys.executable).parent |
| 578 | ns.temp = ns.temp or Path(tempfile.mkdtemp()) |
| 579 | ns.doc_build = ns.doc_build or (ns.source / "Doc" / "build") |
| 580 | if not ns.source.is_absolute(): |
| 581 | ns.source = (Path.cwd() / ns.source).resolve() |
| 582 | if not ns.build.is_absolute(): |
| 583 | ns.build = (Path.cwd() / ns.build).resolve() |
| 584 | if not ns.temp.is_absolute(): |
| 585 | ns.temp = (Path.cwd() / ns.temp).resolve() |
| 586 | if not ns.doc_build.is_absolute(): |
| 587 | ns.doc_build = (Path.cwd() / ns.doc_build).resolve() |
| 588 | if ns.include_cat and not ns.include_cat.is_absolute(): |
| 589 | ns.include_cat = (Path.cwd() / ns.include_cat).resolve() |
| 590 | |
| 591 | if ns.copy and not ns.copy.is_absolute(): |
| 592 | ns.copy = (Path.cwd() / ns.copy).resolve() |
| 593 | if ns.zip and not ns.zip.is_absolute(): |
| 594 | ns.zip = (Path.cwd() / ns.zip).resolve() |
| 595 | if ns.catalog and not ns.catalog.is_absolute(): |
| 596 | ns.catalog = (Path.cwd() / ns.catalog).resolve() |
| 597 | |
| 598 | configure_logger(ns) |
| 599 | |
| 600 | log_info( |
| 601 | """OPTIONS |
| 602 | Source: {ns.source} |
| 603 | Build: {ns.build} |
| 604 | Temp: {ns.temp} |
| 605 | |
| 606 | Copy to: {ns.copy} |
| 607 | Zip to: {ns.zip} |
| 608 | Catalog: {ns.catalog}""", |
| 609 | ns=ns, |
| 610 | ) |
| 611 | |
| 612 | if ns.include_idle and not ns.include_tcltk: |
| 613 | log_warning("Assuming --include-tcltk to support --include-idle") |
| 614 | ns.include_tcltk = True |
| 615 | |
| 616 | try: |
| 617 | generate_source_files(ns) |
| 618 | files = list(get_layout(ns)) |
| 619 | copy_files(files, ns) |
| 620 | except KeyboardInterrupt: |
| 621 | log_info("Interrupted by Ctrl+C") |
| 622 | return 3 |
| 623 | except SystemExit: |
| 624 | raise |
| 625 | except: |
| 626 | log_exception("Unhandled error") |
| 627 | |
| 628 | if error_was_logged(): |
| 629 | log_error("Errors occurred.") |
| 630 | return 1 |
| 631 | |
| 632 | |
| 633 | if __name__ == "__main__": |
| 634 | sys.exit(int(main() or 0)) |