contrib/packaging/hgpackaging/util.py
author Martin von Zweigbergk <martinvonz@google.com>
Tue, 23 Feb 2021 12:29:41 -0800
changeset 46524 e3f23814bac7
parent 44763 94f4f2ec7dee
child 48848 17d5e25b8e78
permissions -rw-r--r--
windows: fix parsing of version number to match format from D9955 Differential Revision: https://phab.mercurial-scm.org/D10061
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
41952
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
     1
# util.py - Common packaging utility code.
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
     2
#
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
     3
# Copyright 2019 Gregory Szorc <gregory.szorc@gmail.com>
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
     4
#
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
     5
# This software may be used and distributed according to the terms of the
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
     6
# GNU General Public License version 2 or any later version.
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
     7
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
     8
# no-check-code because Python 3 native.
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
     9
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
    10
import distutils.version
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
    11
import getpass
43516
d053d3f10b6a packaging: stage installed files for Inno
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43076
diff changeset
    12
import glob
41952
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
    13
import os
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
    14
import pathlib
43518
7c9f63a5cb14 packaging: always pass VERSION into Inno invocation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43516
diff changeset
    15
import re
43516
d053d3f10b6a packaging: stage installed files for Inno
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43076
diff changeset
    16
import shutil
41952
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
    17
import subprocess
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
    18
import tarfile
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
    19
import zipfile
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
    20
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
    21
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
    22
def extract_tar_to_directory(source: pathlib.Path, dest: pathlib.Path):
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
    23
    with tarfile.open(source, 'r') as tf:
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
    24
        tf.extractall(dest)
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
    25
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
    26
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
    27
def extract_zip_to_directory(source: pathlib.Path, dest: pathlib.Path):
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
    28
    with zipfile.ZipFile(source, 'r') as zf:
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
    29
        zf.extractall(dest)
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
    30
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
    31
44763
94f4f2ec7dee packaging: support building Inno installer with PyOxidizer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44220
diff changeset
    32
def find_vc_runtime_dll(x64=False):
94f4f2ec7dee packaging: support building Inno installer with PyOxidizer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44220
diff changeset
    33
    """Finds Visual C++ Runtime DLL to include in distribution."""
94f4f2ec7dee packaging: support building Inno installer with PyOxidizer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44220
diff changeset
    34
    # We invoke vswhere to find the latest Visual Studio install.
94f4f2ec7dee packaging: support building Inno installer with PyOxidizer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44220
diff changeset
    35
    vswhere = (
94f4f2ec7dee packaging: support building Inno installer with PyOxidizer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44220
diff changeset
    36
        pathlib.Path(os.environ["ProgramFiles(x86)"])
94f4f2ec7dee packaging: support building Inno installer with PyOxidizer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44220
diff changeset
    37
        / "Microsoft Visual Studio"
94f4f2ec7dee packaging: support building Inno installer with PyOxidizer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44220
diff changeset
    38
        / "Installer"
94f4f2ec7dee packaging: support building Inno installer with PyOxidizer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44220
diff changeset
    39
        / "vswhere.exe"
94f4f2ec7dee packaging: support building Inno installer with PyOxidizer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44220
diff changeset
    40
    )
94f4f2ec7dee packaging: support building Inno installer with PyOxidizer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44220
diff changeset
    41
94f4f2ec7dee packaging: support building Inno installer with PyOxidizer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44220
diff changeset
    42
    if not vswhere.exists():
94f4f2ec7dee packaging: support building Inno installer with PyOxidizer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44220
diff changeset
    43
        raise Exception(
94f4f2ec7dee packaging: support building Inno installer with PyOxidizer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44220
diff changeset
    44
            "could not find vswhere.exe: %s does not exist" % vswhere
94f4f2ec7dee packaging: support building Inno installer with PyOxidizer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44220
diff changeset
    45
        )
94f4f2ec7dee packaging: support building Inno installer with PyOxidizer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44220
diff changeset
    46
