blob: e9d6dbc6cc99f38af83ae503b77ad8d7efd1297f [file] [log] [blame]
Steve Dowerf70fdd22015-04-14 18:34:04 -04001import argparse
Steve Dower08b18172015-08-04 16:02:40 -07002import py_compile
Steve Dowerf70fdd22015-04-14 18:34:04 -04003import re
4import sys
5import shutil
Steve Dowerae69de62015-09-09 19:32:45 -07006import stat
Steve Dowerf70fdd22015-04-14 18:34:04 -04007import os
8import tempfile
9
10from pathlib import Path
11from zipfile import ZipFile, ZIP_DEFLATED
12import subprocess
13
14TKTCL_RE = re.compile(r'^(_?tk|tcl).+\.(pyd|dll)', re.IGNORECASE)
Steve Dower33128c82016-06-27 09:34:18 -070015DEBUG_RE = re.compile(r'_d\.(pyd|dll|exe|pdb|lib)$', re.IGNORECASE)
Steve Dowerf70fdd22015-04-14 18:34:04 -040016PYTHON_DLL_RE = re.compile(r'python\d\d?\.dll$', re.IGNORECASE)
17
Steve Dower33128c82016-06-27 09:34:18 -070018DEBUG_FILES = {
19 '_ctypes_test',
20 '_testbuffer',
21 '_testcapi',
22 '_testimportmultiple',
23 '_testmultiphase',
24 'xxlimited',
25 'python3_dstub',
26}
27
Steve Dower2495faf2015-09-22 15:03:54 -070028EXCLUDE_FROM_LIBRARY = {
29 '__pycache__',
30 'ensurepip',
31 'idlelib',
32 'pydoc_data',
33 'site-packages',
34 'tkinter',
35 'turtledemo',
Steve Dower10cabcb2016-01-16 13:44:43 -080036 'venv',
Steve Dower2495faf2015-09-22 15:03:54 -070037}
38
39EXCLUDE_FILE_FROM_LIBRARY = {
40 'bdist_wininst.py',
41}
42
Steve Dower33128c82016-06-27 09:34:18 -070043EXCLUDE_FILE_FROM_LIBS = {
44 'ssleay',
45 'libeay',
46 'python3stub',
47}
48
Steve Dower4db86bc2016-09-09 09:17:35 -070049EXCLUDED_FILES = {
50 'pyshellext',
51}
52
Steve Dowerf70fdd22015-04-14 18:34:04 -040053def is_not_debug(p):
Steve Dower6b4c63d2015-05-02 15:32:14 -070054 if DEBUG_RE.search(p.name):
55 return False
56
57 if TKTCL_RE.search(p.name):
58 return False
59
Steve Dower4db86bc2016-09-09 09:17:35 -070060 return p.stem.lower() not in DEBUG_FILES and p.stem.lower() not in EXCLUDED_FILES
Steve Dowerf70fdd22015-04-14 18:34:04 -040061
62def is_not_debug_or_python(p):
63 return is_not_debug(p) and not PYTHON_DLL_RE.search(p.name)
64
65def include_in_lib(p):
66 name = p.name.lower()
67 if p.is_dir():
Steve Dower2495faf2015-09-22 15:03:54 -070068 if name in EXCLUDE_FROM_LIBRARY:
Steve Dowerf70fdd22015-04-14 18:34:04 -040069 return False
Steve Dowerf70fdd22015-04-14 18:34:04 -040070 if name == 'test' and p.parts[-2].lower() == 'lib':
71 return False
Steve Dower2495faf2015-09-22 15:03:54 -070072 if name in {'test', 'tests'} and p.parts[-3].lower() == 'lib':
73 return False
Steve Dowerf70fdd22015-04-14 18:34:04 -040074 return True
75
Steve Dower2495faf2015-09-22 15:03:54 -070076 if name in EXCLUDE_FILE_FROM_LIBRARY:
77 return False
78
Steve Dower777af302015-04-19 19:50:35 -070079 suffix = p.suffix.lower()
Steve Dower2495faf2015-09-22 15:03:54 -070080 return suffix not in {'.pyc', '.pyo', '.exe'}
Steve Dowerf70fdd22015-04-14 18:34:04 -040081
Steve Dower33128c82016-06-27 09:34:18 -070082def include_in_libs(p):
83 if not is_not_debug(p):
84 return False
85
86 return p.stem.lower() not in EXCLUDE_FILE_FROM_LIBS
87
Steve Dowerf70fdd22015-04-14 18:34:04 -040088def include_in_tools(p):
89 if p.is_dir() and p.name.lower() in {'scripts', 'i18n', 'pynche', 'demo', 'parser'}:
90 return True
91
92 return p.suffix.lower() in {'.py', '.pyw', '.txt'}
93
94FULL_LAYOUT = [
Steve Dower41fca9d2016-09-12 13:29:58 -070095 ('/', 'PCBuild/$arch', 'python.exe', is_not_debug),
96 ('/', 'PCBuild/$arch', 'pythonw.exe', is_not_debug),
97 ('/', 'PCBuild/$arch', 'python{0.major}.dll'.format(sys.version_info), is_not_debug),
98 ('/', 'PCBuild/$arch', 'python{0.major}{0.minor}.dll'.format(sys.version_info), is_not_debug),
99 ('DLLs/', 'PCBuild/$arch', '*.pyd', is_not_debug),
100 ('DLLs/', 'PCBuild/$arch', '*.dll', is_not_debug_or_python),
Steve Dowerf70fdd22015-04-14 18:34:04 -0400101 ('include/', 'include', '*.h', None),
102 ('include/', 'PC', 'pyconfig.h', None),
103 ('Lib/', 'Lib', '**/*', include_in_lib),
Steve Dower41fca9d2016-09-12 13:29:58 -0700104 ('libs/', 'PCBuild/$arch', '*.lib', include_in_libs),
Steve Dowerf70fdd22015-04-14 18:34:04 -0400105 ('Tools/', 'Tools', '**/*', include_in_tools),
106]
107
Steve Dowerf70fdd22015-04-14 18:34:04 -0400108EMBED_LAYOUT = [
Steve Dower41fca9d2016-09-12 13:29:58 -0700109 ('/', 'PCBuild/$arch', 'python*.exe', is_not_debug),
110 ('/', 'PCBuild/$arch', '*.pyd', is_not_debug),
111 ('/', 'PCBuild/$arch', '*.dll', is_not_debug),
Steve Dowerd8bf09c2016-05-19 10:47:47 -0700112 ('python{0.major}{0.minor}.zip'.format(sys.version_info), 'Lib', '**/*', include_in_lib),
Steve Dowerf70fdd22015-04-14 18:34:04 -0400113]
114
Steve Dowerfcbe1df2015-09-08 21:39:01 -0700115if os.getenv('DOC_FILENAME'):
116 FULL_LAYOUT.append(('Doc/', 'Doc/build/htmlhelp', os.getenv('DOC_FILENAME'), None))
117if os.getenv('VCREDIST_PATH'):
118 FULL_LAYOUT.append(('/', os.getenv('VCREDIST_PATH'), 'vcruntime*.dll', None))
119 EMBED_LAYOUT.append(('/', os.getenv('VCREDIST_PATH'), 'vcruntime*.dll', None))
120
Steve Dower8c1cee92015-05-02 21:38:26 -0700121def copy_to_layout(target, rel_sources):
Steve Dowerf70fdd22015-04-14 18:34:04 -0400122 count = 0
123
124 if target.suffix.lower() == '.zip':
125 if target.exists():
126 target.unlink()
127
128 with ZipFile(str(target), 'w', ZIP_DEFLATED) as f:
Steve Dower315b7482015-08-05 11:34:50 -0700129 with tempfile.TemporaryDirectory() as tmpdir:
130 for s, rel in rel_sources:
131 if rel.suffix.lower() == '.py':
132 pyc = Path(tmpdir) / rel.with_suffix('.pyc').name
133 try:
134 py_compile.compile(str(s), str(pyc), str(rel), doraise=True, optimize=2)
135 except py_compile.PyCompileError:
136 f.write(str(s), str(rel))
137 else:
138 f.write(str(pyc), str(rel.with_suffix('.pyc')))
Steve Dower08b18172015-08-04 16:02:40 -0700139 else:
Steve Dower315b7482015-08-05 11:34:50 -0700140 f.write(str(s), str(rel))
141 count += 1
Steve Dowerf70fdd22015-04-14 18:34:04 -0400142
143 else:
144 for s, rel in rel_sources:
Steve Dowerae69de62015-09-09 19:32:45 -0700145 dest = target / rel
Steve Dowerf70fdd22015-04-14 18:34:04 -0400146 try:
Steve Dowerae69de62015-09-09 19:32:45 -0700147 dest.parent.mkdir(parents=True)
Steve Dowerf70fdd22015-04-14 18:34:04 -0400148 except FileExistsError:
149 pass
Steve Dowerae69de62015-09-09 19:32:45 -0700150 if dest.is_file():
151 dest.chmod(stat.S_IWRITE)
152 shutil.copy(str(s), str(dest))
153 if dest.is_file():
154 dest.chmod(stat.S_IWRITE)
Steve Dowerf70fdd22015-04-14 18:34:04 -0400155 count += 1
156
157 return count
158
159def rglob(root, pattern, condition):
160 dirs = [root]
161 recurse = pattern[:3] in {'**/', '**\\'}
162 while dirs:
163 d = dirs.pop(0)
164 for f in d.glob(pattern[3:] if recurse else pattern):
165 if recurse and f.is_dir() and (not condition or condition(f)):
166 dirs.append(f)
167 elif f.is_file() and (not condition or condition(f)):
168 yield f, f.relative_to(root)
169
170def main():
171 parser = argparse.ArgumentParser()
172 parser.add_argument('-s', '--source', metavar='dir', help='The directory containing the repository root', type=Path)
Steve Dower41fca9d2016-09-12 13:29:58 -0700173 parser.add_argument('-o', '--out', metavar='file', help='The name of the output self-extracting archive', type=Path, default=None)
Steve Dowerf70fdd22015-04-14 18:34:04 -0400174 parser.add_argument('-t', '--temp', metavar='dir', help='A directory to temporarily extract files into', type=Path, default=None)
175 parser.add_argument('-e', '--embed', help='Create an embedding layout', action='store_true', default=False)
Steve Dower41fca9d2016-09-12 13:29:58 -0700176 parser.add_argument('-a', '--arch', help='Specify the architecture to use (win32/amd64)', type=str, default="win32")
Steve Dowerf70fdd22015-04-14 18:34:04 -0400177 ns = parser.parse_args()
178
Steve Dower33f73102016-06-24 10:32:15 -0700179 source = ns.source or (Path(__file__).resolve().parent.parent.parent)
Steve Dowerf70fdd22015-04-14 18:34:04 -0400180 out = ns.out
Steve Dower41fca9d2016-09-12 13:29:58 -0700181 arch = ns.arch
Steve Dowerf70fdd22015-04-14 18:34:04 -0400182 assert isinstance(source, Path)
Steve Dower33f73102016-06-24 10:32:15 -0700183 assert not out or isinstance(out, Path)
Steve Dower41fca9d2016-09-12 13:29:58 -0700184 assert isinstance(arch, str)
Steve Dowerf70fdd22015-04-14 18:34:04 -0400185
186 if ns.temp:
187 temp = ns.temp
188 delete_temp = False
189 else:
190 temp = Path(tempfile.mkdtemp())
191 delete_temp = True
192
Steve Dower33f73102016-06-24 10:32:15 -0700193 if out:
194 try:
195 out.parent.mkdir(parents=True)
196 except FileExistsError:
197 pass
Steve Dowerf70fdd22015-04-14 18:34:04 -0400198 try:
199 temp.mkdir(parents=True)
200 except FileExistsError:
201 pass
202
203 layout = EMBED_LAYOUT if ns.embed else FULL_LAYOUT
204
205 try:
206 for t, s, p, c in layout:
Steve Dower41fca9d2016-09-12 13:29:58 -0700207 s = source / s.replace("$arch", arch)
Steve Dower8c1cee92015-05-02 21:38:26 -0700208 copied = copy_to_layout(temp / t.rstrip('/'), rglob(s, p, c))
Steve Dowerf70fdd22015-04-14 18:34:04 -0400209 print('Copied {} files'.format(copied))
210
Steve Dower41fca9d2016-09-12 13:29:58 -0700211 if ns.embed:
212 with open(str(temp / 'sys.path'), 'w') as f:
213 print('python{0.major}{0.minor}.zip'.format(sys.version_info), file=f)
214 print('.', file=f)
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700215
Steve Dower33f73102016-06-24 10:32:15 -0700216 if out:
217 total = copy_to_layout(out, rglob(temp, '**/*', None))
218 print('Wrote {} files to {}'.format(total, out))
Steve Dowerf70fdd22015-04-14 18:34:04 -0400219 finally:
220 if delete_temp:
221 shutil.rmtree(temp, True)
222
223
224if __name__ == "__main__":
225 sys.exit(int(main() or 0))