py2exe.py
232 lines
| 7.3 KiB
| text/x-python
|
PythonLexer
|
r42081 | # py2exe.py - Functionality for performing py2exe builds. | ||
# | ||||
# Copyright 2019 Gregory Szorc <gregory.szorc@gmail.com> | ||||
# | ||||
# This software may be used and distributed according to the terms of the | ||||
# GNU General Public License version 2 or any later version. | ||||
# no-check-code because Python 3 native. | ||||
import os | ||||
import pathlib | ||||
import subprocess | ||||
|
r43346 | from .downloads import download_entry | ||
|
r42081 | from .util import ( | ||
extract_tar_to_directory, | ||||
extract_zip_to_directory, | ||||
|
r43916 | process_install_rules, | ||
|
r42081 | python_exe_info, | ||
) | ||||
|
r43916 | STAGING_RULES = [ | ||
('contrib/bash_completion', 'Contrib/'), | ||||
('contrib/hgk', 'Contrib/hgk.tcl'), | ||||
('contrib/hgweb.fcgi', 'Contrib/'), | ||||
('contrib/hgweb.wsgi', 'Contrib/'), | ||||
|
r43920 | ('contrib/logo-droplets.svg', 'Contrib/'), | ||
|
r43916 | ('contrib/mercurial.el', 'Contrib/'), | ||
('contrib/mq.el', 'Contrib/'), | ||||
('contrib/tcsh_completion', 'Contrib/'), | ||||
('contrib/tcsh_completion_build.sh', 'Contrib/'), | ||||
('contrib/vim/*', 'Contrib/Vim/'), | ||||
('contrib/win32/postinstall.txt', 'ReleaseNotes.txt'), | ||||
('contrib/win32/ReadMe.html', 'ReadMe.html'), | ||||
('contrib/xml.rnc', 'Contrib/'), | ||||
('contrib/zsh_completion', 'Contrib/'), | ||||
('dist/hg.exe', './'), | ||||
('dist/lib/*.dll', 'lib/'), | ||||
('dist/lib/*.pyd', 'lib/'), | ||||
('dist/lib/library.zip', 'lib/'), | ||||
('dist/Microsoft.VC*.CRT.manifest', './'), | ||||
('dist/msvc*.dll', './'), | ||||
('dist/python*.dll', './'), | ||||
|
r43919 | ('doc/*.html', 'doc/'), | ||
('doc/style.css', 'doc/'), | ||||
|
r43916 | ('mercurial/help/**/*.txt', 'help/'), | ||
|
r44004 | ('mercurial/default.d/*.rc', 'hgrc.d/'), | ||
|
r43916 | ('mercurial/locale/**/*', 'locale/'), | ||
('mercurial/templates/**/*', 'Templates/'), | ||||
('COPYING', 'Copying.txt'), | ||||
] | ||||
|
r44005 | # List of paths to exclude from the staging area. | ||
STAGING_EXCLUDES = [ | ||||
'doc/hg-ssh.8.html', | ||||
] | ||||
|
r43916 | |||
|
r43346 | def build_py2exe( | ||
source_dir: pathlib.Path, | ||||
build_dir: pathlib.Path, | ||||
python_exe: pathlib.Path, | ||||
build_name: str, | ||||
venv_requirements_txt: pathlib.Path, | ||||
extra_packages=None, | ||||
extra_excludes=None, | ||||
extra_dll_excludes=None, | ||||
extra_packages_script=None, | ||||
): | ||||
|
r42081 | """Build Mercurial with py2exe. | ||
Build files will be placed in ``build_dir``. | ||||
py2exe's setup.py doesn't use setuptools. It doesn't have modern logic | ||||
for finding the Python 2.7 toolchain. So, we require the environment | ||||
to already be configured with an active toolchain. | ||||
""" | ||||
if 'VCINSTALLDIR' not in os.environ: | ||||
|
r43346 | raise Exception( | ||
'not running from a Visual C++ build environment; ' | ||||
'execute the "Visual C++ <version> Command Prompt" ' | ||||
'application shortcut or a vcsvarsall.bat file' | ||||
) | ||||
|
r42081 | |||
# Identity x86/x64 and validate the environment matches the Python | ||||
# architecture. | ||||
vc_x64 = r'\x64' in os.environ['LIB'] | ||||
py_info = python_exe_info(python_exe) | ||||
if vc_x64: | ||||
if py_info['arch'] != '64bit': | ||||
|
r43346 | raise Exception( | ||
'architecture mismatch: Visual C++ environment ' | ||||
'is configured for 64-bit but Python is 32-bit' | ||||
) | ||||
|
r42081 | else: | ||
if py_info['arch'] != '32bit': | ||||
|
r43346 | raise Exception( | ||
'architecture mismatch: Visual C++ environment ' | ||||
'is configured for 32-bit but Python is 64-bit' | ||||
) | ||||
|
r42081 | |||
if py_info['py3']: | ||||
raise Exception('Only Python 2 is currently supported') | ||||
build_dir.mkdir(exist_ok=True) | ||||
gettext_pkg, gettext_entry = download_entry('gettext', build_dir) | ||||
gettext_dep_pkg = download_entry('gettext-dep', build_dir)[0] | ||||
virtualenv_pkg, virtualenv_entry = download_entry('virtualenv', build_dir) | ||||
py2exe_pkg, py2exe_entry = download_entry('py2exe', build_dir) | ||||
|
r43346 | venv_path = build_dir / ( | ||
'venv-%s-%s' % (build_name, 'x64' if vc_x64 else 'x86') | ||||
) | ||||
|
r42081 | |||
|
r43346 | gettext_root = build_dir / ('gettext-win-%s' % gettext_entry['version']) | ||
|
r42081 | |||
if not gettext_root.exists(): | ||||
extract_zip_to_directory(gettext_pkg, gettext_root) | ||||
extract_zip_to_directory(gettext_dep_pkg, gettext_root) | ||||
# This assumes Python 2. We don't need virtualenv on Python 3. | ||||
virtualenv_src_path = build_dir / ( | ||||
|
r43346 | 'virtualenv-%s' % virtualenv_entry['version'] | ||
) | ||||
|
r42081 | virtualenv_py = virtualenv_src_path / 'virtualenv.py' | ||
if not virtualenv_src_path.exists(): | ||||
extract_tar_to_directory(virtualenv_pkg, build_dir) | ||||
py2exe_source_path = build_dir / ('py2exe-%s' % py2exe_entry['version']) | ||||
if not py2exe_source_path.exists(): | ||||
extract_zip_to_directory(py2exe_pkg, build_dir) | ||||
if not venv_path.exists(): | ||||
print('creating virtualenv with dependencies') | ||||
subprocess.run( | ||||
|
r43346 | [str(python_exe), str(virtualenv_py), str(venv_path)], check=True | ||
) | ||||
|
r42081 | |||
venv_python = venv_path / 'Scripts' / 'python.exe' | ||||
venv_pip = venv_path / 'Scripts' / 'pip.exe' | ||||
|
r43346 | subprocess.run( | ||
[str(venv_pip), 'install', '-r', str(venv_requirements_txt)], check=True | ||||
) | ||||
|
r42081 | |||
# Force distutils to use VC++ settings from environment, which was | ||||
# validated above. | ||||
env = dict(os.environ) | ||||
env['DISTUTILS_USE_SDK'] = '1' | ||||
env['MSSdk'] = '1' | ||||
|
r42214 | if extra_packages_script: | ||
|
r43346 | more_packages = set( | ||
subprocess.check_output(extra_packages_script, cwd=build_dir) | ||||
.split(b'\0')[-1] | ||||
.strip() | ||||
.decode('utf-8') | ||||
.splitlines() | ||||
) | ||||
|
r42214 | if more_packages: | ||
if not extra_packages: | ||||
extra_packages = more_packages | ||||
else: | ||||
extra_packages |= more_packages | ||||
|
r42082 | if extra_packages: | ||
env['HG_PY2EXE_EXTRA_PACKAGES'] = ' '.join(sorted(extra_packages)) | ||||
|
r42221 | hgext3rd_extras = sorted( | ||
|
r43346 | e for e in extra_packages if e.startswith('hgext3rd.') | ||
) | ||||
|
r42221 | if hgext3rd_extras: | ||
env['HG_PY2EXE_EXTRA_INSTALL_PACKAGES'] = ' '.join(hgext3rd_extras) | ||||
|
r42082 | if extra_excludes: | ||
env['HG_PY2EXE_EXTRA_EXCLUDES'] = ' '.join(sorted(extra_excludes)) | ||||
if extra_dll_excludes: | ||||
env['HG_PY2EXE_EXTRA_DLL_EXCLUDES'] = ' '.join( | ||||
|
r43346 | sorted(extra_dll_excludes) | ||
) | ||||
|
r42082 | |||
|
r42081 | py2exe_py_path = venv_path / 'Lib' / 'site-packages' / 'py2exe' | ||
if not py2exe_py_path.exists(): | ||||
print('building py2exe') | ||||
|
r43346 | subprocess.run( | ||
[str(venv_python), 'setup.py', 'install'], | ||||
cwd=py2exe_source_path, | ||||
env=env, | ||||
check=True, | ||||
) | ||||
|
r42081 | |||
# Register location of msgfmt and other binaries. | ||||
env['PATH'] = '%s%s%s' % ( | ||||
|
r43346 | env['PATH'], | ||
os.pathsep, | ||||
str(gettext_root / 'bin'), | ||||
) | ||||
|
r42081 | |||
print('building Mercurial') | ||||
subprocess.run( | ||||
|
r43346 | [str(venv_python), 'setup.py', 'py2exe', 'build_doc', '--html'], | ||
|
r42081 | cwd=str(source_dir), | ||
env=env, | ||||
|
r43346 | check=True, | ||
) | ||||
|
r43916 | |||
def stage_install(source_dir: pathlib.Path, staging_dir: pathlib.Path): | ||||
"""Copy all files to be installed to a directory. | ||||
This allows packaging to simply walk a directory tree to find source | ||||
files. | ||||
""" | ||||
process_install_rules(STAGING_RULES, source_dir, staging_dir) | ||||
|
r44005 | |||
|
r44007 | # Write out a default editor.rc file to configure notepad as the | ||
# default editor. | ||||
with (staging_dir / 'hgrc.d' / 'editor.rc').open( | ||||
'w', encoding='utf-8' | ||||
) as fh: | ||||
fh.write('[ui]\neditor = notepad\n') | ||||
|
r44005 | # Purge any files we don't want to be there. | ||
for f in STAGING_EXCLUDES: | ||||
p = staging_dir / f | ||||
if p.exists(): | ||||
print('removing %s' % p) | ||||
p.unlink() | ||||