contrib/packaging/hgpackaging/inno.py
author Gregory Szorc <gregory.szorc@gmail.com>
Wed, 23 Oct 2019 18:39:17 -0700
changeset 43515 7bd88d0d6a82
parent 43514 10454e788111
child 43516 d053d3f10b6a
permissions -rw-r--r--
packaging: process Inno Setup files with Jinja2 I want to make the Inno Setup files dynamically generated. This will enable us to do things like automatically derive the set of files to be packaged instead of having to manually keep lists of files in sync across installers. As the first step towards this, we process the Inno Setup files with Jinja2. As part of this conversion, we had to escape syntax in mercurial.iss that conflicts with Jinja2. I also took the opportunity to include modpath.iss via Jinja2 instead of using Inno's preprocessor. This keeps the Python code a bit simpler. Differential Revision: https://phab.mercurial-scm.org/D7158
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
41911
dc7827a9ba64 packaging: move Inno Setup core logic into a module
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41910
diff changeset
     1
# inno.py - Inno Setup functionality.
41853
d7dc4ac1ff84 inno: script to automate building Inno installer
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     2
#
d7dc4ac1ff84 inno: script to automate building Inno installer
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     3
# Copyright 2019 Gregory Szorc <gregory.szorc@gmail.com>
d7dc4ac1ff84 inno: script to automate building Inno installer
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     4
#
d7dc4ac1ff84 inno: script to automate building Inno installer
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     5
# This software may be used and distributed according to the terms of the
d7dc4ac1ff84 inno: script to automate building Inno installer
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     6
# GNU General Public License version 2 or any later version.
d7dc4ac1ff84 inno: script to automate building Inno installer
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     7
d7dc4ac1ff84 inno: script to automate building Inno installer
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     8
# no-check-code because Python 3 native.
d7dc4ac1ff84 inno: script to automate building Inno installer
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     9
d7dc4ac1ff84 inno: script to automate building Inno installer
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    10
import os
d7dc4ac1ff84 inno: script to automate building Inno installer
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    11
import pathlib
d7dc4ac1ff84 inno: script to automate building Inno installer
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    12
import shutil
d7dc4ac1ff84 inno: script to automate building Inno installer
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    13
import subprocess
d7dc4ac1ff84 inno: script to automate building Inno installer
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    14
43515
7bd88d0d6a82 packaging: process Inno Setup files with Jinja2
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43514
diff changeset
    15
import jinja2
7bd88d0d6a82 packaging: process Inno Setup files with Jinja2
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43514
diff changeset
    16
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41916
diff changeset
    17
from .py2exe import build_py2exe
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41916
diff changeset
    18
from .util import find_vc_runtime_files
41911
dc7827a9ba64 packaging: move Inno Setup core logic into a module
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41910
diff changeset
    19
41916
260305e8ddbd setup: configure py2exe config via environment variables
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41915
diff changeset
    20
EXTRA_PACKAGES = {
260305e8ddbd setup: configure py2exe config via environment variables
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41915
diff changeset
    21
    'dulwich',
260305e8ddbd setup: configure py2exe config via environment variables
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41915
diff changeset
    22
    'keyring',
260305e8ddbd setup: configure py2exe config via environment variables
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41915
diff changeset
    23
    'pygments',
260305e8ddbd setup: configure py2exe config via environment variables
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41915
diff changeset
    24
    'win32ctypes',
260305e8ddbd setup: configure py2exe config via environment variables
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41915
diff changeset
    25
}
260305e8ddbd setup: configure py2exe config via environment variables
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41915
diff changeset
    26
260305e8ddbd setup: configure py2exe config via environment variables
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41915
diff changeset
    27
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41916
diff changeset
    28
def build(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41916
diff changeset
    29
    source_dir: pathlib.Path,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41916
diff changeset
    30
    build_dir: pathlib.Path,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41916
diff changeset
    31
    python_exe: pathlib.Path,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41916
diff changeset
    32
    iscc_exe: pathlib.Path,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41916
diff changeset
    33
    version=None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41916
diff changeset
    34
):
41853
d7dc4ac1ff84 inno: script to automate building Inno installer
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    35
    """Build the Inno installer.
d7dc4ac1ff84 inno: script to automate building Inno installer
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    36
d7dc4ac1ff84 inno: script to automate building Inno installer
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    37
    Build files will be placed in ``build_dir``.
d7dc4ac1ff84 inno: script to automate building Inno installer
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    38
d7dc4ac1ff84 inno: script to automate building Inno installer
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    39
    py2exe's setup.py doesn't use setuptools. It doesn't have modern logic
d7dc4ac1ff84 inno: script to automate building Inno installer
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    40
    for finding the Python 2.7 toolchain. So, we require the environment
d7dc4ac1ff84 inno: script to automate building Inno installer
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    41
    to already be configured with an active toolchain.
d7dc4ac1ff84 inno: script to automate building Inno installer
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    42
    """
