| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import re |
| 4 |
from collections.abc import Collection |
| 5 |
from pathlib import Path |
| 6 |
from subprocess import CalledProcessError |
| 7 |
|
| 8 |
from packaging.version import Version |
| 9 |
|
| 10 |
from briefcase.exceptions import BriefcaseCommandError, MissingToolError |
| 11 |
from briefcase.integrations.base import ManagedTool, ToolCache |
| 12 |
|
| 13 |
|
| 14 |
class WiX(ManagedTool): |
| 15 |
name = "wix" |
| 16 |
full_name = "WiX" |
| 17 |
supported_host_os: Collection[str] = {"Windows"} |
| 18 |
|
| 19 |
# WARNING: version 6 and later have licensing issues: see |
| 20 |
# https://github.com/beeware/briefcase/issues/1185. |
| 21 |
version = Version("5.0.2") |
| 22 |
|
| 23 |
def __init__(self, tools: ToolCache, wix_home: Path | None = None): |
| 24 |
"""Create a wrapper around a WiX install. |
| 25 |
|
| 26 |
:param tools: ToolCache of available tools. |
| 27 |
:param wix_home: The path of the WiX installation. |
| 28 |
""" |
| 29 |
super().__init__(tools=tools) |
| 30 |
if wix_home: |
| 31 |
self.wix_home = wix_home |
| 32 |
else: |
| 33 |
self.wix_home = tools.base_path / "wix" |
| 34 |
|
| 35 |
@property |
| 36 |
def download_url(self) -> str: |
| 37 |
return ( |
| 38 |
f"https://github.com/wixtoolset/wix/releases/download/v{self.version}/" |
| 39 |
f"wix-cli-x64.msi" |
| 40 |
) |
| 41 |
|
| 42 |
@property |
| 43 |
def wix_exe(self) -> Path: |
| 44 |
return ( |
| 45 |
self.wix_home |
| 46 |
/ f"PFiles64/WiX Toolset v{self.version.major}.{self.version.minor}/bin" |
| 47 |
/ "wix.exe" |
| 48 |
) |
| 49 |
|
| 50 |
def ext_path(self, name: str) -> Path: |
| 51 |
return ( |
| 52 |
self.wix_home |
| 53 |
/ f"CFiles64/WixToolset/extensions/WixToolset.{name}.wixext/{self.version}" |
| 54 |
/ f"wixext{self.version.major}/WixToolset.{name}.wixext.dll" |
| 55 |
) |
| 56 |
|
| 57 |
@classmethod |
| 58 |
def verify_install(cls, tools: ToolCache, install: bool = True, **kwargs) -> WiX: |
| 59 |
"""Verify that there is a WiX install available. |
| 60 |
|
| 61 |
WiX is a small tool, and there's a close relationship between the WiX version |
| 62 |
and the template syntax, so we always use a Briefcase-managed copy, and upgrade |
| 63 |
it automatically. |
| 64 |
|
| 65 |
:param tools: ToolCache of available tools |
| 66 |
:param install: Should WiX be installed if it is not found? |
| 67 |
""" |
| 68 |
# short circuit since already verified and available |
| 69 |
if hasattr(tools, "wix"): |
| 70 |
return tools.wix |
| 71 |
|
| 72 |
wix = WiX(tools) |
| 73 |
if not wix.exists(): |
| 74 |
if install: |
| 75 |
tools.console.info( |
| 76 |
"The WiX toolset was not found; downloading and installing...", |
| 77 |
prefix=cls.name, |
| 78 |
) |
| 79 |
wix.install() |
| 80 |
else: |
| 81 |
raise MissingToolError("WiX") |
| 82 |
else: |
| 83 |
try: |
| 84 |
# The string returned by --version may include "+" followed by a |
| 85 |
# commit ID; ignore this. |
| 86 |
installed_version = re.sub( |
| 87 |
r"\+.*", |
| 88 |
"", |
| 89 |
tools.subprocess.check_output([wix.wix_exe, "--version"]), |
| 90 |
).strip() |
| 91 |
except (OSError, CalledProcessError) as e: |
| 92 |
installed_version = None |
| 93 |
tools.console.error( |
| 94 |
f"The WiX toolset is unusable ({type(e).__name__}: {e})" |
| 95 |
) |
| 96 |
|
| 97 |
if installed_version != str(wix.version): |
| 98 |
if installed_version is not None: |
| 99 |
tools.console.info( |
| 100 |
f"The WiX toolset is an unsupported version " |
| 101 |
f"({installed_version})" |
| 102 |
) |
| 103 |
if install: |
| 104 |
wix.upgrade() |
| 105 |
else: |
| 106 |
raise MissingToolError("WiX") |
| 107 |
|
| 108 |
tools.console.debug(f"Using WiX at {wix.wix_home}") |
| 109 |
tools.wix = wix |
| 110 |
return wix |
| 111 |
|
| 112 |
def exists(self) -> bool: |
| 113 |
return self.wix_exe.is_file() |
| 114 |
|
| 115 |
@property |
| 116 |
def managed_install(self) -> bool: |
| 117 |
return True |
| 118 |
|
| 119 |
def install(self): |
| 120 |
"""Download and install WiX.""" |
| 121 |
wix_msi_path = self.tools.file.download( |
| 122 |
url=self.download_url, |
| 123 |
download_path=self.tools.base_path, |
| 124 |
role="WiX", |
| 125 |
) |
| 126 |
|
| 127 |
try: |
| 128 |
with self.tools.console.wait_bar("Installing WiX..."): |
| 129 |
self.tools.subprocess.run( |
| 130 |
[ |
| 131 |
"msiexec", |
| 132 |
"/a", # Unpack the MSI into individual files |
| 133 |
wix_msi_path, |
| 134 |
"/qn", # Disable GUI interaction |
| 135 |
f"TARGETDIR={self.wix_home}", |
| 136 |
], |
| 137 |
check=True, |
| 138 |
) |
| 139 |
except CalledProcessError as e: |
| 140 |
raise BriefcaseCommandError( |
| 141 |
f"""\ |
| 142 |
Unable to unpack WiX MSI file. The download may have been |
| 143 |
interrupted or corrupted. |
| 144 |
|
| 145 |
Delete {wix_msi_path} and run briefcase again. |
| 146 |
""" |
| 147 |
) from e |
| 148 |
|
| 149 |
wix_msi_path.unlink() |
| 150 |
|
| 151 |
def uninstall(self): |
| 152 |
"""Uninstall WiX.""" |
| 153 |
with self.tools.console.wait_bar("Removing old WiX install..."): |
| 154 |
self.tools.shutil.rmtree(self.wix_home) |