94f4f2ec7dee packaging: support building Inno installer with PyOxidizer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44220
diff changeset
    47
    args = [
94f4f2ec7dee packaging: support building Inno installer with PyOxidizer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44220
diff changeset
    48
        str(vswhere),
94f4f2ec7dee packaging: support building Inno installer with PyOxidizer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44220
diff changeset
    49
        # -products * is necessary to return results from Build Tools
94f4f2ec7dee packaging: support building Inno installer with PyOxidizer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44220
diff changeset
    50
        # (as opposed to full IDE installs).
94f4f2ec7dee packaging: support building Inno installer with PyOxidizer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44220
diff changeset
    51
        "-products",
94f4f2ec7dee packaging: support building Inno installer with PyOxidizer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44220
diff changeset
    52
        "*",
94f4f2ec7dee packaging: support building Inno installer with PyOxidizer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44220
diff changeset
    53
        "-requires",
94f4f2ec7dee packaging: support building Inno installer with PyOxidizer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44220
diff changeset
    54
        "Microsoft.VisualCpp.Redist.14.Latest",
94f4f2ec7dee packaging: support building Inno installer with PyOxidizer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44220
diff changeset
    55
        "-latest",
94f4f2ec7dee packaging: support building Inno installer with PyOxidizer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44220
diff changeset
    56
        "-property",
94f4f2ec7dee packaging: support building Inno installer with PyOxidizer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44220
diff changeset
    57
        "installationPath",
94f4f2ec7dee packaging: support building Inno installer with PyOxidizer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44220
diff changeset
    58
    ]
94f4f2ec7dee packaging: support building Inno installer with PyOxidizer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44220
diff changeset
    59
94f4f2ec7dee packaging: support building Inno installer with PyOxidizer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44220
diff changeset
    60
    vs_install_path = pathlib.Path(
94f4f2ec7dee packaging: support building Inno installer with PyOxidizer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44220
diff changeset
    61
        os.fsdecode(subprocess.check_output(args).strip())
94f4f2ec7dee packaging: support building Inno installer with PyOxidizer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44220
diff changeset
    62
    )
94f4f2ec7dee packaging: support building Inno installer with PyOxidizer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44220
diff changeset
    63
94f4f2ec7dee packaging: support building Inno installer with PyOxidizer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44220
diff changeset
    64
    # This just gets us a path like
94f4f2ec7dee packaging: support building Inno installer with PyOxidizer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44220
diff changeset
    65
    # C:\Program Files (x86)\Microsoft Visual Studio\2019\Community
94f4f2ec7dee packaging: support building Inno installer with PyOxidizer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44220
diff changeset
    66
    # Actually vcruntime140.dll is under a path like:
94f4f2ec7dee packaging: support building Inno installer with PyOxidizer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44220
diff changeset
    67
    # VC\Redist\MSVC\<version>\<arch>\Microsoft.VC14<X>.CRT\vcruntime140.dll.
94f4f2ec7dee packaging: support building Inno installer with PyOxidizer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44220
diff changeset
    68
94f4f2ec7dee packaging: support building Inno installer with PyOxidizer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44220
diff changeset
    69
    arch = "x64" if x64 else "x86"
94f4f2ec7dee packaging: support building Inno installer with PyOxidizer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44220
diff changeset
    70
94f4f2ec7dee packaging: support building Inno installer with PyOxidizer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44220
diff changeset
    71
    search_glob = (
94f4f2ec7dee packaging: support building Inno installer with PyOxidizer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44220
diff changeset
    72
        r"%s\VC\Redist\MSVC\*\%s\Microsoft.VC14*.CRT\vcruntime140.dll"
94f4f2ec7dee packaging: support building Inno installer with PyOxidizer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44220
diff changeset
    73
        % (vs_install_path, arch)
94f4f2ec7dee packaging: support building Inno installer with PyOxidizer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44220
diff changeset
    74
    )
94f4f2ec7dee packaging: support building Inno installer with PyOxidizer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44220
diff changeset
    75
94f4f2ec7dee packaging: support building Inno installer with PyOxidizer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44220
diff changeset
    76
    candidates = glob.glob(search_glob, recursive=True)
94f4f2ec7dee packaging: support building Inno installer with PyOxidizer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44220
diff changeset
    77
94f4f2ec7dee packaging: support building Inno installer with PyOxidizer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44220
diff changeset
    78
    for candidate in reversed(candidates):
94f4f2ec7dee packaging: support building Inno installer with PyOxidizer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44220
diff changeset
    79
        return pathlib.Path(candidate)
