contrib/packaging/hgpackaging/wix.py
author Gregory Szorc <gregory.szorc@gmail.com>
Fri, 08 Mar 2019 10:29:48 -0800
changeset 41924 9d4ae5044b4c
parent 41922 c569f769c41d
child 41952 b83de9150c1c
permissions -rw-r--r--
wix: remove enum and future packages These were cargo culted from the THG installer code. I'm not sure what needs them in THG land. But the official MSIs certainly do not - at least not as direct dependencies. .. bc:: The Windows MSI installers no longer include the enum and future Python packages. Differential Revision: https://phab.mercurial-scm.org/D6101
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
41921
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     1
# wix.py - WiX installer functionality
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     2
#
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     3
# Copyright 2019 Gregory Szorc <gregory.szorc@gmail.com>
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     4
#
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     5
# This software may be used and distributed according to the terms of the
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     6
# GNU General Public License version 2 or any later version.
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     7
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     8
# no-check-code because Python 3 native.
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     9
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    10
import os
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    11
import pathlib
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    12
import re
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    13
import subprocess
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    14
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    15
from .downloads import (
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    16
    download_entry,
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    17
)
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    18
from .py2exe import (
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    19
    build_py2exe,
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    20
)
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    21
from .util import (
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    22
    extract_zip_to_directory,
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    23
    sign_with_signtool,
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    24
)
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    25
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    26
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    27
SUPPORT_WXS = [
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    28
    ('contrib.wxs', r'contrib'),
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    29
    ('dist.wxs', r'dist'),
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    30
    ('doc.wxs', r'doc'),
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    31
    ('help.wxs', r'mercurial\help'),
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    32
    ('i18n.wxs', r'i18n'),
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    33
    ('locale.wxs', r'mercurial\locale'),
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    34
    ('templates.wxs', r'mercurial\templates'),
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    35
]
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    36
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    37
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    38
EXTRA_PACKAGES = {
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    39
    'distutils',
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    40
    'pygments',
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    41
}
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    42
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    43
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    44
def find_version(source_dir: pathlib.Path):
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    45
    version_py = source_dir / 'mercurial' / '__version__.py'
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    46
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    47
    with version_py.open('r', encoding='utf-8') as fh:
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    48
        source = fh.read().strip()
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    49
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    50
    m = re.search('version = b"(.*)"', source)
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    51
    return m.group(1)
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    52
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    53
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    54
def normalize_version(version):
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    55
    """Normalize Mercurial version string so WiX accepts it.
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    56
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    57
    Version strings have to be numeric X.Y.Z.
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    58
    """
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    59
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    60
    if '+' in version:
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    61
        version, extra = version.split('+', 1)
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    62
    else:
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    63
        extra = None
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    64
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    65
    # 4.9rc0
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    66
    if version[:-1].endswith('rc'):
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    67
        version = version[:-3]
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    68
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    69
    versions = [int(v) for v in version.split('.')]
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    70
    while len(versions) < 3:
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    71
        versions.append(0)
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    72
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    73
    major, minor, build = versions[:3]
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    74
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    75
    if extra:
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    76
        # <commit count>-<hash>+<date>
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    77
        build = int(extra.split('-')[0])
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    78
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    79
    return '.'.join('%d' % x for x in (major, minor, build))
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    80
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    81
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    82
def ensure_vc90_merge_modules(build_dir):
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    83
    x86 = (
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    84
        download_entry('vc9-crt-x86-msm', build_dir,
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    85
                       local_name='microsoft.vcxx.crt.x86_msm.msm')[0],
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    86
        download_entry('vc9-crt-x86-msm-policy', build_dir,
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    87
                       local_name='policy.x.xx.microsoft.vcxx.crt.x86_msm.msm')[0]
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    88
    )
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    89
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    90
    x64 = (
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    91
        download_entry('vc9-crt-x64-msm', build_dir,
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    92
                       local_name='microsoft.vcxx.crt.x64_msm.msm')[0],
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    93
        download_entry('vc9-crt-x64-msm-policy', build_dir,
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    94
                       local_name='policy.x.xx.microsoft.vcxx.crt.x64_msm.msm')[0]
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    95
    )
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    96
    return {
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    97
        'x86': x86,
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    98
        'x64': x64,
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    99
    }
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   100
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   101
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   102
def run_candle(wix, cwd, wxs, source_dir, defines=None):
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   103
    args = [
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   104
        str(wix / 'candle.exe'),
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   105
        '-nologo',
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   106
        str(wxs),
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   107
        '-dSourceDir=%s' % source_dir,
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   108
    ]
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   109
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   110
    if defines:
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   111
        args.extend('-d%s=%s' % define for define in sorted(defines.items()))
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   112
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   113
    subprocess.run(args, cwd=str(cwd), check=True)
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   114
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   115
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   116
def make_post_build_signing_fn(name, subject_name=None, cert_path=None,
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   117
                               cert_password=None, timestamp_url=None):
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   118
    """Create a callable that will use signtool to sign hg.exe."""
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   119
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   120
    def post_build_sign(source_dir, build_dir, dist_dir, version):
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   121
        description = '%s %s' % (name, version)
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   122
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   123
        sign_with_signtool(dist_dir / 'hg.exe', description,
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   124
                           subject_name=subject_name, cert_path=cert_path,
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   125
                           cert_password=cert_password,
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   126
                           timestamp_url=timestamp_url)
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   127
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   128
    return post_build_sign
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   129
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   130
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   131
def build_installer(source_dir: pathlib.Path, python_exe: pathlib.Path,
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   132
                    msi_name='mercurial', version=None, post_build_fn=None):
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   133
    """Build a WiX MSI installer.
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   134
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   135
    ``source_dir`` is the path to the Mercurial source tree to use.
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   136
    ``arch`` is the target architecture. either ``x86`` or ``x64``.
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   137
    ``python_exe`` is the path to the Python executable to use/bundle.
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   138
    ``version`` is the Mercurial version string. If not defined,
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   139
    ``mercurial/__version__.py`` will be consulted.
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   140
    ``post_build_fn`` is a callable that will be called after building
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   141
    Mercurial but before invoking WiX. It can be used to e.g. facilitate
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   142
    signing. It is passed the paths to the Mercurial source, build, and
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   143
    dist directories and the resolved Mercurial version.
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   144
    """
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   145
    arch = 'x64' if r'\x64' in os.environ.get('LIB', '') else 'x86'
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   146
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   147
    hg_build_dir = source_dir / 'build'
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   148
    dist_dir = source_dir / 'dist'
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   149
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   150
    requirements_txt = (source_dir / 'contrib' / 'packaging' /
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   151
                        'wix' / 'requirements.txt')
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   152
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   153
    build_py2exe(source_dir, hg_build_dir,
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   154
                 python_exe, 'wix', requirements_txt,
41922
c569f769c41d wix: remove sphinx and dependencies
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   155
                 extra_packages=EXTRA_PACKAGES)
