| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import collections |
| 4 |
import locale |
| 5 |
import os |
| 6 |
import platform |
| 7 |
import shutil |
| 8 |
import sys |
| 9 |
from abc import ABC, abstractmethod |
| 10 |
from collections import defaultdict |
| 11 |
from collections.abc import Collection, Mapping |
| 12 |
from functools import cached_property |
| 13 |
from pathlib import Path |
| 14 |
from typing import TYPE_CHECKING, TypeVar |
| 15 |
|
| 16 |
import httpx |
| 17 |
from cookiecutter.main import cookiecutter |
| 18 |
|
| 19 |
from briefcase.config import AppConfig |
| 20 |
from briefcase.console import Console |
| 21 |
from briefcase.exceptions import ( |
| 22 |
MissingToolError, |
| 23 |
NonManagedToolError, |
| 24 |
UnsupportedHostError, |
| 25 |
) |
| 26 |
|
| 27 |
if TYPE_CHECKING: |
| 28 |
# Tools are imported only for type checking |
| 29 |
# to avoid circular import errors. |
| 30 |
import git as git_ |
| 31 |
|
| 32 |
from briefcase.integrations.android_sdk import AndroidSDK |
| 33 |
from briefcase.integrations.docker import Docker, DockerAppContext |
| 34 |
from briefcase.integrations.file import File |
| 35 |
from briefcase.integrations.flatpak import Flatpak |
| 36 |
from briefcase.integrations.java import JDK |
| 37 |
from briefcase.integrations.linuxdeploy import LinuxDeploy |
| 38 |
from briefcase.integrations.rcedit import RCEdit |
| 39 |
from briefcase.integrations.subprocess import Subprocess |
| 40 |
from briefcase.integrations.virtual_environment import VirtualEnvironment |
| 41 |
from briefcase.integrations.visualstudio import VisualStudio |
| 42 |
from briefcase.integrations.windows_sdk import WindowsSDK |
| 43 |
from briefcase.integrations.wix import WiX |
| 44 |
from briefcase.integrations.xcode import Xcode, XcodeCliTools |
| 45 |
|
| 46 |
ToolT = TypeVar("ToolT", bound="Tool") |
| 47 |
ManagedToolT = TypeVar("ManagedToolT", bound="ManagedTool") |
| 48 |
|
| 49 |
# Registry of all defined Tools |
| 50 |
tool_registry: dict[str, type[Tool | ManagedTool]] = {} |
| 51 |
|
| 52 |
DEFAULT_SYSTEM_ENCODING = "UTF-8" |
| 53 |
|
| 54 |
|
| 55 |
class Tool(ABC): |
| 56 |
"""Tool Base.""" |
| 57 |
|
| 58 |
name: str |
| 59 |
full_name: str |
| 60 |
supported_host_os: Collection[str] = {"Darwin", "Linux", "Windows"} |
| 61 |
|
| 62 |
def __init__(self, tools: ToolCache, **kwargs): |
| 63 |
self.tools = tools |
| 64 |
|
| 65 |
def __init_subclass__(cls, **kwargs): |
| 66 |
"""Register each tool when it is defined.""" |
| 67 |
if cls.name != "managed_tool_base": |
| 68 |
tool_registry[cls.name] = cls |
| 69 |
|
| 70 |
@classmethod |
| 71 |
def verify( |
| 72 |
cls: type[ToolT], |
| 73 |
tools: ToolCache, |
| 74 |
app: AppConfig | None = None, |
| 75 |
**kwargs, |
| 76 |
) -> ToolT: |
| 77 |
"""Confirm the tool is available and usable on the host platform.""" |
| 78 |
cls.verify_host(tools=tools) |
| 79 |
tool = cls.verify_install(tools=tools, app=app, **kwargs) |
| 80 |
return tool |
| 81 |
|
| 82 |
@classmethod |
| 83 |
def verify_host(cls, tools: ToolCache): |
| 84 |
"""Confirm the tool is supported on the platform.""" |
| 85 |
if tools.host_os not in cls.supported_host_os: |
| 86 |
raise UnsupportedHostError( |
| 87 |
f"{cls.name} is not supported on {tools.host_os}" |
| 88 |
) |
| 89 |
|
| 90 |
@classmethod |
| 91 |
@abstractmethod |
| 92 |
def verify_install(cls: type[ToolT], tools: ToolCache, **kwargs) -> ToolT: |
| 93 |
"""Confirm the tool is installed and available.""" |
| 94 |
|
| 95 |
@property |
| 96 |
def managed_install(self) -> bool: |
| 97 |
"""Is Briefcase managing the installation of this tool?""" |
| 98 |
return False |
| 99 |
|
| 100 |
|
| 101 |
class ManagedTool(Tool): |
| 102 |
"""Tool that can be managed by Briefcase.""" |
| 103 |
|
| 104 |
name = "managed_tool_base" |
| 105 |
|
| 106 |
@classmethod |
| 107 |
def verify( |
| 108 |
cls: type[ManagedToolT], |
| 109 |
tools: ToolCache, |
| 110 |
app: AppConfig | None = None, |
| 111 |
install: bool = True, |
| 112 |
**kwargs, |
| 113 |
) -> ManagedToolT: |
| 114 |
"""Confirm the managed tool is installed and available.""" |
| 115 |
return super().verify(tools=tools, app=app, install=install, **kwargs) |
| 116 |
|
| 117 |
@abstractmethod |
| 118 |
def exists(self) -> bool: |
| 119 |
"""Is the tool currently installed?""" |
| 120 |
|
| 121 |
@abstractmethod |
| 122 |
def install(self): |
| 123 |
"""Install the tool as managed by Briefcase.""" |
| 124 |
|
| 125 |
@abstractmethod |
| 126 |
def uninstall(self): |
| 127 |
"""Uninstall a Briefcase managed tool.""" |
| 128 |
|
| 129 |
@property |
| 130 |
def managed_install(self) -> bool: |
| 131 |
"""Is Briefcase managing the installation of this tool?""" |
| 132 |
return True |
| 133 |
|
| 134 |
def upgrade(self): |
| 135 |
"""Upgrade a managed tool.""" |
| 136 |
if self.managed_install: |
| 137 |
if not self.exists(): |
| 138 |
raise MissingToolError(self.full_name) |
| 139 |
self.uninstall() |
| 140 |
self.install() |
| 141 |
else: |
| 142 |
raise NonManagedToolError(self.full_name) |
| 143 |
|
| 144 |
|
| 145 |
class ToolCache(Mapping): |
| 146 |
# Useful fixed filesystem locations |
| 147 |
ETC_OS_RELEASE: Path = Path("/etc/os-release") |
| 148 |
|
| 149 |
# Briefcase tools |
| 150 |
android_sdk: AndroidSDK |
| 151 |
app_context: Subprocess | DockerAppContext |
| 152 |
docker: Docker |
| 153 |
file: File |
| 154 |
flatpak: Flatpak |
| 155 |
git: git_ |
| 156 |
java: JDK |
| 157 |
linuxdeploy: LinuxDeploy |
| 158 |
rcedit: RCEdit |
| 159 |
subprocess: Subprocess |
| 160 |
virtual_environment: VirtualEnvironment |
| 161 |
visualstudio: VisualStudio |
| 162 |
windows_sdk: WindowsSDK |
| 163 |
wix: WiX |
| 164 |
xcode: Xcode |
| 165 |
xcode_cli: XcodeCliTools |
| 166 |
|
| 167 |
# Python stdlib tools |
| 168 |
platform = platform |
| 169 |
os = os |
| 170 |
shutil = shutil |
| 171 |
sys = sys |
| 172 |
|
| 173 |
# Third party tools |
| 174 |
cookiecutter = staticmethod(cookiecutter) |
| 175 |
httpx = httpx |
| 176 |
|
| 177 |
def __init__( |
| 178 |
self, |
| 179 |
console: Console, |
| 180 |
base_path: Path, |
| 181 |
home_path: Path | None = None, |
| 182 |
): |
| 183 |
"""Cache for managing tool access and verification. |
| 184 |
|
| 185 |
Non-app-specific tools are available via attribute access: |
| 186 |
e.g.: tools.subprocess |
| 187 |
App-specific tools are available via dictionary access: |
| 188 |
e.g.: tools[app].app_context |
| 189 |
|
| 190 |
:param console: Facilitates console interaction and input solicitation. |
| 191 |
:param base_path: Base directory for tools (e.g. ~/.cache/briefcase/tools). |
| 192 |
:param home_path: Home directory for current user. |
| 193 |
""" |
| 194 |
self.console = console |
| 195 |
self.base_path = Path(base_path) |
| 196 |
self.home_path = Path(os.path.expanduser(home_path or Path.home())) |
| 197 |
|
| 198 |
self.host_arch = self.platform.machine() |
| 199 |
self.host_os = self.platform.system() |
| 200 |
# Python is 32bit if its pointers can only address with 32 bits or fewer |
| 201 |
self.is_32bit_python = self.sys.maxsize <= 2**32 |
| 202 |
|
| 203 |
self.app_tools: collections.defaultdict[AppConfig, ToolCache] = defaultdict( |
| 204 |
lambda: ToolCache( |
| 205 |
console=self.console, |
| 206 |
base_path=self.base_path, |
| 207 |
home_path=self.home_path, |
| 208 |
) |
| 209 |
) |
| 210 |
|
| 211 |
@cached_property |
| 212 |
def system_encoding(self) -> str: |
| 213 |
"""The character encoding for the system's locale. |
| 214 |
|
| 215 |
This locale API tries to determine the system's default encoding and generally |
| 216 |
works on typically configured systems; although, there are potential pitfalls |
| 217 |
in certain situations...so, this is best-effort. |
| 218 |
|
| 219 |
This API is used over getpreferredencoding() to avoid respecting Python's UTF-8 |
| 220 |
mode; the system may not be using UTF-8 even if Python is configured to use it. |
| 221 |
|
| 222 |
:returns: a character encoding (upper-cased), e.g. UTF-8. Defaults to UTF-8. |
| 223 |
""" |
| 224 |
if sys.version_info < (3, 11): # pragma: no-cover-if-gte-py311 |
| 225 |
encoding = locale.getdefaultlocale()[1] # deprecated in Python 3.11 |
| 226 |
else: # pragma: no-cover-if-lt-py311 |
| 227 |
encoding = locale.getencoding() |
| 228 |
|
| 229 |
if not encoding: |
| 230 |
encoding = DEFAULT_SYSTEM_ENCODING |
| 231 |
|
| 232 |
return encoding.upper() |
| 233 |
|
| 234 |
def __getitem__(self, app: AppConfig) -> ToolCache: |
| 235 |
return self.app_tools[app] |
| 236 |
|
| 237 |
def __iter__(self): |
| 238 |
return iter(self.app_tools) |
| 239 |
|
| 240 |
def __len__(self) -> int: |
| 241 |
return len(self.app_tools) |
| 242 |
|
| 243 |
def __bool__(self): |
| 244 |
# always True instead of __len__() != 0 |
| 245 |
return True |