94f4f2ec7dee packaging: support building Inno installer with PyOxidizer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44220
diff changeset
    80
94f4f2ec7dee packaging: support building Inno installer with PyOxidizer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44220
diff changeset
    81
    raise Exception("could not find vcruntime140.dll")
94f4f2ec7dee packaging: support building Inno installer with PyOxidizer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44220
diff changeset
    82
94f4f2ec7dee packaging: support building Inno installer with PyOxidizer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44220
diff changeset
    83
94f4f2ec7dee packaging: support building Inno installer with PyOxidizer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44220
diff changeset
    84
def find_legacy_vc_runtime_files(x64=False):
41952
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
    85
    """Finds Visual C++ Runtime DLLs to include in distribution."""
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
    86
    winsxs = pathlib.Path(os.environ['SYSTEMROOT']) / 'WinSxS'
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
    87
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
    88
    prefix = 'amd64' if x64 else 'x86'
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
    89
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42090
diff changeset
    90
    candidates = sorted(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42090
diff changeset
    91
        p
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42090
diff changeset
    92
        for p in os.listdir(winsxs)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42090
diff changeset
    93
        if p.lower().startswith('%s_microsoft.vc90.crt_' % prefix)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42090
diff changeset
    94
    )
41952
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
    95
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
    96
    for p in candidates:
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
    97
        print('found candidate VC runtime: %s' % p)
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
    98
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
    99
    # Take the newest version.
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   100
    version = candidates[-1]
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   101
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   102
    d = winsxs / version
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   103
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   104
    return [
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   105
        d / 'msvcm90.dll',
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   106
        d / 'msvcp90.dll',
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   107
        d / 'msvcr90.dll',
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   108
        winsxs / 'Manifests' / ('%s.manifest' % version),
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   109
    ]
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   110
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   111
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   112
def windows_10_sdk_info():
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   113
    """Resolves information about the Windows 10 SDK."""
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   114
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   115
    base = pathlib.Path(os.environ['ProgramFiles(x86)']) / 'Windows Kits' / '10'
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   116
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   117
    if not base.is_dir():
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   118
        raise Exception('unable to find Windows 10 SDK at %s' % base)
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   119
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   120
    # Find the latest version.
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   121
    bin_base = base / 'bin'
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   122
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   123
    versions = [v for v in os.listdir(bin_base) if v.startswith('10.')]
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   124
    version = sorted(versions, reverse=True)[0]
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   125
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   126
    bin_version = bin_base / version
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   127
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   128
    return {
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   129
        'root': base,
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   130
        'version': version,
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   131
        'bin_root': bin_version,
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   132
        'bin_x86': bin_version / 'x86',
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42090
diff changeset
   133
        'bin_x64': bin_version / 'x64',
41952
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   134
    }
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   135
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   136
44220
a70108a3d7cc packaging: move the version normalization function to the util module
Matt Harbison <matt_harbison@yahoo.com>
parents: 43518
diff changeset
   137
