Debian Codegraph @

src/briefcase/platforms/linux/flatpak.py

raw
1 from __future__ import annotations
2
3 from collections.abc import Collection
4
5 from briefcase.commands import (
6 BuildCommand,
7 CreateCommand,
8 DevCommand,
9 OpenCommand,
10 PackageCommand,
11 PublishCommand,
12 RunCommand,
13 UpdateCommand,
14 )
15 from briefcase.config import AppConfig
16 from briefcase.exceptions import BriefcaseConfigError
17 from briefcase.integrations.flatpak import Flatpak
18 from briefcase.platforms.linux import LinuxMixin
19
20
21 class LinuxFlatpakMixin(LinuxMixin):
22 output_format = "flatpak"
23 supported_host_os: Collection[str] = {"Linux"}
24 supported_host_os_reason = "Flatpaks can only be built on Linux."
25 platform_target_version = "0.3.20"
26
27 def binary_path(self, app):
28 # Flatpak doesn't really produce an identifiable "binary" as part of its
29 # build process, so the SDK wrapper creates a file that can use to identify
30 # if run has been invoked. As a neat side effect, it's also a shell script
31 # that can invoke the flatpak.
32 return self.bundle_path(app) / app.bundle_identifier
33
34 def project_path(self, app):
35 return self.bundle_path(app)
36
37 def distribution_path(self, app):
38 binary_name = app.formal_name.replace(" ", "_")
39 return (
40 self.dist_path
41 / f"{binary_name}-{app.version}-{self.tools.host_arch}.flatpak"
42 )
43
44 def verify_tools(self):
45 """Verify that we're on Linux."""
46 super().verify_tools()
47 Flatpak.verify(tools=self.tools)
48
49 def flatpak_runtime_repo(self, app):
50 try:
51 repo_url = app.flatpak_runtime_repo_url
52 try:
53 repo_alias = app.flatpak_runtime_repo_alias
54 except AttributeError:
55 raise BriefcaseConfigError(
56 "If you specify a custom Flatpak runtime repository, "
57 "you must also specify an alias for that repository using "
58 "`flatpak_runtime_repo_alias`"
59 ) from None
60
61 except AttributeError:
62 repo_alias = Flatpak.DEFAULT_REPO_ALIAS
63 repo_url = Flatpak.DEFAULT_REPO_URL
64
65 return repo_alias, repo_url
66
67 def flatpak_runtime(self, app):
68 try:
69 return app.flatpak_runtime
70 except AttributeError as e:
71 raise BriefcaseConfigError("""\
72 The App does not specify the Flatpak runtime to use.
73
74 Your application configuration must provide values for
75 `flatpak_sdk`, `flatpak_runtime`, and `flatpak_runtime_version`.
76 """) from e
77
78 def flatpak_runtime_version(self, app):
79 try:
80 return app.flatpak_runtime_version
81 except AttributeError as e:
82 raise BriefcaseConfigError("""\
83 The App does not specify the version of the Flatpak runtime to use.
84
85 Your application configuration must provide values for
86 `flatpak_sdk`, `flatpak_runtime`, and `flatpak_runtime_version`.
87 """) from e
88
89 def flatpak_sdk(self, app):
90 try:
91 return app.flatpak_sdk
92 except AttributeError as e:
93 raise BriefcaseConfigError("""\
94 The App does not specify the Flatpak SDK to use.
95
96 Your application configuration must provide values for
97 `flatpak_sdk`, `flatpak_runtime`, and `flatpak_runtime_version`.
98 """) from e
99
100
101 class LinuxFlatpakCreateCommand(LinuxFlatpakMixin, CreateCommand):
102 description = "Create and populate a Linux Flatpak."
103 hidden_app_properties: Collection[str] = {"permission", "finish_arg"}
104
105 def output_format_template_context(self, app: AppConfig):
106 """Add flatpak runtime/SDK details to the app template."""
107 return {
108 "flatpak_runtime": self.flatpak_runtime(app),
109 "flatpak_runtime_version": self.flatpak_runtime_version(app),
110 "flatpak_sdk": self.flatpak_sdk(app),
111 }
112
113 def permissions_context(self, app: AppConfig, x_permissions: dict[str, str]):
114 """Additional template context for permissions.
115
116 :param app: The config object for the app
117 :param x_permissions: The dictionary of known cross-platform permission
118 definitions.
119 :returns: The template context describing permissions for the app.
120 """
121 # The default finish arguments that Briefcase adds on every Flatpak.
122 finish_args = {
123 # X11 + XShm access
124 "share=ipc": True,
125 "socket=x11": True,
126 # Disable Wayland access
127 "nosocket=wayland": True,
128 # Network access
129 "share=network": True,
130 # GPU access
131 "device=dri": True,
132 # Sound access
133 "socket=pulseaudio": True,
134 # Host filesystem access
135 "filesystem=xdg-cache": True,
136 "filesystem=xdg-config": True,
137 "filesystem=xdg-data": True,
138 "filesystem=xdg-documents": True,
139 }
140
141 finish_args.update(getattr(app, "finish_arg", {}))
142
143 return {
144 "finish_args": finish_args,
145 }
146
147
148 class LinuxFlatpakUpdateCommand(LinuxFlatpakCreateCommand, UpdateCommand):
149 description = "Update an existing Linux Flatpak."
150
151
152 class LinuxFlatpakOpenCommand(LinuxFlatpakMixin, OpenCommand):
153 description = "Open the folder containing an existing Linux Flatpak project."
154
155
156 class LinuxFlatpakBuildCommand(LinuxFlatpakMixin, BuildCommand):
157 description = "Build a Linux Flatpak."
158
159 def build_app(self, app: AppConfig, **kwargs):
160 """Build an application.
161
162 :param app: The application to build
163 """
164 self.console.info(
165 "Ensuring Flatpak runtime for the app is available...",
166 prefix=app.app_name,
167 )
168 flatpak_repo_alias, flatpak_repo_url = self.flatpak_runtime_repo(app)
169
170 with self.console.wait_bar("Ensuring Flatpak runtime repo is registered..."):
171 self.tools.flatpak.verify_repo(
172 repo_alias=flatpak_repo_alias,
173 url=flatpak_repo_url,
174 )
175
176 # ``flatpak install`` uses a lot of console animations, and there
177 # doesn't appear to be a way to turn off these animations. Use those
178 # native animations rather than wrapping in a wait_bar.
179 self.tools.flatpak.verify_runtime(
180 repo_alias=flatpak_repo_alias,
181 runtime=self.flatpak_runtime(app),
182 runtime_version=self.flatpak_runtime_version(app),
183 sdk=self.flatpak_sdk(app),
184 )
185
186 self.console.info("Building Flatpak...", prefix=app.app_name)
187 with self.console.wait_bar("Building..."):
188 self.tools.flatpak.build(
189 bundle_identifier=app.bundle_identifier,
190 app_name=app.app_name,
191 path=self.bundle_path(app),
192 )
193
194
195 class LinuxFlatpakRunCommand(LinuxFlatpakMixin, RunCommand):
196 description = "Run a Linux Flatpak."
197
198 def run_app(
199 self,
200 app: AppConfig,
201 passthrough: list[str],
202 **kwargs,
203 ):
204 """Start the application.
205
206 :param app: The config object for the app
207 :param passthrough: The list of arguments to pass to the app
208 """
209 # Set up the log stream
210 kwargs = self._prepare_app_kwargs(app=app)
211
212 # Console apps must operate in non-streaming mode so that console input can
213 # be handled correctly. However, if we're in test mode, we *must* stream so
214 # that we can see the test exit sentinel
215 if app.console_app and not app.test_mode:
216 self.console.info("=" * 75)
217 self.tools.flatpak.run(
218 bundle_identifier=app.bundle_identifier,
219 args=passthrough,
220 stream_output=False,
221 **kwargs,
222 )
223 else:
224 # Start the app in a way that lets us stream the logs
225 app_popen = self.tools.flatpak.run(
226 bundle_identifier=app.bundle_identifier,
227 args=passthrough,
228 stream_output=True,
229 **kwargs,
230 )
231
232 # Start streaming logs for the app.
233 self._stream_app_logs(
234 app,
235 popen=app_popen,
236 clean_output=False,
237 )
238
239
240 class LinuxFlatpakDevCommand(LinuxFlatpakMixin, DevCommand):
241 description = "Run a Linux Flatpak app in development mode"
242
243
244 class LinuxFlatpakPackageCommand(LinuxFlatpakMixin, PackageCommand):
245 description = "Package a Linux Flatpak for distribution."
246
247 def package_app(self, app: AppConfig, **kwargs):
248 """Start the application.
249
250 :param app: The config object for the app
251 """
252 self.console.info("Building bundle...", prefix=app.app_name)
253 with self.console.wait_bar("Bundling..."):
254 _, flatpak_repo_url = self.flatpak_runtime_repo(app)
255 self.tools.flatpak.bundle(
256 repo_url=flatpak_repo_url,
257 bundle_identifier=app.bundle_identifier,
258 app_name=app.app_name,
259 version=app.version,
260 build_path=self.bundle_path(app),
261 output_path=self.distribution_path(app),
262 )
263
264
265 class LinuxFlatpakPublishCommand(LinuxFlatpakMixin, PublishCommand):
266 description = "Publish a Linux Flatpak."
267
268
269 # Declare the briefcase command bindings
270 create = LinuxFlatpakCreateCommand
271 update = LinuxFlatpakUpdateCommand
272 open = LinuxFlatpakOpenCommand
273 build = LinuxFlatpakBuildCommand
274 run = LinuxFlatpakRunCommand
275 package = LinuxFlatpakPackageCommand
276 publish = LinuxFlatpakPublishCommand
277 dev = LinuxFlatpakDevCommand