/
gnome.py
305 lines (273 loc) · 10.9 KB
/
gnome.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# -*- Mode:Python; indent-tabs-mode:nil; tab-width:4 -*-
#
# Copyright 2022,2024 Canonical Ltd.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""Generic GNOME extension to support core22 and onwards."""
import dataclasses
import functools
import re
from typing import Any, Dict, List, Optional, Tuple
from overrides import overrides
from .extension import Extension, get_extensions_data_dir, prepend_to_env
_SDK_SNAP = {"core22": "gnome-42-2204-sdk", "core24": "gnome-46-2404-sdk"}
_PLATFORM_TRANSLATION = {"core22": "2204", "core24": "2404"}
@dataclasses.dataclass
class GNOMESnaps:
"""A structure of GNOME related snaps.
:cvar sdk: The name of the SDK snap to use.
:cvar content: The name of the content snap to use.
:cvar builtin: True if the SDK is built into the content snap.
"""
sdk: str
content: str
builtin: bool = True
class GNOME(Extension):
"""An extension that eases the creation of snaps that integrate with GNOME.
When used with core22 GNOME 42 will be used.
At build time it ensures the right build dependencies are setup and for
the runtime it ensures the application is run in an environment catered
for GNOME applications.
It configures each application with the following plugs:
- GTK3 Themes.
- Common Icon Themes.
- Common Sound Themes.
- The GNOME runtime libraries and utilities corresponding to 3.38.
For easier desktop integration, it also configures each application
entry with these additional plugs:
- desktop (https://snapcraft.io/docs/desktop-interface)
- desktop-legacy (https://snapcraft.io/docs/desktop-legacy-interface)
- gsettings (https://snapcraft.io/docs/gsettings-interface)
- opengl (https://snapcraft.io/docs/opengl-interface)
- wayland (https://snapcraft.io/docs/wayland-interface)
- x11 (https://snapcraft.io/docs/x11-interface)
"""
@staticmethod
@overrides
def get_supported_bases() -> Tuple[str, ...]:
return ("core22", "core24")
@staticmethod
@overrides
def get_supported_confinement() -> Tuple[str, ...]:
return "strict", "devmode"
@staticmethod
@overrides
def is_experimental(base: Optional[str]) -> bool:
return False
@overrides
def get_app_snippet(self) -> Dict[str, Any]:
return {
"command-chain": ["snap/command-chain/desktop-launch"],
"plugs": [
"desktop",
"desktop-legacy",
"gsettings",
"opengl",
"wayland",
"x11",
],
}
@functools.cached_property
def gnome_snaps(self) -> GNOMESnaps:
"""Return the GNOME related snaps to use to construct the environment."""
base = self.yaml_data["base"]
sdk_snap = _SDK_SNAP[base]
build_snaps: List[str] = []
for part in self.yaml_data["parts"].values():
build_snaps.extend(part.get("build-snaps", []))
# use the sdk snap if it is defined in any part's build-snaps
# otherwise, assume it is built into the content snap
matcher = re.compile(r"gnome-\d+-" + _PLATFORM_TRANSLATION[base] + r"-sdk.*")
sdk_snap_candidates = [s for s in build_snaps if matcher.match(s)]
if sdk_snap_candidates:
sdk_snap = sdk_snap_candidates[0].split("/")[0]
builtin = False
else:
builtin = True
# The same except the trailing -sdk
content = sdk_snap[:-4]
return GNOMESnaps(sdk=sdk_snap, content=content, builtin=builtin)
@overrides
def get_root_snippet(self) -> Dict[str, Any]:
platform_snap = self.gnome_snaps.content
return {
"assumes": ["snapd2.43"], # for 'snapctl is-connected'
"plugs": {
"desktop": {"mount-host-font-cache": False},
"gtk-3-themes": {
"interface": "content",
"target": "$SNAP/data-dir/themes",
"default-provider": "gtk-common-themes",
},
"icon-themes": {
"interface": "content",
"target": "$SNAP/data-dir/icons",
"default-provider": "gtk-common-themes",
},
"sound-themes": {
"interface": "content",
"target": "$SNAP/data-dir/sounds",
"default-provider": "gtk-common-themes",
},
platform_snap: {
"interface": "content",
"target": "$SNAP/gnome-platform",
"default-provider": platform_snap,
},
},
"environment": {
"SNAP_DESKTOP_RUNTIME": "$SNAP/gnome-platform",
"GTK_USE_PORTAL": "1",
},
"hooks": {
"configure": {
"plugs": ["desktop"],
"command-chain": ["snap/command-chain/hooks-configure-fonts"],
}
},
"layout": {
"/usr/lib/$CRAFT_ARCH_TRIPLET_BUILD_FOR/webkit2gtk-4.0": {
"bind": (
"$SNAP/gnome-platform/usr/lib/"
"$CRAFT_ARCH_TRIPLET_BUILD_FOR/webkit2gtk-4.0"
)
},
"/usr/lib/$CRAFT_ARCH_TRIPLET_BUILD_FOR/webkit2gtk-4.1": {
"bind": (
"$SNAP/gnome-platform/usr/lib/"
"$CRAFT_ARCH_TRIPLET_BUILD_FOR/webkit2gtk-4.1"
)
},
"/usr/share/xml/iso-codes": {
"bind": "$SNAP/gnome-platform/usr/share/xml/iso-codes"
},
"/usr/share/libdrm": {"bind": "$SNAP/gnome-platform/usr/share/libdrm"},
},
}
@overrides
def get_part_snippet(self, *, plugin_name: str) -> Dict[str, Any]:
sdk_snap = self.gnome_snaps.sdk
return {
"build-environment": [
{
"PATH": prepend_to_env(
"PATH", [f"/snap/{sdk_snap}/current/usr/bin"]
),
},
{
"XDG_DATA_DIRS": prepend_to_env(
"XDG_DATA_DIRS",
[
f"$SNAPCRAFT_STAGE/usr/share:/snap/{sdk_snap}/current/usr/share",
"/usr/share",
],
),
},
{
"LD_LIBRARY_PATH": prepend_to_env(
"LD_LIBRARY_PATH",
[
f"/snap/{sdk_snap}/current/lib/$CRAFT_ARCH_TRIPLET_BUILD_FOR",
f"/snap/{sdk_snap}/current/usr/lib/$CRAFT_ARCH_TRIPLET_BUILD_FOR",
f"/snap/{sdk_snap}/current/usr/lib",
f"/snap/{sdk_snap}/current/usr/lib/vala-current",
(
f"/snap/{sdk_snap}/current/usr/lib/"
"$CRAFT_ARCH_TRIPLET_BUILD_FOR/pulseaudio"
),
],
),
},
{
"PKG_CONFIG_PATH": prepend_to_env(
"PKG_CONFIG_PATH",
[
(
f"/snap/{sdk_snap}/current/usr/lib/"
"$CRAFT_ARCH_TRIPLET_BUILD_FOR/pkgconfig"
),
f"/snap/{sdk_snap}/current/usr/lib/pkgconfig",
f"/snap/{sdk_snap}/current/usr/share/pkgconfig",
],
),
},
{
"GETTEXTDATADIRS": prepend_to_env(
"GETTEXTDATADIRS",
[
f"/snap/{sdk_snap}/current/usr/share/gettext-current",
],
),
},
{
"GDK_PIXBUF_MODULE_FILE": (
f"/snap/{sdk_snap}/current/usr/lib/$CRAFT_ARCH_TRIPLET_BUILD_FOR"
"/gdk-pixbuf-current/loaders.cache"
),
},
{
"ACLOCAL_PATH": prepend_to_env(
"ACLOCAL_PATH",
[
f"/snap/{sdk_snap}/current/usr/share/aclocal",
],
),
},
{
"PYTHONPATH": prepend_to_env(
"PYTHONPATH",
[
f"/snap/{sdk_snap}/current/usr/lib/python3.10",
f"/snap/{sdk_snap}/current/usr/lib/python3/dist-packages",
f"/snap/{sdk_snap}/current/usr/lib/$CRAFT_ARCH_TRIPLET_BUILD_FOR"
"/gobject-introspection",
],
),
},
{
"GI_TYPELIB_PATH": prepend_to_env(
"GI_TYPELIB_PATH",
[
f"/snap/{sdk_snap}/current/usr/lib/girepository-1.0",
(
f"/snap/{sdk_snap}/usr/lib/"
"$CRAFT_ARCH_TRIPLET_BUILD_FOR/girepository-1.0"
),
],
)
},
],
}
@overrides
def get_parts_snippet(self) -> Dict[str, Any]:
"""Get the parts snippet for the GNOME extension.
If the GNOME SDK is not built into the content snap, the add the
sdk snap as a build-snap.
"""
source = get_extensions_data_dir() / "desktop" / "command-chain"
if self.gnome_snaps.builtin:
base = self.yaml_data["base"]
sdk_snap = _SDK_SNAP[base]
return {
"gnome/sdk": {
"source": str(source),
"plugin": "make",
"build-snaps": [sdk_snap],
}
}
return {
"gnome/sdk": {
"source": str(source),
"plugin": "make",
}
}