2015-04-14 19:34:04 -03:00
|
|
|
import argparse
|
2015-08-04 20:02:40 -03:00
|
|
|
import py_compile
|
2015-04-14 19:34:04 -03:00
|
|
|
import re
|
|
|
|
import sys
|
|
|
|
import shutil
|
2015-09-09 23:32:45 -03:00
|
|
|
import stat
|
2015-04-14 19:34:04 -03: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 13:34:18 -03:00
|
|
|
DEBUG_RE = re.compile(r'_d\.(pyd|dll|exe|pdb|lib)$', re.IGNORECASE)
|
2015-04-14 19:34:04 -03:00
|
|
|
PYTHON_DLL_RE = re.compile(r'python\d\d?\.dll$', re.IGNORECASE)
|
|
|
|
|
2016-06-27 13:34:18 -03:00
|
|
|
DEBUG_FILES = {
|
|
|
|
'_ctypes_test',
|
|
|
|
'_testbuffer',
|
|
|
|
'_testcapi',
|
2016-10-29 13:23:39 -03:00
|
|
|
'_testconsole',
|
2016-06-27 13:34:18 -03:00
|
|
|
'_testimportmultiple',
|
|
|
|
'_testmultiphase',
|
|
|
|
'xxlimited',
|
|
|
|
'python3_dstub',
|
|
|
|
}
|
|
|
|
|
2015-09-22 19:03:54 -03:00
|
|
|
EXCLUDE_FROM_LIBRARY = {
|
|
|
|
'__pycache__',
|
|
|
|
'ensurepip',
|
|
|
|
'idlelib',
|
|
|
|
'pydoc_data',
|
|
|
|
'site-packages',
|
|
|
|
'tkinter',
|
|
|
|
'turtledemo',
|
2016-01-16 17:44:43 -04:00
|
|
|
'venv',
|
2015-09-22 19:03:54 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
EXCLUDE_FILE_FROM_LIBRARY = {
|
|
|
|
'bdist_wininst.py',
|
|
|
|
}
|
|
|
|
|
2016-06-27 13:34:18 -03:00
|
|
|
EXCLUDE_FILE_FROM_LIBS = {
|
|
|
|
'ssleay',
|
|
|
|
'libeay',
|
|
|
|
'python3stub',
|
|
|
|
}
|
|
|
|
|
2016-09-09 13:17:35 -03:00
|
|
|
EXCLUDED_FILES = {
|
|
|
|
'pyshellext',
|
|
|
|
}
|
|
|
|
|
2015-04-14 19:34:04 -03:00
|
|
|
def is_not_debug(p):
|
2015-05-02 19:32:14 -03:00
|
|
|
if DEBUG_RE.search(p.name):
|
|
|
|
return False
|
|
|
|
|
|
|
|
if TKTCL_RE.search(p.name):
|
|
|
|
return False
|
|
|
|
|
2016-09-09 13:17:35 -03:00
|
|
|
return p.stem.lower() not in DEBUG_FILES and p.stem.lower() not in EXCLUDED_FILES
|
2015-04-14 19:34:04 -03: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 19:03:54 -03:00
|
|
|
if name in EXCLUDE_FROM_LIBRARY:
|
2015-04-14 19:34:04 -03:00
|
|
|
return False
|
|
|
|
if name == 'test' and p.parts[-2].lower() == 'lib':
|
|
|
|
return False
|
2015-09-22 19:03:54 -03:00
|
|
|
if name in {'test', 'tests'} and p.parts[-3].lower() == 'lib':
|
|
|
|
return False
|
2015-04-14 19:34:04 -03:00
|
|
|
return True
|
|
|
|
|
2015-09-22 19:03:54 -03:00
|
|
|
if name in EXCLUDE_FILE_FROM_LIBRARY:
|
|
|
|
return False
|
|
|
|
|
2015-04-19 23:50:35 -03:00
|
|
|
suffix = p.suffix.lower()
|
2015-09-22 19:03:54 -03:00
|
|
|
return suffix not in {'.pyc', '.pyo', '.exe'}
|
2015-04-14 19:34:04 -03:00
|
|
|
|
2016-06-27 13:34:18 -03: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 19:34:04 -03: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'}
|
|
|
|
|
2016-09-17 16:54:06 -03:00
|
|
|
BASE_NAME = 'python{0.major}{0.minor}'.format(sys.version_info)
|
|
|
|
|
2015-04-14 19:34:04 -03:00
|
|
|
FULL_LAYOUT = [
|
2016-09-12 17:29:58 -03:00
|
|
|
('/', 'PCBuild/$arch', 'python.exe', is_not_debug),
|
|
|
|
('/', 'PCBuild/$arch', 'pythonw.exe', is_not_debug),
|
2016-09-17 16:54:06 -03:00
|
|
|
('/', 'PCBuild/$arch', 'python{}.dll'.format(sys.version_info.major), is_not_debug),
|
|
|
|
('/', 'PCBuild/$arch', '{}.dll'.format(BASE_NAME), is_not_debug),
|
2016-09-12 17:29:58 -03:00
|
|
|
('DLLs/', 'PCBuild/$arch', '*.pyd', is_not_debug),
|
|
|
|
('DLLs/', 'PCBuild/$arch', '*.dll', is_not_debug_or_python),
|
2015-04-14 19:34:04 -03:00
|
|
|
('include/', 'include', '*.h', None),
|
|
|
|
('include/', 'PC', 'pyconfig.h', None),
|
|
|
|
('Lib/', 'Lib', '**/*', include_in_lib),
|
2016-09-12 17:29:58 -03:00
|
|
|
('libs/', 'PCBuild/$arch', '*.lib', include_in_libs),
|
2015-04-14 19:34:04 -03:00
|
|
|
('Tools/', 'Tools', '**/*', include_in_tools),
|
|
|
|
]
|
|
|
|
|
|
|
|
EMBED_LAYOUT = [
|
2016-09-12 17:29:58 -03:00
|
|
|
('/', 'PCBuild/$arch', 'python*.exe', is_not_debug),
|
|
|
|
('/', 'PCBuild/$arch', '*.pyd', is_not_debug),
|
|
|
|
('/', 'PCBuild/$arch', '*.dll', is_not_debug),
|
2016-09-17 16:54:06 -03:00
|
|
|
('{}.zip'.format(BASE_NAME), 'Lib', '**/*', include_in_lib),
|
2015-04-14 19:34:04 -03:00
|
|
|
]
|
|
|
|
|
2015-09-09 01:39:01 -03: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-03 01:38:26 -03:00
|
|
|
def copy_to_layout(target, rel_sources):
|
2015-04-14 19:34:04 -03: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 15:34:50 -03: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 20:02:40 -03:00
|
|
|
else:
|
2015-08-05 15:34:50 -03:00
|
|
|
f.write(str(s), str(rel))
|
|
|
|
count += 1
|
2015-04-14 19:34:04 -03:00
|
|
|
|
|
|
|
else:
|
|
|
|
for s, rel in rel_sources:
|
2015-09-09 23:32:45 -03:00
|
|
|
dest = target / rel
|
2015-04-14 19:34:04 -03:00
|
|
|
try:
|
2015-09-09 23:32:45 -03:00
|
|
|
dest.parent.mkdir(parents=True)
|
2015-04-14 19:34:04 -03:00
|
|
|
except FileExistsError:
|
|
|
|
pass
|
2015-09-09 23:32:45 -03: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 19:34:04 -03: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 17:29:58 -03:00
|
|
|
parser.add_argument('-o', '--out', metavar='file', help='The name of the output self-extracting archive', type=Path, default=None)
|
2015-04-14 19:34:04 -03: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 17:29:58 -03:00
|
|
|
parser.add_argument('-a', '--arch', help='Specify the architecture to use (win32/amd64)', type=str, default="win32")
|
2015-04-14 19:34:04 -03:00
|
|
|
ns = parser.parse_args()
|
|
|
|
|
2016-06-24 14:32:15 -03:00
|
|
|
source = ns.source or (Path(__file__).resolve().parent.parent.parent)
|
2015-04-14 19:34:04 -03:00
|
|
|
out = ns.out
|
2016-09-12 17:29:58 -03:00
|
|
|
arch = ns.arch
|
2015-04-14 19:34:04 -03:00
|
|
|
assert isinstance(source, Path)
|
2016-06-24 14:32:15 -03:00
|
|
|
assert not out or isinstance(out, Path)
|
2016-09-12 17:29:58 -03:00
|
|
|
assert isinstance(arch, str)
|
2015-04-14 19:34:04 -03:00
|
|
|
|
|
|
|
if ns.temp:
|
|
|
|
temp = ns.temp
|
|
|
|
delete_temp = False
|
|
|
|
else:
|
|
|
|
temp = Path(tempfile.mkdtemp())
|
|
|
|
delete_temp = True
|
|
|
|
|
2016-06-24 14:32:15 -03:00
|
|
|
if out:
|
|
|
|
try:
|
|
|
|
out.parent.mkdir(parents=True)
|
|
|
|
except FileExistsError:
|
|
|
|
pass
|
2015-04-14 19:34:04 -03: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 17:29:58 -03:00
|
|
|
s = source / s.replace("$arch", arch)
|
2015-05-03 01:38:26 -03:00
|
|
|
copied = copy_to_layout(temp / t.rstrip('/'), rglob(s, p, c))
|
2015-04-14 19:34:04 -03:00
|
|
|
print('Copied {} files'.format(copied))
|
|
|
|
|
2016-09-12 17:29:58 -03:00
|
|
|
if ns.embed:
|
2016-09-17 16:54:06 -03:00
|
|
|
with open(str(temp / (BASE_NAME + '._pth')), 'w') as f:
|
|
|
|
print(BASE_NAME + '.zip', file=f)
|
2016-09-12 17:29:58 -03:00
|
|
|
print('.', file=f)
|
2016-09-17 16:54:06 -03:00
|
|
|
print('', file=f)
|
|
|
|
print('# Uncomment to run site.main() automatically', file=f)
|
|
|
|
print('#import site', file=f)
|
2015-05-22 19:10:10 -03:00
|
|
|
|
2016-06-24 14:32:15 -03:00
|
|
|
if out:
|
|
|
|
total = copy_to_layout(out, rglob(temp, '**/*', None))
|
|
|
|
print('Wrote {} files to {}'.format(total, out))
|
2015-04-14 19:34:04 -03:00
|
|
|
finally:
|
|
|
|
if delete_temp:
|
|
|
|
shutil.rmtree(temp, True)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
sys.exit(int(main() or 0))
|