| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import subprocess |
| 4 |
from collections.abc import Collection |
| 5 |
from pathlib import Path |
| 6 |
|
| 7 |
from packaging.version import Version |
| 8 |
|
| 9 |
from briefcase.exceptions import BriefcaseCommandError |
| 10 |
from briefcase.integrations.base import Tool, ToolCache |
| 11 |
from briefcase.integrations.subprocess import SubprocessArgT |
| 12 |
|
| 13 |
|
| 14 |
class Flatpak(Tool): |
| 15 |
name = "flatpak" |
| 16 |
full_name = "Flatpak" |
| 17 |
supported_host_os: Collection[str] = {"Linux"} |
| 18 |
|
| 19 |
DEFAULT_REPO_ALIAS = "flathub" |
| 20 |
DEFAULT_REPO_URL = "https://flathub.org/repo/flathub.flatpakrepo" |
| 21 |
|
| 22 |
@classmethod |
| 23 |
def verify_install(cls, tools: ToolCache, **kwargs) -> Flatpak: |
| 24 |
"""Verify that the Flatpak toolchain is available. |
| 25 |
|
| 26 |
:param tools: ToolCache of available tools |
| 27 |
:returns: A wrapper for the Flatpak tools. |
| 28 |
""" |
| 29 |
# short circuit since already verified and available |
| 30 |
if hasattr(tools, "flatpak"): |
| 31 |
return tools.flatpak |
| 32 |
|
| 33 |
flatpak = Flatpak(tools=tools) |
| 34 |
try: |
| 35 |
output = tools.subprocess.check_output(["flatpak", "--version"]).strip("\n") |
| 36 |
parts = output.split(" ") |
| 37 |
try: |
| 38 |
if parts[0] == "Flatpak": |
| 39 |
version = parts[1].split(".") |
| 40 |
if int(version[0]) < 1: |
| 41 |
raise BriefcaseCommandError( |
| 42 |
"Briefcase requires Flatpak 1.0 or later." |
| 43 |
) |
| 44 |
else: |
| 45 |
raise ValueError(f"Unexpected tool name {parts[0]}") from None |
| 46 |
except (ValueError, IndexError): |
| 47 |
tools.console.warning( |
| 48 |
"""\ |
| 49 |
************************************************************************* |
| 50 |
** WARNING: Unable to determine the version of Flatpak ** |
| 51 |
************************************************************************* |
| 52 |
|
| 53 |
Briefcase will proceed, assuming everything is OK. If you |
| 54 |
experience problems, this is almost certainly the cause of those |
| 55 |
problems. |
| 56 |
|
| 57 |
Please report this as a bug at: |
| 58 |
|
| 59 |
https://github.com/beeware/briefcase/issues/new |
| 60 |
|
| 61 |
In your report, please including the output from running: |
| 62 |
|
| 63 |
flatpak --version |
| 64 |
|
| 65 |
from the command prompt. |
| 66 |
|
| 67 |
************************************************************************* |
| 68 |
""" |
| 69 |
) |
| 70 |
|
| 71 |
except OSError as e: |
| 72 |
raise BriefcaseCommandError( |
| 73 |
"""\ |
| 74 |
Briefcase requires the Flatpak toolchain, but it does not appear to be installed. |
| 75 |
|
| 76 |
Instructions for installing the Flatpak toolchain can be found at: |
| 77 |
|
| 78 |
https://flatpak.org/setup/ |
| 79 |
|
| 80 |
You must install both flatpak and flatpak-builder. |
| 81 |
""" |
| 82 |
) from e |
| 83 |
except subprocess.CalledProcessError as e: |
| 84 |
raise BriefcaseCommandError("Unable to invoke flatpak.") from e |
| 85 |
|
| 86 |
try: |
| 87 |
output = tools.subprocess.check_output( |
| 88 |
["flatpak-builder", "--version"] |
| 89 |
).strip("\n") |
| 90 |
|
| 91 |
# flatpak-builder 1.3 changed the output of --version |
| 92 |
# from "flatpak-builder 1.2.X" to "flatpak-build-1.3.X". |
| 93 |
# Converge on the new-style format. |
| 94 |
parts = output.replace(" ", "-").rsplit("-", 1) |
| 95 |
try: |
| 96 |
if parts[0] == "flatpak-builder": |
| 97 |
version = parts[1].split(".") |
| 98 |
if int(version[0]) < 1: |
| 99 |
raise BriefcaseCommandError( |
| 100 |
"Briefcase requires flatpak-builder 1.0 or later." |
| 101 |
) |
| 102 |
else: |
| 103 |
raise ValueError(f"Unexpected tool name {parts[0]}") |
| 104 |
except (ValueError, IndexError): |
| 105 |
tools.console.warning( |
| 106 |
"""\ |
| 107 |
************************************************************************* |
| 108 |
** WARNING: Unable to determine the version of flatpak-builder ** |
| 109 |
************************************************************************* |
| 110 |
|
| 111 |
Briefcase will proceed, assuming everything is OK. If you |
| 112 |
experience problems, this is almost certainly the cause of those |
| 113 |
problems. |
| 114 |
|
| 115 |
Please report this as a bug at: |
| 116 |
|
| 117 |
https://github.com/beeware/briefcase/issues/new |
| 118 |
|
| 119 |
In your report, please including the output from running: |
| 120 |
|
| 121 |
flatpak-builder --version |
| 122 |
|
| 123 |
from the command prompt. |
| 124 |
|
| 125 |
************************************************************************* |
| 126 |
""" |
| 127 |
) |
| 128 |
|
| 129 |
except OSError as e: |
| 130 |
raise BriefcaseCommandError( |
| 131 |
"""\ |
| 132 |
Briefcase requires the full Flatpak development toolchain, but flatpak-builder |
| 133 |
does not appear to be installed. |
| 134 |
|
| 135 |
Instructions for installing the Flatpak toolchain can be found at: |
| 136 |
|
| 137 |
https://flatpak.org/setup/ |
| 138 |
|
| 139 |
You must install both flatpak and flatpak-builder. |
| 140 |
""" |
| 141 |
) from e |
| 142 |
except subprocess.CalledProcessError as e: |
| 143 |
raise BriefcaseCommandError("Unable to invoke flatpak-builder.") from e |
| 144 |
|
| 145 |
tools.flatpak = flatpak |
| 146 |
return flatpak |
| 147 |
|
| 148 |
def verify_repo(self, repo_alias: str, url: str): |
| 149 |
"""Verify that the Flatpak repository has been registered. |
| 150 |
|
| 151 |
:param repo_alias: The alias to use when registering the repo. |
| 152 |
:param url: The URL of the Flatpak repo. |
| 153 |
""" |
| 154 |
try: |
| 155 |
self.tools.subprocess.run( |
| 156 |
[ |
| 157 |
"flatpak", |
| 158 |
"remote-add", |
| 159 |
"--user", |
| 160 |
"--if-not-exists", |
| 161 |
repo_alias, |
| 162 |
url, |
| 163 |
] |
| 164 |
+ (["--verbose"] if self.tools.console.is_deep_debug else []), |
| 165 |
check=True, |
| 166 |
) |
| 167 |
except subprocess.CalledProcessError as e: |
| 168 |
raise BriefcaseCommandError( |
| 169 |
f"Unable to add Flatpak repo {url} with alias {repo_alias}." |
| 170 |
) from e |
| 171 |
|
| 172 |
def verify_runtime( |
| 173 |
self, |
| 174 |
repo_alias: str, |
| 175 |
runtime: str, |
| 176 |
runtime_version: str, |
| 177 |
sdk: str, |
| 178 |
): |
| 179 |
"""Verify that a specific Flatpak runtime and SDK are available. |
| 180 |
|
| 181 |
:param repo_alias: The alias of the repo where the runtime and SDK are stored. |
| 182 |
:param runtime: The identifier of the Flatpak runtime |
| 183 |
:param runtime_version: The version of the Flatpak runtime |
| 184 |
:param sdk: The Flatpak SDK |
| 185 |
""" |
| 186 |
try: |
| 187 |
self.tools.subprocess.run( |
| 188 |
[ |
| 189 |
"flatpak", |
| 190 |
"install", |
| 191 |
"--assumeyes", |
| 192 |
"--user", |
| 193 |
repo_alias, |
| 194 |
f"{runtime}/{self.tools.host_arch}/{runtime_version}", |
| 195 |
f"{sdk}/{self.tools.host_arch}/{runtime_version}", |
| 196 |
] |
| 197 |
+ (["--verbose"] if self.tools.console.is_deep_debug else []), |
| 198 |
check=True, |
| 199 |
# flatpak install uses many animations that cannot be disabled |
| 200 |
stream_output=False, |
| 201 |
) |
| 202 |
except subprocess.CalledProcessError as e: |
| 203 |
raise BriefcaseCommandError( |
| 204 |
f"Unable to install " |
| 205 |
f"Flatpak runtime {runtime}/{self.tools.host_arch}/{runtime_version} " |
| 206 |
f"and SDK {sdk}/{self.tools.host_arch}/{runtime_version} " |
| 207 |
f"from repo {repo_alias}." |
| 208 |
) from e |
| 209 |
|
| 210 |
def build(self, bundle_identifier: str, app_name: str, path: Path): |
| 211 |
"""Build a Flatpak manifest. |
| 212 |
|
| 213 |
On success, the app is installed into the user's local Flatpak install, and a |
| 214 |
shell script is created that can be used to start the app. The shell file isn't |
| 215 |
really needed to start the app, but it serves as a marker for a successful build |
| 216 |
that Briefcase can use. |
| 217 |
|
| 218 |
:param bundle_identifier: The bundle identifier for the app being built. |
| 219 |
:param app_name: The app name. |
| 220 |
:param path: The path to the folder containing the app's Flatpak manifest file. |
| 221 |
""" |
| 222 |
try: |
| 223 |
self.tools.subprocess.run( |
| 224 |
[ |
| 225 |
"flatpak-builder", |
| 226 |
"--force-clean", |
| 227 |
# Archive into a local repository |
| 228 |
"--repo", |
| 229 |
"repo", |
| 230 |
# Install the app into the user space |
| 231 |
"--install", |
| 232 |
"--user", |
| 233 |
"build", |
| 234 |
"manifest.yml", |
| 235 |
] |
| 236 |
+ (["--verbose"] if self.tools.console.is_deep_debug else []), |
| 237 |
check=True, |
| 238 |
cwd=path, |
| 239 |
) |
| 240 |
|
| 241 |
# Create a marker file to indicate a build has completed. |
| 242 |
# For bonus points, the marker file also is executable |
| 243 |
# and is an alias for the command that would actually start |
| 244 |
# the flatpak. |
| 245 |
bin_path = path / bundle_identifier |
| 246 |
with bin_path.open("w", encoding="utf-8") as f: |
| 247 |
f.write( |
| 248 |
f"""\ |
| 249 |
#!/bin/sh |
| 250 |
# echo To run this flatpak, run: |
| 251 |
flatpak run {bundle_identifier} |
| 252 |
""" |
| 253 |
) |
| 254 |
self.tools.os.chmod(bin_path, 0o755) |
| 255 |
except subprocess.CalledProcessError as e: |
| 256 |
raise BriefcaseCommandError(f"Error while building app {app_name}.") from e |
| 257 |
|
| 258 |
def run( |
| 259 |
self, |
| 260 |
bundle_identifier: str, |
| 261 |
args: list[SubprocessArgT] | None = None, |
| 262 |
stream_output: bool = True, |
| 263 |
**kwargs, |
| 264 |
) -> subprocess.Popen[str]: |
| 265 |
"""Run a Flatpak in a way that allows for log streaming. |
| 266 |
|
| 267 |
:param bundle_identifier: The bundle identifier for the app being built. |
| 268 |
:param args: (Optional) The list of arguments to pass to the app |
| 269 |
:param stream_output: Should output be streamed? |
| 270 |
:returns: A Popen object for the running app; or ``None`` if the app isn't |
| 271 |
streaming |
| 272 |
""" |
| 273 |
flatpak_run_cmd = ["flatpak", "run", bundle_identifier] |
| 274 |
flatpak_run_cmd.extend([] if args is None else args) |
| 275 |
|
| 276 |
if self.tools.console.is_deep_debug: |
| 277 |
# Must come before bundle identifier; |
| 278 |
# otherwise, it's passed as an arg to app |
| 279 |
flatpak_run_cmd.insert(2, "--verbose") |
| 280 |
|
| 281 |
if stream_output: |
| 282 |
return self.tools.subprocess.Popen( |
| 283 |
flatpak_run_cmd, |
| 284 |
stdout=subprocess.PIPE, |
| 285 |
stderr=subprocess.STDOUT, |
| 286 |
bufsize=1, |
| 287 |
**kwargs, |
| 288 |
) |
| 289 |
else: |
| 290 |
return self.tools.subprocess.run( |
| 291 |
flatpak_run_cmd, |
| 292 |
bufsize=1, |
| 293 |
stream_output=False, |
| 294 |
**kwargs, |
| 295 |
) |
| 296 |
|
| 297 |
def bundle( |
| 298 |
self, |
| 299 |
repo_url: str, |
| 300 |
bundle_identifier: str, |
| 301 |
app_name: str, |
| 302 |
version: Version, |
| 303 |
build_path: Path, |
| 304 |
output_path: Path, |
| 305 |
): |
| 306 |
"""Bundle a Flatpak for distribution. |
| 307 |
|
| 308 |
Generates a standalone .flatpak file that can be installed into another user's |
| 309 |
Flatpak repository. |
| 310 |
|
| 311 |
:param repo_url: The URL of the repository that contains the runtime used by the |
| 312 |
app. |
| 313 |
:param bundle_identifier: The bundle identifier for the app being built. |
| 314 |
:param app_name: The app name. |
| 315 |
:param version: The version of the app being built. |
| 316 |
:param build_path: The path where the flatpak was built. This path will contain |
| 317 |
the repo where the built flatpak was exported. |
| 318 |
:param output_path: The path of the output file to create as an export. |
| 319 |
""" |
| 320 |
try: |
| 321 |
self.tools.subprocess.run( |
| 322 |
[ |
| 323 |
"flatpak", |
| 324 |
"build-bundle", |
| 325 |
# Set the repo where the runtime can be found |
| 326 |
"--runtime-repo", |
| 327 |
repo_url, |
| 328 |
# Sign the export |
| 329 |
# "--gpg-sign", "..." |
| 330 |
"repo", |
| 331 |
output_path, |
| 332 |
bundle_identifier, |
| 333 |
str(version), |
| 334 |
] |
| 335 |
+ (["--verbose"] if self.tools.console.is_deep_debug else []), |
| 336 |
check=True, |
| 337 |
cwd=build_path, |
| 338 |
) |
| 339 |
except subprocess.CalledProcessError as e: |
| 340 |
raise BriefcaseCommandError( |
| 341 |
f"Unable to build a Flatpak bundle for app {app_name}." |
| 342 |
) from e |