| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import gzip |
| 4 |
import re |
| 5 |
import subprocess |
| 6 |
import tarfile |
| 7 |
from collections.abc import Collection |
| 8 |
from pathlib import Path |
| 9 |
|
| 10 |
from briefcase.commands import ( |
| 11 |
BuildCommand, |
| 12 |
CreateCommand, |
| 13 |
DevCommand, |
| 14 |
PackageCommand, |
| 15 |
PublishCommand, |
| 16 |
RunCommand, |
| 17 |
UpdateCommand, |
| 18 |
) |
| 19 |
from briefcase.commands.convert import find_changelog_filename |
| 20 |
from briefcase.config import AppConfig, merge_config |
| 21 |
from briefcase.exceptions import BriefcaseCommandError, UnsupportedHostError |
| 22 |
from briefcase.integrations.docker import Docker, DockerAppContext |
| 23 |
from briefcase.integrations.subprocess import NativeAppContext |
| 24 |
from briefcase.integrations.virtual_environment import VenvContext |
| 25 |
from briefcase.platforms.linux import ( |
| 26 |
ARCH, |
| 27 |
DEBIAN, |
| 28 |
RHEL, |
| 29 |
SUSE, |
| 30 |
DockerOpenCommand, |
| 31 |
LinuxMixin, |
| 32 |
LocalRequirementsMixin, |
| 33 |
parse_freedesktop_os_release, |
| 34 |
) |
| 35 |
|
| 36 |
|
| 37 |
class LinuxSystemMixin(LinuxMixin): |
| 38 |
# The base mixin for system packages. It only supports native Linux usage, not usage |
| 39 |
# through Docker. |
| 40 |
output_format = "system" |
| 41 |
supported_host_os: Collection[str] = {"Linux"} |
| 42 |
supports_external_packaging = True |
| 43 |
|
| 44 |
def build_path(self, app): |
| 45 |
# Override the default build path to use the vendor name, |
| 46 |
# rather than "linux" |
| 47 |
return self.base_path / "build" / app.app_name / app.target_vendor |
| 48 |
|
| 49 |
def bundle_path(self, app): |
| 50 |
# Override the default bundle path to use the codename, |
| 51 |
# rather than "system" |
| 52 |
return self.build_path(app) / app.target_codename |
| 53 |
|
| 54 |
def project_path(self, app): |
| 55 |
return self.bundle_path(app) / f"{app.app_name}-{app.version}" |
| 56 |
|
| 57 |
def binary_path(self, app): |
| 58 |
return self.project_path(app) / "usr/bin" / app.app_name |
| 59 |
|
| 60 |
def bundle_package_path(self, app): |
| 61 |
return self.project_path(app) |
| 62 |
|
| 63 |
def rpm_tag(self, app): |
| 64 |
if app.target_vendor == "fedora": |
| 65 |
return f"fc{app.target_codename}" |
| 66 |
else: |
| 67 |
return f"el{app.target_codename}" |
| 68 |
|
| 69 |
def _build_env_abi(self, app: AppConfig): |
| 70 |
"""Retrieves the ABI the packaging system is targeting in the build env. |
| 71 |
|
| 72 |
Each packaging system uses different values to identify the exact ABI that |
| 73 |
describes the target environment...so just defer to the packaging system. |
| 74 |
""" |
| 75 |
command = { |
| 76 |
"deb": ["dpkg", "--print-architecture"], |
| 77 |
"rpm": ["rpm", "--eval", "%_target_cpu"], |
| 78 |
"pkg": ["pacman-conf", "Architecture"], |
| 79 |
}[app.packaging_format] |
| 80 |
try: |
| 81 |
return ( |
| 82 |
self.tools[app].app_context.check_output(command).split("\n")[0].strip() |
| 83 |
) |
| 84 |
except (OSError, subprocess.CalledProcessError) as e: |
| 85 |
raise BriefcaseCommandError( |
| 86 |
"Failed to determine build environment's ABI for packaging." |
| 87 |
) from e |
| 88 |
|
| 89 |
def deb_abi(self, app: AppConfig) -> str: |
| 90 |
"""The default ABI for dpkg packaging for the target environment.""" |
| 91 |
try: |
| 92 |
return self._deb_abi |
| 93 |
except AttributeError: |
| 94 |
self._deb_abi = self._build_env_abi(app) |
| 95 |
return self._deb_abi |
| 96 |
|
| 97 |
def rpm_abi(self, app: AppConfig) -> str: |
| 98 |
"""The default ABI for rpm packaging for the target environment.""" |
| 99 |
try: |
| 100 |
return self._rpm_abi |
| 101 |
except AttributeError: |
| 102 |
self._rpm_abi = self._build_env_abi(app) |
| 103 |
return self._rpm_abi |
| 104 |
|
| 105 |
def pkg_abi(self, app: AppConfig) -> str: |
| 106 |
"""The default ABI for pacman packaging for the target environment.""" |
| 107 |
try: |
| 108 |
return self._pkg_abi |
| 109 |
except AttributeError: |
| 110 |
self._pkg_abi = self._build_env_abi(app) |
| 111 |
return self._pkg_abi |
| 112 |
|
| 113 |
def distribution_filename(self, app: AppConfig) -> str: |
| 114 |
if app.packaging_format == "deb": |
| 115 |
return ( |
| 116 |
f"{app.bundle_name}" |
| 117 |
f"_{app.version}" |
| 118 |
f"-{getattr(app, 'revision', 1)}" |
| 119 |
f"~{app.target_vendor}" |
| 120 |
f"-{app.target_codename}" |
| 121 |
f"_{self.deb_abi(app)}" |
| 122 |
".deb" |
| 123 |
) |
| 124 |
elif app.packaging_format == "rpm": |
| 125 |
# openSUSE doesn't include a distro tag |
| 126 |
if app.target_vendor_base == SUSE: |
| 127 |
distro_tag = "" |
| 128 |
else: |
| 129 |
distro_tag = f".{self.rpm_tag(app)}" |
| 130 |
return ( |
| 131 |
f"{app.app_name}" |
| 132 |
f"-{app.version}" |
| 133 |
f"-{getattr(app, 'revision', 1)}" |
| 134 |
f"{distro_tag}" |
| 135 |
f".{self.rpm_abi(app)}" |
| 136 |
".rpm" |
| 137 |
) |
| 138 |
elif app.packaging_format == "pkg": |
| 139 |
return ( |
| 140 |
f"{app.app_name}" |
| 141 |
f"-{app.version}" |
| 142 |
f"-{getattr(app, 'revision', 1)}" |
| 143 |
f"-{self.pkg_abi(app)}" |
| 144 |
".pkg.tar.zst" |
| 145 |
) |
| 146 |
else: |
| 147 |
raise BriefcaseCommandError( |
| 148 |
"Briefcase doesn't currently know how to build system packages in " |
| 149 |
f"{app.packaging_format.upper()} format." |
| 150 |
) |
| 151 |
|
| 152 |
def distribution_path(self, app: AppConfig): |
| 153 |
return self.dist_path / self.distribution_filename(app) |
| 154 |
|
| 155 |
def target_glibc_version(self, app): |
| 156 |
target_glibc = self.tools.os.confstr("CS_GNU_LIBC_VERSION").split()[1] |
| 157 |
return target_glibc |
| 158 |
|
| 159 |
def app_python_version_tag(self, app): |
| 160 |
# Use the version of Python that was used to run Briefcase. |
| 161 |
return self.python_version_tag |
| 162 |
|
| 163 |
def platform_freedesktop_info(self, app): |
| 164 |
try: |
| 165 |
freedesktop_info = self.tools.platform.freedesktop_os_release() |
| 166 |
|
| 167 |
except OSError as e: |
| 168 |
raise BriefcaseCommandError( |
| 169 |
"Could not find the /etc/os-release file. " |
| 170 |
"Is this a FreeDesktop-compliant Linux distribution?" |
| 171 |
) from e |
| 172 |
|
| 173 |
return freedesktop_info |
| 174 |
|
| 175 |
def _finalize_target_image(self, app: AppConfig): |
| 176 |
app.target_image = f"{app.target_vendor}:{app.target_codename}" |
| 177 |
|
| 178 |
def finalize_app_config(self, app: AppConfig): |
| 179 |
"""Finalize app configuration. |
| 180 |
|
| 181 |
Linux .deb app configurations are deeper than other platforms, because they need |
| 182 |
to include components that are dependent on the target vendor and codename. |
| 183 |
Those properties are extracted from command-line options. |
| 184 |
|
| 185 |
The final app configuration merges the target-specific configuration into the |
| 186 |
generic "linux.deb" app configuration, as well as setting the Python version. |
| 187 |
|
| 188 |
:param app: The app configuration to finalize. |
| 189 |
""" |
| 190 |
self.console.verbose( |
| 191 |
"Finalizing application configuration...", prefix=app.app_name |
| 192 |
) |
| 193 |
freedesktop_info = self.platform_freedesktop_info(app) |
| 194 |
|
| 195 |
# Process the FreeDesktop content to give the vendor, codename and vendor base. |
| 196 |
( |
| 197 |
app.target_vendor, |
| 198 |
app.target_codename, |
| 199 |
app.target_vendor_base, |
| 200 |
) = self.vendor_details(freedesktop_info) |
| 201 |
|
| 202 |
self.console.verbose( |
| 203 |
f"Targeting {app.target_vendor}:{app.target_codename} " |
| 204 |
f"(Vendor base {app.target_vendor_base})" |
| 205 |
) |
| 206 |
|
| 207 |
# Finalize the target image being used by the app |
| 208 |
self._finalize_target_image(app) |
| 209 |
|
| 210 |
# Merge target-specific configuration items into the app config This |
| 211 |
# means: |
| 212 |
# * merging app.linux.debian into app, overwriting anything global |
| 213 |
# * merging app.linux.ubuntu into app, overwriting anything vendor-base |
| 214 |
# specific |
| 215 |
# * merging app.linux.ubuntu.focal into app, overwriting anything vendor |
| 216 |
# specific |
| 217 |
# The vendor base config (e.g., redhat). The vendor base might not |
| 218 |
# be known, so fall back to an empty vendor config. |
| 219 |
if app.target_vendor_base: |
| 220 |
vendor_base_config = getattr(app, app.target_vendor_base, {}) |
| 221 |
else: |
| 222 |
vendor_base_config = {} |
| 223 |
vendor_config = getattr(app, app.target_vendor, {}) |
| 224 |
try: |
| 225 |
codename_config = vendor_config[app.target_codename] |
| 226 |
except KeyError: |
| 227 |
codename_config = {} |
| 228 |
|
| 229 |
# Copy all the specific configurations to the app config |
| 230 |
for config in [ |
| 231 |
vendor_base_config, |
| 232 |
vendor_config, |
| 233 |
codename_config, |
| 234 |
]: |
| 235 |
merge_config(app, config) |
| 236 |
|
| 237 |
app.glibc_version = self.target_glibc_version(app) |
| 238 |
self.console.verbose(f"Targeting glibc {app.glibc_version}") |
| 239 |
|
| 240 |
app.python_version_tag = self.app_python_version_tag(app) |
| 241 |
|
| 242 |
self.console.verbose(f"Targeting Python{app.python_version_tag}") |
| 243 |
|
| 244 |
def _deb_devirtualize(self, package: str) -> str: |
| 245 |
"""Convert a debian virtual package into a "real" package. |
| 246 |
|
| 247 |
Debian has the concept of "virtual" packages, where you can install one target, |
| 248 |
but another package is installed in practice, and the explicitly requested |
| 249 |
package is never shown in the installed list. |
| 250 |
|
| 251 |
A virtual package is identified as a package that defines a single `Reverse |
| 252 |
Provides` definition (although there may be multiple versions of that single |
| 253 |
package name), and doesn't have any `Provides` definitions. (e.g., `make` |
| 254 |
reverse-provides `make-guile`; but actually provides `make`, so it's not a |
| 255 |
virtual package; `mail-transport-agent` returns multiple *different* |
| 256 |
reverse-provides, so it can't be devirtualized). |
| 257 |
|
| 258 |
:param package: The possibly virtualized package name |
| 259 |
:returns: The devirtualized package name, or `None` if the package isn't |
| 260 |
a virtual package |
| 261 |
""" |
| 262 |
devirtualized = None |
| 263 |
try: |
| 264 |
pkg_detail = self.tools.subprocess.check_output( |
| 265 |
["apt-cache", "showpkg", package], quiet=1 |
| 266 |
) |
| 267 |
except subprocess.CalledProcessError as e: |
| 268 |
raise BriefcaseCommandError( |
| 269 |
f"""Unable to check apt-cache package record for {package!r}""" |
| 270 |
) from e |
| 271 |
else: |
| 272 |
candidates = None |
| 273 |
for line in pkg_detail.split("\n"): |
| 274 |
if line == "Provides: ": |
| 275 |
# If we hit a Provides line, start gathering package names |
| 276 |
candidates = set() |
| 277 |
elif line == "Reverse Provides: ": |
| 278 |
# Reverse Provides are listed after Provides. If the named package |
| 279 |
# provides anything, it's not virtual, so reset the candidate list. |
| 280 |
if candidates: |
| 281 |
return None |
| 282 |
else: |
| 283 |
candidates = set() |
| 284 |
elif candidates is not None and line: |
| 285 |
# Store the first part of the package name. |
| 286 |
candidates.add(line.split(" ")[0]) |
| 287 |
|
| 288 |
# If we've found exactly one candidate name, that's our actual package. |
| 289 |
if candidates is not None and len(candidates) == 1: |
| 290 |
devirtualized = next(iter(candidates)) |
| 291 |
|
| 292 |
return devirtualized |
| 293 |
|
| 294 |
def _system_requirement_tools(self, app: AppConfig): |
| 295 |
"""Utility method returning the packages and tools needed to verify system |
| 296 |
requirements. |
| 297 |
|
| 298 |
:param app: The app being built. |
| 299 |
:returns: A triple containing (0) The list of package names that must |
| 300 |
be installed at a bare minimum; (1) the arguments for the command |
| 301 |
used to verify the existence of a package on a system, and (2) |
| 302 |
the command used to install packages. All three values are `None` |
| 303 |
if the system cannot be identified. |
| 304 |
""" |
| 305 |
if app.target_vendor_base == DEBIAN: |
| 306 |
base_system_packages = [ |
| 307 |
"python3-dev", |
| 308 |
# The consitutent parts of build-essential |
| 309 |
("dpkg-dev", "build-essential"), |
| 310 |
("g++", "build-essential"), |
| 311 |
("gcc", "build-essential"), |
| 312 |
("libc6-dev", "build-essential"), |
| 313 |
("make", "build-essential"), |
| 314 |
] |
| 315 |
system_verify = ["dpkg", "-s"] |
| 316 |
system_devirtualize = self._deb_devirtualize |
| 317 |
system_installer = ["apt", "install"] |
| 318 |
elif app.target_vendor_base == RHEL: |
| 319 |
base_system_packages = [ |
| 320 |
"python3-devel", |
| 321 |
"gcc", |
| 322 |
"make", |
| 323 |
"pkgconf-pkg-config", |
| 324 |
] |
| 325 |
system_verify = ["rpm", "-q"] |
| 326 |
system_devirtualize = None |
| 327 |
system_installer = ["dnf", "install"] |
| 328 |
elif app.target_vendor_base == SUSE: |
| 329 |
base_system_packages = [ |
| 330 |
"python3-devel", |
| 331 |
"patterns-devel-base-devel_basis", |
| 332 |
] |
| 333 |
system_verify = ["rpm", "-q", "--whatprovides"] |
| 334 |
system_devirtualize = None |
| 335 |
system_installer = ["zypper", "install"] |
| 336 |
elif app.target_vendor_base == ARCH: |
| 337 |
base_system_packages = [ |
| 338 |
"python3", |
| 339 |
"base-devel", |
| 340 |
] |
| 341 |
system_verify = ["pacman", "-Q"] |
| 342 |
system_devirtualize = None |
| 343 |
system_installer = ["pacman", "-Syu"] |
| 344 |
else: |
| 345 |
base_system_packages = None |
| 346 |
system_verify = None |
| 347 |
system_devirtualize = None |
| 348 |
system_installer = None |
| 349 |
|
| 350 |
return ( |
| 351 |
base_system_packages, |
| 352 |
system_devirtualize, |
| 353 |
system_verify, |
| 354 |
system_installer, |
| 355 |
) |
| 356 |
|
| 357 |
def verify_system_packages(self, app: AppConfig): |
| 358 |
"""Verify that the required system packages are installed. |
| 359 |
|
| 360 |
Verifies both `system_requires` and `system_runtime_requires`. |
| 361 |
|
| 362 |
:param app: The app being built. |
| 363 |
""" |
| 364 |
( |
| 365 |
base_system_packages, |
| 366 |
system_devirtualize, |
| 367 |
system_verify, |
| 368 |
system_installer, |
| 369 |
) = self._system_requirement_tools(app) |
| 370 |
|
| 371 |
if not (system_verify and self.tools.shutil.which(system_verify[0])): |
| 372 |
self.console.warning(""" |
| 373 |
************************************************************************* |
| 374 |
** WARNING: Can't verify system packages ** |
| 375 |
************************************************************************* |
| 376 |
|
| 377 |
Briefcase doesn't know how to verify the installation of system |
| 378 |
packages on your Linux distribution. If you have any problems |
| 379 |
building this app, ensure that the packages listed in the app's |
| 380 |
`system_requires` setting have been installed. |
| 381 |
|
| 382 |
************************************************************************* |
| 383 |
""") |
| 384 |
return |
| 385 |
|
| 386 |
# Run a check for each package listed in the app's system_requires, |
| 387 |
# plus the baseline system packages that are required. |
| 388 |
missing = set() |
| 389 |
verified = set() |
| 390 |
for package in ( |
| 391 |
base_system_packages |
| 392 |
+ getattr(app, "system_requires", []) |
| 393 |
+ getattr(app, "system_runtime_requires", []) |
| 394 |
): |
| 395 |
# Look for tuples in the package list. If there's a tuple, we're looking |
| 396 |
# for the first name in the tuple on the installed list, but we install |
| 397 |
# the package using the second name. This is to handle `build-essential` |
| 398 |
# style installation aliases. If it's not a tuple, the package name is |
| 399 |
# provided by the same name that we're checking for. |
| 400 |
if isinstance(package, tuple): |
| 401 |
installed, provided_by = package |
| 402 |
else: |
| 403 |
installed = provided_by = package |
| 404 |
|
| 405 |
if installed not in verified: |
| 406 |
verified.add(installed) |
| 407 |
try: |
| 408 |
self.tools.subprocess.check_output( |
| 409 |
[*system_verify, installed], quiet=1 |
| 410 |
) |
| 411 |
except subprocess.CalledProcessError: |
| 412 |
# If the system uses devirtualization, try a devirtualized name |
| 413 |
if system_devirtualize: |
| 414 |
if devirtualized := system_devirtualize(installed): |
| 415 |
try: |
| 416 |
self.tools.subprocess.check_output( |
| 417 |
[*system_verify, devirtualized], quiet=1 |
| 418 |
) |
| 419 |
except subprocess.CalledProcessError: |
| 420 |
# Couldn't check devirtualized |
| 421 |
missing.add(provided_by) |
| 422 |
else: |
| 423 |
# package isn't devirtualized |
| 424 |
missing.add(provided_by) |
| 425 |
else: |
| 426 |
missing.add(provided_by) |
| 427 |
|
| 428 |
# If any required packages are missing, raise an error. |
| 429 |
if missing: |
| 430 |
raise BriefcaseCommandError(f"""\ |
| 431 |
Unable to build {app.app_name} due to missing system dependencies. Run: |
| 432 |
|
| 433 |
sudo {" ".join(system_installer)} {" ".join(sorted(missing))} |
| 434 |
|
| 435 |
to install the missing dependencies, and re-run Briefcase. |
| 436 |
""") |
| 437 |
|
| 438 |
|
| 439 |
class LinuxSystemDockerMixin(LinuxSystemMixin): |
| 440 |
# Add options to allow the use of Docker. |
| 441 |
supported_host_os: Collection[str] = {"Darwin", "Linux"} |
| 442 |
supported_host_os_reason = ( |
| 443 |
"Linux system projects can only be built on Linux, or on macOS using Docker." |
| 444 |
) |
| 445 |
|
| 446 |
@property |
| 447 |
def use_docker(self): |
| 448 |
# The system backend doesn't have a literal "--use-docker" option, but |
| 449 |
# `use_docker` is a useful flag for shared logic purposes, so evaluate |
| 450 |
# what "use docker" means in terms of target_image. |
| 451 |
return bool(self.target_image) |
| 452 |
|
| 453 |
def add_options(self, parser): |
| 454 |
super().add_options(parser) |
| 455 |
parser.add_argument( |
| 456 |
"--target", |
| 457 |
dest="target", |
| 458 |
help=( |
| 459 |
"Docker base image tag for the distribution to target for the build " |
| 460 |
"(e.g., `ubuntu:jammy`)" |
| 461 |
), |
| 462 |
required=False, |
| 463 |
) |
| 464 |
parser.add_argument( |
| 465 |
"--Xdocker-build", |
| 466 |
action="append", |
| 467 |
dest="extra_docker_build_args", |
| 468 |
help="Additional arguments to use when building the Docker image", |
| 469 |
required=False, |
| 470 |
) |
| 471 |
|
| 472 |
def parse_options(self, extra): |
| 473 |
"""Extract the target_image option.""" |
| 474 |
options, overrides = super().parse_options(extra) |
| 475 |
self.target_image = options.pop("target") |
| 476 |
self.extra_docker_build_args = options.pop("extra_docker_build_args") |
| 477 |
|
| 478 |
return options, overrides |
| 479 |
|
| 480 |
def app_python_version_tag(self, app: AppConfig): |
| 481 |
if self.use_docker: |
| 482 |
# If we're running in Docker, we can't know the Python3 version |
| 483 |
# before rolling out the template; so we fall back to "3". Later, |
| 484 |
# once we have a container in which we can run Python, this will be |
| 485 |
# updated to the actual Python version as part of the |
| 486 |
# `verify_python` app check. |
| 487 |
python_version_tag = "3" |
| 488 |
else: |
| 489 |
python_version_tag = super().app_python_version_tag(app) |
| 490 |
return python_version_tag |
| 491 |
|
| 492 |
def _finalize_target_image(self, app): |
| 493 |
if self.use_docker: |
| 494 |
# If we're using Docker, the target image is set by the --target command |
| 495 |
# line argument |
| 496 |
if app.external_package_path: |
| 497 |
raise BriefcaseCommandError( |
| 498 |
"Briefcase can't currently use Docker to package " |
| 499 |
"external apps as Linux system packages." |
| 500 |
) |
| 501 |
|
| 502 |
# If we're building for Arch, and Docker does user mapping, we can't build, |
| 503 |
# because Arch won't let makepkg run as root. Docker on macOS *does* map the |
| 504 |
# user, but introducing a step-down user doesn't alter behavior, so we can |
| 505 |
# allow it. |
| 506 |
if ( |
| 507 |
app.target_vendor_base == ARCH |
| 508 |
and self.tools.docker.is_user_mapped |
| 509 |
and self.tools.host_os != "Darwin" |
| 510 |
): |
| 511 |
raise BriefcaseCommandError("""\ |
| 512 |
Briefcase cannot use this Docker installation to target Arch Linux since the |
| 513 |
tools to build packages for Arch cannot be run as root. |
| 514 |
|
| 515 |
The Docker available to Briefcase requires the use of the root user in |
| 516 |
containers to maintain accurate file permissions of the build artefacts. |
| 517 |
|
| 518 |
This most likely means you're using Docker Desktop or rootless Docker. |
| 519 |
|
| 520 |
Install Docker Engine and try again or run Briefcase on an Arch host system. |
| 521 |
""") |
| 522 |
else: |
| 523 |
super()._finalize_target_image(app) |
| 524 |
|
| 525 |
def target_glibc_version(self, app): |
| 526 |
"""Determine the glibc version. |
| 527 |
|
| 528 |
If running in Docker, this is done by interrogating libc.so.6; outside docker, |
| 529 |
we can use os.confstr(). |
| 530 |
""" |
| 531 |
if self.use_docker: |
| 532 |
with self.console.wait_bar("Determining glibc version..."): |
| 533 |
try: |
| 534 |
output = self.tools.docker.check_output( |
| 535 |
["ldd", "--version"], |
| 536 |
image_tag=app.target_image, |
| 537 |
) |
| 538 |
# On Debian/Ubuntu, ldd --version will give you output of the form: |
| 539 |
# |
| 540 |
# ldd (Ubuntu GLIBC 2.31-0ubuntu9.9) 2.31 |
| 541 |
# Copyright (C) 2020 Free Software Foundation, Inc. |
| 542 |
# ... |
| 543 |
# |
| 544 |
# Other platforms produce output of the form: |
| 545 |
# |
| 546 |
# ldd (GNU libc) 2.36 |
| 547 |
# Copyright (C) 2020 Free Software Foundation, Inc. |
| 548 |
# ... |
| 549 |
# |
| 550 |
# Note that the exact text will vary version to version. |
| 551 |
# Look for the "2.NN" pattern. |
| 552 |
if match := re.search(r"\d\.\d+", output): |
| 553 |
target_glibc = match.group(0) |
| 554 |
else: |
| 555 |
raise BriefcaseCommandError( |
| 556 |
"Unable to parse glibc dependency version from version" |
| 557 |
" string." |
| 558 |
) |
| 559 |
except subprocess.CalledProcessError as e: |
| 560 |
raise BriefcaseCommandError( |
| 561 |
"Unable to determine glibc dependency version." |
| 562 |
) from e |
| 563 |
|
| 564 |
else: |
| 565 |
target_glibc = super().target_glibc_version(app) |
| 566 |
|
| 567 |
return target_glibc |
| 568 |
|
| 569 |
def platform_freedesktop_info(self, app: AppConfig): |
| 570 |
if self.use_docker: |
| 571 |
# Preserve the target image on the command line as the app's target |
| 572 |
app.target_image = self.target_image |
| 573 |
|
| 574 |
# Extract release information from the image. |
| 575 |
with self.console.wait_bar( |
| 576 |
f"Checking Docker target image {app.target_image}..." |
| 577 |
): |
| 578 |
output = self.tools.docker.check_output( |
| 579 |
["cat", "/etc/os-release"], |
| 580 |
image_tag=app.target_image, |
| 581 |
) |
| 582 |
freedesktop_info = parse_freedesktop_os_release(output) |
| 583 |
else: |
| 584 |
freedesktop_info = super().platform_freedesktop_info(app) |
| 585 |
|
| 586 |
return freedesktop_info |
| 587 |
|
| 588 |
def docker_image_tag(self, app: AppConfig): |
| 589 |
"""The Docker image tag for an app.""" |
| 590 |
return ( |
| 591 |
f"briefcase/{app.bundle_identifier.lower()}:" |
| 592 |
f"{app.target_vendor}-{app.target_codename}" |
| 593 |
) |
| 594 |
|
| 595 |
def verify_host(self): |
| 596 |
"""If we're *not* using Docker, verify that we're actually on Linux.""" |
| 597 |
super().verify_host() |
| 598 |
if not self.use_docker and self.tools.host_os != "Linux": |
| 599 |
raise UnsupportedHostError(self.supported_host_os_reason) |
| 600 |
|
| 601 |
def verify_tools(self): |
| 602 |
"""If we're using Docker, verify that it is available.""" |
| 603 |
super().verify_tools() |
| 604 |
if self.use_docker: |
| 605 |
Docker.verify(tools=self.tools, image_tag=self.target_image) |
| 606 |
|
| 607 |
def clone_options(self, command): |
| 608 |
"""Clone the target_image option.""" |
| 609 |
super().clone_options(command) |
| 610 |
self.target_image = command.target_image |
| 611 |
self.extra_docker_build_args = command.extra_docker_build_args |
| 612 |
|
| 613 |
def verify_docker_python(self, app: AppConfig): |
| 614 |
"""Verify that the version of Python being used to build the app in Docker is |
| 615 |
compatible with the version being used to run Briefcase. |
| 616 |
|
| 617 |
Will raise an exception if the Python version is fundamentally |
| 618 |
incompatible (i.e., if Briefcase doesn't support it); any other version |
| 619 |
discrepancy will log a warning, but continue. |
| 620 |
|
| 621 |
Requires that the app tools have been verified. |
| 622 |
|
| 623 |
As a side effect of verifying Python, the `python_version_tag` will be |
| 624 |
updated to reflect the *actual* python version, not just a generic "3". |
| 625 |
|
| 626 |
:param app: The application being built |
| 627 |
""" |
| 628 |
output = self.tools[app].app_context.check_output( |
| 629 |
[ |
| 630 |
f"python{app.python_version_tag}", |
| 631 |
"-c", |
| 632 |
( |
| 633 |
"import sys; " |
| 634 |
"print(f'{sys.version_info.major}.{sys.version_info.minor}')" |
| 635 |
), |
| 636 |
] |
| 637 |
) |
| 638 |
# Update the python version tag with the *actual* python version. |
| 639 |
app.python_version_tag = output.split("\n")[0] |
| 640 |
target_python_version = tuple(int(v) for v in app.python_version_tag.split(".")) |
| 641 |
|
| 642 |
if target_python_version < self.briefcase_required_python_version: |
| 643 |
briefcase_min_version = ".".join( |
| 644 |
str(v) for v in self.briefcase_required_python_version |
| 645 |
) |
| 646 |
raise BriefcaseCommandError( |
| 647 |
f"The system python3 version provided by {app.target_image} " |
| 648 |
f"is {app.python_version_tag}; Briefcase requires a " |
| 649 |
f"minimum Python3 version of {briefcase_min_version}." |
| 650 |
) |
| 651 |
elif target_python_version != ( |
| 652 |
self.tools.sys.version_info.major, |
| 653 |
self.tools.sys.version_info.minor, |
| 654 |
): |
| 655 |
self.console.warning(f""" |
| 656 |
************************************************************************* |
| 657 |
** WARNING: Python version mismatch! ** |
| 658 |
************************************************************************* |
| 659 |
|
| 660 |
The system python3 provided by {app.target_image} is {app.python_version_tag}. |
| 661 |
This is not the same as your local system ({self.python_version_tag}). |
| 662 |
|
| 663 |
Ensure you have tested for Python version compatibility before |
| 664 |
releasing this app. |
| 665 |
|
| 666 |
************************************************************************* |
| 667 |
""") |
| 668 |
|
| 669 |
def verify_system_python(self): |
| 670 |
"""Verify that the Python being used to run Briefcase is the default system |
| 671 |
python. |
| 672 |
|
| 673 |
Will raise an exception if the system Python isn't an obvious Python3, or the |
| 674 |
Briefcase Python isn't the same version as the system Python. |
| 675 |
|
| 676 |
Requires that the app tools have been verified. |
| 677 |
""" |
| 678 |
system_python_bin = Path("/usr/bin/python3") |
| 679 |
if not system_python_bin.exists(): |
| 680 |
raise BriefcaseCommandError( |
| 681 |
"Can't determine the system python version " |
| 682 |
"('/usr/bin/python3' does not exist)" |
| 683 |
) |
| 684 |
|
| 685 |
running_version = self.tools.sys.version |
| 686 |
system_version = self.tools.subprocess.check_output( |
| 687 |
[system_python_bin, "-c", "import sys; print(sys.version)"] |
| 688 |
).strip() |
| 689 |
|
| 690 |
if system_version != running_version: |
| 691 |
raise BriefcaseCommandError( |
| 692 |
"The version of Python being used to run Briefcase " |
| 693 |
f"({running_version!r}) is not the system python3 " |
| 694 |
f"({system_version!r})." |
| 695 |
) |
| 696 |
|
| 697 |
def verify_app_tools(self, app: AppConfig): |
| 698 |
"""Verify App environment is prepared and available. |
| 699 |
|
| 700 |
When Docker is used, create or update a Docker image for the App. Without |
| 701 |
Docker, the host machine will be used as the App environment. |
| 702 |
|
| 703 |
:param app: The application being built |
| 704 |
""" |
| 705 |
# Verifying the App context is idempotent; but we have some |
| 706 |
# additional logic that we only want to run the first time through. |
| 707 |
# Check (and store) the pre-verify app tool state. |
| 708 |
verify_python = not hasattr(self.tools[app], "app_context") |
| 709 |
|
| 710 |
if self.use_docker: |
| 711 |
DockerAppContext.verify( |
| 712 |
tools=self.tools, |
| 713 |
app=app, |
| 714 |
image_tag=self.docker_image_tag(app), |
| 715 |
dockerfile_path=self.bundle_path(app) / "Dockerfile", |
| 716 |
app_base_path=self.base_path, |
| 717 |
host_bundle_path=self.bundle_path(app), |
| 718 |
host_data_path=self.data_path, |
| 719 |
python_version=app.python_version_tag, |
| 720 |
extra_build_args=self.extra_docker_build_args, |
| 721 |
) |
| 722 |
|
| 723 |
# Check the system Python on the target system to see if it is |
| 724 |
# compatible with Briefcase. |
| 725 |
if verify_python: |
| 726 |
self.verify_docker_python(app) |
| 727 |
else: |
| 728 |
NativeAppContext.verify(tools=self.tools, app=app) |
| 729 |
|
| 730 |
# Check the system Python on the target system to see if it is |
| 731 |
# compatible with Briefcase, and that the required system packages |
| 732 |
# are installed. |
| 733 |
if verify_python: |
| 734 |
self.verify_system_python() |
| 735 |
self.verify_system_packages(app) |
| 736 |
|
| 737 |
# Establish Docker as app context before letting super set subprocess |
| 738 |
super().verify_app_tools(app) |
| 739 |
|
| 740 |
|
| 741 |
class LinuxSystemCreateCommand( |
| 742 |
LinuxSystemDockerMixin, LocalRequirementsMixin, CreateCommand |
| 743 |
): |
| 744 |
description = "Create and populate a Linux system project." |
| 745 |
|
| 746 |
def output_format_template_context(self, app: AppConfig): |
| 747 |
context = super().output_format_template_context(app) |
| 748 |
|
| 749 |
# Linux system templates use the target codename, rather than |
| 750 |
# the format "system" as the leaf of the bundle path |
| 751 |
context["format"] = app.target_codename |
| 752 |
|
| 753 |
# The base template context includes the host Python version; |
| 754 |
# override that with an app-specific Python version, allowing |
| 755 |
# for the app to be built with the system Python. |
| 756 |
context["python_version"] = app.python_version_tag |
| 757 |
|
| 758 |
# Add the docker base image |
| 759 |
context["docker_base_image"] = app.target_image |
| 760 |
|
| 761 |
# Add the vendor base |
| 762 |
context["vendor_base"] = app.target_vendor_base |
| 763 |
|
| 764 |
# Use the non-root user if Docker is not mapping usernames. Also use a non-root |
| 765 |
# user if we're on macOS; user mapping doesn't alter Docker operation, but some |
| 766 |
# packaging tools (e.g., Arch's makepkg) don't like running as root. If we're |
| 767 |
# not using Docker, this will fall back to the template default, which should be |
| 768 |
# enabling the root user. This might cause problems later, but it's part of a |
| 769 |
# much bigger "does the project need to be updated in light of configuration |
| 770 |
# changes" problem. |
| 771 |
try: |
| 772 |
context["use_non_root_user"] = ( |
| 773 |
self.tools.host_os == "Darwin" or not self.tools.docker.is_user_mapped |
| 774 |
) |
| 775 |
except AttributeError: |
| 776 |
pass # ignore if not using Docker |
| 777 |
|
| 778 |
return context |
| 779 |
|
| 780 |
|
| 781 |
class LinuxSystemUpdateCommand(LinuxSystemCreateCommand, UpdateCommand): |
| 782 |
description = "Update an existing Linux system project." |
| 783 |
|
| 784 |
|
| 785 |
class LinuxSystemOpenCommand(LinuxSystemDockerMixin, DockerOpenCommand): |
| 786 |
description = ( |
| 787 |
"Open a shell in a Docker container for an existing Linux system project." |
| 788 |
) |
| 789 |
|
| 790 |
|
| 791 |
class LinuxSystemBuildCommand(LinuxSystemDockerMixin, BuildCommand): |
| 792 |
description = "Build a Linux system project." |
| 793 |
|
| 794 |
def build_app(self, app: AppConfig, **kwargs): |
| 795 |
"""Build an application. |
| 796 |
|
| 797 |
:param app: The application to build |
| 798 |
""" |
| 799 |
self.console.info("Building application...", prefix=app.app_name) |
| 800 |
|
| 801 |
self.console.info("Build bootstrap binary...") |
| 802 |
with self.console.wait_bar("Building bootstrap binary..."): |
| 803 |
try: |
| 804 |
# Build the bootstrap binary. |
| 805 |
self.tools[app].app_context.run( |
| 806 |
[ |
| 807 |
"make", |
| 808 |
"-C", |
| 809 |
"bootstrap", |
| 810 |
"install", |
| 811 |
], |
| 812 |
check=True, |
| 813 |
cwd=self.bundle_path(app), |
| 814 |
) |
| 815 |
except subprocess.CalledProcessError as e: |
| 816 |
raise BriefcaseCommandError( |
| 817 |
f"Error building bootstrap binary for {app.app_name}." |
| 818 |
) from e |
| 819 |
|
| 820 |
# Make the folder for docs |
| 821 |
doc_folder = ( |
| 822 |
self.bundle_path(app) |
| 823 |
/ f"{app.app_name}-{app.version}" |
| 824 |
/ "usr" |
| 825 |
/ "share" |
| 826 |
/ "doc" |
| 827 |
/ app.app_name |
| 828 |
) |
| 829 |
doc_folder.mkdir(parents=True, exist_ok=True) |
| 830 |
|
| 831 |
with self.console.wait_bar("Installing license..."): |
| 832 |
if license_file := app.license.get("file"): |
| 833 |
license_file = self.base_path / license_file |
| 834 |
if license_file.is_file(): |
| 835 |
self.tools.shutil.copy(license_file, doc_folder / "copyright") |
| 836 |
else: |
| 837 |
_relative_license_path = license_file.relative_to(self.base_path) |
| 838 |
raise BriefcaseCommandError(f"""\ |
| 839 |
Your `pyproject.toml` specifies a license file of {str(_relative_license_path)!r}. |
| 840 |
However, this file does not exist. |
| 841 |
|
| 842 |
Ensure you have correctly spelled the filename in your `license.file` setting. |
| 843 |
|
| 844 |
""") |
| 845 |
elif license_text := app.license.get("text"): |
| 846 |
(doc_folder / "copyright").write_text(license_text, encoding="utf-8") |
| 847 |
if len(license_text.splitlines()) <= 1: |
| 848 |
self.console.warning(""" |
| 849 |
Your app specifies a license using `license.text`, but the value doesn't appear to be a |
| 850 |
full license. Briefcase will generate a `copyright` file for your project; you should |
| 851 |
ensure that the contents of this file is adequate. |
| 852 |
""") |
| 853 |
else: |
| 854 |
raise BriefcaseCommandError("""\ |
| 855 |
Your project does not contain a LICENSE definition. |
| 856 |
|
| 857 |
Create a file named `LICENSE` in the same directory as your `pyproject.toml` |
| 858 |
with your app's licensing terms, and set `license.file = 'LICENSE'` in your |
| 859 |
app's configuration. |
| 860 |
""") |
| 861 |
|
| 862 |
with self.console.wait_bar("Installing changelog..."): |
| 863 |
changelog = find_changelog_filename(self.base_path) |
| 864 |
|
| 865 |
if changelog is None: |
| 866 |
raise BriefcaseCommandError("""\ |
| 867 |
Your project does not contain a changelog file with a known file name. You |
| 868 |
must provide a changelog file in the same directory as your `pyproject.toml`, |
| 869 |
with a known changelog file name (one of 'CHANGELOG', 'HISTORY', 'NEWS' or |
| 870 |
'RELEASES'; the file may have an extension of '.md', '.rst', or '.txt', or have |
| 871 |
no extension). |
| 872 |
""") |
| 873 |
|
| 874 |
changelog_source = self.base_path / changelog |
| 875 |
|
| 876 |
with changelog_source.open(encoding="utf-8") as infile: |
| 877 |
outfile = gzip.GzipFile(doc_folder / "changelog.gz", mode="wb", mtime=0) |
| 878 |
outfile.write(infile.read().encode("utf-8")) |
| 879 |
outfile.close() |
| 880 |
|
| 881 |
# Make a folder for manpages |
| 882 |
man_folder = ( |
| 883 |
self.bundle_path(app) |
| 884 |
/ f"{app.app_name}-{app.version}" |
| 885 |
/ "usr" |
| 886 |
/ "share" |
| 887 |
/ "man" |
| 888 |
/ "man1" |
| 889 |
) |
| 890 |
man_folder.mkdir(parents=True, exist_ok=True) |
| 891 |
|
| 892 |
with self.console.wait_bar("Installing man page..."): |
| 893 |
manpage_source = self.bundle_path(app) / f"{app.app_name}.1" |
| 894 |
if manpage_source.is_file(): |
| 895 |
with manpage_source.open(encoding="utf-8") as infile: |
| 896 |
outfile = gzip.GzipFile( |
| 897 |
man_folder / f"{app.app_name}.1.gz", mode="wb", mtime=0 |
| 898 |
) |
| 899 |
outfile.write(infile.read().encode("utf-8")) |
| 900 |
outfile.close() |
| 901 |
else: |
| 902 |
raise BriefcaseCommandError( |
| 903 |
"Template does not provide a manpage source file " |
| 904 |
f"`{app.app_name}.1`" |
| 905 |
) |
| 906 |
|
| 907 |
self.console.verbose("Update file permissions...") |
| 908 |
with self.console.wait_bar("Updating file permissions..."): |
| 909 |
for path in self.project_path(app).glob("**/*"): |
| 910 |
old_perms = self.tools.os.stat(path).st_mode & 0o777 |
| 911 |
user_perms = old_perms & 0o700 |
| 912 |
world_perms = old_perms & 0o007 |
| 913 |
|
| 914 |
# File permissions like 775 and 664 (where the group and user |
| 915 |
# permissions are the same), cause Debian heartburn. So, make |
| 916 |
# sure the group and world permissions are the same |
| 917 |
new_perms = user_perms | (world_perms << 3) | world_perms |
| 918 |
|
| 919 |
# If there's been any change in permissions, apply them |
| 920 |
if new_perms != old_perms: # pragma: no-cover-if-is-windows |
| 921 |
self.console.verbose( |
| 922 |
"Updating file permissions on " |
| 923 |
f"{path.relative_to(self.bundle_path(app))} " |
| 924 |
f"from {old_perms:o} to {new_perms:o}" |
| 925 |
) |
| 926 |
path.chmod(new_perms) |
| 927 |
|
| 928 |
with self.console.wait_bar("Stripping binary..."): |
| 929 |
self.tools.subprocess.check_output(["strip", self.binary_path(app)]) |
| 930 |
|
| 931 |
|
| 932 |
class LinuxSystemRunCommand(LinuxSystemDockerMixin, RunCommand): |
| 933 |
description = "Run a Linux system project." |
| 934 |
supported_host_os: Collection[str] = {"Linux"} |
| 935 |
supported_host_os_reason = "Linux system projects can only be executed on Linux." |
| 936 |
|
| 937 |
def run_app( |
| 938 |
self, |
| 939 |
app: AppConfig, |
| 940 |
passthrough: list[str], |
| 941 |
**kwargs, |
| 942 |
): |
| 943 |
"""Start the application. |
| 944 |
|
| 945 |
:param app: The config object for the app |
| 946 |
:param passthrough: The list of arguments to pass to the app |
| 947 |
""" |
| 948 |
# Set up the log stream |
| 949 |
kwargs = self._prepare_app_kwargs(app=app) |
| 950 |
|
| 951 |
with self.tools[app].app_context.run_app_context(kwargs) as kwargs: |
| 952 |
# Console apps must operate in non-streaming mode so that console input can |
| 953 |
# be handled correctly. However, if we're in test mode, we *must* stream so |
| 954 |
# that we can see the test exit sentinel |
| 955 |
if app.console_app and not app.test_mode: |
| 956 |
self.console.info("=" * 75) |
| 957 |
self.tools[app].app_context.run( |
| 958 |
[self.binary_path(app), *passthrough], |
| 959 |
cwd=self.tools.home_path, |
| 960 |
bufsize=1, |
| 961 |
stream_output=False, |
| 962 |
**kwargs, |
| 963 |
) |
| 964 |
else: |
| 965 |
# Start the app in a way that lets us stream the logs |
| 966 |
app_popen = self.tools[app].app_context.Popen( |
| 967 |
[self.binary_path(app), *passthrough], |
| 968 |
cwd=self.tools.home_path, |
| 969 |
stdout=subprocess.PIPE, |
| 970 |
stderr=subprocess.STDOUT, |
| 971 |
bufsize=1, |
| 972 |
**kwargs, |
| 973 |
) |
| 974 |
|
| 975 |
# Start streaming logs for the app. |
| 976 |
self._stream_app_logs( |
| 977 |
app, |
| 978 |
popen=app_popen, |
| 979 |
clean_output=False, |
| 980 |
) |
| 981 |
|
| 982 |
|
| 983 |
class LinuxSystemDevCommand(LinuxSystemMixin, DevCommand): |
| 984 |
description = "Run a Linux system app in development mode" |
| 985 |
supported_host_os_reason = "Linux system dev mode is only supported on Linux." |
| 986 |
|
| 987 |
def install_dev_requirements(self, app: AppConfig, venv: VenvContext, **options): |
| 988 |
"""Install the requirements into the dev environment. |
| 989 |
|
| 990 |
This also verifies that the system packages have been installed. This is to |
| 991 |
ensure that any dependencies exist before installing wheels that might need to |
| 992 |
be compiled (e.g., PyGOobject requires compiler tooling and system GTK |
| 993 |
packages). |
| 994 |
""" |
| 995 |
self.verify_system_packages(app) |
| 996 |
super().install_dev_requirements(app, venv, **options) |
| 997 |
|
| 998 |
|
| 999 |
def debian_multiline_description(description): |
| 1000 |
"""Generate a Debian multiline description string. |
| 1001 |
|
| 1002 |
The long description in a Debian control file must *not* contain any blank lines, |
| 1003 |
and each line must start with a single space. Convert a long description into Debian |
| 1004 |
format. |
| 1005 |
|
| 1006 |
:param description: A multi-line long description string. |
| 1007 |
:returns: A string in Debian's multiline format |
| 1008 |
""" |
| 1009 |
return "\n ".join(line for line in description.split("\n") if line.strip() != "") |
| 1010 |
|
| 1011 |
|
| 1012 |
class LinuxSystemPackageCommand(LinuxSystemDockerMixin, PackageCommand): |
| 1013 |
description = "Package a Linux system project." |
| 1014 |
|
| 1015 |
@property |
| 1016 |
def packaging_formats(self): |
| 1017 |
return ["deb", "rpm", "pkg", "system"] |
| 1018 |
|
| 1019 |
def _verify_packaging_tools(self, app: AppConfig): |
| 1020 |
"""Verify that the local environment contains the packaging tools.""" |
| 1021 |
tool_name, executable_name, package_name = { |
| 1022 |
"deb": ("dpkg", "dpkg-deb", "dpkg-dev"), |
| 1023 |
"rpm": ("rpm-build", "rpmbuild", "rpm-build"), |
| 1024 |
"pkg": ("makepkg", "makepkg", "pacman"), |
| 1025 |
}[app.packaging_format] |
| 1026 |
|
| 1027 |
if not self.tools.shutil.which(executable_name): |
| 1028 |
if install_cmd := self._system_requirement_tools(app)[3]: |
| 1029 |
raise BriefcaseCommandError( |
| 1030 |
f"Can't find the {tool_name} tools. " |
| 1031 |
f"Try running `sudo {' '.join(install_cmd)} {package_name}`." |
| 1032 |
) |
| 1033 |
else: |
| 1034 |
raise BriefcaseCommandError( |
| 1035 |
f"Can't find the {executable_name} tool. " |
| 1036 |
f"Install this first to package the {app.packaging_format}." |
| 1037 |
) |
| 1038 |
|
| 1039 |
def verify_app_tools(self, app): |
| 1040 |
super().verify_app_tools(app) |
| 1041 |
# If "system" packaging format was selected, determine what that means. |
| 1042 |
if app.packaging_format == "system": |
| 1043 |
app.packaging_format = { |
| 1044 |
DEBIAN: "deb", |
| 1045 |
RHEL: "rpm", |
| 1046 |
ARCH: "pkg", |
| 1047 |
SUSE: "rpm", |
| 1048 |
}.get(app.target_vendor_base) |
| 1049 |
|
| 1050 |
if app.packaging_format is None: |
| 1051 |
raise BriefcaseCommandError( |
| 1052 |
"Briefcase doesn't know the system packaging format for " |
| 1053 |
f"{app.target_vendor}. You may be able to build a package " |
| 1054 |
"by manually specifying a format with -p/--packaging-format" |
| 1055 |
) |
| 1056 |
|
| 1057 |
if not self.use_docker: |
| 1058 |
self._verify_packaging_tools(app) |
| 1059 |
|
| 1060 |
def package_app(self, app: AppConfig, **kwargs): |
| 1061 |
if app.packaging_format == "deb": |
| 1062 |
self._package_deb(app, **kwargs) |
| 1063 |
elif app.packaging_format == "rpm": |
| 1064 |
self._package_rpm(app, **kwargs) |
| 1065 |
elif app.packaging_format == "pkg": |
| 1066 |
self._package_pkg(app, **kwargs) |
| 1067 |
else: |
| 1068 |
raise BriefcaseCommandError( |
| 1069 |
"Briefcase doesn't currently know how to build system packages in " |
| 1070 |
f"{app.packaging_format.upper()} format." |
| 1071 |
) |
| 1072 |
|
| 1073 |
def _package_deb(self, app: AppConfig, **kwargs): |
| 1074 |
self.console.info("Building .deb package...", prefix=app.app_name) |
| 1075 |
|
| 1076 |
# The long description *must* exist. |
| 1077 |
if app.long_description is None: |
| 1078 |
raise BriefcaseCommandError( |
| 1079 |
"App configuration does not define `long_description`. " |
| 1080 |
"Debian projects require a long description." |
| 1081 |
) |
| 1082 |
|
| 1083 |
# Write the Debian metadata control file. |
| 1084 |
with self.console.wait_bar("Write Debian package control file..."): |
| 1085 |
DEBIAN_path = self.package_path(app) / "DEBIAN" |
| 1086 |
|
| 1087 |
if DEBIAN_path.exists(): |
| 1088 |
self.tools.shutil.rmtree(DEBIAN_path) |
| 1089 |
|
| 1090 |
DEBIAN_path.mkdir() |
| 1091 |
# dpkg-dep requires "DEBIAN" directory is >=0755 and <=0775 |
| 1092 |
DEBIAN_path.chmod(0o755) |
| 1093 |
|
| 1094 |
# Add runtime package dependencies. App config has been finalized, |
| 1095 |
# so this will be the target-specific definition, if one exists. |
| 1096 |
# libc6 is added because lintian complains without it, even though |
| 1097 |
# it's a dependency of the thing we *do* care about - python. |
| 1098 |
system_runtime_requires = ", ".join( |
| 1099 |
[ |
| 1100 |
f"libc6 (>={app.glibc_version})", |
| 1101 |
f"libpython{app.python_version_tag}", |
| 1102 |
*getattr(app, "system_runtime_requires", []), |
| 1103 |
] |
| 1104 |
) |
| 1105 |
|
| 1106 |
with (DEBIAN_path / "control").open("w", encoding="utf-8") as f: |
| 1107 |
f.write( |
| 1108 |
"\n".join( |
| 1109 |
[ |
| 1110 |
f"Package: {app.bundle_name}", |
| 1111 |
f"Version: {app.version}", |
| 1112 |
f"Architecture: {self.deb_abi(app)}", |
| 1113 |
f"Maintainer: {app.author} <{app.author_email}>", |
| 1114 |
f"Homepage: {app.url}", |
| 1115 |
f"Description: {app.description}", |
| 1116 |
f" {debian_multiline_description(app.long_description)}", |
| 1117 |
f"Depends: {system_runtime_requires}", |
| 1118 |
f"Section: {getattr(app, 'system_section', 'utils')}", |
| 1119 |
"Priority: optional\n", |
| 1120 |
] |
| 1121 |
) |
| 1122 |
) |
| 1123 |
|
| 1124 |
with self.console.wait_bar("Building Debian package..."): |
| 1125 |
try: |
| 1126 |
# Build the dpkg. |
| 1127 |
self.tools[app].app_context.run( |
| 1128 |
[ |
| 1129 |
"dpkg-deb", |
| 1130 |
"--build", |
| 1131 |
"--root-owner-group", |
| 1132 |
self.package_path(app), |
| 1133 |
], |
| 1134 |
check=True, |
| 1135 |
cwd=self.bundle_path(app), |
| 1136 |
) |
| 1137 |
except subprocess.CalledProcessError as e: |
| 1138 |
raise BriefcaseCommandError( |
| 1139 |
f"Error while building .deb package for {app.app_name}." |
| 1140 |
) from e |
| 1141 |
|
| 1142 |
# Move the deb file to its final location |
| 1143 |
self.tools.shutil.move( |
| 1144 |
self.package_path(app).parent / f"{self.package_path(app).name}.deb", |
| 1145 |
self.distribution_path(app), |
| 1146 |
) |
| 1147 |
|
| 1148 |
def _package_rpm(self, app: AppConfig, **kwargs): # pragma: no-cover-if-is-windows |
| 1149 |
self.console.info("Building .rpm package...", prefix=app.app_name) |
| 1150 |
|
| 1151 |
# The long description *must* exist. |
| 1152 |
if app.long_description is None: |
| 1153 |
raise BriefcaseCommandError( |
| 1154 |
"App configuration does not define `long_description`. " |
| 1155 |
"Red Hat projects require a long description." |
| 1156 |
) |
| 1157 |
|
| 1158 |
# Generate the rpmbuild layout |
| 1159 |
rpmbuild_path = self.bundle_path(app) / "rpmbuild" |
| 1160 |
with self.console.wait_bar("Generating rpmbuild layout..."): |
| 1161 |
if rpmbuild_path.exists(): |
| 1162 |
self.tools.shutil.rmtree(rpmbuild_path) |
| 1163 |
|
| 1164 |
(rpmbuild_path / "BUILD").mkdir(parents=True) |
| 1165 |
(rpmbuild_path / "BUILDROOT").mkdir(parents=True) |
| 1166 |
(rpmbuild_path / "RPMS").mkdir(parents=True) |
| 1167 |
(rpmbuild_path / "SOURCES").mkdir(parents=True) |
| 1168 |
(rpmbuild_path / "SRPMS").mkdir(parents=True) |
| 1169 |
(rpmbuild_path / "SPECS").mkdir(parents=True) |
| 1170 |
|
| 1171 |
# Add runtime package dependencies. App config has been finalized, |
| 1172 |
# so this will be the target-specific definition, if one exists. |
| 1173 |
system_runtime_requires = [ |
| 1174 |
"python3", |
| 1175 |
*getattr(app, "system_runtime_requires", []), |
| 1176 |
] |
| 1177 |
|
| 1178 |
# Write the spec file |
| 1179 |
with ( |
| 1180 |
self.console.wait_bar("Write RPM spec file..."), |
| 1181 |
(rpmbuild_path / "SPECS" / f"{app.app_name}.spec").open( |
| 1182 |
"w", encoding="utf-8" |
| 1183 |
) as f, |
| 1184 |
): |
| 1185 |
f.write( |
| 1186 |
"\n".join( |
| 1187 |
[ |
| 1188 |
# By default, rpmbuild thinks all .py files are executable, |
| 1189 |
# and if a .py doesn't have a shebang line, it will |
| 1190 |
# tell you that it will remove the executable bit - |
| 1191 |
# even if the executable bit isn't set. |
| 1192 |
# We disable the processor that does this. |
| 1193 |
"%global __brp_mangle_shebangs %{nil}", |
| 1194 |
# rpmbuild tries to strip binaries, which messes with |
| 1195 |
# binary wheels. Disable these checks. |
| 1196 |
"%global __brp_strip %{nil}", |
| 1197 |
"%global __brp_strip_static_archive %{nil}", |
| 1198 |
"%global __brp_strip_comment_note %{nil}", |
| 1199 |
# Disable RPATH checking, because check-rpaths can't deal with |
| 1200 |
# the structure of manylinux wheels |
| 1201 |
"%global __brp_check_rpaths %{nil}", |
| 1202 |
# Disable all the auto-detection that tries to magically |
| 1203 |
# determine requirements from the binaries |
| 1204 |
( |
| 1205 |
"%global __requires_exclude_from" |
| 1206 |
f" ^%{{_libdir}}/{app.app_name}/.*$" |
| 1207 |
), |
| 1208 |
( |
| 1209 |
"%global __provides_exclude_from" |
| 1210 |
f" ^%{{_libdir}}/{app.app_name}/.*$" |
| 1211 |
), |
| 1212 |
# Disable debug processing. |
| 1213 |
"%global _enable_debug_package 0", |
| 1214 |
"%global debug_package %{nil}", |
| 1215 |
"", |
| 1216 |
# Base package metadata |
| 1217 |
f"Name: {app.app_name}", |
| 1218 |
f"Version: {app.version}", |
| 1219 |
f"Release: {getattr(app, 'revision', 1)}%{{?dist}}", |
| 1220 |
f"Summary: {app.description}", |
| 1221 |
"", |
| 1222 |
# TODO: Add license information (see #1829) |
| 1223 |
"License: Unknown", |
| 1224 |
f"URL: {app.url}", |
| 1225 |
"Source0: %{name}-%{version}.tar.gz", |
| 1226 |
"", |
| 1227 |
] |
| 1228 |
+ [ |
| 1229 |
f"Requires: {requirement}" |
| 1230 |
for requirement in system_runtime_requires |
| 1231 |
] |
| 1232 |
+ [ |
| 1233 |
"", |
| 1234 |
f"ExclusiveArch: {self.rpm_abi(app)}", |
| 1235 |
"", |
| 1236 |
"%description", |
| 1237 |
app.long_description, |
| 1238 |
"", |
| 1239 |
"%prep", |
| 1240 |
"%autosetup", |
| 1241 |
"", |
| 1242 |
"%build", |
| 1243 |
"", |
| 1244 |
"%install", |
| 1245 |
"cp -r usr %{buildroot}/usr", |
| 1246 |
] |
| 1247 |
) |
| 1248 |
) |
| 1249 |
|
| 1250 |
f.write("\n\n%files\n") |
| 1251 |
# Build the file manifest. Include any file that is found; also include |
| 1252 |
# any directory that includes an app_name component, as those paths |
| 1253 |
# will need to be cleaned up afterwards. Files that *aren't* |
| 1254 |
# in <app_name> (sub)directories (e.g., /usr/bin/<app_name> or |
| 1255 |
# /usr/share/man/man1/<app_name>.1.gz) will be included, but paths |
| 1256 |
# *not* cleaned up, as they're part of more general system structures. |
| 1257 |
for filename in sorted(self.package_path(app).glob("**/*")): |
| 1258 |
path = filename.relative_to(self.package_path(app)) |
| 1259 |
|
| 1260 |
if filename.is_dir(): |
| 1261 |
if app.app_name in path.parts: |
| 1262 |
f.write(f'%dir "/{path}"\n') |
| 1263 |
else: |
| 1264 |
f.write(f'"/{path}"\n') |
| 1265 |
|
| 1266 |
# Add the changelog content to the bottom of the spec file. |
| 1267 |
f.write("\n%changelog\n") |
| 1268 |
changelog = find_changelog_filename(self.base_path) |
| 1269 |
|
| 1270 |
if changelog is None: |
| 1271 |
raise BriefcaseCommandError("""\ |
| 1272 |
Your project does not contain a changelog file with a known file name. You |
| 1273 |
must provide a changelog file in the same directory as your `pyproject.toml`, |
| 1274 |
with a known changelog file name (one of 'CHANGELOG', 'HISTORY', 'NEWS' or |
| 1275 |
'RELEASES'; the file may have an extension of '.md', '.rst', or '.txt', or have |
| 1276 |
no extension). |
| 1277 |
""") |
| 1278 |
|
| 1279 |
# Write the changelog content |
| 1280 |
f.write((self.base_path / changelog).read_text(encoding="utf-8")) |
| 1281 |
|
| 1282 |
with ( |
| 1283 |
self.console.wait_bar("Building source archive..."), |
| 1284 |
tarfile.open( |
| 1285 |
rpmbuild_path / f"SOURCES/{self.bundle_package_path(app).name}.tar.gz", |
| 1286 |
"w:gz", |
| 1287 |
) as archive, |
| 1288 |
): |
| 1289 |
archive.add( |
| 1290 |
self.package_path(app), |
| 1291 |
arcname=self.bundle_package_path(app).name, |
| 1292 |
) |
| 1293 |
|
| 1294 |
with self.console.wait_bar("Building RPM package..."): |
| 1295 |
try: |
| 1296 |
# Build the rpm. |
| 1297 |
self.tools[app].app_context.run( |
| 1298 |
[ |
| 1299 |
"rpmbuild", |
| 1300 |
"-bb", |
| 1301 |
"--define", |
| 1302 |
f"_topdir {self.bundle_path(app) / 'rpmbuild'}", |
| 1303 |
f"./rpmbuild/SPECS/{app.app_name}.spec", |
| 1304 |
], |
| 1305 |
check=True, |
| 1306 |
cwd=self.bundle_path(app), |
| 1307 |
) |
| 1308 |
except subprocess.CalledProcessError as e: |
| 1309 |
raise BriefcaseCommandError( |
| 1310 |
f"Error while building .rpm package for {app.app_name}." |
| 1311 |
) from e |
| 1312 |
|
| 1313 |
# Move the rpm file to its final location |
| 1314 |
self.tools.shutil.move( |
| 1315 |
rpmbuild_path |
| 1316 |
/ "RPMS" |
| 1317 |
/ self.rpm_abi(app) |
| 1318 |
/ self.distribution_filename(app), |
| 1319 |
self.distribution_path(app), |
| 1320 |
) |
| 1321 |
|
| 1322 |
def _package_pkg(self, app: AppConfig, **kwargs): # pragma: no-cover-if-is-windows |
| 1323 |
self.console.info("Building .pkg.tar.zst package...", prefix=app.app_name) |
| 1324 |
|
| 1325 |
# The description *must* exist. |
| 1326 |
# pkgdesc has 80 char limit. |
| 1327 |
if app.description is None: |
| 1328 |
raise BriefcaseCommandError( |
| 1329 |
"App configuration does not define `description`. " |
| 1330 |
"Arch projects require a description." |
| 1331 |
) |
| 1332 |
|
| 1333 |
changelog = find_changelog_filename(self.base_path) |
| 1334 |
|
| 1335 |
if changelog is None: |
| 1336 |
raise BriefcaseCommandError( |
| 1337 |
"Your project does not contain a changelog file with a valid file name." |
| 1338 |
"\n\n" |
| 1339 |
"Create a changelog file with the following as its name (CHANGELOG, " |
| 1340 |
"HISTORY, NEWS or RELEASES) with extensions (.md, .rst, .txt or no " |
| 1341 |
"extension) in the same directory as your `pyproject.toml` with " |
| 1342 |
"details about the release." |
| 1343 |
) |
| 1344 |
|
| 1345 |
changelog_source = self.base_path / changelog |
| 1346 |
|
| 1347 |
# Generate the pkgbuild layout |
| 1348 |
pkgbuild_path = self.bundle_path(app) / "pkgbuild" |
| 1349 |
with self.console.wait_bar("Generating pkgbuild layout..."): |
| 1350 |
if pkgbuild_path.exists(): |
| 1351 |
self.tools.shutil.rmtree(pkgbuild_path) |
| 1352 |
pkgbuild_path.mkdir(parents=True) |
| 1353 |
|
| 1354 |
# Copy the CHANGELOG file build_path so that it is visible to PKGBUILD |
| 1355 |
self.tools.shutil.copy(changelog_source, pkgbuild_path / changelog) |
| 1356 |
|
| 1357 |
# Build the source archive |
| 1358 |
with ( |
| 1359 |
self.console.wait_bar("Building source archive..."), |
| 1360 |
tarfile.open( |
| 1361 |
pkgbuild_path / f"{self.bundle_package_path(app).name}.tar.gz", |
| 1362 |
"w:gz", |
| 1363 |
) as archive, |
| 1364 |
): |
| 1365 |
archive.add( |
| 1366 |
self.package_path(app), |
| 1367 |
arcname=self.bundle_package_path(app).name, |
| 1368 |
) |
| 1369 |
|
| 1370 |
# Write the arch PKGBUILD file. |
| 1371 |
with self.console.wait_bar("Write PKGBUILD file..."): |
| 1372 |
# Add runtime package dependencies. App config has been finalized, |
| 1373 |
# so this will be the target-specific definition, if one exists. |
| 1374 |
system_runtime_requires_list = [ |
| 1375 |
f"glibc>={app.glibc_version}", |
| 1376 |
"python3", |
| 1377 |
*getattr(app, "system_runtime_requires", []), |
| 1378 |
] |
| 1379 |
|
| 1380 |
system_runtime_requires = " ".join( |
| 1381 |
f"'{pkg}'" for pkg in system_runtime_requires_list |
| 1382 |
) |
| 1383 |
|
| 1384 |
with (pkgbuild_path / "PKGBUILD").open("w", encoding="utf-8") as f: |
| 1385 |
f.write( |
| 1386 |
"\n".join( |
| 1387 |
[ |
| 1388 |
f"# Maintainer: {app.author} <{app.author_email}>", |
| 1389 |
f'export PACKAGER="{app.author} <{app.author_email}>"', |
| 1390 |
f"pkgname={app.app_name}", |
| 1391 |
f"pkgver={app.version}", |
| 1392 |
f"pkgrel={getattr(app, 'revision', 1)}", |
| 1393 |
f'pkgdesc="{app.description}"', |
| 1394 |
f"arch=('{self.pkg_abi(app)}')", |
| 1395 |
f'url="{app.url}"', |
| 1396 |
"license=('Unknown')", |
| 1397 |
f"depends=({system_runtime_requires})", |
| 1398 |
"changelog=CHANGELOG", |
| 1399 |
'source=("$pkgname-$pkgver.tar.gz")', |
| 1400 |
"md5sums=('SKIP')", |
| 1401 |
"options=('!strip')", |
| 1402 |
"package() {", |
| 1403 |
' cp -r "$srcdir/$pkgname-$pkgver/usr/" "$pkgdir"/usr/', |
| 1404 |
"}", |
| 1405 |
] |
| 1406 |
) |
| 1407 |
) |
| 1408 |
|
| 1409 |
with self.console.wait_bar("Building Arch package..."): |
| 1410 |
try: |
| 1411 |
# Build the pkg. |
| 1412 |
self.tools[app].app_context.run( |
| 1413 |
[ |
| 1414 |
"makepkg", |
| 1415 |
], |
| 1416 |
env={"PKGEXT": ".pkg.tar.zst"}, |
| 1417 |
check=True, |
| 1418 |
cwd=pkgbuild_path, |
| 1419 |
) |
| 1420 |
except subprocess.CalledProcessError as e: |
| 1421 |
raise BriefcaseCommandError( |
| 1422 |
f"Error while building .pkg.tar.zst package for {app.app_name}." |
| 1423 |
) from e |
| 1424 |
|
| 1425 |
# Move the pkg file to its final location |
| 1426 |
self.tools.shutil.move( |
| 1427 |
pkgbuild_path / self.distribution_filename(app), |
| 1428 |
self.distribution_path(app), |
| 1429 |
) |
| 1430 |
|
| 1431 |
|
| 1432 |
class LinuxSystemPublishCommand(LinuxSystemDockerMixin, PublishCommand): |
| 1433 |
description = "Publish a Linux system project." |
| 1434 |
|
| 1435 |
|
| 1436 |
# Declare the briefcase command bindings |
| 1437 |
create = LinuxSystemCreateCommand |
| 1438 |
update = LinuxSystemUpdateCommand |
| 1439 |
open = LinuxSystemOpenCommand |
| 1440 |
build = LinuxSystemBuildCommand |
| 1441 |
run = LinuxSystemRunCommand |
| 1442 |
package = LinuxSystemPackageCommand |
| 1443 |
publish = LinuxSystemPublishCommand |
| 1444 |
dev = LinuxSystemDevCommand |