|
| 1 | +#!/usr/bin/env python |
| 2 | +"""Create a WASM asset bundle directory structure. |
| 3 | +
|
| 4 | +The WASM asset bundles are pre-loaded by the final WASM build. The bundle |
| 5 | +contains: |
| 6 | +
|
| 7 | +- a stripped down, pyc-only stdlib zip file, e.g. {PREFIX}/lib/python311.zip |
| 8 | +- os.py as marker module {PREFIX}/lib/python3.11/os.py |
| 9 | +- empty lib-dynload directory, to make sure it is copied into the bundle {PREFIX}/lib/python3.11/lib-dynload/.empty |
| 10 | +""" |
| 11 | + |
| 12 | +import argparse |
| 13 | +import pathlib |
| 14 | +import shutil |
| 15 | +import sys |
| 16 | +import zipfile |
| 17 | + |
| 18 | +# source directory |
| 19 | +SRCDIR = pathlib.Path(__file__).parent.parent.parent.absolute() |
| 20 | +SRCDIR_LIB = SRCDIR / "Lib" |
| 21 | + |
| 22 | +# sysconfig data relative to build dir. |
| 23 | +SYSCONFIGDATA_GLOB = "build/lib.*/_sysconfigdata_*.py" |
| 24 | + |
| 25 | +# Library directory relative to $(prefix). |
| 26 | +WASM_LIB = pathlib.PurePath("lib") |
| 27 | +WASM_STDLIB_ZIP = ( |
| 28 | + WASM_LIB / f"python{sys.version_info.major}{sys.version_info.minor}.zip" |
| 29 | +) |
| 30 | +WASM_STDLIB = ( |
| 31 | + WASM_LIB / f"python{sys.version_info.major}.{sys.version_info.minor}" |
| 32 | +) |
| 33 | +WASM_DYNLOAD = WASM_STDLIB / "lib-dynload" |
| 34 | + |
| 35 | + |
| 36 | +# Don't ship large files / packages that are not particularly useful at |
| 37 | +# the moment. |
| 38 | +OMIT_FILES = ( |
| 39 | + # regression tests |
| 40 | + "test/", |
| 41 | + # user interfaces: TK, curses |
| 42 | + "curses/", |
| 43 | + "idlelib/", |
| 44 | + "tkinter/", |
| 45 | + "turtle.py", |
| 46 | + "turtledemo/", |
| 47 | + # package management |
| 48 | + "ensurepip/", |
| 49 | + "venv/", |
| 50 | + # build system |
| 51 | + "distutils/", |
| 52 | + "lib2to3/", |
| 53 | + # concurrency |
| 54 | + "concurrent/", |
| 55 | + "multiprocessing/", |
| 56 | + # deprecated |
| 57 | + "asyncore.py", |
| 58 | + "asynchat.py", |
| 59 | + # Synchronous network I/O and protocols are not supported; for example, |
| 60 | + # socket.create_connection() raises an exception: |
| 61 | + # "BlockingIOError: [Errno 26] Operation in progress". |
| 62 | + "cgi.py", |
| 63 | + "cgitb.py", |
| 64 | + "email/", |
| 65 | + "ftplib.py", |
| 66 | + "http/", |
| 67 | + "imaplib.py", |
| 68 | + "nntplib.py", |
| 69 | + "poplib.py", |
| 70 | + "smtpd.py", |
| 71 | + "smtplib.py", |
| 72 | + "socketserver.py", |
| 73 | + "telnetlib.py", |
| 74 | + "urllib/", |
| 75 | + "wsgiref/", |
| 76 | + "xmlrpc/", |
| 77 | + # dbm / gdbm |
| 78 | + "dbm/", |
| 79 | + # other platforms |
| 80 | + "_aix_support.py", |
| 81 | + "_bootsubprocess.py", |
| 82 | + "_osx_support.py", |
| 83 | + # webbrowser |
| 84 | + "antigravity.py", |
| 85 | + "webbrowser.py", |
| 86 | + # ctypes |
| 87 | + "ctypes/", |
| 88 | + # Pure Python implementations of C extensions |
| 89 | + "_pydecimal.py", |
| 90 | + "_pyio.py", |
| 91 | + # Misc unused or large files |
| 92 | + "pydoc_data/", |
| 93 | + "msilib/", |
| 94 | +) |
| 95 | + |
| 96 | +# regression test sub directories |
| 97 | +OMIT_SUBDIRS = ( |
| 98 | + "ctypes/test/", |
| 99 | + "tkinter/test/", |
| 100 | + "unittest/test/", |
| 101 | +) |
| 102 | + |
| 103 | + |
| 104 | +OMIT_ABSOLUTE = {SRCDIR_LIB / name for name in OMIT_FILES} |
| 105 | +OMIT_SUBDIRS_ABSOLUTE = tuple(str(SRCDIR_LIB / name) for name in OMIT_SUBDIRS) |
| 106 | + |
| 107 | + |
| 108 | +def filterfunc(name: str) -> bool: |
| 109 | + return not name.startswith(OMIT_SUBDIRS_ABSOLUTE) |
| 110 | + |
| 111 | + |
| 112 | +def create_stdlib_zip( |
| 113 | + args: argparse.Namespace, compression: int = zipfile.ZIP_DEFLATED, *, optimize: int = 0 |
| 114 | +) -> None: |
| 115 | + sysconfig_data = list(args.builddir.glob(SYSCONFIGDATA_GLOB)) |
| 116 | + if not sysconfig_data: |
| 117 | + raise ValueError("No sysconfigdata file found") |
| 118 | + |
| 119 | + with zipfile.PyZipFile( |
| 120 | + args.wasm_stdlib_zip, mode="w", compression=compression, optimize=0 |
| 121 | + ) as pzf: |
| 122 | + for entry in sorted(args.srcdir_lib.iterdir()): |
| 123 | + if entry.name == "__pycache__": |
| 124 | + continue |
| 125 | + if entry in OMIT_ABSOLUTE: |
| 126 | + continue |
| 127 | + if entry.name.endswith(".py") or entry.is_dir(): |
| 128 | + # writepy() writes .pyc files (bytecode). |
| 129 | + pzf.writepy(entry, filterfunc=filterfunc) |
| 130 | + for entry in sysconfig_data: |
| 131 | + pzf.writepy(entry) |
| 132 | + |
| 133 | + |
| 134 | +def path(val: str) -> pathlib.Path: |
| 135 | + return pathlib.Path(val).absolute() |
| 136 | + |
| 137 | + |
| 138 | +parser = argparse.ArgumentParser() |
| 139 | +parser.add_argument( |
| 140 | + "--builddir", |
| 141 | + help="absolute build directory", |
| 142 | + default=pathlib.Path(".").absolute(), |
| 143 | + type=path, |
| 144 | +) |
| 145 | +parser.add_argument( |
| 146 | + "--prefix", help="install prefix", default=pathlib.Path("/usr/local"), type=path |
| 147 | +) |
| 148 | + |
| 149 | + |
| 150 | +def main(): |
| 151 | + args = parser.parse_args() |
| 152 | + |
| 153 | + relative_prefix = args.prefix.relative_to(pathlib.Path("/")) |
| 154 | + args.srcdir = SRCDIR |
| 155 | + args.srcdir_lib = SRCDIR_LIB |
| 156 | + args.wasm_root = args.builddir / relative_prefix |
| 157 | + args.wasm_stdlib_zip = args.wasm_root / WASM_STDLIB_ZIP |
| 158 | + args.wasm_stdlib = args.wasm_root / WASM_STDLIB |
| 159 | + args.wasm_dynload = args.wasm_root / WASM_DYNLOAD |
| 160 | + |
| 161 | + # Empty, unused directory for dynamic libs, but required for site initialization. |
| 162 | + args.wasm_dynload.mkdir(parents=True, exist_ok=True) |
| 163 | + marker = args.wasm_dynload / ".empty" |
| 164 | + marker.touch() |
| 165 | + # os.py is a marker for finding the correct lib directory. |
| 166 | + shutil.copy(args.srcdir_lib / "os.py", args.wasm_stdlib) |
| 167 | + # The rest of stdlib that's useful in a WASM context. |
| 168 | + create_stdlib_zip(args) |
| 169 | + size = round(args.wasm_stdlib_zip.stat().st_size / 1024 ** 2, 2) |
| 170 | + parser.exit(0, f"Created {args.wasm_stdlib_zip} ({size} MiB)\n") |
| 171 | + |
| 172 | + |
| 173 | +if __name__ == "__main__": |
| 174 | + main() |
0 commit comments