##// END OF EJS Templates
wix: functionality to automate building WiX installers...
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

File last commit:

r42087:4371f543 default
r42087:4371f543 default
Show More
util.py
157 lines | 4.4 KiB | text/x-python | PythonLexer
# util.py - Common packaging utility code.
#
# 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 distutils.version
import getpass
import os
import pathlib
import subprocess
import tarfile
import zipfile
def extract_tar_to_directory(source: pathlib.Path, dest: pathlib.Path):
with tarfile.open(source, 'r') as tf:
tf.extractall(dest)
def extract_zip_to_directory(source: pathlib.Path, dest: pathlib.Path):
with zipfile.ZipFile(source, 'r') as zf:
zf.extractall(dest)
def find_vc_runtime_files(x64=False):
"""Finds Visual C++ Runtime DLLs to include in distribution."""
winsxs = pathlib.Path(os.environ['SYSTEMROOT']) / 'WinSxS'
prefix = 'amd64' if x64 else 'x86'
candidates = sorted(p for p in os.listdir(winsxs)
if p.lower().startswith('%s_microsoft.vc90.crt_' % prefix))
for p in candidates:
print('found candidate VC runtime: %s' % p)
# Take the newest version.
version = candidates[-1]
d = winsxs / version
return [
d / 'msvcm90.dll',
d / 'msvcp90.dll',
d / 'msvcr90.dll',
winsxs / 'Manifests' / ('%s.manifest' % version),
]
def windows_10_sdk_info():
"""Resolves information about the Windows 10 SDK."""
base = pathlib.Path(os.environ['ProgramFiles(x86)']) / 'Windows Kits' / '10'
if not base.is_dir():
raise Exception('unable to find Windows 10 SDK at %s' % base)
# Find the latest version.
bin_base = base / 'bin'
versions = [v for v in os.listdir(bin_base) if v.startswith('10.')]
version = sorted(versions, reverse=True)[0]
bin_version = bin_base / version
return {
'root': base,
'version': version,
'bin_root': bin_version,
'bin_x86': bin_version / 'x86',
'bin_x64': bin_version / 'x64'
}
def find_signtool():
"""Find signtool.exe from the Windows SDK."""
sdk = windows_10_sdk_info()
for key in ('bin_x64', 'bin_x86'):
p = sdk[key] / 'signtool.exe'
if p.exists():
return p
raise Exception('could not find signtool.exe in Windows 10 SDK')
def sign_with_signtool(file_path, description, subject_name=None,
cert_path=None, cert_password=None,
timestamp_url=None):
"""Digitally sign a file with signtool.exe.
``file_path`` is file to sign.
``description`` is text that goes in the signature.
The signing certificate can be specified by ``cert_path`` or
``subject_name``. These correspond to the ``/f`` and ``/n`` arguments
to signtool.exe, respectively.
The certificate password can be specified via ``cert_password``. If
not provided, you will be prompted for the password.
``timestamp_url`` is the URL of a RFC 3161 timestamp server (``/tr``
argument to signtool.exe).
"""
if cert_path and subject_name:
raise ValueError('cannot specify both cert_path and subject_name')
while cert_path and not cert_password:
cert_password = getpass.getpass('password for %s: ' % cert_path)
args = [
str(find_signtool()), 'sign',
'/v',
'/fd', 'sha256',
'/d', description,
]
if cert_path:
args.extend(['/f', str(cert_path), '/p', cert_password])
elif subject_name:
args.extend(['/n', subject_name])
if timestamp_url:
args.extend(['/tr', timestamp_url, '/td', 'sha256'])
args.append(str(file_path))
print('signing %s' % file_path)
subprocess.run(args, check=True)
PRINT_PYTHON_INFO = '''
import platform; print("%s:%s" % (platform.architecture()[0], platform.python_version()))
'''.strip()
def python_exe_info(python_exe: pathlib.Path):
"""Obtain information about a Python executable."""
res = subprocess.run(
[str(python_exe), '-c', PRINT_PYTHON_INFO],
capture_output=True, check=True)
arch, version = res.stdout.decode('utf-8').split(':')
version = distutils.version.LooseVersion(version)
return {
'arch': arch,
'version': version,
'py3': version >= distutils.version.LooseVersion('3'),
}