def normalize_windows_version(version):
a70108a3d7cc packaging: move the version normalization function to the util module
Matt Harbison <matt_harbison@yahoo.com>
parents: 43518
diff changeset
   138
    """Normalize Mercurial version string so WiX/Inno accepts it.
a70108a3d7cc packaging: move the version normalization function to the util module
Matt Harbison <matt_harbison@yahoo.com>
parents: 43518
diff changeset
   139
a70108a3d7cc packaging: move the version normalization function to the util module
Matt Harbison <matt_harbison@yahoo.com>
parents: 43518
diff changeset
   140
    Version strings have to be numeric ``A.B.C[.D]`` to conform with MSI's
a70108a3d7cc packaging: move the version normalization function to the util module
Matt Harbison <matt_harbison@yahoo.com>
parents: 43518
diff changeset
   141
    requirements.
a70108a3d7cc packaging: move the version normalization function to the util module
Matt Harbison <matt_harbison@yahoo.com>
parents: 43518
diff changeset
   142
a70108a3d7cc packaging: move the version normalization function to the util module
Matt Harbison <matt_harbison@yahoo.com>
parents: 43518
diff changeset
   143
    We normalize RC version or the commit count to a 4th version component.
a70108a3d7cc packaging: move the version normalization function to the util module
Matt Harbison <matt_harbison@yahoo.com>
parents: 43518
diff changeset
   144
    We store this in the 4th component because ``A.B.C`` releases do occur
a70108a3d7cc packaging: move the version normalization function to the util module
Matt Harbison <matt_harbison@yahoo.com>
parents: 43518
diff changeset
   145
    and we want an e.g. ``5.3rc0`` version to be semantically less than a
a70108a3d7cc packaging: move the version normalization function to the util module
Matt Harbison <matt_harbison@yahoo.com>
parents: 43518
diff changeset
   146
    ``5.3.1rc2`` version. This requires always reserving the 3rd version
a70108a3d7cc packaging: move the version normalization function to the util module
Matt Harbison <matt_harbison@yahoo.com>
parents: 43518
diff changeset
   147
    component for the point release and the ``X.YrcN`` release is always
a70108a3d7cc packaging: move the version normalization function to the util module
Matt Harbison <matt_harbison@yahoo.com>
parents: 43518
diff changeset
   148
    point release 0.
a70108a3d7cc packaging: move the version normalization function to the util module
Matt Harbison <matt_harbison@yahoo.com>
parents: 43518
diff changeset
   149
a70108a3d7cc packaging: move the version normalization function to the util module
Matt Harbison <matt_harbison@yahoo.com>
parents: 43518
diff changeset
   150
    In the case of an RC and presence of ``+`` suffix data, we can't use both
a70108a3d7cc packaging: move the version normalization function to the util module
Matt Harbison <matt_harbison@yahoo.com>
parents: 43518
diff changeset
   151
    because the version format is limited to 4 components. We choose to use
a70108a3d7cc packaging: move the version normalization function to the util module
Matt Harbison <matt_harbison@yahoo.com>
parents: 43518
diff changeset
   152
    RC and throw away the commit count in the suffix. This means we could
a70108a3d7cc packaging: move the version normalization function to the util module
Matt Harbison <matt_harbison@yahoo.com>
parents: 43518
diff changeset
   153
    produce multiple installers with the same normalized version string.
a70108a3d7cc packaging: move the version normalization function to the util module
Matt Harbison <matt_harbison@yahoo.com>
parents: 43518
diff changeset
   154
a70108a3d7cc packaging: move the version normalization function to the util module
Matt Harbison <matt_harbison@yahoo.com>
parents: 43518
diff changeset
   155
    >>> normalize_windows_version("5.3")
a70108a3d7cc packaging: move the version normalization function to the util module
Matt Harbison <matt_harbison@yahoo.com>
parents: 43518
diff changeset
   156
    '5.3.0'
a70108a3d7cc packaging: move the version normalization function to the util module
Matt Harbison <matt_harbison@yahoo.com>
parents: 43518
diff changeset
   157
a70108a3d7cc packaging: move the version normalization function to the util module
Matt Harbison <matt_harbison@yahoo.com>
parents: 43518
diff changeset
   158
    >>> normalize_windows_version("5.3rc0")
a70108a3d7cc packaging: move the version normalization function to the util module
Matt Harbison <matt_harbison@yahoo.com>
parents: 43518
diff changeset
   159
    '5.3.0.0'
a70108a3d7cc packaging: move the version normalization function to the util module
Matt Harbison <matt_harbison@yahoo.com>
parents: 43518
diff changeset
   160
a70108a3d7cc packaging: move the version normalization function to the util module
Matt Harbison <matt_harbison@yahoo.com>
parents: 43518
diff changeset
   161
    >>> normalize_windows_version("5.3rc1")
a70108a3d7cc packaging: move the version normalization function to the util module
Matt Harbison <matt_harbison@yahoo.com>
parents: 43518
diff changeset
   162
    '5.3.0.1'
a70108a3d7cc packaging: move the version normalization function to the util module
Matt Harbison <matt_harbison@yahoo.com>
parents: 43518
diff changeset
   163
46524
e3f23814bac7 windows: fix parsing of version number to match format from D9955
Martin von Zweigbergk <martinvonz@google.com>
parents: 44763
diff changeset
   164
    >>> normalize_windows_version("5.3rc1+hg2.abcdef")
44220
a70108a3d7cc packaging: move the version normalization function to the util module
Matt Harbison <matt_harbison@yahoo.com>
parents: 43518
diff changeset
   165
    '5.3.0.1'
a70108a3d7cc packaging: move the version normalization function to the util module
Matt Harbison <matt_harbison@yahoo.com>
parents: 43518
diff changeset
   166
46524
e3f23814bac7 windows: fix parsing of version number to match format from D9955
Martin von Zweigbergk <martinvonz@google.com>
parents: 44763
diff changeset
   167
    >>> normalize_windows_version("5.3+hg2.abcdef")
44220
a70108a3d7cc packaging: move the version normalization function to the util module
Matt Harbison <matt_harbison@yahoo.com>
parents: 43518
diff changeset
   168
    '5.3.0.2'
a70108a3d7cc packaging: move the version normalization function to the util module
Matt Harbison <matt_harbison@yahoo.com>
parents: 43518
diff changeset
   169
    """