41911
dc7827a9ba64 packaging: move Inno Setup core logic into a module
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41910
diff changeset
    43
    if not iscc_exe.exists():
dc7827a9ba64 packaging: move Inno Setup core logic into a module
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41910
diff changeset
    44
        raise Exception('%s does not exist' % iscc_exe)
41853
d7dc4ac1ff84 inno: script to automate building Inno installer
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    45
41915
a2e191a937a9 packaging: extract py2exe functionality to own module
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41914
diff changeset
    46
    vc_x64 = r'\x64' in os.environ.get('LIB', '')
43514
10454e788111 packaging: install and run Inno files in a build directory
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43076
diff changeset
    47
    arch = 'x64' if vc_x64 else 'x86'
10454e788111 packaging: install and run Inno files in a build directory
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43076
diff changeset
    48
    inno_source_dir = source_dir / 'contrib' / 'packaging' / 'inno'
10454e788111 packaging: install and run Inno files in a build directory
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43076
diff changeset
    49
    inno_build_dir = build_dir / ('inno-%s' % arch)
41853
d7dc4ac1ff84 inno: script to automate building Inno installer
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    50
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41916
diff changeset
    51
    requirements_txt = (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41916
diff changeset
    52
        source_dir / 'contrib' / 'packaging' / 'inno' / 'requirements.txt'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41916
diff changeset
    53
    )
41853
d7dc4ac1ff84 inno: script to automate building Inno installer
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    54
43514
10454e788111 packaging: install and run Inno files in a build directory
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43076
diff changeset
    55
    inno_build_dir.mkdir(parents=True, exist_ok=True)
10454e788111 packaging: install and run Inno files in a build directory
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43076
diff changeset
    56
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41916
diff changeset
    57
    build_py2exe(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41916
diff changeset
    58
        source_dir,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41916
diff changeset
    59
        build_dir,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41916
diff changeset
    60
        python_exe,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41916
diff changeset
    61
        'inno',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41916
diff changeset
    62
        requirements_txt,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41916
diff changeset
    63
        extra_packages=EXTRA_PACKAGES,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41916
diff changeset
    64
    )
41853
d7dc4ac1ff84 inno: script to automate building Inno installer
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    65
41913
5e923355c595 packaging: don't use temporary directory
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41912
diff changeset
    66
    # hg.exe depends on VC9 runtime DLLs. Copy those into place.
5e923355c595 packaging: don't use temporary directory
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41912
diff changeset
    67
    for f in find_vc_runtime_files(vc_x64):
5e923355c595 packaging: don't use temporary directory
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41912
diff changeset
    68
        if f.name.endswith('.manifest'):
5e923355c595 packaging: don't use temporary directory
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41912
diff changeset
    69
            basename = 'Microsoft.VC90.CRT.manifest'
5e923355c595 packaging: don't use temporary directory
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41912
diff changeset
    70
        else:
5e923355c595 packaging: don't use temporary directory
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41912
diff changeset
    71
            basename = f.name
41853
d7dc4ac1ff84 inno: script to automate building Inno installer
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    72
41913
5e923355c595 packaging: don't use temporary directory
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41912
diff changeset
    73
        dest_path = source_dir / 'dist' / basename
41853
d7dc4ac1ff84 inno: script to automate building Inno installer
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    74
41913
5e923355c595 packaging: don't use temporary directory
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41912
diff changeset
    75
        print('copying %s to %s' % (f, dest_path))
5e923355c595 packaging: don't use temporary directory
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41912
diff changeset
    76
        shutil.copyfile(f, dest_path)
41853
d7dc4ac1ff84 inno: script to automate building Inno installer
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    77
41913
5e923355c595 packaging: don't use temporary directory
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41912
diff changeset
    78
    print('creating installer')
