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