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