| 
									
										
										
										
											2015-04-14 18:34:04 -04:00
										 |  |  | import argparse | 
					
						
							| 
									
										
										
										
											2015-08-04 16:02:40 -07:00
										 |  |  | import py_compile | 
					
						
							| 
									
										
										
										
											2015-04-14 18:34:04 -04:00
										 |  |  | import re | 
					
						
							|  |  |  | import sys | 
					
						
							|  |  |  | import shutil | 
					
						
							| 
									
										
										
										
											2015-09-09 19:32:45 -07:00
										 |  |  | import stat | 
					
						
							| 
									
										
										
										
											2015-04-14 18:34:04 -04:00
										 |  |  | import os | 
					
						
							|  |  |  | import tempfile | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | from pathlib import Path | 
					
						
							|  |  |  | from zipfile import ZipFile, ZIP_DEFLATED | 
					
						
							|  |  |  | import subprocess | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TKTCL_RE = re.compile(r'^(_?tk|tcl).+\.(pyd|dll)', re.IGNORECASE) | 
					
						
							| 
									
										
										
										
											2016-06-27 09:34:18 -07:00
										 |  |  | DEBUG_RE = re.compile(r'_d\.(pyd|dll|exe|pdb|lib)$', re.IGNORECASE) | 
					
						
							| 
									
										
										
										
											2015-04-14 18:34:04 -04:00
										 |  |  | PYTHON_DLL_RE = re.compile(r'python\d\d?\.dll$', re.IGNORECASE) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-27 09:34:18 -07:00
										 |  |  | DEBUG_FILES = { | 
					
						
							|  |  |  |     '_ctypes_test', | 
					
						
							|  |  |  |     '_testbuffer', | 
					
						
							|  |  |  |     '_testcapi', | 
					
						
							|  |  |  |     '_testimportmultiple', | 
					
						
							|  |  |  |     '_testmultiphase', | 
					
						
							|  |  |  |     'xxlimited', | 
					
						
							|  |  |  |     'python3_dstub', | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-22 15:03:54 -07:00
										 |  |  | EXCLUDE_FROM_LIBRARY = { | 
					
						
							|  |  |  |     '__pycache__', | 
					
						
							|  |  |  |     'ensurepip', | 
					
						
							|  |  |  |     'idlelib', | 
					
						
							|  |  |  |     'pydoc_data', | 
					
						
							|  |  |  |     'site-packages', | 
					
						
							|  |  |  |     'tkinter', | 
					
						
							|  |  |  |     'turtledemo', | 
					
						
							| 
									
										
										
										
											2016-01-16 13:44:43 -08:00
										 |  |  |     'venv', | 
					
						
							| 
									
										
										
										
											2015-09-22 15:03:54 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | EXCLUDE_FILE_FROM_LIBRARY = { | 
					
						
							|  |  |  |     'bdist_wininst.py', | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-27 09:34:18 -07:00
										 |  |  | EXCLUDE_FILE_FROM_LIBS = { | 
					
						
							|  |  |  |     'ssleay', | 
					
						
							|  |  |  |     'libeay', | 
					
						
							|  |  |  |     'python3stub', | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-09 09:17:35 -07:00
										 |  |  | EXCLUDED_FILES = { | 
					
						
							|  |  |  |     'pyshellext', | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-14 18:34:04 -04:00
										 |  |  | def is_not_debug(p): | 
					
						
							| 
									
										
										
										
											2015-05-02 15:32:14 -07:00
										 |  |  |     if DEBUG_RE.search(p.name): | 
					
						
							|  |  |  |         return False | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if TKTCL_RE.search(p.name): | 
					
						
							|  |  |  |         return False | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-09 09:17:35 -07:00
										 |  |  |     return p.stem.lower() not in DEBUG_FILES and p.stem.lower() not in EXCLUDED_FILES | 
					
						
							| 
									
										
										
										
											2015-04-14 18:34:04 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | def is_not_debug_or_python(p): | 
					
						
							|  |  |  |     return is_not_debug(p) and not PYTHON_DLL_RE.search(p.name) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def include_in_lib(p): | 
					
						
							|  |  |  |     name = p.name.lower() | 
					
						
							|  |  |  |     if p.is_dir(): | 
					
						
							| 
									
										
										
										
											2015-09-22 15:03:54 -07:00
										 |  |  |         if name in EXCLUDE_FROM_LIBRARY: | 
					
						
							| 
									
										
										
										
											2015-04-14 18:34:04 -04:00
										 |  |  |             return False | 
					
						
							|  |  |  |         if name == 'test' and p.parts[-2].lower() == 'lib': | 
					
						
							|  |  |  |             return False | 
					
						
							| 
									
										
										
										
											2015-09-22 15:03:54 -07:00
										 |  |  |         if name in {'test', 'tests'} and p.parts[-3].lower() == 'lib': | 
					
						
							|  |  |  |             return False | 
					
						
							| 
									
										
										
										
											2015-04-14 18:34:04 -04:00
										 |  |  |         return True | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-22 15:03:54 -07:00
										 |  |  |     if name in EXCLUDE_FILE_FROM_LIBRARY: | 
					
						
							|  |  |  |         return False | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-19 19:50:35 -07:00
										 |  |  |     suffix = p.suffix.lower() | 
					
						
							| 
									
										
										
										
											2015-09-22 15:03:54 -07:00
										 |  |  |     return suffix not in {'.pyc', '.pyo', '.exe'} | 
					
						
							| 
									
										
										
										
											2015-04-14 18:34:04 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-27 09:34:18 -07:00
										 |  |  | def include_in_libs(p): | 
					
						
							|  |  |  |     if not is_not_debug(p): | 
					
						
							|  |  |  |         return False | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return p.stem.lower() not in EXCLUDE_FILE_FROM_LIBS | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-14 18:34:04 -04:00
										 |  |  | def include_in_tools(p): | 
					
						
							|  |  |  |     if p.is_dir() and p.name.lower() in {'scripts', 'i18n', 'pynche', 'demo', 'parser'}: | 
					
						
							|  |  |  |         return True | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return p.suffix.lower() in {'.py', '.pyw', '.txt'} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | FULL_LAYOUT = [ | 
					
						
							| 
									
										
										
										
											2016-09-12 13:29:58 -07:00
										 |  |  |     ('/', 'PCBuild/$arch', 'python.exe', is_not_debug), | 
					
						
							|  |  |  |     ('/', 'PCBuild/$arch', 'pythonw.exe', is_not_debug), | 
					
						
							|  |  |  |     ('/', 'PCBuild/$arch', 'python{0.major}.dll'.format(sys.version_info), is_not_debug), | 
					
						
							|  |  |  |     ('/', 'PCBuild/$arch', 'python{0.major}{0.minor}.dll'.format(sys.version_info), is_not_debug), | 
					
						
							|  |  |  |     ('DLLs/', 'PCBuild/$arch', '*.pyd', is_not_debug), | 
					
						
							|  |  |  |     ('DLLs/', 'PCBuild/$arch', '*.dll', is_not_debug_or_python), | 
					
						
							| 
									
										
										
										
											2015-04-14 18:34:04 -04:00
										 |  |  |     ('include/', 'include', '*.h', None), | 
					
						
							|  |  |  |     ('include/', 'PC', 'pyconfig.h', None), | 
					
						
							|  |  |  |     ('Lib/', 'Lib', '**/*', include_in_lib), | 
					
						
							| 
									
										
										
										
											2016-09-12 13:29:58 -07:00
										 |  |  |     ('libs/', 'PCBuild/$arch', '*.lib', include_in_libs), | 
					
						
							| 
									
										
										
										
											2015-04-14 18:34:04 -04:00
										 |  |  |     ('Tools/', 'Tools', '**/*', include_in_tools), | 
					
						
							|  |  |  | ] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | EMBED_LAYOUT = [ | 
					
						
							| 
									
										
										
										
											2016-09-12 13:29:58 -07:00
										 |  |  |     ('/', 'PCBuild/$arch', 'python*.exe', is_not_debug), | 
					
						
							|  |  |  |     ('/', 'PCBuild/$arch', '*.pyd', is_not_debug), | 
					
						
							|  |  |  |     ('/', 'PCBuild/$arch', '*.dll', is_not_debug), | 
					
						
							| 
									
										
										
										
											2016-05-19 10:47:47 -07:00
										 |  |  |     ('python{0.major}{0.minor}.zip'.format(sys.version_info), 'Lib', '**/*', include_in_lib), | 
					
						
							| 
									
										
										
										
											2015-04-14 18:34:04 -04:00
										 |  |  | ] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-08 21:39:01 -07:00
										 |  |  | if os.getenv('DOC_FILENAME'): | 
					
						
							|  |  |  |     FULL_LAYOUT.append(('Doc/', 'Doc/build/htmlhelp', os.getenv('DOC_FILENAME'), None)) | 
					
						
							|  |  |  | if os.getenv('VCREDIST_PATH'): | 
					
						
							|  |  |  |     FULL_LAYOUT.append(('/', os.getenv('VCREDIST_PATH'), 'vcruntime*.dll', None)) | 
					
						
							|  |  |  |     EMBED_LAYOUT.append(('/', os.getenv('VCREDIST_PATH'), 'vcruntime*.dll', None)) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-02 21:38:26 -07:00
										 |  |  | def copy_to_layout(target, rel_sources): | 
					
						
							| 
									
										
										
										
											2015-04-14 18:34:04 -04:00
										 |  |  |     count = 0 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if target.suffix.lower() == '.zip': | 
					
						
							|  |  |  |         if target.exists(): | 
					
						
							|  |  |  |             target.unlink() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         with ZipFile(str(target), 'w', ZIP_DEFLATED) as f: | 
					
						
							| 
									
										
										
										
											2015-08-05 11:34:50 -07:00
										 |  |  |             with tempfile.TemporaryDirectory() as tmpdir: | 
					
						
							|  |  |  |                 for s, rel in rel_sources: | 
					
						
							|  |  |  |                     if rel.suffix.lower() == '.py': | 
					
						
							|  |  |  |                         pyc = Path(tmpdir) / rel.with_suffix('.pyc').name | 
					
						
							|  |  |  |                         try: | 
					
						
							|  |  |  |                             py_compile.compile(str(s), str(pyc), str(rel), doraise=True, optimize=2) | 
					
						
							|  |  |  |                         except py_compile.PyCompileError: | 
					
						
							|  |  |  |                             f.write(str(s), str(rel)) | 
					
						
							|  |  |  |                         else: | 
					
						
							|  |  |  |                             f.write(str(pyc), str(rel.with_suffix('.pyc'))) | 
					
						
							| 
									
										
										
										
											2015-08-04 16:02:40 -07:00
										 |  |  |                     else: | 
					
						
							| 
									
										
										
										
											2015-08-05 11:34:50 -07:00
										 |  |  |                         f.write(str(s), str(rel)) | 
					
						
							|  |  |  |                     count += 1 | 
					
						
							| 
									
										
										
										
											2015-04-14 18:34:04 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     else: | 
					
						
							|  |  |  |         for s, rel in rel_sources: | 
					
						
							| 
									
										
										
										
											2015-09-09 19:32:45 -07:00
										 |  |  |             dest = target / rel | 
					
						
							| 
									
										
										
										
											2015-04-14 18:34:04 -04:00
										 |  |  |             try: | 
					
						
							| 
									
										
										
										
											2015-09-09 19:32:45 -07:00
										 |  |  |                 dest.parent.mkdir(parents=True) | 
					
						
							| 
									
										
										
										
											2015-04-14 18:34:04 -04:00
										 |  |  |             except FileExistsError: | 
					
						
							|  |  |  |                 pass | 
					
						
							| 
									
										
										
										
											2015-09-09 19:32:45 -07:00
										 |  |  |             if dest.is_file(): | 
					
						
							|  |  |  |                 dest.chmod(stat.S_IWRITE) | 
					
						
							|  |  |  |             shutil.copy(str(s), str(dest)) | 
					
						
							|  |  |  |             if dest.is_file(): | 
					
						
							|  |  |  |                 dest.chmod(stat.S_IWRITE) | 
					
						
							| 
									
										
										
										
											2015-04-14 18:34:04 -04:00
										 |  |  |             count += 1 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return count | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def rglob(root, pattern, condition): | 
					
						
							|  |  |  |     dirs = [root] | 
					
						
							|  |  |  |     recurse = pattern[:3] in {'**/', '**\\'} | 
					
						
							|  |  |  |     while dirs: | 
					
						
							|  |  |  |         d = dirs.pop(0) | 
					
						
							|  |  |  |         for f in d.glob(pattern[3:] if recurse else pattern): | 
					
						
							|  |  |  |             if recurse and f.is_dir() and (not condition or condition(f)): | 
					
						
							|  |  |  |                 dirs.append(f) | 
					
						
							|  |  |  |             elif f.is_file() and (not condition or condition(f)): | 
					
						
							|  |  |  |                 yield f, f.relative_to(root) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def main(): | 
					
						
							|  |  |  |     parser = argparse.ArgumentParser() | 
					
						
							|  |  |  |     parser.add_argument('-s', '--source', metavar='dir', help='The directory containing the repository root', type=Path) | 
					
						
							| 
									
										
										
										
											2016-09-12 13:29:58 -07:00
										 |  |  |     parser.add_argument('-o', '--out', metavar='file', help='The name of the output self-extracting archive', type=Path, default=None) | 
					
						
							| 
									
										
										
										
											2015-04-14 18:34:04 -04:00
										 |  |  |     parser.add_argument('-t', '--temp', metavar='dir', help='A directory to temporarily extract files into', type=Path, default=None) | 
					
						
							|  |  |  |     parser.add_argument('-e', '--embed', help='Create an embedding layout', action='store_true', default=False) | 
					
						
							| 
									
										
										
										
											2016-09-12 13:29:58 -07:00
										 |  |  |     parser.add_argument('-a', '--arch', help='Specify the architecture to use (win32/amd64)', type=str, default="win32") | 
					
						
							| 
									
										
										
										
											2015-04-14 18:34:04 -04:00
										 |  |  |     ns = parser.parse_args() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:32:15 -07:00
										 |  |  |     source = ns.source or (Path(__file__).resolve().parent.parent.parent) | 
					
						
							| 
									
										
										
										
											2015-04-14 18:34:04 -04:00
										 |  |  |     out = ns.out | 
					
						
							| 
									
										
										
										
											2016-09-12 13:29:58 -07:00
										 |  |  |     arch = ns.arch | 
					
						
							| 
									
										
										
										
											2015-04-14 18:34:04 -04:00
										 |  |  |     assert isinstance(source, Path) | 
					
						
							| 
									
										
										
										
											2016-06-24 10:32:15 -07:00
										 |  |  |     assert not out or isinstance(out, Path) | 
					
						
							| 
									
										
										
										
											2016-09-12 13:29:58 -07:00
										 |  |  |     assert isinstance(arch, str) | 
					
						
							| 
									
										
										
										
											2015-04-14 18:34:04 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if ns.temp: | 
					
						
							|  |  |  |         temp = ns.temp | 
					
						
							|  |  |  |         delete_temp = False | 
					
						
							|  |  |  |     else: | 
					
						
							|  |  |  |         temp = Path(tempfile.mkdtemp()) | 
					
						
							|  |  |  |         delete_temp = True | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:32:15 -07:00
										 |  |  |     if out: | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             out.parent.mkdir(parents=True) | 
					
						
							|  |  |  |         except FileExistsError: | 
					
						
							|  |  |  |             pass | 
					
						
							| 
									
										
										
										
											2015-04-14 18:34:04 -04:00
										 |  |  |     try: | 
					
						
							|  |  |  |         temp.mkdir(parents=True) | 
					
						
							|  |  |  |     except FileExistsError: | 
					
						
							|  |  |  |         pass | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     layout = EMBED_LAYOUT if ns.embed else FULL_LAYOUT | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     try: | 
					
						
							|  |  |  |         for t, s, p, c in layout: | 
					
						
							| 
									
										
										
										
											2016-09-12 13:29:58 -07:00
										 |  |  |             s = source / s.replace("$arch", arch) | 
					
						
							| 
									
										
										
										
											2015-05-02 21:38:26 -07:00
										 |  |  |             copied = copy_to_layout(temp / t.rstrip('/'), rglob(s, p, c)) | 
					
						
							| 
									
										
										
										
											2015-04-14 18:34:04 -04:00
										 |  |  |             print('Copied {} files'.format(copied)) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-12 13:29:58 -07:00
										 |  |  |         if ns.embed: | 
					
						
							|  |  |  |             with open(str(temp / 'sys.path'), 'w') as f: | 
					
						
							|  |  |  |                 print('python{0.major}{0.minor}.zip'.format(sys.version_info), file=f) | 
					
						
							|  |  |  |                 print('.', file=f) | 
					
						
							| 
									
										
										
										
											2015-05-22 15:10:10 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:32:15 -07:00
										 |  |  |         if out: | 
					
						
							|  |  |  |             total = copy_to_layout(out, rglob(temp, '**/*', None)) | 
					
						
							|  |  |  |             print('Wrote {} files to {}'.format(total, out)) | 
					
						
							| 
									
										
										
										
											2015-04-14 18:34:04 -04:00
										 |  |  |     finally: | 
					
						
							|  |  |  |         if delete_temp: | 
					
						
							|  |  |  |             shutil.rmtree(temp, True) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if __name__ == "__main__": | 
					
						
							|  |  |  |     sys.exit(int(main() or 0)) |