forked from hughsie/fedora-appstream
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-status.py
executable file
·238 lines (220 loc) · 8.78 KB
/
create-status.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
#!/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) 2013
# Richard Hughes <richard@hughsie.com>
#
import xml.etree.ElementTree as ET
import gzip
import sys
import os
# internal
from application import Application
from logger import LoggerItem
from config import Config
from screenshot import Screenshot
XML_LANG = '{http://www.w3.org/XML/1998/namespace}lang'
def _to_utf8(txt, errors='replace'):
if isinstance(txt, str):
return txt
if isinstance(txt, unicode):
return txt.encode('utf-8', errors=errors)
return str(txt)
def _to_html(app):
doc = "<a name=\"%s\"/><h2>%s</h2>\n" % (app.app_id, app.app_id)
mirror_url = app.cfg.get_screenshot_mirror_url()
if mirror_url and len(app.screenshots) > 0:
for s in app.screenshots:
url = mirror_url + u'624x351/' + s.basename
thumb_url = mirror_url + u'112x63/' + s.basename
if s.caption:
doc += u"<a href=\"%s\"><img src=\"%s\" alt=\"%s\"/></a>\n" % (url, thumb_url, s.caption)
else:
doc += u"<a href=\"%s\"><img src=\"%s\"/></a>\n" % (url, thumb_url)
doc += u"<table>\n"
doc += u"<tr><td>%s</td><td><code>%s</code></td></tr>\n" % ("Type", app.type_id)
doc += u"<tr><td>%s</td><td>%s</td></tr>\n" % ("Name", app.names['C'])
doc += u"<tr><td>%s</td><td>%s</td></tr>\n" % ("Comment", app.comments['C'])
if app.descriptions and 'C' in app.descriptions:
doc += u"<tr><td>%s</td><td>%s</td></tr>\n" % ("Description", app.descriptions['C'])
if len(app.pkgnames) > 0:
for pkgname in app.pkgnames:
url = 'https://apps.fedoraproject.org/packages/' + pkgname
doc += u"<tr><td>%s</td><td><a href=\"%s\"><code>%s</code></a></td></tr>\n" % ("Package", url, pkgname)
if app.categories:
doc += u"<tr><td>%s</td><td>%s</td></tr>\n" % ("Categories", ', '.join(app.categories))
if len(app.keywords):
doc += u"<tr><td>%s</td><td>%s</td></tr>\n" % ("Keywords", ', '.join(app.keywords))
if 'homepage' in app.urls:
doc += u"<tr><td>%s</td><td><a href=\"%s\">%s</a></td></tr>\n" % ("Homepage", app.urls['homepage'], app.urls['homepage'])
if app.project_group:
doc += u"<tr><td>%s</td><td>%s</td></tr>\n" % ("Project", app.project_group)
if len(app.compulsory_for_desktop):
doc += u"<tr><td>%s</td><td>%s</td></tr>\n" % ("Compulsory", ', '.join(app.compulsory_for_desktop))
doc += u"</table>\n"
doc += u"<hr/>\n"
return doc
def ensure_unicode(text):
if isinstance(text, unicode):
return text
return text.decode('utf-8')
def main():
log = LoggerItem()
cfg = Config()
# read in AppStream file into several Application objects
f = gzip.open(sys.argv[1], 'rb')
tree = ET.parse(f)
apps = []
for app in tree.getroot():
a = Application(None, cfg)
for elem in app:
if elem.tag == 'id':
a.set_id(elem.text)
a.type_id = elem.get('type')
log.update_key(a.app_id_full)
log.write(LoggerItem.INFO, "parsing")
elif elem.tag == 'name':
if elem.get(XML_LANG):
continue
a.names['C'] = ensure_unicode(elem.text)
elif elem.tag == 'summary':
if elem.get(XML_LANG):
continue
a.comments['C'] = ensure_unicode(elem.text)
elif elem.tag == 'pkgname':
a.pkgnames.append(ensure_unicode(elem.text))
elif elem.tag == 'appcategories':
for elem2 in elem:
a.categories.append(ensure_unicode(elem2.text))
elif elem.tag == 'keywords':
for elem2 in elem:
a.keywords.append(ensure_unicode(elem2.text))
elif elem.tag == 'url':
a.urls[elem.get('type')] = ensure_unicode(elem.text)
elif elem.tag == 'compulsory_for_desktop':
a.compulsory_for_desktop.append(ensure_unicode(elem.text))
elif elem.tag == 'project_group':
a.project_group = ensure_unicode(elem.text)
elif elem.tag == 'description':
description = ''
if len(elem._children):
for elem2 in elem:
description += elem2.text + u' '
else:
description = elem.text
a.descriptions['C'] = ensure_unicode(description)
elif elem.tag == 'screenshots':
if a.type_id == 'font':
continue
for elem2 in elem:
if elem2.tag != 'screenshot':
continue
caption = None
for elem3 in elem2:
if elem3.tag == 'caption':
caption = elem3.text
elif elem3.tag == 'image':
if elem3.get('type') != 'source':
continue
s = Screenshot(a.app_id, None, caption)
s.basename = os.path.basename(elem3.text)
a.screenshots.append(s)
apps.append(a)
f.close()
# build status page
status = open('./screenshots/status.html', 'w')
status.write('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 ' +
'Transitional//EN" ' +
'"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\n')
status.write('<html xmlns="http://www.w3.org/1999/xhtml">\n')
status.write('<head>\n')
status.write('<meta http-equiv="Content-Type" content="text/html; ' +
'charset=UTF-8" />\n')
status.write('<title>Application Data Review</title>\n')
status.write('</head>\n')
status.write('<body>\n')
status.write('<h1>Executive summary</h1>\n')
status.write('<ul>\n')
# long descriptions
cnt = 0
total = len(apps)
for app in apps:
if len(app.descriptions) > 0:
cnt += 1
tmp = 100 * cnt / total
status.write("<li>Applications in Fedora with long descriptions: %i (%i%%)</li>" % (cnt, tmp))
# keywords
cnt = 0
total = len(apps)
for app in apps:
if len(app.keywords) > 0:
cnt += 1
tmp = 100 * cnt / total
status.write("<li>Applications in Fedora with keywords: %i (%i%%)</li>" % (cnt, tmp))
# categories
cnt = 0
total = len(apps)
for app in apps:
if len(app.categories) > 0:
cnt += 1
tmp = 100 * cnt / total
status.write("<li>Applications in Fedora with categories: %i (%i%%)</li>" % (cnt, tmp))
# screenshots
cnt = 0
total = len(apps)
for app in apps:
if len(app.screenshots) > 0:
cnt += 1
tmp = 100 * cnt / total
status.write("<li>Applications in Fedora with screenshots: %i (%i%%)</li>" % (cnt, tmp))
# project apps with appdata
for project_group in ['GNOME', 'KDE', 'XFCE']:
cnt = 0
total = 0
for app in apps:
if app.project_group != project_group:
continue
total += 1
if len(app.screenshots) > 0 or len(app.descriptions) > 0:
cnt += 1
tmp = 0
if total > 0:
tmp = 100 * cnt / total
status.write("<li>Applications in %s with AppData: %i (%i%%)</li>" % (project_group, cnt, tmp))
status.write('</ul>\n')
# write applications
status.write('<h1>Applications</h1>\n')
for app in apps:
if app.type_id == 'font':
continue
if app.type_id == 'inputmethod':
continue
if app.type_id == 'codec':
continue
log.update_key(app.app_id_full)
log.write(LoggerItem.INFO, "writing")
try:
status.write(_to_utf8(_to_html(app)))
except AttributeError, e:
log.write(LoggerItem.WARNING, "failed to write %s: %s" % (app, str(e)))
continue
status.write('</body>\n')
status.write('</html>\n')
status.close()
if __name__ == "__main__":
main()