| 1 |
import ast |
| 2 |
import re |
| 3 |
import subprocess |
| 4 |
import sys |
| 5 |
from pathlib import Path |
| 6 |
|
| 7 |
from briefcase.commands.create import _is_local_path |
| 8 |
from briefcase.commands.open import OpenCommand |
| 9 |
from briefcase.config import AppConfig |
| 10 |
from briefcase.exceptions import BriefcaseCommandError, ParseError |
| 11 |
|
| 12 |
DEFAULT_OUTPUT_FORMAT = "system" |
| 13 |
|
| 14 |
DEBIAN = "debian" |
| 15 |
RHEL = "rhel" |
| 16 |
ARCH = "arch" |
| 17 |
SUSE = "suse" |
| 18 |
|
| 19 |
|
| 20 |
def parse_freedesktop_os_release(content): |
| 21 |
"""Parse the content of an /etc/os-release file. |
| 22 |
|
| 23 |
Implementation adapted from Example 5 of |
| 24 |
https://www.freedesktop.org/software/systemd/man/os-release.html |
| 25 |
|
| 26 |
:param content: The text content of the /etc/os-release file. |
| 27 |
:returns: A dictionary of key-value pairs, in the same format returned by |
| 28 |
`platform.freedesktop_os_release()`. |
| 29 |
""" |
| 30 |
values = {} |
| 31 |
for line_number, line in enumerate(content.split("\n"), start=1): |
| 32 |
line = line.rstrip() |
| 33 |
if not line or line.startswith("#"): |
| 34 |
continue |
| 35 |
m = re.match(r"([A-Z][A-Z_0-9]+)=(.*)", line) |
| 36 |
if m: |
| 37 |
name, val = m.groups() |
| 38 |
if val and val[0] in "\"'": |
| 39 |
try: |
| 40 |
val = ast.literal_eval(val) |
| 41 |
except SyntaxError as e: |
| 42 |
raise ParseError( |
| 43 |
"Failed to parse output of FreeDesktop os-release file; " |
| 44 |
f"Line {line_number}: {e}" |
| 45 |
) from e |
| 46 |
values[name] = val |
| 47 |
else: |
| 48 |
raise ParseError( |
| 49 |
"Failed to parse output of FreeDesktop os-release file; " |
| 50 |
f"Line {line_number}: {line!r}" |
| 51 |
) |
| 52 |
|
| 53 |
return values |
| 54 |
|
| 55 |
|
| 56 |
class LinuxMixin: |
| 57 |
platform = "linux" |
| 58 |
|
| 59 |
def support_package_url(self, support_revision): |
| 60 |
"""The URL of the support package to use for apps of this type. |
| 61 |
|
| 62 |
Linux builds that use a support package (AppImage, Flatpak) use Astral's (nee |
| 63 |
indygreg's) Standalone Python to provide system packages. See |
| 64 |
`https://github.com/astral-sh/python-build-standalone` for details. |
| 65 |
|
| 66 |
System packages don't use a support package; this is defined by the template, so |
| 67 |
this method won't be invoked. |
| 68 |
""" |
| 69 |
python_download_arch = self.tools.host_arch |
| 70 |
# use a 32bit Python if using 32bit Python on 64bit hardware |
| 71 |
if self.tools.is_32bit_python and self.tools.host_arch == "aarch64": |
| 72 |
python_download_arch = "armv7" |
| 73 |
elif self.tools.is_32bit_python and self.tools.host_arch == "x86_64": |
| 74 |
python_download_arch = "i686" |
| 75 |
|
| 76 |
_version, datestamp = support_revision.split("+") |
| 77 |
return ( |
| 78 |
"https://github.com/astral-sh/python-build-standalone/releases/download/" |
| 79 |
f"{datestamp}/" |
| 80 |
f"cpython-{support_revision}-{python_download_arch}-unknown-linux-gnu-install_only_stripped.tar.gz" |
| 81 |
) |
| 82 |
|
| 83 |
def vendor_details(self, freedesktop_info): |
| 84 |
"""Normalize the identity of the target Linux vendor, version, and base. |
| 85 |
|
| 86 |
:param freedesktop_info: The parsed content of the FreeDesktop /etc/os-release |
| 87 |
file. This is the same format returned by |
| 88 |
`platform.freedesktop_os_release()`. |
| 89 |
:returns: A tuple of (vendor, version, vendor_base). |
| 90 |
""" |
| 91 |
vendor = freedesktop_info["ID"] |
| 92 |
try: |
| 93 |
codename = freedesktop_info["VERSION_CODENAME"] |
| 94 |
if not codename: |
| 95 |
# Fedora *has* a VERSION_CODENAME key, but it is empty. |
| 96 |
# Treat it as missing. |
| 97 |
raise KeyError("VERSION_CODENAME") |
| 98 |
except KeyError: |
| 99 |
try: |
| 100 |
# Arch uses a specific constant in VERSION_ID |
| 101 |
if freedesktop_info["VERSION_ID"] == "TEMPLATE_VERSION_ID": |
| 102 |
codename = "rolling" |
| 103 |
else: |
| 104 |
codename = freedesktop_info["VERSION_ID"].split(".")[0] |
| 105 |
except KeyError: |
| 106 |
# Manjaro doesn't have a VERSION_ID key |
| 107 |
codename = "rolling" |
| 108 |
|
| 109 |
# Process the vendor_base from the vendor. |
| 110 |
id_like = freedesktop_info.get("ID_LIKE", "").split() |
| 111 |
if vendor == DEBIAN or DEBIAN in id_like or "ubuntu" in id_like: |
| 112 |
vendor_base = DEBIAN |
| 113 |
elif vendor == RHEL or vendor == "fedora" or RHEL in id_like: |
| 114 |
vendor_base = RHEL |
| 115 |
elif vendor == ARCH or vendor == "cachyos" or ARCH in id_like: |
| 116 |
vendor_base = ARCH |
| 117 |
elif vendor == SUSE or SUSE in id_like: |
| 118 |
vendor_base = SUSE |
| 119 |
else: |
| 120 |
vendor_base = None |
| 121 |
|
| 122 |
return vendor, codename, vendor_base |
| 123 |
|
| 124 |
|
| 125 |
class LocalRequirementsMixin: # pragma: no-cover-if-is-windows |
| 126 |
# A mixin that captures the process of compiling requirements that are specified |
| 127 |
# as local file references into sdists, and then installing those requirements |
| 128 |
# from the sdist. |
| 129 |
|
| 130 |
def local_requirements_path(self, app): |
| 131 |
return self.bundle_path(app) / "_requirements" |
| 132 |
|
| 133 |
def _install_app_requirements( |
| 134 |
self, |
| 135 |
app: AppConfig, |
| 136 |
requires: list[str], |
| 137 |
app_packages_path: Path, |
| 138 |
**kwargs, |
| 139 |
): |
| 140 |
"""Install requirements for the app with pip. |
| 141 |
|
| 142 |
This method pre-compiles any requirement defined using a local path reference |
| 143 |
into an sdist tarball. This will be used when installing under Docker, as local |
| 144 |
file references can't be accessed in the Docker container. |
| 145 |
|
| 146 |
:param app: The app configuration |
| 147 |
:param requires: The list of requirements to install |
| 148 |
:param app_packages_path: The full path of the app_packages folder into which |
| 149 |
requirements should be installed. |
| 150 |
""" |
| 151 |
# If we're re-building requirements, purge any pre-existing local |
| 152 |
# requirements. |
| 153 |
local_requirements_path = self.local_requirements_path(app) |
| 154 |
if local_requirements_path.exists(): |
| 155 |
self.tools.shutil.rmtree(local_requirements_path) |
| 156 |
self.tools.os.mkdir(local_requirements_path) |
| 157 |
|
| 158 |
# Iterate over every requirement, looking for local references |
| 159 |
localized_requires = [] |
| 160 |
for requirement in requires: |
| 161 |
if _is_local_path(requirement): |
| 162 |
parts = requirement.rsplit("[", 1) |
| 163 |
req_name = parts[0] |
| 164 |
try: |
| 165 |
extras = f"[{parts[1]}" |
| 166 |
except IndexError: |
| 167 |
extras = "" |
| 168 |
|
| 169 |
local_req = (self.base_path / req_name).resolve() |
| 170 |
if local_req.is_dir(): |
| 171 |
# Requirement is a filesystem reference |
| 172 |
# Build a wheel for the local requirement |
| 173 |
with self.console.wait_bar(f"Building wheels for {req_name}..."): |
| 174 |
try: |
| 175 |
self.tools.subprocess.check_output( |
| 176 |
[ |
| 177 |
sys.executable, |
| 178 |
"-X", |
| 179 |
"utf8", |
| 180 |
"-m", |
| 181 |
"build", |
| 182 |
"--wheel", |
| 183 |
"--outdir", |
| 184 |
local_requirements_path, |
| 185 |
local_req, |
| 186 |
], |
| 187 |
encoding="UTF-8", |
| 188 |
) |
| 189 |
|
| 190 |
# The newest file in the directory will be the wheel that |
| 191 |
# was just created. |
| 192 |
newest_file = max( |
| 193 |
( |
| 194 |
f |
| 195 |
for f in self.local_requirements_path(app).iterdir() |
| 196 |
if f.is_file() |
| 197 |
), |
| 198 |
key=lambda f: f.stat().st_mtime, |
| 199 |
) |
| 200 |
|
| 201 |
localized_requires.append(str(newest_file) + extras) |
| 202 |
|
| 203 |
except subprocess.CalledProcessError as e: |
| 204 |
raise BriefcaseCommandError( |
| 205 |
f"Unable to build wheel for {requirement}" |
| 206 |
) from e |
| 207 |
else: |
| 208 |
try: |
| 209 |
# Requirement is an existing sdist or wheel file. |
| 210 |
self.tools.shutil.copy(local_req, local_requirements_path) |
| 211 |
|
| 212 |
# The requirement must be re-written as a local file reference |
| 213 |
localized_requires.append( |
| 214 |
str(self.local_requirements_path(app) / local_req.name) |
| 215 |
+ extras |
| 216 |
) |
| 217 |
|
| 218 |
except OSError as e: |
| 219 |
raise BriefcaseCommandError( |
| 220 |
f"Unable to find local requirement {requirement}" |
| 221 |
) from e |
| 222 |
else: |
| 223 |
# The requirement can be used as-is |
| 224 |
localized_requires.append(requirement) |
| 225 |
|
| 226 |
# Continue with the default app requirement handling. |
| 227 |
return super()._install_app_requirements( |
| 228 |
app, |
| 229 |
requires=localized_requires, |
| 230 |
app_packages_path=app_packages_path, |
| 231 |
) |
| 232 |
|
| 233 |
|
| 234 |
class DockerOpenCommand(OpenCommand): # pragma: no-cover-if-is-windows |
| 235 |
# A command that redirects Open to an interactive shell in the container |
| 236 |
# if Docker is being used. Relies on the final command to provide |
| 237 |
# verification that Docker is available, and verify the app context. |
| 238 |
|
| 239 |
def _open_app(self, app: AppConfig): |
| 240 |
# If we're using Docker, open an interactive shell in the container. |
| 241 |
# Rely on the default CMD statement in the image's Dockerfile to |
| 242 |
# define a default shell. |
| 243 |
if self.use_docker: |
| 244 |
self.tools[app].app_context.run([], interactive=True) |
| 245 |
else: |
| 246 |
super()._open_app(app) |