a70108a3d7cc packaging: move the version normalization function to the util module
Matt Harbison <matt_harbison@yahoo.com>
parents: 43518
diff changeset
   170
    if '+' in version:
a70108a3d7cc packaging: move the version normalization function to the util module
Matt Harbison <matt_harbison@yahoo.com>
parents: 43518
diff changeset
   171
        version, extra = version.split('+', 1)
a70108a3d7cc packaging: move the version normalization function to the util module
Matt Harbison <matt_harbison@yahoo.com>
parents: 43518
diff changeset
   172
    else:
a70108a3d7cc packaging: move the version normalization function to the util module
Matt Harbison <matt_harbison@yahoo.com>
parents: 43518
diff changeset
   173
        extra = None
a70108a3d7cc packaging: move the version normalization function to the util module
Matt Harbison <matt_harbison@yahoo.com>
parents: 43518
diff changeset
   174
a70108a3d7cc packaging: move the version normalization function to the util module
Matt Harbison <matt_harbison@yahoo.com>
parents: 43518
diff changeset
   175
    # 4.9rc0
a70108a3d7cc packaging: move the version normalization function to the util module
Matt Harbison <matt_harbison@yahoo.com>
parents: 43518
diff changeset
   176
    if version[:-1].endswith('rc'):
a70108a3d7cc packaging: move the version normalization function to the util module
Matt Harbison <matt_harbison@yahoo.com>
parents: 43518
diff changeset
   177
        rc = int(version[-1:])
a70108a3d7cc packaging: move the version normalization function to the util module
Matt Harbison <matt_harbison@yahoo.com>
parents: 43518
diff changeset
   178
        version = version[:-3]
a70108a3d7cc packaging: move the version normalization function to the util module
Matt Harbison <matt_harbison@yahoo.com>
parents: 43518
diff changeset
   179
    else:
a70108a3d7cc packaging: move the version normalization function to the util module
Matt Harbison <matt_harbison@yahoo.com>
parents: 43518
diff changeset
   180
        rc = None
a70108a3d7cc packaging: move the version normalization function to the util module
Matt Harbison <matt_harbison@yahoo.com>
parents: 43518
diff changeset
   181
a70108a3d7cc packaging: move the version normalization function to the util module
Matt Harbison <matt_harbison@yahoo.com>
parents: 43518
diff changeset
   182
    # Ensure we have at least X.Y version components.
a70108a3d7cc packaging: move the version normalization function to the util module
Matt Harbison <matt_harbison@yahoo.com>
parents: 43518
diff changeset
   183
    versions = [int(v) for v in version.split('.')]
a70108a3d7cc packaging: move the version normalization function to the util module
Matt Harbison <matt_harbison@yahoo.com>
parents: 43518
diff changeset
   184
    while len(versions) < 3:
a70108a3d7cc packaging: move the version normalization function to the util module
Matt Harbison <matt_harbison@yahoo.com>
parents: 43518
diff changeset
   185
        versions.append(0)
a70108a3d7cc packaging: move the version normalization function to the util module
Matt Harbison <matt_harbison@yahoo.com>
parents: 43518
diff changeset
   186
a70108a3d7cc packaging: move the version normalization function to the util module
Matt Harbison <matt_harbison@yahoo.com>
parents: 43518
diff changeset
   187
    if len(versions) < 4:
