| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import os |
| 4 |
import subprocess |
| 5 |
from collections.abc import Collection |
| 6 |
|
| 7 |
from briefcase.commands import ( |
| 8 |
BuildCommand, |
| 9 |
CreateCommand, |
| 10 |
DevCommand, |
| 11 |
PackageCommand, |
| 12 |
PublishCommand, |
| 13 |
RunCommand, |
| 14 |
UpdateCommand, |
| 15 |
) |
| 16 |
from briefcase.config import AppConfig |
| 17 |
from briefcase.exceptions import ( |
| 18 |
BriefcaseCommandError, |
| 19 |
BriefcaseConfigError, |
| 20 |
UnsupportedHostError, |
| 21 |
) |
| 22 |
from briefcase.integrations.docker import Docker, DockerAppContext |
| 23 |
from briefcase.integrations.linuxdeploy import LinuxDeploy |
| 24 |
from briefcase.integrations.subprocess import NativeAppContext |
| 25 |
from briefcase.platforms.linux import ( |
| 26 |
DockerOpenCommand, |
| 27 |
LinuxMixin, |
| 28 |
LocalRequirementsMixin, |
| 29 |
) |
| 30 |
|
| 31 |
|
| 32 |
class LinuxAppImagePassiveMixin(LinuxMixin): |
| 33 |
# The Passive mixin honors the docker options, but doesn't try to verify |
| 34 |
# docker exists. It is used by commands that are "passive" from the |
| 35 |
# perspective of the build system, like open and run. |
| 36 |
output_format = "appimage" |
| 37 |
supported_host_os: Collection[str] = {"Darwin", "Linux"} |
| 38 |
supported_host_os_reason = ( |
| 39 |
"Linux AppImages can only be built on Linux, or on macOS using Docker." |
| 40 |
) |
| 41 |
platform_target_version: str | None = "0.3.20" |
| 42 |
|
| 43 |
def appdir_path(self, app): |
| 44 |
return self.bundle_path(app) / f"{app.formal_name}.AppDir" |
| 45 |
|
| 46 |
def project_path(self, app): |
| 47 |
return self.bundle_path(app) |
| 48 |
|
| 49 |
def binary_name(self, app): |
| 50 |
safe_name = app.formal_name.replace(" ", "_") |
| 51 |
arch = LinuxDeploy.arch(self.tools) |
| 52 |
return f"{safe_name}-{app.version}-{arch}.AppImage" |
| 53 |
|
| 54 |
def binary_path(self, app): |
| 55 |
return self.bundle_path(app) / self.binary_name(app) |
| 56 |
|
| 57 |
def distribution_path(self, app): |
| 58 |
return self.dist_path / self.binary_name(app) |
| 59 |
|
| 60 |
def verify_tools(self): |
| 61 |
"""Verify the AppImage LinuxDeploy tool and its plugins exist.""" |
| 62 |
super().verify_tools() |
| 63 |
LinuxDeploy.verify(tools=self.tools) |
| 64 |
|
| 65 |
def add_options(self, parser): |
| 66 |
super().add_options(parser) |
| 67 |
parser.add_argument( |
| 68 |
"--no-docker", |
| 69 |
dest="use_docker", |
| 70 |
action="store_false", |
| 71 |
help="Don't use Docker for building the AppImage", |
| 72 |
required=False, |
| 73 |
) |
| 74 |
parser.add_argument( |
| 75 |
"--Xdocker-build", |
| 76 |
action="append", |
| 77 |
dest="extra_docker_build_args", |
| 78 |
help="Additional arguments to use when building the Docker image", |
| 79 |
required=False, |
| 80 |
) |
| 81 |
|
| 82 |
def parse_options(self, extra): |
| 83 |
"""Extract the use_docker option.""" |
| 84 |
options, overrides = super().parse_options(extra) |
| 85 |
self.use_docker = options.pop("use_docker") |
| 86 |
self.extra_docker_build_args = options.pop("extra_docker_build_args") |
| 87 |
return options, overrides |
| 88 |
|
| 89 |
def clone_options(self, command): |
| 90 |
"""Clone the use_docker option.""" |
| 91 |
super().clone_options(command) |
| 92 |
self.use_docker = command.use_docker |
| 93 |
self.extra_docker_build_args = command.extra_docker_build_args |
| 94 |
|
| 95 |
def finalize_app_config(self, app: AppConfig): |
| 96 |
"""If we're *not* using Docker, warn the user about portability.""" |
| 97 |
if not self.use_docker: |
| 98 |
self.console.warning("""\ |
| 99 |
************************************************************************* |
| 100 |
** WARNING: Building a Local AppImage! ** |
| 101 |
************************************************************************* |
| 102 |
|
| 103 |
You are building an AppImage outside Docker. The resulting AppImage |
| 104 |
will work, but will not be as portable as a Docker-based AppImage. |
| 105 |
Any `manylinux` setting will be ignored. |
| 106 |
|
| 107 |
************************************************************************* |
| 108 |
""") |
| 109 |
|
| 110 |
self.console.warning("""\ |
| 111 |
************************************************************************* |
| 112 |
** WARNING: Use of AppImage is not recommended! ** |
| 113 |
************************************************************************* |
| 114 |
|
| 115 |
Briefcase supports AppImage in a best-effort capacity. It has proven |
| 116 |
to be highly unreliable as a distribution platform. AppImages cannot |
| 117 |
use pre-compiled binary wheels, and has significant problems with |
| 118 |
most commonly used GUI toolkits (including GTK and PySide). |
| 119 |
|
| 120 |
Consider using system packages or Flatpak for Linux app |
| 121 |
distribution. |
| 122 |
|
| 123 |
************************************************************************* |
| 124 |
""") |
| 125 |
|
| 126 |
|
| 127 |
class LinuxAppImageMostlyPassiveMixin(LinuxAppImagePassiveMixin): |
| 128 |
# The Mostly Passive mixin verifies that Docker exists and can be run, but |
| 129 |
# doesn't require that we're actually in a Linux environment. |
| 130 |
def docker_image_tag(self, app): |
| 131 |
"""The Docker image tag for an app.""" |
| 132 |
try: |
| 133 |
return f"briefcase/{app.bundle_identifier.lower()}:{app.manylinux}-appimage" |
| 134 |
except AttributeError: |
| 135 |
return f"briefcase/{app.bundle_identifier.lower()}:appimage" |
| 136 |
|
| 137 |
def verify_tools(self): |
| 138 |
"""If we're using docker, verify that it is available.""" |
| 139 |
super().verify_tools() |
| 140 |
if self.use_docker: |
| 141 |
Docker.verify(tools=self.tools) |
| 142 |
|
| 143 |
def verify_app_tools(self, app: AppConfig): |
| 144 |
"""Verify App environment is prepared and available. |
| 145 |
|
| 146 |
When Docker is used, create or update a Docker image for the App. Without |
| 147 |
Docker, the host machine will be used as the App environment. |
| 148 |
|
| 149 |
:param app: The application being built |
| 150 |
""" |
| 151 |
if self.use_docker: |
| 152 |
DockerAppContext.verify( |
| 153 |
tools=self.tools, |
| 154 |
app=app, |
| 155 |
image_tag=self.docker_image_tag(app), |
| 156 |
dockerfile_path=self.bundle_path(app) / "Dockerfile", |
| 157 |
app_base_path=self.base_path, |
| 158 |
host_bundle_path=self.bundle_path(app), |
| 159 |
host_data_path=self.data_path, |
| 160 |
python_version=self.python_version_tag, |
| 161 |
extra_build_args=self.extra_docker_build_args, |
| 162 |
) |
| 163 |
else: |
| 164 |
NativeAppContext.verify(tools=self.tools, app=app) |
| 165 |
|
| 166 |
# Establish Docker as app context before letting super set subprocess |
| 167 |
super().verify_app_tools(app) |
| 168 |
|
| 169 |
|
| 170 |
class LinuxAppImageMixin(LinuxAppImageMostlyPassiveMixin): |
| 171 |
def verify_host(self): |
| 172 |
"""If we're *not* using Docker, verify that we're actually on Linux.""" |
| 173 |
super().verify_host() |
| 174 |
if not self.use_docker and self.tools.host_os != "Linux": |
| 175 |
raise UnsupportedHostError(self.supported_host_os_reason) |
| 176 |
|
| 177 |
|
| 178 |
class LinuxAppImageCreateCommand( |
| 179 |
LinuxAppImageMixin, |
| 180 |
LocalRequirementsMixin, |
| 181 |
CreateCommand, |
| 182 |
): |
| 183 |
description = "Create and populate a Linux AppImage." |
| 184 |
|
| 185 |
def output_format_template_context(self, app: AppConfig): |
| 186 |
context = super().output_format_template_context(app) |
| 187 |
|
| 188 |
try: |
| 189 |
manylinux_arch = { |
| 190 |
"x86_64": "x86_64", |
| 191 |
"i386": "i686", |
| 192 |
"aarch64": "aarch64", |
| 193 |
}[LinuxDeploy.arch(self.tools)] |
| 194 |
except KeyError: |
| 195 |
manylinux_arch = LinuxDeploy.arch(self.tools) |
| 196 |
self.console.warning( |
| 197 |
f"There is no manylinux base image for {manylinux_arch}" |
| 198 |
) |
| 199 |
|
| 200 |
# Add the manylinux tag to the template context. |
| 201 |
try: |
| 202 |
tag = getattr(app, "manylinux_image_tag", "latest") |
| 203 |
context["manylinux_image"] = f"{app.manylinux}_{manylinux_arch}:{tag}" |
| 204 |
if app.manylinux in {"manylinux1", "manylinux2010", "manylinux2014"}: |
| 205 |
context["vendor_base"] = "centos" |
| 206 |
elif app.manylinux == "manylinux_2_24": |
| 207 |
context["vendor_base"] = "debian" |
| 208 |
elif app.manylinux.startswith("manylinux_2_"): |
| 209 |
context["vendor_base"] = "almalinux" |
| 210 |
else: |
| 211 |
raise BriefcaseConfigError(f"Unknown manylinux tag {app.manylinux!r}") |
| 212 |
except AttributeError: |
| 213 |
pass |
| 214 |
|
| 215 |
# Use the non-root user if Docker is not mapping usernames |
| 216 |
try: |
| 217 |
context["use_non_root_user"] = not self.tools.docker.is_user_mapped |
| 218 |
except AttributeError: |
| 219 |
pass # ignore if not using Docker |
| 220 |
|
| 221 |
return context |
| 222 |
|
| 223 |
def _cleanup_app_support_package(self, support_path): |
| 224 |
# On Windows, the support path is co-mingled with app content. |
| 225 |
# This means updating the support package is imperfect. |
| 226 |
# Warn the user that there could be problems. |
| 227 |
self.console.warning(""" |
| 228 |
************************************************************************* |
| 229 |
** WARNING: Support package update may be imperfect ** |
| 230 |
************************************************************************* |
| 231 |
|
| 232 |
Support packages in Linux AppImages are overlaid with app content, |
| 233 |
so it isn't possible to remove all old support files before |
| 234 |
installing new ones. |
| 235 |
|
| 236 |
Briefcase will unpack the new support package without cleaning up |
| 237 |
existing support package content. This *should* work; however, |
| 238 |
ensure a reproducible release artefacts, it is advisable to |
| 239 |
perform a clean app build before release. |
| 240 |
|
| 241 |
************************************************************************* |
| 242 |
""") |
| 243 |
|
| 244 |
|
| 245 |
class LinuxAppImageUpdateCommand(LinuxAppImageCreateCommand, UpdateCommand): |
| 246 |
description = "Update an existing Linux AppImage." |
| 247 |
|
| 248 |
|
| 249 |
class LinuxAppImageOpenCommand(LinuxAppImageMostlyPassiveMixin, DockerOpenCommand): |
| 250 |
description = ( |
| 251 |
"Open a shell in a Docker container for an existing Linux AppImage project." |
| 252 |
) |
| 253 |
|
| 254 |
|
| 255 |
class LinuxAppImageBuildCommand(LinuxAppImageMixin, BuildCommand): |
| 256 |
description = "Build a Linux AppImage." |
| 257 |
|
| 258 |
def build_app(self, app: AppConfig, **kwargs): # pragma: no-cover-if-is-windows |
| 259 |
"""Build an application. |
| 260 |
|
| 261 |
:param app: The application to build |
| 262 |
""" |
| 263 |
# Build a dictionary of environment definitions that are required |
| 264 |
env = {} |
| 265 |
|
| 266 |
self.console.info("Checking for Linuxdeploy plugins...", prefix=app.app_name) |
| 267 |
try: |
| 268 |
plugins = self.tools.linuxdeploy.verify_plugins( |
| 269 |
app.linuxdeploy_plugins, |
| 270 |
bundle_path=self.bundle_path(app), |
| 271 |
) |
| 272 |
|
| 273 |
self.console.info("Configuring Linuxdeploy plugins...", prefix=app.app_name) |
| 274 |
# We need to add the location of the linuxdeploy plugins to the PATH. |
| 275 |
# However, if we are running inside Docker, we need to know the |
| 276 |
# environment *inside* the Docker container. |
| 277 |
echo_cmd = ["/bin/sh", "-c", "echo $PATH"] |
| 278 |
base_path = self.tools[app].app_context.check_output(echo_cmd).strip() |
| 279 |
|
| 280 |
# Add any plugin-required environment variables |
| 281 |
for plugin in plugins.values(): |
| 282 |
env.update(plugin.env) |
| 283 |
|
| 284 |
# Construct a path that has been prepended with the path to the plugins |
| 285 |
env["PATH"] = os.pathsep.join( |
| 286 |
[os.fsdecode(plugin.file_path) for plugin in plugins.values()] |
| 287 |
+ [base_path] |
| 288 |
) |
| 289 |
except AttributeError: |
| 290 |
self.console.info("No linuxdeploy plugins configured.") |
| 291 |
plugins = {} |
| 292 |
|
| 293 |
self.console.info("Building AppImage...", prefix=app.app_name) |
| 294 |
with self.console.wait_bar("Building..."): |
| 295 |
try: |
| 296 |
# For some reason, the version has to be passed in as an |
| 297 |
# environment variable, *not* in the configuration. |
| 298 |
env["LINUXDEPLOY_OUTPUT_VERSION"] = str(app.version) |
| 299 |
# The internals of the binary aren't inherently visible, so |
| 300 |
# there's no need to package copyright files. These files |
| 301 |
# appear to be missing by default in the OS dev packages anyway, |
| 302 |
# so this effectively silences a bunch of warnings that can't |
| 303 |
# be easily resolved by the end user. |
| 304 |
env["DISABLE_COPYRIGHT_FILES_DEPLOYMENT"] = "1" |
| 305 |
# AppImages do not run natively within a Docker container. This |
| 306 |
# treats the AppImage like a self-extracting executable. Using |
| 307 |
# this environment variable instead of --appimage-extract-and-run |
| 308 |
# is necessary to ensure AppImage plugins are extracted as well. |
| 309 |
env["APPIMAGE_EXTRACT_AND_RUN"] = "1" |
| 310 |
# Explicitly declare target architecture as the current architecture. |
| 311 |
# This can be used by some linuxdeploy plugins. |
| 312 |
env["ARCH"] = self.tools.host_arch |
| 313 |
|
| 314 |
# Enable debug logging for linuxdeploy GTK and Qt plugins |
| 315 |
if self.console.is_deep_debug: |
| 316 |
env["DEBUG"] = "1" |
| 317 |
|
| 318 |
# Find all the .so files in app and app_packages, |
| 319 |
# so they can be passed in to linuxdeploy to have their |
| 320 |
# requirements added to the AppImage. Looks for any .so file |
| 321 |
# in the application, and make sure it is marked for deployment. |
| 322 |
so_folders = { |
| 323 |
so_file.parent for so_file in self.appdir_path(app).glob("**/*.so") |
| 324 |
} |
| 325 |
|
| 326 |
additional_args = [] |
| 327 |
for folder in sorted(so_folders): |
| 328 |
additional_args.extend(["--deploy-deps-only", str(folder)]) |
| 329 |
|
| 330 |
for plugin in plugins: |
| 331 |
additional_args.extend(["--plugin", plugin]) |
| 332 |
|
| 333 |
# Build the AppImage. |
| 334 |
self.tools[app].app_context.run( |
| 335 |
[ |
| 336 |
self.tools.linuxdeploy.file_path |
| 337 |
/ self.tools.linuxdeploy.file_name, |
| 338 |
"--appdir", |
| 339 |
self.appdir_path(app), |
| 340 |
"--desktop-file", |
| 341 |
self.appdir_path(app) / f"{app.bundle_identifier}.desktop", |
| 342 |
"--output", |
| 343 |
"appimage", |
| 344 |
"-v0" if self.console.is_deep_debug else "-v1", |
| 345 |
*additional_args, |
| 346 |
], |
| 347 |
env=env, |
| 348 |
check=True, |
| 349 |
cwd=self.bundle_path(app), |
| 350 |
) |
| 351 |
|
| 352 |
# Make the binary executable. |
| 353 |
self.tools.os.chmod(self.binary_path(app), 0o755) |
| 354 |
except subprocess.CalledProcessError as e: |
| 355 |
raise BriefcaseCommandError( |
| 356 |
f"Error while building app {app.app_name}." |
| 357 |
) from e |
| 358 |
|
| 359 |
|
| 360 |
class LinuxAppImageRunCommand(LinuxAppImagePassiveMixin, RunCommand): |
| 361 |
description = "Run a Linux AppImage." |
| 362 |
supported_host_os: Collection[str] = {"Linux"} |
| 363 |
supported_host_os_reason = "Linux AppImages can only be executed on Linux." |
| 364 |
|
| 365 |
def run_app( |
| 366 |
self, |
| 367 |
app: AppConfig, |
| 368 |
passthrough: list[str], |
| 369 |
**kwargs, |
| 370 |
): |
| 371 |
"""Start the application. |
| 372 |
|
| 373 |
:param app: The config object for the app |
| 374 |
:param passthrough: The list of arguments to pass to the app |
| 375 |
""" |
| 376 |
# Set up the log stream |
| 377 |
kwargs = self._prepare_app_kwargs(app=app) |
| 378 |
|
| 379 |
# Console apps must operate in non-streaming mode so that console input can |
| 380 |
# be handled correctly. However, if we're in test mode, we *must* stream so |
| 381 |
# that we can see the test exit sentinel |
| 382 |
if app.console_app and not app.test_mode: |
| 383 |
self.console.info("=" * 75) |
| 384 |
self.tools.subprocess.run( |
| 385 |
[self.binary_path(app), *passthrough], |
| 386 |
cwd=self.tools.home_path, |
| 387 |
bufsize=1, |
| 388 |
stream_output=False, |
| 389 |
**kwargs, |
| 390 |
) |
| 391 |
else: |
| 392 |
# Start the app in a way that lets us stream the logs |
| 393 |
app_popen = self.tools.subprocess.Popen( |
| 394 |
[self.binary_path(app), *passthrough], |
| 395 |
cwd=self.tools.home_path, |
| 396 |
**kwargs, |
| 397 |
stdout=subprocess.PIPE, |
| 398 |
stderr=subprocess.STDOUT, |
| 399 |
bufsize=1, |
| 400 |
) |
| 401 |
|
| 402 |
# Start streaming logs for the app. |
| 403 |
self._stream_app_logs( |
| 404 |
app, |
| 405 |
popen=app_popen, |
| 406 |
clean_output=False, |
| 407 |
) |
| 408 |
|
| 409 |
|
| 410 |
class LinuxAppImageDevCommand(LinuxAppImageMixin, DevCommand): |
| 411 |
description = "Run a Linux AppImage app in development mode" |
| 412 |
|
| 413 |
|
| 414 |
class LinuxAppImagePackageCommand(LinuxAppImageMixin, PackageCommand): |
| 415 |
description = "Package a Linux AppImage." |
| 416 |
|
| 417 |
def package_app(self, app: AppConfig, **kwargs): |
| 418 |
"""Package an AppImage. |
| 419 |
|
| 420 |
:param app: The application to package |
| 421 |
""" |
| 422 |
self.tools.shutil.copy(self.binary_path(app), self.distribution_path(app)) |
| 423 |
|
| 424 |
|
| 425 |
class LinuxAppImagePublishCommand(LinuxAppImageMixin, PublishCommand): |
| 426 |
description = "Publish a Linux AppImage." |
| 427 |
|
| 428 |
|
| 429 |
# Declare the briefcase command bindings |
| 430 |
create = LinuxAppImageCreateCommand |
| 431 |
update = LinuxAppImageUpdateCommand |
| 432 |
open = LinuxAppImageOpenCommand |
| 433 |
build = LinuxAppImageBuildCommand |
| 434 |
run = LinuxAppImageRunCommand |
| 435 |
package = LinuxAppImagePackageCommand |
| 436 |
publish = LinuxAppImagePublishCommand |
| 437 |
dev = LinuxAppImageDevCommand |