5e923355c595 packaging: don't use temporary directory
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41912
diff changeset
    79
43515
7bd88d0d6a82 packaging: process Inno Setup files with Jinja2
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43514
diff changeset
    80
    # Install Inno files by rendering a template.
7bd88d0d6a82 packaging: process Inno Setup files with Jinja2
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43514
diff changeset
    81
    jinja_env = jinja2.Environment(
7bd88d0d6a82 packaging: process Inno Setup files with Jinja2
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43514
diff changeset
    82
        loader=jinja2.FileSystemLoader(str(inno_source_dir)),
7bd88d0d6a82 packaging: process Inno Setup files with Jinja2
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43514
diff changeset
    83
        # Need to change these to prevent conflict with Inno Setup.
7bd88d0d6a82 packaging: process Inno Setup files with Jinja2
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43514
diff changeset
    84
        comment_start_string='{##',
7bd88d0d6a82 packaging: process Inno Setup files with Jinja2
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43514
diff changeset
    85
        comment_end_string='##}',
7bd88d0d6a82 packaging: process Inno Setup files with Jinja2
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43514
diff changeset
    86
    )
7bd88d0d6a82 packaging: process Inno Setup files with Jinja2
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43514
diff changeset
    87
7bd88d0d6a82 packaging: process Inno Setup files with Jinja2
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43514
diff changeset
    88
    try:
7bd88d0d6a82 packaging: process Inno Setup files with Jinja2
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43514
diff changeset
    89
        template = jinja_env.get_template('mercurial.iss')
7bd88d0d6a82 packaging: process Inno Setup files with Jinja2
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43514
diff changeset
    90
    except jinja2.TemplateSyntaxError as e:
7bd88d0d6a82 packaging: process Inno Setup files with Jinja2
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43514
diff changeset
    91
        raise Exception(
7bd88d0d6a82 packaging: process Inno Setup files with Jinja2
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43514
diff changeset
    92
            'template syntax error at %s:%d: %s'
7bd88d0d6a82 packaging: process Inno Setup files with Jinja2
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43514
diff changeset
    93
            % (e.name, e.lineno, e.message,)
7bd88d0d6a82 packaging: process Inno Setup files with Jinja2
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43514
diff changeset
    94
        )
7bd88d0d6a82 packaging: process Inno Setup files with Jinja2
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43514
diff changeset
    95
7bd88d0d6a82 packaging: process Inno Setup files with Jinja2
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43514
diff changeset
    96
    content = template.render()
7bd88d0d6a82 packaging: process Inno Setup files with Jinja2
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43514
diff changeset
    97
7bd88d0d6a82 packaging: process Inno Setup files with Jinja2
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43514
diff changeset
    98
    with (inno_build_dir / 'mercurial.iss').open('w', encoding='utf-8') as fh:
7bd88d0d6a82 packaging: process Inno Setup files with Jinja2
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43514
diff changeset
    99
        fh.write(content)
43514
10454e788111 packaging: install and run Inno files in a build directory
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43076
diff changeset
   100
41913
5e923355c595 packaging: don't use temporary directory
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41912
diff changeset
   101
    args = [str(iscc_exe)]
41853
d7dc4ac1ff84 inno: script to automate building Inno installer
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   102
41913
5e923355c595 packaging: don't use temporary directory
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41912
diff changeset
   103
    if vc_x64:
5e923355c595 packaging: don't use temporary directory
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41912
diff changeset
   104
        args.append('/dARCH=x64')
41853
d7dc4ac1ff84 inno: script to automate building Inno installer
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   105
41913
5e923355c595 packaging: don't use temporary directory
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41912
diff changeset
   106
    if version:
5e923355c595 packaging: don't use temporary directory
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41912
diff changeset
   107
        args.append('/dVERSION=%s' % version)
41853
d7dc4ac1ff84 inno: script to automate building Inno installer
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   108
41913
5e923355c595 packaging: don't use temporary directory
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41912
diff changeset
   109
    args.append('/Odist')
43514
10454e788111 packaging: install and run Inno files in a build directory
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43076
diff changeset
   110
    args.append(str(inno_build_dir / 'mercurial.iss'))
41853
d7dc4ac1ff84 inno: script to automate building Inno installer
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   111
41913
5e923355c595 packaging: don't use temporary directory
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41912
diff changeset
   112
    subprocess.run(args, cwd=str(source_dir), check=True)