a70108a3d7cc packaging: move the version normalization function to the util module
Matt Harbison <matt_harbison@yahoo.com>
parents: 43518
diff changeset
   188
        if rc is not None:
a70108a3d7cc packaging: move the version normalization function to the util module
Matt Harbison <matt_harbison@yahoo.com>
parents: 43518
diff changeset
   189
            versions.append(rc)
a70108a3d7cc packaging: move the version normalization function to the util module
Matt Harbison <matt_harbison@yahoo.com>
parents: 43518
diff changeset
   190
        elif extra:
46524
e3f23814bac7 windows: fix parsing of version number to match format from D9955
Martin von Zweigbergk <martinvonz@google.com>
parents: 44763
diff changeset
   191
            # hg<commit count>.<hash>+<date>
e3f23814bac7 windows: fix parsing of version number to match format from D9955
Martin von Zweigbergk <martinvonz@google.com>
parents: 44763
diff changeset
   192
            versions.append(int(extra.split('.')[0][2:]))
44220
a70108a3d7cc packaging: move the version normalization function to the util module
Matt Harbison <matt_harbison@yahoo.com>
parents: 43518
diff changeset
   193
a70108a3d7cc packaging: move the version normalization function to the util module
Matt Harbison <matt_harbison@yahoo.com>
parents: 43518
diff changeset
   194
    return '.'.join('%d' % x for x in versions[0:4])
a70108a3d7cc packaging: move the version normalization function to the util module
Matt Harbison <matt_harbison@yahoo.com>
parents: 43518
diff changeset
   195
a70108a3d7cc packaging: move the version normalization function to the util module
Matt Harbison <matt_harbison@yahoo.com>
parents: 43518
diff changeset
   196
41952
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   197
def find_signtool():
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   198
    """Find signtool.exe from the Windows SDK."""
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   199
    sdk = windows_10_sdk_info()
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   200
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   201
    for key in ('bin_x64', 'bin_x86'):
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   202
        p = sdk[key] / 'signtool.exe'
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   203
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   204
        if p.exists():
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   205
            return p
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   206
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   207
    raise Exception('could not find signtool.exe in Windows 10 SDK')
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   208
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   209
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42090
diff changeset
   210
def sign_with_signtool(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42090
diff changeset
   211
    file_path,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42090
diff changeset
   212
    description,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42090
diff changeset
   213
    subject_name=None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42090
diff changeset
   214
    cert_path=None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42090
diff changeset
   215
    cert_password=None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42090
diff changeset
   216
    timestamp_url=None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42090
diff changeset
   217
):
41952
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   218
    """Digitally sign a file with signtool.exe.
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   219
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   220
    ``file_path`` is file to sign.
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   221
    ``description`` is text that goes in the signature.
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   222
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   223
    The signing certificate can be specified by ``cert_path`` or
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   224
    ``subject_name``. These correspond to the ``/f`` and ``/n`` arguments
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   225
    to signtool.exe, respectively.
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   226
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   227
    The certificate password can be specified via ``cert_password``. If
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   228
    not provided, you will be prompted for the password.
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   229
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   230
    ``timestamp_url`` is the URL of a RFC 3161 timestamp server (``/tr``
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   231
    argument to signtool.exe).
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   232
    """
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   233
    if cert_path and subject_name:
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   234
        raise ValueError('cannot specify both cert_path and subject_name')
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   235
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   236
    while cert_path and not cert_password:
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   237
        cert_password = getpass.getpass('password for %s: ' % cert_path)
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   238
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   239
    args = [
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42090
diff changeset
   240
        str(find_signtool()),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42090
diff changeset
   241
        'sign',
41952
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   242
        '/v',
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42090
diff changeset
   243
        '/fd',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42090
diff changeset
   244
        'sha256',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42090
diff changeset
   245
        '/d',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42090
diff changeset
   246
        description,
41952
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   247
    ]
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   248
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   249
    if cert_path:
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   250
        args.extend(['/f', str(cert_path), '/p', cert_password])
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   251
    elif subject_name:
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   252
        args.extend(['/n', subject_name])
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   253
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   254
    if timestamp_url:
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   255
        args.extend(['/tr', timestamp_url, '/td', 'sha256'])
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   256
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   257
    args.append(str(file_path))
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   258
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   259
    print('signing %s' % file_path)
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   260
    subprocess.run(args, check=True)
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   261
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   262
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   263
PRINT_PYTHON_INFO = '''
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   264
import platform; print("%s:%s" % (platform.architecture()[0], platform.python_version()))
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   265
'''.strip()
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   266
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   267
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   268
def python_exe_info(python_exe: pathlib.Path):
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   269
    """Obtain information about a Python executable."""
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   270
42090
9c07d345fd6d packaging: don't crash building wix with python3.6 and earlier
Matt Harbison <matt_harbison@yahoo.com>
parents: 41952
diff changeset
   271
    res = subprocess.check_output([str(python_exe), '-c', PRINT_PYTHON_INFO])
