| 1 |
# pyoxidizer.py - Packaging support for PyOxidizer |
| 2 |
# |
| 3 |
# Copyright 2020 Gregory Szorc <[email protected]> |
| 4 |
# |
| 5 |
# This software may be used and distributed according to the terms of the |
| 6 |
# GNU General Public License version 2 or any later version. |
| 7 |
|
| 8 |
# no-check-code because Python 3 native. |
| 9 |
|
| 10 |
import os |
| 11 |
import pathlib |
| 12 |
import shutil |
| 13 |
import subprocess |
| 14 |
import sys |
| 15 |
import typing |
| 16 |
|
| 17 |
from .downloads import download_entry |
| 18 |
from .util import ( |
| 19 |
extract_zip_to_directory, |
| 20 |
process_install_rules, |
| 21 |
find_vc_runtime_dll, |
| 22 |
) |
| 23 |
|
| 24 |
|
| 25 |
STAGING_RULES_WINDOWS = [ |
| 26 |
('contrib/hgk', 'contrib/hgk.tcl'), |
| 27 |
('contrib/hgweb.fcgi', 'contrib/'), |
| 28 |
('contrib/hgweb.wsgi', 'contrib/'), |
| 29 |
('contrib/logo-droplets.svg', 'contrib/'), |
| 30 |
('contrib/mercurial.el', 'contrib/'), |
| 31 |
('contrib/mq.el', 'contrib/'), |
| 32 |
('contrib/tcsh_completion', 'contrib/'), |
| 33 |
('contrib/tcsh_completion_build.sh', 'contrib/'), |
| 34 |
('contrib/vim/*', 'contrib/vim/'), |
| 35 |
('contrib/win32/postinstall.txt', 'ReleaseNotes.txt'), |
| 36 |
('contrib/win32/ReadMe.html', 'ReadMe.html'), |
| 37 |
('contrib/xml.rnc', 'contrib/'), |
| 38 |
('doc/*.html', 'doc/'), |
| 39 |
('doc/style.css', 'doc/'), |
| 40 |
('COPYING', 'Copying.txt'), |
| 41 |
] |
| 42 |
|
| 43 |
STAGING_RULES_APP = [ |
| 44 |
('lib/mercurial/helptext/**/*.txt', 'helptext/'), |
| 45 |
('lib/mercurial/defaultrc/*.rc', 'defaultrc/'), |
| 46 |
('lib/mercurial/locale/**/*', 'locale/'), |
| 47 |
('lib/mercurial/templates/**/*', 'templates/'), |
| 48 |
] |
| 49 |
|
| 50 |
STAGING_EXCLUDES_WINDOWS = [ |
| 51 |
"doc/hg-ssh.8.html", |
| 52 |
] |
| 53 |
|
| 54 |
|
| 55 |
def build_docs_html(source_dir: pathlib.Path): |
| 56 |
"""Ensures HTML documentation is built. |
| 57 |
|
| 58 |
This will fail if docutils isn't available. |
| 59 |
|
| 60 |
(The HTML docs aren't built as part of `pip install` so we need to build them |
| 61 |
out of band.) |
| 62 |
""" |
| 63 |
subprocess.run( |
| 64 |
[sys.executable, str(source_dir / "setup.py"), "build_doc", "--html"], |
| 65 |
cwd=str(source_dir), |
| 66 |
check=True, |
| 67 |
) |
| 68 |
|
| 69 |
|
| 70 |
def run_pyoxidizer( |
| 71 |
source_dir: pathlib.Path, |
| 72 |
build_dir: pathlib.Path, |
| 73 |
target_triple: str, |
| 74 |
build_vars: typing.Optional[dict[str, str]] = None, |
| 75 |
target: typing.Optional[str] = None, |
| 76 |
) -> pathlib.Path: |
| 77 |
"""Run `pyoxidizer` in an environment with access to build dependencies. |
| 78 |
|
| 79 |
Returns the output directory that pyoxidizer would have used for build |
| 80 |
artifacts. Actual build artifacts are likely in a sub-directory with the |
| 81 |
name of the pyoxidizer build target that was built. |
| 82 |
""" |
| 83 |
build_vars = build_vars or {} |
| 84 |
|
| 85 |
# We need to make gettext binaries available for compiling i18n files. |
| 86 |
gettext_pkg, gettext_entry = download_entry('gettext', build_dir) |
| 87 |
gettext_dep_pkg = download_entry('gettext-dep', build_dir)[0] |
| 88 |
|
| 89 |
gettext_root = build_dir / ('gettext-win-%s' % gettext_entry['version']) |
| 90 |
|
| 91 |
if not gettext_root.exists(): |
| 92 |
extract_zip_to_directory(gettext_pkg, gettext_root) |
| 93 |
extract_zip_to_directory(gettext_dep_pkg, gettext_root) |
| 94 |
|
| 95 |
env = dict(os.environ) |
| 96 |
env["PATH"] = "%s%s%s" % ( |
| 97 |
env["PATH"], |
| 98 |
os.pathsep, |
| 99 |
str(gettext_root / "bin"), |
| 100 |
) |
| 101 |
|
| 102 |
args = [ |
| 103 |
"pyoxidizer", |
| 104 |
"build", |
| 105 |
"--path", |
| 106 |
str(source_dir / "rust" / "hgcli"), |
| 107 |
"--release", |
| 108 |
"--target-triple", |
| 109 |
target_triple, |
| 110 |
] |
| 111 |
|
| 112 |
for k, v in sorted(build_vars.items()): |
| 113 |
args.extend(["--var", k, v]) |
| 114 |
|
| 115 |
if target: |
| 116 |
args.append(target) |
| 117 |
|
| 118 |
subprocess.run(args, env=env, check=True) |
| 119 |
|
| 120 |
return source_dir / "build" / "pyoxidizer" / target_triple / "release" |
| 121 |
|
| 122 |
|
| 123 |
def create_pyoxidizer_install_layout( |
| 124 |
source_dir: pathlib.Path, |
| 125 |
build_dir: pathlib.Path, |
| 126 |
out_dir: pathlib.Path, |
| 127 |
target_triple: str, |
| 128 |
): |
| 129 |
"""Build Mercurial with PyOxidizer and copy additional files into place. |
| 130 |
|
| 131 |
After successful completion, ``out_dir`` contains files constituting a |
| 132 |
Mercurial install. |
| 133 |
""" |
| 134 |
|
| 135 |
run_pyoxidizer(source_dir, build_dir, target_triple) |
| 136 |
|
| 137 |
build_dir = ( |
| 138 |
source_dir / "build" / "pyoxidizer" / target_triple / "release" / "app" |
| 139 |
) |
| 140 |
|
| 141 |
if out_dir.exists(): |
| 142 |
print("purging %s" % out_dir) |
| 143 |
shutil.rmtree(out_dir) |
| 144 |
|
| 145 |
# Now assemble all the files from PyOxidizer into the staging directory. |
| 146 |
shutil.copytree(build_dir, out_dir) |
| 147 |
|
| 148 |
# Move some of those files around. We can get rid of this once Mercurial |
| 149 |
# is taught to use the importlib APIs for reading resources. |
| 150 |
process_install_rules(STAGING_RULES_APP, build_dir, out_dir) |
| 151 |
|
| 152 |
build_docs_html(source_dir) |
| 153 |
|
| 154 |
if "windows" in target_triple: |
| 155 |
process_install_rules(STAGING_RULES_WINDOWS, source_dir, out_dir) |
| 156 |
|
| 157 |
# Write out a default editor.rc file to configure notepad as the |
| 158 |
# default editor. |
| 159 |
os.makedirs(out_dir / "defaultrc", exist_ok=True) |
| 160 |
with (out_dir / "defaultrc" / "editor.rc").open( |
| 161 |
"w", encoding="utf-8" |
| 162 |
) as fh: |
| 163 |
fh.write("[ui]\neditor = notepad\n") |
| 164 |
|
| 165 |
for f in STAGING_EXCLUDES_WINDOWS: |
| 166 |
p = out_dir / f |
| 167 |
if p.exists(): |
| 168 |
print("removing %s" % p) |
| 169 |
p.unlink() |
| 170 |
|
| 171 |
# Add vcruntimeXXX.dll next to executable. |
| 172 |
vc_runtime_dll = find_vc_runtime_dll(x64="x86_64" in target_triple) |
| 173 |
shutil.copy(vc_runtime_dll, out_dir / vc_runtime_dll.name) |