forked from hughsie/fedora-appstream
/
build.py
executable file
·337 lines (286 loc) · 12.4 KB
/
build.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Licensed under the GNU General Public License Version 2
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# 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, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
# Copyright (C) 2009-2013
# Richard Hughes <richard@hughsie.com>
#
# Copyright (C) 2013
# Florian Festi <ffesti@redhat.com>
#
import glob
import os
import shutil
import subprocess
import sys
import tarfile
import fnmatch
from gi.repository import Gio
# internal
from package import Package
from appdata import AppData
from config import Config
from desktop_file import DesktopFile
from font_file import FontFile
from input_method import InputMethodTable, InputMethodComponent
def package_decompress(pkg):
if os.path.exists('./extract-package'):
p = subprocess.Popen(['./extract-package', pkg.filename, 'tmp'],
cwd='.', stdout=subprocess.PIPE)
p.wait()
if p.returncode:
raise StandardError('Cannot extract package: ' + p.stdout)
else:
wildcards = []
if not os.getenv('APPSTREAM_DEBUG'):
wildcards.append('./usr/share/applications/*.desktop')
wildcards.append('./usr/share/applications/kde4/*.desktop')
wildcards.append('./usr/share/appdata/*.xml')
wildcards.append('./usr/share/icons/hicolor/*/apps/*')
wildcards.append('./usr/share/pixmaps/*.*')
wildcards.append('./usr/share/icons/*.*')
wildcards.append('./usr/share/*/images/*')
pkg.extract('./tmp', wildcards)
else:
wildcards.append('./*/*.*')
pkg.extract('./tmp', wildcards)
class Build:
def __init__(self):
self.cfg = Config()
def build(self, filename):
# check the package has .desktop files
print 'SOURCE\t', filename
pkg = Package(filename)
for b in self.cfg.get_package_blacklist():
if fnmatch.fnmatch(pkg.name, b):
print 'IGNORE\t', filename, '\t', "package is blacklisted:", pkg.name
return
# set up state
if not os.path.exists('./appstream'):
os.makedirs('./appstream')
if not os.path.exists('./icons'):
os.makedirs('./icons')
if not os.path.exists('./screenshot-cache'):
os.makedirs('./screenshot-cache')
if not os.path.exists('./screenshots'):
os.makedirs('./screenshots')
if not os.path.exists('./screenshots/source'):
os.makedirs('./screenshots/source')
for size in self.cfg.get_screenshot_thumbnail_sizes():
path = './screenshots/' + str(size[0]) + 'x' + str(size[1])
if not os.path.exists(path):
os.makedirs(path)
# remove tmp
if os.path.exists('./tmp'):
shutil.rmtree('./tmp')
os.makedirs('./tmp')
# decompress main file and search for desktop files
package_decompress(pkg)
files = []
for f in self.cfg.get_interesting_installed_files():
files.extend(glob.glob("./tmp" + f))
files.sort()
# we only need to install additional files if we're not running on
# the builders
for c in self.cfg.get_package_data_list():
if fnmatch.fnmatch(pkg.name, c[0]):
extra_files = glob.glob("./packages/%s*.rpm" % c[1])
for f in extra_files:
extra_pkg = Package(f)
print "INFO\tAdding extra package %s for %s" % (extra_pkg.name, pkg.name)
package_decompress(extra_pkg)
# open the AppStream file for writing
xml_output_file = './appstream/' + pkg.name + '.xml'
xml = open(xml_output_file, 'w')
xml.write("<?xml version=\"1.0\"?>\n")
xml.write("<applications version=\"0.1\">\n")
# check for duplicate apps in the package
application_ids = []
has_valid_content = False
# process each desktop file in the original package
for f in files:
print 'PROCESS\t', f
fi = Gio.file_new_for_path(f)
info = fi.query_info('standard::content-type', 0, None)
# create the right object depending on the content type
content_type = info.get_content_type()
if content_type == 'inode/symlink':
continue
if content_type == 'application/x-font-ttf':
app = FontFile(pkg, self.cfg)
elif content_type == 'application/x-font-otf':
app = FontFile(pkg, self.cfg)
elif content_type == 'application/x-desktop':
app = DesktopFile(pkg, self.cfg)
elif content_type == 'application/xml':
app = InputMethodComponent(pkg, self.cfg)
elif content_type == 'application/x-sqlite3':
app = InputMethodTable(pkg, self.cfg)
else:
print 'IGNORE\t', f, '\t', "content type " + content_type + " not supported"
continue
# the ID is the filename
app.set_id(f.split('/')[-1])
# application is blacklisted
blacklisted = False
for b in self.cfg.get_id_blacklist():
if fnmatch.fnmatch(app.app_id, b):
print 'IGNORE\t', f, '\t', "application is blacklisted:", app.app_id
blacklisted = True
break
if blacklisted:
continue
# packages that ship .desktop files in /usr/share/applications
# *and* /usr/share/applications/kde4 do not need multiple entries
if app.app_id in application_ids:
print 'IGNORE\t', f, '\t', app.app_id, 'duplicate ID in package'
continue
application_ids.append(app.app_id)
# parse desktop file
if not app.parse_file(f):
continue
# do we have an AppData file?
appdata_file = './tmp/usr/share/appdata/' + app.app_id + '.appdata.xml'
appdata_extra_file = './appdata-extra/' + app.type_id + '/' + app.app_id + '.appdata.xml'
if os.path.exists(appdata_file) and os.path.exists(appdata_extra_file):
print 'DELETE\t', appdata_extra_file, 'as upstream AppData file exists'
os.remove(appdata_extra_file)
# just use the extra file in places of the missing upstream one
if os.path.exists(appdata_extra_file):
appdata_file = appdata_extra_file
# need to extract details
if os.path.exists(appdata_file):
data = AppData()
data.extract(appdata_file)
# check AppData file validates
if os.path.exists('/usr/bin/appdata-validate'):
env = os.environ
p = subprocess.Popen(['/usr/bin/appdata-validate',
'--relax', appdata_file],
cwd='.', env=env, stdout=subprocess.PIPE)
p.wait()
if p.returncode:
for line in p.stdout:
line = line.replace('\n', '')
print 'WARNING\tAppData did not validate: ' + line
# check the id matches
if data.get_id() != app.app_id and data.get_id() != app.app_id_full:
raise StandardError('The AppData id does not match: ' + app.app_id)
# check the licence is okay
if data.get_licence() not in self.cfg.get_content_licences():
raise StandardError('The AppData licence is not okay for ' +
app.app_id + ': \'' +
data.get_licence() + '\'')
# if we have an override, use it for all languages
tmp = data.get_names()
if tmp:
app.names = tmp
# if we have an override, use it for all languages
tmp = data.get_summaries()
if tmp:
app.comments = tmp
# get optional bits
tmp = data.get_url()
if tmp:
app.homepage_url = tmp
tmp = data.get_project_group()
if tmp:
app.project_group = tmp
app.descriptions = data.get_descriptions()
# get screenshots
tmp = data.get_screenshots()
for image in tmp:
print 'DOWNLOADING\t', image
app.add_screenshot_url(image)
elif app.requires_appdata:
print 'IGNORE\t', f, '\t', app.app_id_full, 'requires AppData to be included'
continue
# use the homepage to filter out same more generic apps
if not app.project_group:
# GNOME
project_urls = [ 'http*://*.gnome.org*',
'http://gnome-*.sourceforge.net/']
for m in project_urls:
if fnmatch.fnmatch(app.homepage_url, m):
app.project_group = "GNOME"
# KDE
project_urls = [ 'http*://*.kde.org*',
'http://*kde-apps.org/*' ]
for m in project_urls:
if fnmatch.fnmatch(app.homepage_url, m):
app.project_group = "KDE"
# XFCE
project_urls = [ 'http://*xfce.org*' ]
for m in project_urls:
if fnmatch.fnmatch(app.homepage_url, m):
app.project_group = "XFCE"
# LXDE
project_urls = [ 'http://lxde.org*',
'http://lxde.sourceforge.net/*' ]
for m in project_urls:
if fnmatch.fnmatch(app.homepage_url, m):
app.project_group = "LXDE"
# MATE
project_urls = [ 'http://*mate-desktop.org*' ]
for m in project_urls:
if fnmatch.fnmatch(app.homepage_url, m):
app.project_group = "MATE"
# print that we auto-added it
if app.project_group:
print 'INFO\t', f, '\t', app.app_id, 'assigned', app.project_group
# we got something useful
if not has_valid_content:
has_valid_content = True
# Do not include apps without a name
if not 'C' in app.names:
print 'IGNORE\t', f, '\t', "no Name"
continue
# Do not include apps without a summary
if not 'C' in app.comments:
print 'IGNORE\t', f, '\t', "no Comment"
continue
# Do not include apps without an icon
if not app.icon:
print 'IGNORE\t', f, '\t', "Icon unspecified"
continue
# write content
app.write(xml)
# create AppStream XML
xml.write("</applications>\n")
xml.close()
if not has_valid_content:
os.remove(xml_output_file)
# create AppStream icon tar
if has_valid_content:
output_file = "./appstream/%s-icons.tar" % pkg.name
print 'WRITING\t', output_file
tar = tarfile.open(output_file, "w")
files = glob.glob("./icons/*.png")
for f in files:
tar.add(f, arcname=f.split('/')[-1])
tar.close()
# remove tmp
if not os.getenv('APPSTREAM_DEBUG'):
shutil.rmtree('./tmp')
shutil.rmtree('./icons')
def main():
job = Build()
for fn in sys.argv[1:]:
job.build(fn)
sys.exit(0)
if __name__ == "__main__":
main()