| 1 |
# util.py - Common packaging utility code. |
| 2 |
# |
| 3 |
# Copyright 2019 Gregory Szorc <[email protected]> |
| 4 |
# |
| 5 |
# This software may be used and distributed according to the terms of the |
| 6 |
# GNU General Public License version 2 or any later version. |
| 7 |
|
| 8 |
# no-check-code because Python 3 native. |
| 9 |
|
| 10 |
import glob |
| 11 |
import os |
| 12 |
import pathlib |
| 13 |
import re |
| 14 |
import shutil |
| 15 |
import subprocess |
| 16 |
import zipfile |
| 17 |
|
| 18 |
|
| 19 |
def extract_zip_to_directory(source: pathlib.Path, dest: pathlib.Path): |
| 20 |
with zipfile.ZipFile(source, 'r') as zf: |
| 21 |
zf.extractall(dest) |
| 22 |
|
| 23 |
|
| 24 |
def find_vc_runtime_dll(x64=False): |
| 25 |
"""Finds Visual C++ Runtime DLL to include in distribution.""" |
| 26 |
# We invoke vswhere to find the latest Visual Studio install. |
| 27 |
vswhere = ( |
| 28 |
pathlib.Path(os.environ["ProgramFiles(x86)"]) |
| 29 |
/ "Microsoft Visual Studio" |
| 30 |
/ "Installer" |
| 31 |
/ "vswhere.exe" |
| 32 |
) |
| 33 |
|
| 34 |
if not vswhere.exists(): |
| 35 |
raise Exception( |
| 36 |
"could not find vswhere.exe: %s does not exist" % vswhere |
| 37 |
) |
| 38 |
|
| 39 |
args = [ |
| 40 |
str(vswhere), |
| 41 |
# -products * is necessary to return results from Build Tools |
| 42 |
# (as opposed to full IDE installs). |
| 43 |
"-products", |
| 44 |
"*", |
| 45 |
"-requires", |
| 46 |
"Microsoft.VisualCpp.Redist.14.Latest", |
| 47 |
"-latest", |
| 48 |
"-property", |
| 49 |
"installationPath", |
| 50 |
] |
| 51 |
|
| 52 |
vs_install_path = pathlib.Path( |
| 53 |
os.fsdecode(subprocess.check_output(args).strip()) |
| 54 |
) |
| 55 |
|
| 56 |
# This just gets us a path like |
| 57 |
# C:\Program Files (x86)\Microsoft Visual Studio\2019\Community |
| 58 |
# Actually vcruntime140.dll is under a path like: |
| 59 |
# VC\Redist\MSVC\<version>\<arch>\Microsoft.VC14<X>.CRT\vcruntime140.dll. |
| 60 |
|
| 61 |
arch = "x64" if x64 else "x86" |
| 62 |
|
| 63 |
search_glob = ( |
| 64 |
r"%s\VC\Redist\MSVC\*\%s\Microsoft.VC14*.CRT\vcruntime140.dll" |
| 65 |
% (vs_install_path, arch) |
| 66 |
) |
| 67 |
|
| 68 |
candidates = glob.glob(search_glob, recursive=True) |
| 69 |
|
| 70 |
for candidate in reversed(candidates): |
| 71 |
return pathlib.Path(candidate) |
| 72 |
|
| 73 |
raise Exception("could not find vcruntime140.dll") |
| 74 |
|
| 75 |
|
| 76 |
def normalize_windows_version(version): |
| 77 |
"""Normalize Mercurial version string so WiX/Inno accepts it. |
| 78 |
|
| 79 |
Version strings have to be numeric ``A.B.C[.D]`` to conform with MSI's |
| 80 |
requirements. |
| 81 |
|
| 82 |
We normalize RC version or the commit count to a 4th version component. |
| 83 |
We store this in the 4th component because ``A.B.C`` releases do occur |
| 84 |
and we want an e.g. ``5.3rc0`` version to be semantically less than a |
| 85 |
``5.3.1rc2`` version. This requires always reserving the 3rd version |
| 86 |
component for the point release and the ``X.YrcN`` release is always |
| 87 |
point release 0. |
| 88 |
|
| 89 |
In the case of an RC and presence of ``+`` suffix data, we can't use both |
| 90 |
because the version format is limited to 4 components. We choose to use |
| 91 |
RC and throw away the commit count in the suffix. This means we could |
| 92 |
produce multiple installers with the same normalized version string. |
| 93 |
|
| 94 |
>>> normalize_windows_version("5.3") |
| 95 |
'5.3.0' |
| 96 |
|
| 97 |
>>> normalize_windows_version("5.3rc0") |
| 98 |
'5.3.0.0' |
| 99 |
|
| 100 |
>>> normalize_windows_version("5.3rc1") |
| 101 |
'5.3.0.1' |
| 102 |
|
| 103 |
>>> normalize_windows_version("5.3rc1+hg2.abcdef") |
| 104 |
'5.3.0.1' |
| 105 |
|
| 106 |
>>> normalize_windows_version("5.3+hg2.abcdef") |
| 107 |
'5.3.0.2' |
| 108 |
""" |
| 109 |
if '+' in version: |
| 110 |
version, extra = version.split('+', 1) |
| 111 |
else: |
| 112 |
extra = None |
| 113 |
|
| 114 |
# 4.9rc0 |
| 115 |
if version[:-1].endswith('rc'): |
| 116 |
rc = int(version[-1:]) |
| 117 |
version = version[:-3] |
| 118 |
else: |
| 119 |
rc = None |
| 120 |
|
| 121 |
# Ensure we have at least X.Y version components. |
| 122 |
versions = [int(v) for v in version.split('.')] |
| 123 |
while len(versions) < 3: |
| 124 |
versions.append(0) |
| 125 |
|
| 126 |
if len(versions) < 4: |
| 127 |
if rc is not None: |
| 128 |
versions.append(rc) |
| 129 |
elif extra: |
| 130 |
# hg<commit count>.<hash>+<date> |
| 131 |
versions.append(int(extra.split('.')[0][2:])) |
| 132 |
|
| 133 |
return '.'.join('%d' % x for x in versions[0:4]) |
| 134 |
|
| 135 |
|
| 136 |
def process_install_rules( |
| 137 |
rules: list, source_dir: pathlib.Path, dest_dir: pathlib.Path |
| 138 |
): |
| 139 |
for source, dest in rules: |
| 140 |
if '*' in source: |
| 141 |
if not dest.endswith('/'): |
| 142 |
raise ValueError('destination must end in / when globbing') |
| 143 |
|
| 144 |
# We strip off the source path component before the first glob |
| 145 |
# character to construct the relative install path. |
| 146 |
prefix_end_index = source[: source.index('*')].rindex('/') |
| 147 |
relative_prefix = source_dir / source[0:prefix_end_index] |
| 148 |
|
| 149 |
for res in glob.glob(str(source_dir / source), recursive=True): |
| 150 |
source_path = pathlib.Path(res) |
| 151 |
|
| 152 |
if source_path.is_dir(): |
| 153 |
continue |
| 154 |
|
| 155 |
rel_path = source_path.relative_to(relative_prefix) |
| 156 |
|
| 157 |
dest_path = dest_dir / dest[:-1] / rel_path |
| 158 |
|
| 159 |
dest_path.parent.mkdir(parents=True, exist_ok=True) |
| 160 |
print('copying %s to %s' % (source_path, dest_path)) |
| 161 |
shutil.copy(source_path, dest_path) |
| 162 |
|
| 163 |
# Simple file case. |
| 164 |
else: |
| 165 |
source_path = pathlib.Path(source) |
| 166 |
|
| 167 |
if dest.endswith('/'): |
| 168 |
dest_path = pathlib.Path(dest) / source_path.name |
| 169 |
else: |
| 170 |
dest_path = pathlib.Path(dest) |
| 171 |
|
| 172 |
full_source_path = source_dir / source_path |
| 173 |
full_dest_path = dest_dir / dest_path |
| 174 |
|
| 175 |
full_dest_path.parent.mkdir(parents=True, exist_ok=True) |
| 176 |
shutil.copy(full_source_path, full_dest_path) |
| 177 |
print('copying %s to %s' % (full_source_path, full_dest_path)) |
| 178 |
|
| 179 |
|
| 180 |
def read_version_py(source_dir): |
| 181 |
"""Read the mercurial/__version__.py file to resolve the version string.""" |
| 182 |
p = source_dir / 'mercurial' / '__version__.py' |
| 183 |
|
| 184 |
with p.open('r', encoding='utf-8') as fh: |
| 185 |
m = re.search("version = '([^']+)'", fh.read(), re.MULTILINE) |
| 186 |
|
| 187 |
if not m: |
| 188 |
raise Exception('could not parse %s' % p) |
| 189 |
|
| 190 |
return m.group(1) |