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