41952
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   272
42090
9c07d345fd6d packaging: don't crash building wix with python3.6 and earlier
Matt Harbison <matt_harbison@yahoo.com>
parents: 41952
diff changeset
   273
    arch, version = res.decode('utf-8').split(':')
41952
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   274
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   275
    version = distutils.version.LooseVersion(version)
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   276
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   277
    return {
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   278
        'arch': arch,
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   279
        'version': version,
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   280
        'py3': version >= distutils.version.LooseVersion('3'),
b83de9150c1c packaging: convert files to LF
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41921
diff changeset
   281
    }
43516
d053d3f10b6a packaging: stage installed files for Inno
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43076
diff changeset
   282
d053d3f10b6a packaging: stage installed files for Inno
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43076
diff changeset
   283
d053d3f10b6a packaging: stage installed files for Inno
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43076
diff changeset
   284
def process_install_rules(
d053d3f10b6a packaging: stage installed files for Inno
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43076
diff changeset
   285
    rules: list, source_dir: pathlib.Path, dest_dir: pathlib.Path
d053d3f10b6a packaging: stage installed files for Inno
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43076
diff changeset
   286
):
d053d3f10b6a packaging: stage installed files for Inno
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43076
diff changeset
   287
    for source, dest in rules:
d053d3f10b6a packaging: stage installed files for Inno
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43076
diff changeset
   288
        if '*' in source:
d053d3f10b6a packaging: stage installed files for Inno
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43076
diff changeset
   289
            if not dest.endswith('/'):
d053d3f10b6a packaging: stage installed files for Inno
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43076
diff changeset
   290
                raise ValueError('destination must end in / when globbing')
d053d3f10b6a packaging: stage installed files for Inno
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43076
diff changeset
   291
d053d3f10b6a packaging: stage installed files for Inno
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43076
diff changeset
   292
            # We strip off the source path component before the first glob
d053d3f10b6a packaging: stage installed files for Inno
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43076
diff changeset
   293
            # character to construct the relative install path.
d053d3f10b6a packaging: stage installed files for Inno
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43076
diff changeset
   294
            prefix_end_index = source[: source.index('*')].rindex('/')
d053d3f10b6a packaging: stage installed files for Inno
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43076
diff changeset
   295
            relative_prefix = source_dir / source[0:prefix_end_index]
d053d3f10b6a packaging: stage installed files for Inno
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43076
diff changeset
   296
d053d3f10b6a packaging: stage installed files for Inno
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43076
diff changeset
   297
            for res in glob.glob(str(source_dir / source), recursive=True):
d053d3f10b6a packaging: stage installed files for Inno
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43076
diff changeset
   298
                source_path = pathlib.Path(res)
d053d3f10b6a packaging: stage installed files for Inno
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43076
diff changeset
   299
d053d3f10b6a packaging: stage installed files for Inno
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43076
diff changeset
   300
                if source_path.is_dir():
d053d3f10b6a packaging: stage installed files for Inno
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43076
diff changeset
   301
                    continue
d053d3f10b6a packaging: stage installed files for Inno
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43076
diff changeset
   302
d053d3f10b6a packaging: stage installed files for Inno
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43076
diff changeset
   303
                rel_path = source_path.relative_to(relative_prefix)
d053d3f10b6a packaging: stage installed files for Inno
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43076
diff changeset
   304
d053d3f10b6a packaging: stage installed files for Inno
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43076
diff changeset
   305
                dest_path = dest_dir / dest[:-1] / rel_path