41921
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   156
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   157
    version = version or normalize_version(find_version(source_dir))
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   158
    print('using version string: %s' % version)
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   159
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   160
    if post_build_fn:
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   161
        post_build_fn(source_dir, hg_build_dir, dist_dir, version)
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   162
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   163
    build_dir = hg_build_dir / ('wix-%s' % arch)
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   164
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   165
    build_dir.mkdir(exist_ok=True)
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   166
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   167
    wix_pkg, wix_entry = download_entry('wix', hg_build_dir)
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   168
    wix_path = hg_build_dir / ('wix-%s' % wix_entry['version'])
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   169
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   170
    if not wix_path.exists():
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   171
        extract_zip_to_directory(wix_pkg, wix_path)
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   172
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   173
    ensure_vc90_merge_modules(hg_build_dir)
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   174
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   175
    source_build_rel = pathlib.Path(os.path.relpath(source_dir, build_dir))
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   176
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   177
    defines = {'Platform': arch}
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   178
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   179
    for wxs, rel_path in SUPPORT_WXS:
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   180
        wxs = source_dir / 'contrib' / 'packaging' / 'wix' / wxs
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   181
        wxs_source_dir = source_dir / rel_path
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   182
        run_candle(wix_path, build_dir, wxs, wxs_source_dir, defines=defines)
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   183
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   184
    source = source_dir / 'contrib' / 'packaging' / 'wix' / 'mercurial.wxs'
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   185
    defines['Version'] = version
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   186
    defines['Comments'] = 'Installs Mercurial version %s' % version
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   187
    defines['VCRedistSrcDir'] = str(hg_build_dir)
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   188
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   189
    run_candle(wix_path, build_dir, source, source_build_rel, defines=defines)
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   190
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   191
    msi_path = source_dir / 'dist' / (
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   192
        '%s-%s-%s.msi' % (msi_name, version, arch))
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   193
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   194
    args = [
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   195
        str(wix_path / 'light.exe'),
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   196
        '-nologo',
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   197
        '-ext', 'WixUIExtension',
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   198
        '-sw1076',
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   199
        '-spdb',
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   200
        '-o', str(msi_path),
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   201
    ]
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   202
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   203
    for source, rel_path in SUPPORT_WXS:
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   204
        assert source.endswith('.wxs')
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   205
        args.append(str(build_dir / ('%s.wixobj' % source[:-4])))
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   206
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   207
    args.append(str(build_dir / 'mercurial.wixobj'))
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   208
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   209
    subprocess.run(args, cwd=str(source_dir), check=True)
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   210
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   211
    print('%s created' % msi_path)
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   212
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   213
    return {
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   214
        'msi_path': msi_path,
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   215
    }
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   216
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   217
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   218
def build_signed_installer(source_dir: pathlib.Path, python_exe: pathlib.Path,
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   219
                           name: str, version=None, subject_name=None,
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   220
                           cert_path=None, cert_password=None,
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   221
                           timestamp_url=None):
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   222
    """Build an installer with signed executables."""
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   223
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   224
    post_build_fn = make_post_build_signing_fn(
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   225
        name,
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   226
        subject_name=subject_name,
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   227
        cert_path=cert_path,
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   228
        cert_password=cert_password,
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   229
        timestamp_url=timestamp_url)
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   230
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   231
    info = build_installer(source_dir, python_exe=python_exe,
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   232
                           msi_name=name.lower(), version=version,
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   233
                           post_build_fn=post_build_fn)
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   234
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   235
    description = '%s %s' % (name, version)
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   236
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   237
    sign_with_signtool(info['msi_path'], description,
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   238
                       subject_name=subject_name, cert_path=cert_path,
4371f543efda wix: functionality to automate building WiX installers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   239
                       cert_password=cert_password, timestamp_url=timestamp_url)