| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import sys |
| 4 |
from operator import attrgetter |
| 5 |
|
| 6 |
from briefcase.exceptions import ( |
| 7 |
BriefcaseCommandError, |
| 8 |
UnsupportedHostError, |
| 9 |
UpgradeToolError, |
| 10 |
) |
| 11 |
from briefcase.integrations.base import ManagedTool, Tool, tool_registry |
| 12 |
|
| 13 |
from .base import BaseCommand |
| 14 |
|
| 15 |
|
| 16 |
class UpgradeCommand(BaseCommand): |
| 17 |
cmd_line = "briefcase upgrade" |
| 18 |
command = "upgrade" |
| 19 |
output_format = "" |
| 20 |
description = "Upgrade Briefcase-managed tools." |
| 21 |
|
| 22 |
@property |
| 23 |
def platform(self): |
| 24 |
"""The upgrade command always reports as the local platform.""" |
| 25 |
return { |
| 26 |
"darwin": "macOS", |
| 27 |
"linux": "linux", |
| 28 |
"win32": "windows", |
| 29 |
}[sys.platform] |
| 30 |
|
| 31 |
def bundle_path(self, app): |
| 32 |
"""A placeholder; Upgrade command doesn't have a bundle path.""" |
| 33 |
raise NotImplementedError() |
| 34 |
|
| 35 |
def binary_path(self, app): |
| 36 |
"""A placeholder; Upgrade command doesn't have a binary path.""" |
| 37 |
raise NotImplementedError() |
| 38 |
|
| 39 |
def add_options(self, parser): |
| 40 |
parser.add_argument( |
| 41 |
"-l", |
| 42 |
"--list", |
| 43 |
dest="list_tools", |
| 44 |
action="store_true", |
| 45 |
help="List the Briefcase-managed tools that are currently installed", |
| 46 |
) |
| 47 |
parser.add_argument( |
| 48 |
"tool_list", |
| 49 |
metavar="tool", |
| 50 |
nargs="*", |
| 51 |
help=( |
| 52 |
"The Briefcase-managed tool to upgrade. " |
| 53 |
"If no tool is named, all tools will be upgraded" |
| 54 |
), |
| 55 |
) |
| 56 |
|
| 57 |
def get_tools_to_upgrade(self, tool_list: set[str]) -> list[ManagedTool]: |
| 58 |
"""Returns set of managed Tools that can be upgraded. |
| 59 |
|
| 60 |
Raises ``BriefcaseCommandError`` if user list contains any invalid tool names. |
| 61 |
""" |
| 62 |
upgrade_list: set[type[Tool]] |
| 63 |
tools_to_upgrade: set[ManagedTool] = set() |
| 64 |
|
| 65 |
# Validate user tool list against tool registry |
| 66 |
if tool_list: |
| 67 |
if invalid_tools := tool_list - set(tool_registry): |
| 68 |
raise UpgradeToolError( |
| 69 |
"Briefcase does not know how to manage " |
| 70 |
f"{', '.join(sorted(invalid_tools))}." |
| 71 |
) |
| 72 |
upgrade_list = { |
| 73 |
tool for name, tool in tool_registry.items() if name in tool_list |
| 74 |
} |
| 75 |
else: |
| 76 |
upgrade_list = set(tool_registry.values()) |
| 77 |
|
| 78 |
# Filter list of tools to those that are being managed |
| 79 |
for tool_klass in upgrade_list: |
| 80 |
if issubclass(tool_klass, ManagedTool): |
| 81 |
try: |
| 82 |
tool = tool_klass.verify(tools=self.tools, install=False) |
| 83 |
except (BriefcaseCommandError, UnsupportedHostError): |
| 84 |
pass |
| 85 |
else: |
| 86 |
if tool.managed_install: |
| 87 |
tools_to_upgrade.add(tool) |
| 88 |
|
| 89 |
# Let the user know if any requested tools are not being managed |
| 90 |
if tool_list: |
| 91 |
unmanaged_tools = tool_list - {tool.name for tool in tools_to_upgrade} |
| 92 |
if unmanaged_tools: |
| 93 |
error_msg = ( |
| 94 |
f"Briefcase is not managing {', '.join(sorted(unmanaged_tools))}." |
| 95 |
) |
| 96 |
if not tools_to_upgrade: |
| 97 |
raise UpgradeToolError(error_msg) |
| 98 |
else: |
| 99 |
self.console.warning(error_msg) |
| 100 |
|
| 101 |
return sorted(tools_to_upgrade, key=attrgetter("name")) |
| 102 |
|
| 103 |
def __call__(self, tool_list: list[str], list_tools: bool = False, **options): |
| 104 |
"""Perform tool upgrades or list tools qualifying for upgrade. |
| 105 |
|
| 106 |
:param tool_list: List of tool names from user to upgrade. |
| 107 |
:param list_tools: Boolean to only list upgradeable tools (default False). |
| 108 |
""" |
| 109 |
if tools_to_upgrade := self.get_tools_to_upgrade(set(tool_list)): |
| 110 |
self.console.info( |
| 111 |
( |
| 112 |
f"Briefcase {'is managing' if list_tools else 'will upgrade'} " |
| 113 |
f"the following tools:" |
| 114 |
), |
| 115 |
prefix=self.command, |
| 116 |
) |
| 117 |
for tool in tools_to_upgrade: |
| 118 |
self.console.info(f" - {tool.full_name} ({tool.name})") |
| 119 |
|
| 120 |
if not list_tools: |
| 121 |
for tool in tools_to_upgrade: |
| 122 |
self.console.info( |
| 123 |
f"Upgrading {tool.full_name}...", prefix=tool.name |
| 124 |
) |
| 125 |
tool.upgrade() |
| 126 |
else: |
| 127 |
self.console.info("Briefcase is not managing any tools.") |