d053d3f10b6a packaging: stage installed files for Inno
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43076
diff changeset
   306
d053d3f10b6a packaging: stage installed files for Inno
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43076
diff changeset
   307
                dest_path.parent.mkdir(parents=True, exist_ok=True)
d053d3f10b6a packaging: stage installed files for Inno
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43076
diff changeset
   308
                print('copying %s to %s' % (source_path, dest_path))
d053d3f10b6a packaging: stage installed files for Inno
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43076
diff changeset
   309
                shutil.copy(source_path, dest_path)
d053d3f10b6a packaging: stage installed files for Inno
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43076
diff changeset
   310
d053d3f10b6a packaging: stage installed files for Inno
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43076
diff changeset
   311
        # Simple file case.
d053d3f10b6a packaging: stage installed files for Inno
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43076
diff changeset
   312
        else:
d053d3f10b6a packaging: stage installed files for Inno
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43076
diff changeset
   313
            source_path = pathlib.Path(source)
d053d3f10b6a packaging: stage installed files for Inno
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43076
diff changeset
   314
d053d3f10b6a packaging: stage installed files for Inno
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43076
diff changeset
   315
            if dest.endswith('/'):
d053d3f10b6a packaging: stage installed files for Inno
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43076
diff changeset
   316
                dest_path = pathlib.Path(dest) / source_path.name
d053d3f10b6a packaging: stage installed files for Inno
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43076
diff changeset
   317
            else:
d053d3f10b6a packaging: stage installed files for Inno
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43076
diff changeset
   318
                dest_path = pathlib.Path(dest)
d053d3f10b6a packaging: stage installed files for Inno
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43076
diff changeset
   319
d053d3f10b6a packaging: stage installed files for Inno
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43076
diff changeset
   320
            full_source_path = source_dir / source_path
d053d3f10b6a packaging: stage installed files for Inno
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43076
diff changeset
   321
            full_dest_path = dest_dir / dest_path
d053d3f10b6a packaging: stage installed files for Inno
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43076
diff changeset
   322
d053d3f10b6a packaging: stage installed files for Inno
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43076
diff changeset
   323
            full_dest_path.parent.mkdir(parents=True, exist_ok=True)
d053d3f10b6a packaging: stage installed files for Inno
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43076
diff changeset
   324
            shutil.copy(full_source_path, full_dest_path)
d053d3f10b6a packaging: stage installed files for Inno
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43076
diff changeset
   325
            print('copying %s to %s' % (full_source_path, full_dest_path))
43518
7c9f63a5cb14 packaging: always pass VERSION into Inno invocation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43516
diff changeset
   326
7c9f63a5cb14 packaging: always pass VERSION into Inno invocation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43516
diff changeset
   327
7c9f63a5cb14 packaging: always pass VERSION into Inno invocation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43516
diff changeset
   328
def read_version_py(source_dir):
7c9f63a5cb14 packaging: always pass VERSION into Inno invocation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43516
diff changeset
   329
    """Read the mercurial/__version__.py file to resolve the version string."""
7c9f63a5cb14 packaging: always pass VERSION into Inno invocation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43516
diff changeset
   330
    p = source_dir / 'mercurial' / '__version__.py'
7c9f63a5cb14 packaging: always pass VERSION into Inno invocation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43516
diff changeset
   331
7c9f63a5cb14 packaging: always pass VERSION into Inno invocation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43516
diff changeset
   332
    with p.open('r', encoding='utf-8') as fh:
7c9f63a5cb14 packaging: always pass VERSION into Inno invocation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43516
diff changeset
   333
        m = re.search('version = b"([^"]+)"', fh.read(), re.MULTILINE)
7c9f63a5cb14 packaging: always pass VERSION into Inno invocation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43516
diff changeset
   334
7c9f63a5cb14 packaging: always pass VERSION into Inno invocation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43516
diff changeset
   335
        if not m:
7c9f63a5cb14 packaging: always pass VERSION into Inno invocation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43516
diff changeset
   336
            raise Exception('could not parse %s' % p)
7c9f63a5cb14 packaging: always pass VERSION into Inno invocation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43516
diff changeset
   337
7c9f63a5cb14 packaging: always pass VERSION into Inno invocation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43516
diff changeset
   338
        return m.group(1)