/
appstream.py
278 lines (223 loc) · 9.17 KB
/
appstream.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
# -*- Mode:Python; indent-tabs-mode:nil; tab-width:4 -*-
#
# Copyright 2017-2022 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/>.
"""Appstream metadata extractor."""
import contextlib
import operator
import os
from io import StringIO
from typing import List, Optional, cast
import lxml.etree
from xdg.DesktopEntry import DesktopEntry
from snapcraft import errors
from .extracted_metadata import ExtractedMetadata
_XSLT = """\
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()[not(@xml:lang)] | node()[@xml:lang = en]" />
</xsl:copy>
</xsl:template>
<xsl:template match="comment()"/>
<xsl:template match="p">
<xsl:text>
</xsl:text>
<xsl:apply-templates />
<xsl:text>
</xsl:text>
</xsl:template>
<xsl:template match="ul">
<xsl:text>
</xsl:text>
<xsl:for-each select="li[not(@xml:lang)] | li[@xml:lang = en]">
<xsl:text>- </xsl:text>
<xsl:apply-templates />
<xsl:text>
</xsl:text>
</xsl:for-each>
</xsl:template>
<xsl:template match="ol">
<xsl:text>
</xsl:text>
<xsl:for-each select="li[not(@xml:lang)] | li[@xml:lang = en]">
<xsl:number count="li[not(@xml:lang)] | li[@xml:lang = en]" format="1. "/>
<xsl:apply-templates />
<xsl:text>
</xsl:text>
</xsl:for-each>
</xsl:template>
<xsl:template match="em">
<xsl:text>_</xsl:text>
<xsl:apply-templates />
<xsl:text>_</xsl:text>
</xsl:template>
<xsl:template match="code">
<xsl:apply-templates />
</xsl:template>
</xsl:stylesheet>
"""
def extract(relpath: str, *, workdir: str) -> Optional[ExtractedMetadata]:
"""Extract appstream metadata.
:param file_relpath: Relative path to the file containing metadata.
:param workdir: The part working directory where the metadata file is located.
:return: The extracted metadata, if any.
"""
if not relpath.endswith(".metainfo.xml") and not relpath.endswith(".appdata.xml"):
return None
dom = _get_transformed_dom(os.path.join(workdir, relpath))
common_id = _get_value_from_xml_element(dom, "id")
summary = _get_value_from_xml_element(dom, "summary")
description = _get_value_from_xml_element(dom, "description")
title = _get_value_from_xml_element(dom, "name")
version = _get_latest_release_from_nodes(dom.findall("releases/release"))
desktop_file_paths = []
desktop_file_ids = _get_desktop_file_ids_from_nodes(dom.findall("launchable"))
# if there are no launchables, use the appstream id to take into
# account the legacy appstream definitions
if common_id and not desktop_file_ids:
if common_id.endswith(".desktop"):
desktop_file_ids.append(common_id)
else:
desktop_file_ids.append(common_id + ".desktop")
for desktop_file_id in desktop_file_ids:
desktop_file_path = _desktop_file_id_to_path(desktop_file_id, workdir=workdir)
if desktop_file_path:
desktop_file_paths.append(desktop_file_path)
icon = _extract_icon(dom, workdir, desktop_file_paths)
return ExtractedMetadata(
common_id=common_id,
title=title,
summary=summary,
description=description,
version=version,
icon=icon,
desktop_file_paths=desktop_file_paths,
)
def _get_transformed_dom(path: str):
dom = _get_dom(path)
transform = _get_xslt()
return transform(dom)
def _get_dom(path: str) -> lxml.etree.ElementTree:
try:
return lxml.etree.parse(path) # noqa S320
except OSError as err:
raise errors.SnapcraftError(str(err)) from err
except lxml.etree.ParseError as err:
raise errors.MetadataExtractionError(path, str(err)) from err
def _get_xslt():
xslt = lxml.etree.parse(StringIO(_XSLT)) # noqa S320
return lxml.etree.XSLT(xslt)
def _get_value_from_xml_element(tree, key) -> Optional[str]:
node = tree.find(key)
if node is not None and node.text:
# Lines that should be empty end up with empty space after the
# transformation. One example of this is seen for paragraphs (i.e.; <p>)
# than hold list in then (i.e.; <ol> or <ul>) so we split all lines
# here and strip any unwanted space.
# TODO: Improve the XSLT to remove the need for this.
return "\n".join([n.strip() for n in node.text.splitlines()]).strip()
return None
def _get_latest_release_from_nodes(nodes) -> Optional[str]:
for node in nodes:
if "version" in node.attrib:
return node.attrib["version"]
return None
def _get_desktop_file_ids_from_nodes(nodes) -> List[str]:
desktop_file_ids = [] # type: List[str]
for node in nodes:
if "type" in node.attrib and node.attrib["type"] == "desktop-id":
desktop_file_ids.append(node.text.strip())
return desktop_file_ids
def _desktop_file_id_to_path(desktop_file_id: str, *, workdir: str) -> Optional[str]:
# For details about desktop file ids and their corresponding paths, see
# https://standards.freedesktop.org/desktop-entry-spec/desktop-entry-spec-latest.html#desktop-file-id
for xdg_data_dir in ("usr/local/share", "usr/share"):
desktop_file_path = os.path.join(
xdg_data_dir, "applications", desktop_file_id.replace("-", "/")
)
# Check if it exists in workdir, but do not add it to the resulting path
# as it later needs to exist in the prime directory to effectively be
# used.
if os.path.exists(os.path.join(workdir, desktop_file_path)):
return desktop_file_path
return None
def _extract_icon(dom, workdir: str, desktop_file_paths: List[str]) -> Optional[str]:
icon_node = dom.find("icon")
if icon_node is not None and "type" in icon_node.attrib:
icon_node_type = icon_node.attrib["type"]
else:
icon_node_type = None
icon = icon_node.text.strip() if icon_node is not None else None
if icon_node_type == "remote":
return icon
if icon_node_type == "stock" and icon is not None:
return _get_icon_from_theme(workdir, "hicolor", icon)
# If an icon path is specified and the icon file exists, we'll use that, otherwise
# we'll fall back to what's listed in the desktop file.
if icon is None:
return _get_icon_from_desktop_file(workdir, desktop_file_paths)
if os.path.exists(os.path.join(workdir, icon.lstrip("/"))):
return icon
return _get_icon_from_desktop_file(workdir, desktop_file_paths)
def _get_icon_from_desktop_file(
workdir: str, desktop_file_paths: List[str]
) -> Optional[str]:
# Icons in the desktop file can be either a full path to the icon file, or a name
# to be searched in the standard locations. If the path is specified, use that,
# otherwise look for the icon in the hicolor theme (also covers icon type="stock").
# See https://standards.freedesktop.org/icon-theme-spec/icon-theme-spec-latest.html
# for further information.
for path in desktop_file_paths:
entry = DesktopEntry()
entry.parse(os.path.join(workdir, path))
icon = cast(str, entry.getIcon())
icon_path = (
icon
if os.path.isabs(icon)
else _get_icon_from_theme(workdir, "hicolor", icon)
)
return icon_path
return None
def _get_icon_from_theme(workdir: str, theme: str, icon: str) -> Optional[str]:
# Icon themes can carry icons in different pre-rendered sizes or scalable. Scalable
# implementation is optional, so we'll try the largest pixmap and then scalable if
# no other sizes are available.
theme_dir = os.path.join("usr", "share", "icons", theme)
if not os.path.exists(os.path.join(workdir, theme_dir)):
return None
# TODO: use index.theme
entries = os.listdir(os.path.join(workdir, theme_dir))
# size is NxN
x_entries = (e.split("x") for e in entries if "x" in e)
sized_entries = (e[0] for e in x_entries if e[0] == e[1])
sizes = {}
for icon_size_entry in sized_entries:
with contextlib.suppress(ValueError):
isize = int(icon_size_entry)
sizes[isize] = f"{isize}x{isize}"
icon_size = None
suffixes = []
if sizes:
size = max(sizes.items(), key=operator.itemgetter(1))[0]
icon_size = sizes[size]
suffixes = [".png", ".xpm"]
elif "scalable" in entries:
icon_size = "scalable"
suffixes = [".svg", ".svgz"]
icon_path = None
if icon_size:
for suffix in suffixes:
icon_path = os.path.join(theme_dir, icon_size, "apps", icon + suffix)
if os.path.exists(os.path.join(workdir, icon_path)):
break
return icon_path