Mercurial > hg
comparison contrib/packaging/hgpackaging/wix.py @ 41921:4371f543efda
wix: functionality to automate building WiX installers
Like we did for Inno Setup, we want to make it easier to
produce WiX installers. This commit does that.
We introduce a new hgpackaging.wix module for performing
all the high-level tasks required to produce WiX installers.
This required miscellaneous enhancements to existing code in
hgpackaging, including support for signing binaries.
A new build.py script for calling into the module APIs has been
created. It behaves very similarly to the Inno Setup build.py
script.
Unlike Inno Setup, we didn't have code in the repo previously
to generate WiX installers. It appears that all existing
automation for building WiX installers lives in the
https://bitbucket.org/tortoisehg/thg-winbuild repository - most
notably in its setup.py file. My strategy for inventing the
code in this commit was to step through the code in that repo's
setup.py and observe what it was doing. Despite the length of
setup.py in that repository, the actual amount of steps required
to produce a WiX installer is actually quite low. It consists
of a basic py2exe build plus invocations of candle.exe and
light.exe to produce the MSI.
One rabbit hole that gave me fits was locating the Visual Studio
9 C Runtime merge modules. These merge modules are only present
on your system if you have a full Visual Studio 2008 installation.
Fortunately, I have a copy of Visual Studio 2008 and was able
to install all the required updates. I then uploaded these merge
modules to a personal repository on GitHub. That is where the
added code references them from. We probably don't need to
ship the merge modules. But that is for another day.
The installs from the MSIs produced with the new automation
differ from the last official MSI in the following ways:
* Our HTML manual pages have UNIX line endings instead of Windows.
* We ship modules in the mercurial.pure package. It appears the
upstream packaging code is not including this package due to
omission (they supply an explicit list of packages that has
drifted out of sync with our setup.py).
* We do not ship various distutils.* modules. This is because
virtualenvs have a custom distutils/__init__.py that automagically
imports distutils from its original location and py2exe gets
confused by this. We don't use distutils in core Mercurial and
don't provide a usable python.exe, so this omission should be
acceptable.
* The version of the enum package is different and we ship
an enum.pyc instead of an enum/__init__.py.
* The version of the docutils package is different and we
ship a different set of files.
* The version of Sphinx is drastically newer and we ship a
number of files the old version did not. (I'm not sure why
we ship Sphinx - I think it is a side-effect of the way the
THG code was installing dependencies.)
* We ship the idna package (dependent of requests which is a
dependency of newer versions of Sphinx).
* The version of imagesize is different and we ship an
imagesize.pyc instead of an imagesize/__init__.pyc.
* The version of the jinja2 package is different and the sets
of files differs.
* We ship the packaging package, which is a dependency for Sphinx.
* The version of the pygments package is different and the sets
of files differs.
* We ship the requests package, which is a dependency for Sphinx.
* We ship the snowballstemmer package, which is a dependency for
Sphinx.
* We ship the urllib3 package, which is a dependency for requests,
which is a dependency for Sphinx.
* We ship a newer version of the futures package, which includes a
handful of extra modules that match Python 3 module names.
# no-check-commit because foo_bar naming
Differential Revision: https://phab.mercurial-scm.org/D6097
author | Gregory Szorc <gregory.szorc@gmail.com> |
---|---|
date | Fri, 08 Mar 2019 10:48:22 -0800 |
parents | |
children | c569f769c41d |
comparison
equal
deleted
inserted
replaced
41920:c68a1df5c79a | 41921:4371f543efda |
---|---|
1 # wix.py - WiX installer functionality | |
2 # | |
3 # Copyright 2019 Gregory Szorc <gregory.szorc@gmail.com> | |
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 re | |
13 import subprocess | |
14 | |
15 from .downloads import ( | |
16 download_entry, | |
17 ) | |
18 from .py2exe import ( | |
19 build_py2exe, | |
20 ) | |
21 from .util import ( | |
22 extract_zip_to_directory, | |
23 sign_with_signtool, | |
24 ) | |
25 | |
26 | |
27 SUPPORT_WXS = [ | |
28 ('contrib.wxs', r'contrib'), | |
29 ('dist.wxs', r'dist'), | |
30 ('doc.wxs', r'doc'), | |
31 ('help.wxs', r'mercurial\help'), | |
32 ('i18n.wxs', r'i18n'), | |
33 ('locale.wxs', r'mercurial\locale'), | |
34 ('templates.wxs', r'mercurial\templates'), | |
35 ] | |
36 | |
37 | |
38 EXTRA_PACKAGES = { | |
39 'distutils', | |
40 'enum', | |
41 'imagesize', | |
42 'pygments', | |
43 'sphinx', | |
44 } | |
45 | |
46 | |
47 EXCLUDES = { | |
48 # Python 3 only. | |
49 'jinja2.asyncsupport', | |
50 } | |
51 | |
52 | |
53 def find_version(source_dir: pathlib.Path): | |
54 version_py = source_dir / 'mercurial' / '__version__.py' | |
55 | |
56 with version_py.open('r', encoding='utf-8') as fh: | |
57 source = fh.read().strip() | |
58 | |
59 m = re.search('version = b"(.*)"', source) | |
60 return m.group(1) | |
61 | |
62 | |
63 def normalize_version(version): | |
64 """Normalize Mercurial version string so WiX accepts it. | |
65 | |
66 Version strings have to be numeric X.Y.Z. | |
67 """ | |
68 | |
69 if '+' in version: | |
70 version, extra = version.split('+', 1) | |
71 else: | |
72 extra = None | |
73 | |
74 # 4.9rc0 | |
75 if version[:-1].endswith('rc'): | |
76 version = version[:-3] | |
77 | |
78 versions = [int(v) for v in version.split('.')] | |
79 while len(versions) < 3: | |
80 versions.append(0) | |
81 | |
82 major, minor, build = versions[:3] | |
83 | |
84 if extra: | |
85 # <commit count>-<hash>+<date> | |
86 build = int(extra.split('-')[0]) | |
87 | |
88 return '.'.join('%d' % x for x in (major, minor, build)) | |
89 | |
90 | |
91 def ensure_vc90_merge_modules(build_dir): | |
92 x86 = ( | |
93 download_entry('vc9-crt-x86-msm', build_dir, | |
94 local_name='microsoft.vcxx.crt.x86_msm.msm')[0], | |
95 download_entry('vc9-crt-x86-msm-policy', build_dir, | |
96 local_name='policy.x.xx.microsoft.vcxx.crt.x86_msm.msm')[0] | |
97 ) | |
98 | |
99 x64 = ( | |
100 download_entry('vc9-crt-x64-msm', build_dir, | |
101 local_name='microsoft.vcxx.crt.x64_msm.msm')[0], | |
102 download_entry('vc9-crt-x64-msm-policy', build_dir, | |
103 local_name='policy.x.xx.microsoft.vcxx.crt.x64_msm.msm')[0] | |
104 ) | |
105 return { | |
106 'x86': x86, | |
107 'x64': x64, | |
108 } | |
109 | |
110 | |
111 def run_candle(wix, cwd, wxs, source_dir, defines=None): | |
112 args = [ | |
113 str(wix / 'candle.exe'), | |
114 '-nologo', | |
115 str(wxs), | |
116 '-dSourceDir=%s' % source_dir, | |
117 ] | |
118 | |
119 if defines: | |
120 args.extend('-d%s=%s' % define for define in sorted(defines.items())) | |
121 | |
122 subprocess.run(args, cwd=str(cwd), check=True) | |
123 | |
124 | |
125 def make_post_build_signing_fn(name, subject_name=None, cert_path=None, | |
126 cert_password=None, timestamp_url=None): | |
127 """Create a callable that will use signtool to sign hg.exe.""" | |
128 | |
129 def post_build_sign(source_dir, build_dir, dist_dir, version): | |
130 description = '%s %s' % (name, version) | |
131 | |
132 sign_with_signtool(dist_dir / 'hg.exe', description, | |
133 subject_name=subject_name, cert_path=cert_path, | |
134 cert_password=cert_password, | |
135 timestamp_url=timestamp_url) | |
136 | |
137 return post_build_sign | |
138 | |
139 | |
140 def build_installer(source_dir: pathlib.Path, python_exe: pathlib.Path, | |
141 msi_name='mercurial', version=None, post_build_fn=None): | |
142 """Build a WiX MSI installer. | |
143 | |
144 ``source_dir`` is the path to the Mercurial source tree to use. | |
145 ``arch`` is the target architecture. either ``x86`` or ``x64``. | |
146 ``python_exe`` is the path to the Python executable to use/bundle. | |
147 ``version`` is the Mercurial version string. If not defined, | |
148 ``mercurial/__version__.py`` will be consulted. | |
149 ``post_build_fn`` is a callable that will be called after building | |
150 Mercurial but before invoking WiX. It can be used to e.g. facilitate | |
151 signing. It is passed the paths to the Mercurial source, build, and | |
152 dist directories and the resolved Mercurial version. | |
153 """ | |
154 arch = 'x64' if r'\x64' in os.environ.get('LIB', '') else 'x86' | |
155 | |
156 hg_build_dir = source_dir / 'build' | |
157 dist_dir = source_dir / 'dist' | |
158 | |
159 requirements_txt = (source_dir / 'contrib' / 'packaging' / | |
160 'wix' / 'requirements.txt') | |
161 | |
162 build_py2exe(source_dir, hg_build_dir, | |
163 python_exe, 'wix', requirements_txt, | |
164 extra_packages=EXTRA_PACKAGES, extra_excludes=EXCLUDES) | |
165 | |
166 version = version or normalize_version(find_version(source_dir)) | |
167 print('using version string: %s' % version) | |
168 | |
169 if post_build_fn: | |
170 post_build_fn(source_dir, hg_build_dir, dist_dir, version) | |
171 | |
172 build_dir = hg_build_dir / ('wix-%s' % arch) | |
173 | |
174 build_dir.mkdir(exist_ok=True) | |
175 | |
176 wix_pkg, wix_entry = download_entry('wix', hg_build_dir) | |
177 wix_path = hg_build_dir / ('wix-%s' % wix_entry['version']) | |
178 | |
179 if not wix_path.exists(): | |
180 extract_zip_to_directory(wix_pkg, wix_path) | |
181 | |
182 ensure_vc90_merge_modules(hg_build_dir) | |
183 | |
184 source_build_rel = pathlib.Path(os.path.relpath(source_dir, build_dir)) | |
185 | |
186 defines = {'Platform': arch} | |
187 | |
188 for wxs, rel_path in SUPPORT_WXS: | |
189 wxs = source_dir / 'contrib' / 'packaging' / 'wix' / wxs | |
190 wxs_source_dir = source_dir / rel_path | |
191 run_candle(wix_path, build_dir, wxs, wxs_source_dir, defines=defines) | |
192 | |
193 source = source_dir / 'contrib' / 'packaging' / 'wix' / 'mercurial.wxs' | |
194 defines['Version'] = version | |
195 defines['Comments'] = 'Installs Mercurial version %s' % version | |
196 defines['VCRedistSrcDir'] = str(hg_build_dir) | |
197 | |
198 run_candle(wix_path, build_dir, source, source_build_rel, defines=defines) | |
199 | |
200 msi_path = source_dir / 'dist' / ( | |
201 '%s-%s-%s.msi' % (msi_name, version, arch)) | |
202 | |
203 args = [ | |
204 str(wix_path / 'light.exe'), | |
205 '-nologo', | |
206 '-ext', 'WixUIExtension', | |
207 '-sw1076', | |
208 '-spdb', | |
209 '-o', str(msi_path), | |
210 ] | |
211 | |
212 for source, rel_path in SUPPORT_WXS: | |
213 assert source.endswith('.wxs') | |
214 args.append(str(build_dir / ('%s.wixobj' % source[:-4]))) | |
215 | |
216 args.append(str(build_dir / 'mercurial.wixobj')) | |
217 | |
218 subprocess.run(args, cwd=str(source_dir), check=True) | |
219 | |
220 print('%s created' % msi_path) | |
221 | |
222 return { | |
223 'msi_path': msi_path, | |
224 } | |
225 | |
226 | |
227 def build_signed_installer(source_dir: pathlib.Path, python_exe: pathlib.Path, | |
228 name: str, version=None, subject_name=None, | |
229 cert_path=None, cert_password=None, | |
230 timestamp_url=None): | |
231 """Build an installer with signed executables.""" | |
232 | |
233 post_build_fn = make_post_build_signing_fn( | |
234 name, | |
235 subject_name=subject_name, | |
236 cert_path=cert_path, | |
237 cert_password=cert_password, | |
238 timestamp_url=timestamp_url) | |
239 | |
240 info = build_installer(source_dir, python_exe=python_exe, | |
241 msi_name=name.lower(), version=version, | |
242 post_build_fn=post_build_fn) | |
243 | |
244 description = '%s %s' % (name, version) | |
245 | |
246 sign_with_signtool(info['msi_path'], description, | |
247 subject_name=subject_name, cert_path=cert_path, | |
248 cert_password=cert_password, timestamp_url=timestamp_url) |