| 1 |
# inno.py - Inno Setup functionality. |
| 2 |
# |
| 3 |
# Copyright 2019 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 |
|
| 15 |
import jinja2 |
| 16 |
|
| 17 |
from .pyoxidizer import create_pyoxidizer_install_layout |
| 18 |
from .util import ( |
| 19 |
normalize_windows_version, |
| 20 |
process_install_rules, |
| 21 |
read_version_py, |
| 22 |
) |
| 23 |
|
| 24 |
|
| 25 |
EXTRA_INSTALL_RULES = [ |
| 26 |
('contrib/win32/mercurial.ini', 'defaultrc/mercurial.rc'), |
| 27 |
] |
| 28 |
|
| 29 |
PACKAGE_FILES_METADATA = { |
| 30 |
'ReadMe.html': 'Flags: isreadme', |
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
def build_with_pyoxidizer( |
| 35 |
source_dir: pathlib.Path, |
| 36 |
build_dir: pathlib.Path, |
| 37 |
target_triple: str, |
| 38 |
iscc_exe: pathlib.Path, |
| 39 |
version=None, |
| 40 |
): |
| 41 |
"""Build the Inno installer using PyOxidizer.""" |
| 42 |
if not iscc_exe.exists(): |
| 43 |
raise Exception("%s does not exist" % iscc_exe) |
| 44 |
|
| 45 |
inno_build_dir = build_dir / ("inno-pyoxidizer-%s" % target_triple) |
| 46 |
staging_dir = inno_build_dir / "stage" |
| 47 |
|
| 48 |
inno_build_dir.mkdir(parents=True, exist_ok=True) |
| 49 |
create_pyoxidizer_install_layout( |
| 50 |
source_dir, inno_build_dir, staging_dir, target_triple |
| 51 |
) |
| 52 |
|
| 53 |
process_install_rules(EXTRA_INSTALL_RULES, source_dir, staging_dir) |
| 54 |
|
| 55 |
build_installer( |
| 56 |
source_dir, |
| 57 |
inno_build_dir, |
| 58 |
staging_dir, |
| 59 |
iscc_exe, |
| 60 |
version, |
| 61 |
arch="x64" if "x86_64" in target_triple else None, |
| 62 |
) |
| 63 |
|
| 64 |
|
| 65 |
def build_installer( |
| 66 |
source_dir: pathlib.Path, |
| 67 |
inno_build_dir: pathlib.Path, |
| 68 |
staging_dir: pathlib.Path, |
| 69 |
iscc_exe: pathlib.Path, |
| 70 |
version, |
| 71 |
arch=None, |
| 72 |
suffix="", |
| 73 |
): |
| 74 |
"""Build an Inno installer from staged Mercurial files. |
| 75 |
|
| 76 |
This function is agnostic about how to build Mercurial. It just |
| 77 |
cares that Mercurial files are in ``staging_dir``. |
| 78 |
""" |
| 79 |
inno_source_dir = source_dir / "contrib" / "packaging" / "inno" |
| 80 |
|
| 81 |
# The final package layout is simply a mirror of the staging directory. |
| 82 |
package_files = [] |
| 83 |
for root, dirs, files in os.walk(staging_dir): |
| 84 |
dirs.sort() |
| 85 |
|
| 86 |
root = pathlib.Path(root) |
| 87 |
|
| 88 |
for f in sorted(files): |
| 89 |
full = root / f |
| 90 |
rel = full.relative_to(staging_dir) |
| 91 |
if str(rel.parent) == '.': |
| 92 |
dest_dir = '{app}' |
| 93 |
else: |
| 94 |
dest_dir = '{app}\\%s' % rel.parent |
| 95 |
|
| 96 |
package_files.append( |
| 97 |
{ |
| 98 |
'source': rel, |
| 99 |
'dest_dir': dest_dir, |
| 100 |
'metadata': PACKAGE_FILES_METADATA.get(str(rel), None), |
| 101 |
} |
| 102 |
) |
| 103 |
|
| 104 |
print('creating installer') |
| 105 |
|
| 106 |
# Install Inno files by rendering a template. |
| 107 |
jinja_env = jinja2.Environment( |
| 108 |
loader=jinja2.FileSystemLoader(str(inno_source_dir)), |
| 109 |
# Need to change these to prevent conflict with Inno Setup. |
| 110 |
comment_start_string='{##', |
| 111 |
comment_end_string='##}', |
| 112 |
) |
| 113 |
|
| 114 |
try: |
| 115 |
template = jinja_env.get_template('mercurial.iss') |
| 116 |
except jinja2.TemplateSyntaxError as e: |
| 117 |
raise Exception( |
| 118 |
'template syntax error at %s:%d: %s' |
| 119 |
% ( |
| 120 |
e.name, |
| 121 |
e.lineno, |
| 122 |
e.message, |
| 123 |
) |
| 124 |
) |
| 125 |
|
| 126 |
content = template.render(package_files=package_files) |
| 127 |
|
| 128 |
with (inno_build_dir / 'mercurial.iss').open('w', encoding='utf-8') as fh: |
| 129 |
fh.write(content) |
| 130 |
|
| 131 |
# Copy additional files used by Inno. |
| 132 |
for p in ('mercurial.ico', 'postinstall.txt'): |
| 133 |
shutil.copyfile( |
| 134 |
source_dir / 'contrib' / 'win32' / p, inno_build_dir / p |
| 135 |
) |
| 136 |
|
| 137 |
args = [str(iscc_exe)] |
| 138 |
|
| 139 |
if arch: |
| 140 |
args.append('/dARCH=%s' % arch) |
| 141 |
args.append('/dSUFFIX=-%s%s' % (arch, suffix)) |
| 142 |
else: |
| 143 |
args.append('/dSUFFIX=-x86%s' % suffix) |
| 144 |
|
| 145 |
if not version: |
| 146 |
version = read_version_py(source_dir) |
| 147 |
|
| 148 |
args.append('/dVERSION=%s' % version) |
| 149 |
args.append('/dQUAD_VERSION=%s' % normalize_windows_version(version)) |
| 150 |
|
| 151 |
args.append('/Odist') |
| 152 |
args.append(str(inno_build_dir / 'mercurial.iss')) |
| 153 |
|
| 154 |
subprocess.run(args, cwd=str(source_dir), check=True) |