Victor Stinner | cb0b78a | 2018-12-07 12:57:43 +0100 | [diff] [blame] | 1 | import argparse |
| 2 | import py_compile |
| 3 | import re |
| 4 | import sys |
| 5 | import shutil |
| 6 | import stat |
| 7 | import os |
| 8 | import tempfile |
| 9 | |
| 10 | from itertools import chain |
| 11 | from pathlib import Path |
| 12 | from zipfile import ZipFile, ZIP_DEFLATED |
| 13 | |
| 14 | |
| 15 | TKTCL_RE = re.compile(r'^(_?tk|tcl).+\.(pyd|dll)', re.IGNORECASE) |
| 16 | DEBUG_RE = re.compile(r'_d\.(pyd|dll|exe|pdb|lib)$', re.IGNORECASE) |
| 17 | PYTHON_DLL_RE = re.compile(r'python\d\d?\.dll$', re.IGNORECASE) |
| 18 | |
| 19 | DEBUG_FILES = { |
| 20 | '_ctypes_test', |
| 21 | '_testbuffer', |
| 22 | '_testcapi', |
| 23 | '_testconsole', |
| 24 | '_testimportmultiple', |
| 25 | '_testmultiphase', |
| 26 | 'xxlimited', |
| 27 | 'python3_dstub', |
| 28 | } |
| 29 | |
| 30 | EXCLUDE_FROM_LIBRARY = { |
| 31 | '__pycache__', |
| 32 | 'idlelib', |
| 33 | 'pydoc_data', |
| 34 | 'site-packages', |
| 35 | 'tkinter', |
| 36 | 'turtledemo', |
| 37 | } |
| 38 | |
| 39 | EXCLUDE_FROM_EMBEDDABLE_LIBRARY = { |
| 40 | 'ensurepip', |
| 41 | 'venv', |
| 42 | } |
| 43 | |
| 44 | EXCLUDE_FILE_FROM_LIBRARY = { |
| 45 | 'bdist_wininst.py', |
| 46 | } |
| 47 | |
| 48 | EXCLUDE_FILE_FROM_LIBS = { |
| 49 | 'liblzma', |
| 50 | 'python3stub', |
| 51 | } |
| 52 | |
| 53 | EXCLUDED_FILES = { |
| 54 | 'pyshellext', |
| 55 | } |
| 56 | |
| 57 | def is_not_debug(p): |
| 58 | if DEBUG_RE.search(p.name): |
| 59 | return False |
| 60 | |
| 61 | if TKTCL_RE.search(p.name): |
| 62 | return False |
| 63 | |
| 64 | return p.stem.lower() not in DEBUG_FILES and p.stem.lower() not in EXCLUDED_FILES |
| 65 | |
| 66 | def is_not_debug_or_python(p): |
| 67 | return is_not_debug(p) and not PYTHON_DLL_RE.search(p.name) |
| 68 | |
| 69 | def include_in_lib(p): |
| 70 | name = p.name.lower() |
| 71 | if p.is_dir(): |
| 72 | if name in EXCLUDE_FROM_LIBRARY: |
| 73 | return False |
| 74 | if name == 'test' and p.parts[-2].lower() == 'lib': |
| 75 | return False |
| 76 | if name in {'test', 'tests'} and p.parts[-3].lower() == 'lib': |
| 77 | return False |
| 78 | return True |
| 79 | |
| 80 | if name in EXCLUDE_FILE_FROM_LIBRARY: |
| 81 | return False |
| 82 | |
| 83 | suffix = p.suffix.lower() |
| 84 | return suffix not in {'.pyc', '.pyo', '.exe'} |
| 85 | |
| 86 | def include_in_embeddable_lib(p): |
| 87 | if p.is_dir() and p.name.lower() in EXCLUDE_FROM_EMBEDDABLE_LIBRARY: |
| 88 | return False |
| 89 | |
| 90 | return include_in_lib(p) |
| 91 | |
| 92 | def include_in_libs(p): |
| 93 | if not is_not_debug(p): |
| 94 | return False |
| 95 | |
| 96 | return p.stem.lower() not in EXCLUDE_FILE_FROM_LIBS |
| 97 | |
| 98 | def include_in_tools(p): |
| 99 | if p.is_dir() and p.name.lower() in {'scripts', 'i18n', 'pynche', 'demo', 'parser'}: |
| 100 | return True |
| 101 | |
| 102 | return p.suffix.lower() in {'.py', '.pyw', '.txt'} |
| 103 | |
| 104 | BASE_NAME = 'python{0.major}{0.minor}'.format(sys.version_info) |
| 105 | |
| 106 | FULL_LAYOUT = [ |
| 107 | ('/', '$build', 'python.exe', is_not_debug), |
| 108 | ('/', '$build', 'pythonw.exe', is_not_debug), |
| 109 | ('/', '$build', 'python{}.dll'.format(sys.version_info.major), is_not_debug), |
| 110 | ('/', '$build', '{}.dll'.format(BASE_NAME), is_not_debug), |
| 111 | ('DLLs/', '$build', '*.pyd', is_not_debug), |
| 112 | ('DLLs/', '$build', '*.dll', is_not_debug_or_python), |
| 113 | ('include/', 'include', '*.h', None), |
| 114 | ('include/', 'PC', 'pyconfig.h', None), |
| 115 | ('Lib/', 'Lib', '**/*', include_in_lib), |
| 116 | ('libs/', '$build', '*.lib', include_in_libs), |
| 117 | ('Tools/', 'Tools', '**/*', include_in_tools), |
| 118 | ] |
| 119 | |
| 120 | EMBED_LAYOUT = [ |
| 121 | ('/', '$build', 'python*.exe', is_not_debug), |
| 122 | ('/', '$build', '*.pyd', is_not_debug), |
| 123 | ('/', '$build', '*.dll', is_not_debug), |
| 124 | ('{}.zip'.format(BASE_NAME), 'Lib', '**/*', include_in_embeddable_lib), |
| 125 | ] |
| 126 | |
| 127 | if os.getenv('DOC_FILENAME'): |
| 128 | FULL_LAYOUT.append(('Doc/', 'Doc/build/htmlhelp', os.getenv('DOC_FILENAME'), None)) |
| 129 | if os.getenv('VCREDIST_PATH'): |
| 130 | FULL_LAYOUT.append(('/', os.getenv('VCREDIST_PATH'), 'vcruntime*.dll', None)) |
| 131 | EMBED_LAYOUT.append(('/', os.getenv('VCREDIST_PATH'), 'vcruntime*.dll', None)) |
| 132 | |
| 133 | def copy_to_layout(target, rel_sources): |
| 134 | count = 0 |
| 135 | |
| 136 | if target.suffix.lower() == '.zip': |
| 137 | if target.exists(): |
| 138 | target.unlink() |
| 139 | |
| 140 | with ZipFile(str(target), 'w', ZIP_DEFLATED) as f: |
| 141 | with tempfile.TemporaryDirectory() as tmpdir: |
| 142 | for s, rel in rel_sources: |
| 143 | if rel.suffix.lower() == '.py': |
| 144 | pyc = Path(tmpdir) / rel.with_suffix('.pyc').name |
| 145 | try: |
| 146 | py_compile.compile(str(s), str(pyc), str(rel), doraise=True, optimize=2) |
| 147 | except py_compile.PyCompileError: |
| 148 | f.write(str(s), str(rel)) |
| 149 | else: |
| 150 | f.write(str(pyc), str(rel.with_suffix('.pyc'))) |
| 151 | else: |
| 152 | f.write(str(s), str(rel)) |
| 153 | count += 1 |
| 154 | |
| 155 | else: |
| 156 | for s, rel in rel_sources: |
| 157 | dest = target / rel |
| 158 | try: |
| 159 | dest.parent.mkdir(parents=True) |
| 160 | except FileExistsError: |
| 161 | pass |
| 162 | if dest.is_file(): |
| 163 | dest.chmod(stat.S_IWRITE) |
| 164 | shutil.copy(str(s), str(dest)) |
| 165 | if dest.is_file(): |
| 166 | dest.chmod(stat.S_IWRITE) |
| 167 | count += 1 |
| 168 | |
| 169 | return count |
| 170 | |
| 171 | def rglob(root, pattern, condition): |
| 172 | dirs = [root] |
| 173 | recurse = pattern[:3] in {'**/', '**\\'} |
| 174 | while dirs: |
| 175 | d = dirs.pop(0) |
| 176 | for f in d.glob(pattern[3:] if recurse else pattern): |
| 177 | if recurse and f.is_dir() and (not condition or condition(f)): |
| 178 | dirs.append(f) |
| 179 | elif f.is_file() and (not condition or condition(f)): |
| 180 | yield f, f.relative_to(root) |
| 181 | |
| 182 | def main(): |
| 183 | parser = argparse.ArgumentParser() |
| 184 | parser.add_argument('-s', '--source', metavar='dir', help='The directory containing the repository root', type=Path) |
| 185 | parser.add_argument('-o', '--out', metavar='file', help='The name of the output archive', type=Path, default=None) |
| 186 | parser.add_argument('-t', '--temp', metavar='dir', help='A directory to temporarily extract files into', type=Path, default=None) |
| 187 | parser.add_argument('-e', '--embed', help='Create an embedding layout', action='store_true', default=False) |
| 188 | parser.add_argument('-b', '--build', help='Specify the build directory', type=Path, default=None) |
| 189 | ns = parser.parse_args() |
| 190 | |
| 191 | source = ns.source or (Path(__file__).resolve().parent.parent.parent) |
| 192 | out = ns.out |
| 193 | build = ns.build or Path(sys.exec_prefix) |
| 194 | assert isinstance(source, Path) |
| 195 | assert not out or isinstance(out, Path) |
| 196 | assert isinstance(build, Path) |
| 197 | |
| 198 | if ns.temp: |
| 199 | temp = ns.temp |
| 200 | delete_temp = False |
| 201 | else: |
| 202 | temp = Path(tempfile.mkdtemp()) |
| 203 | delete_temp = True |
| 204 | |
| 205 | if out: |
| 206 | try: |
| 207 | out.parent.mkdir(parents=True) |
| 208 | except FileExistsError: |
| 209 | pass |
| 210 | try: |
| 211 | temp.mkdir(parents=True) |
| 212 | except FileExistsError: |
| 213 | pass |
| 214 | |
| 215 | layout = EMBED_LAYOUT if ns.embed else FULL_LAYOUT |
| 216 | |
| 217 | try: |
| 218 | for t, s, p, c in layout: |
| 219 | if s == '$build': |
| 220 | fs = build |
| 221 | else: |
| 222 | fs = source / s |
| 223 | files = rglob(fs, p, c) |
| 224 | extra_files = [] |
| 225 | if s == 'Lib' and p == '**/*': |
| 226 | extra_files.append(( |
| 227 | source / 'tools' / 'msi' / 'distutils.command.bdist_wininst.py', |
| 228 | Path('distutils') / 'command' / 'bdist_wininst.py' |
| 229 | )) |
| 230 | copied = copy_to_layout(temp / t.rstrip('/'), chain(files, extra_files)) |
| 231 | print('Copied {} files'.format(copied)) |
| 232 | |
| 233 | if ns.embed: |
| 234 | with open(str(temp / (BASE_NAME + '._pth')), 'w') as f: |
| 235 | print(BASE_NAME + '.zip', file=f) |
| 236 | print('.', file=f) |
| 237 | print('', file=f) |
| 238 | print('# Uncomment to run site.main() automatically', file=f) |
| 239 | print('#import site', file=f) |
| 240 | |
| 241 | if out: |
| 242 | total = copy_to_layout(out, rglob(temp, '**/*', None)) |
| 243 | print('Wrote {} files to {}'.format(total, out)) |
| 244 | finally: |
| 245 | if delete_temp: |
| 246 | shutil.rmtree(temp, True) |
| 247 | |
| 248 | |
| 249 | if __name__ == "__main__": |
| 250 | sys.exit(int(main() or 0)) |