chiark / gitweb /
977efb89aba499599fbc4524517857b36447cfd7
[fdroidserver.git] / fdroidserver / lint.py
1 #!/usr/bin/env python2
2 # -*- coding: utf-8 -*-
3 #
4 # lint.py - part of the FDroid server tool
5 # Copyright (C) 2013-2014 Daniel Martí <mvdan@mvdan.cc>
6 #
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See th
15 # GNU Affero General Public License for more details.
16 #
17 # You should have received a copy of the GNU Affero General Public Licen
18 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
20 from argparse import ArgumentParser
21 import re
22 import common
23 import metadata
24 import sys
25 from sets import Set
26
27 config = None
28 options = None
29
30
31 def enforce_https(domain):
32     return (re.compile(r'.*[^sS]://[^/]*' + re.escape(domain) + r'(/.*)?'),
33             domain + " URLs should always use https://")
34
35 https_enforcings = [
36     enforce_https('github.com'),
37     enforce_https('gitlab.com'),
38     enforce_https('bitbucket.org'),
39     enforce_https('apache.org'),
40     enforce_https('google.com'),
41     enforce_https('svn.code.sf.net'),
42 ]
43
44
45 def forbid_shortener(domain):
46     return (re.compile(r'https?://[^/]*' + re.escape(domain) + r'/.*'),
47             "URL shorteners should not be used")
48
49 http_url_shorteners = [
50     forbid_shortener('goo.gl'),
51     forbid_shortener('t.co'),
52     forbid_shortener('ur1.ca'),
53 ]
54
55 http_checks = https_enforcings + http_url_shorteners + [
56     (re.compile(r'.*github\.com/[^/]+/[^/]+\.git'),
57      "Appending .git is not necessary"),
58     (re.compile(r'.*://[^/]*(github|gitlab|bitbucket|rawgit)[^/]*/([^/]+/){1,3}master'),
59      "Use /HEAD instead of /master to point at a file in the default branch"),
60 ]
61
62 regex_checks = {
63     'Web Site': http_checks,
64     'Source Code': http_checks,
65     'Repo': https_enforcings,
66     'Issue Tracker': http_checks + [
67         (re.compile(r'.*github\.com/[^/]+/[^/]+[/]*$'),
68          "/issues is missing"),
69     ],
70     'Donate': http_checks + [
71         (re.compile(r'.*flattr\.com'),
72          "Flattr donation methods belong in the FlattrID flag"),
73     ],
74     'Changelog': http_checks,
75     'License': [
76         (re.compile(r'^(|None|Unknown)$'),
77          "No license specified"),
78     ],
79     'Summary': [
80         (re.compile(r'^$'),
81          "Summary yet to be filled"),
82         (re.compile(r'.*\b(free software|open source)\b.*', re.IGNORECASE),
83          "No need to specify that the app is Free Software"),
84         (re.compile(r'.*((your|for).*android|android.*(app|device|client|port|version))', re.IGNORECASE),
85          "No need to specify that the app is for Android"),
86         (re.compile(r'.*[a-z0-9][.!?]( |$)'),
87          "Punctuation should be avoided"),
88     ],
89     'Description': [
90         (re.compile(r'^No description available$'),
91          "Description yet to be filled"),
92         (re.compile(r'\s*[*#][^ .]'),
93          "Invalid bulleted list"),
94         (re.compile(r'^\s'),
95          "Unnecessary leading space"),
96         (re.compile(r'.*\s$'),
97          "Unnecessary trailing space"),
98         (re.compile(r'.*([^[]|^)\[[^:[\]]+( |\]|$)'),
99          "Invalid link - use [http://foo.bar Link title] or [http://foo.bar]"),
100         (re.compile(r'(^|.* )https?://[^ ]+'),
101          "Unlinkified link - use [http://foo.bar Link title] or [http://foo.bar]"),
102     ],
103 }
104
105
106 def check_regexes(app):
107     for f, checks in regex_checks.iteritems():
108         for m, r in checks:
109             v = app.get_field(f)
110             t = metadata.fieldtype(f)
111             if t == metadata.TYPE_MULTILINE:
112                 for l in v.splitlines():
113                     if m.match(l):
114                         yield "%s at line '%s': %s" % (f, l, r)
115             else:
116                 if v is None:
117                     continue
118                 if m.match(v):
119                     yield "%s '%s': %s" % (f, v, r)
120
121
122 def get_lastbuild(builds):
123     lowest_vercode = -1
124     lastbuild = None
125     for build in builds:
126         if not build.disable:
127             vercode = int(build.vercode)
128             if lowest_vercode == -1 or vercode < lowest_vercode:
129                 lowest_vercode = vercode
130         if not lastbuild or int(build.vercode) > int(lastbuild.vercode):
131             lastbuild = build
132     return lastbuild
133
134
135 def check_ucm_tags(app):
136     lastbuild = get_lastbuild(app.builds)
137     if (lastbuild is not None
138             and lastbuild.commit
139             and app.UpdateCheckMode == 'RepoManifest'
140             and not lastbuild.commit.startswith('unknown')
141             and lastbuild.vercode == app.CurrentVersionCode
142             and not lastbuild.forcevercode
143             and any(s in lastbuild.commit for s in '.,_-/')):
144         yield "Last used commit '%s' looks like a tag, but Update Check Mode is '%s'" % (
145             lastbuild.commit, app.UpdateCheckMode)
146
147
148 def check_char_limits(app):
149     limits = config['char_limits']
150
151     summ_chars = len(app.Summary)
152     if summ_chars > limits['Summary']:
153         yield "Summary of length %s is over the %i char limit" % (
154             summ_chars, limits['Summary'])
155
156     if len(app.Description) > limits['Description']:
157         yield "Description of length %s is over the %i char limit" % (
158             desc_charcount, limits['Description'])
159
160
161 def check_old_links(app):
162     usual_sites = [
163         'github.com',
164         'gitlab.com',
165         'bitbucket.org',
166     ]
167     old_sites = [
168         'gitorious.org',
169         'code.google.com',
170     ]
171     if any(s in app.Repo for s in usual_sites):
172         for f in ['Web Site', 'Source Code', 'Issue Tracker', 'Changelog']:
173             v = app.get_field(f)
174             if any(s in v for s in old_sites):
175                 yield "App is in '%s' but has a link to '%s'" % (app.Repo, v)
176
177
178 def check_useless_fields(app):
179     if app.UpdateCheckName == app.id:
180         yield "Update Check Name is set to the known app id - it can be removed"
181
182 filling_ucms = re.compile(r'^(Tags.*|RepoManifest.*)')
183
184
185 def check_checkupdates_ran(app):
186     if filling_ucms.match(app.UpdateCheckMode):
187         if not app.AutoName and not app.CurrentVersion and app.CurrentVersionCode == '0':
188             yield "UCM is set but it looks like checkupdates hasn't been run yet"
189
190
191 def check_empty_fields(app):
192     if not app.Categories:
193         yield "Categories are not set"
194
195 all_categories = Set([
196     "Connectivity",
197     "Development",
198     "Games",
199     "Graphics",
200     "Internet",
201     "Money",
202     "Multimedia",
203     "Navigation",
204     "Phone & SMS",
205     "Reading",
206     "Science & Education",
207     "Security",
208     "Sports & Health",
209     "System",
210     "Theming",
211     "Time",
212     "Writing",
213 ])
214
215
216 def check_categories(app):
217     for categ in app.Categories:
218         if categ not in all_categories:
219             yield "Category '%s' is not valid" % categ
220
221
222 def check_duplicates(app):
223     if app.Name and app.Name == app.AutoName:
224         yield "Name '%s' is just the auto name - remove it" % app.Name
225
226     links_seen = set()
227     for f in ['Source Code', 'Web Site', 'Issue Tracker', 'Changelog']:
228         v = app.get_field(f)
229         if not v:
230             continue
231         v = v.lower()
232         if v in links_seen:
233             yield "Duplicate link in '%s': %s" % (f, v)
234         else:
235             links_seen.add(v)
236
237     name = app.Name or app.AutoName
238     if app.Summary and name:
239         if app.Summary.lower() == name.lower():
240             yield "Summary '%s' is just the app's name" % app.Summary
241
242     if app.Summary and app.Description and len(app.Description) == 1:
243         if app.Summary.lower() == app.Description[0].lower():
244             yield "Description '%s' is just the app's summary" % app.Summary
245
246     seenlines = set()
247     for l in app.Description.splitlines():
248         if len(l) < 1:
249             continue
250         if l in seenlines:
251             yield "Description has a duplicate line"
252         seenlines.add(l)
253
254
255 desc_url = re.compile(r'(^|[^[])\[([^ ]+)( |\]|$)')
256
257
258 def check_mediawiki_links(app):
259     wholedesc = ' '.join(app.Description)
260     for um in desc_url.finditer(wholedesc):
261         url = um.group(1)
262         for m, r in http_checks:
263             if m.match(url):
264                 yield "URL '%s' in Description: %s" % (url, r)
265
266
267 def check_bulleted_lists(app):
268     validchars = ['*', '#']
269     lchar = ''
270     lcount = 0
271     for l in app.Description.splitlines():
272         if len(l) < 1:
273             lcount = 0
274             continue
275
276         if l[0] == lchar and l[1] == ' ':
277             lcount += 1
278             if lcount > 2 and lchar not in validchars:
279                 yield "Description has a list (%s) but it isn't bulleted (*) nor numbered (#)" % lchar
280                 break
281         else:
282             lchar = l[0]
283             lcount = 1
284
285
286 def check_builds(app):
287     for build in app.builds:
288         if build.disable:
289             continue
290         for s in ['master', 'origin', 'HEAD', 'default', 'trunk']:
291             if build.commit and build.commit.startswith(s):
292                 yield "Branch '%s' used as commit in build '%s'" % (s, build.version)
293             for srclib in build.srclibs:
294                 ref = srclib.split('@')[1].split('/')[0]
295                 if ref.startswith(s):
296                     yield "Branch '%s' used as commit in srclib '%s'" % (s, srclib)
297         if build.target and build.method() == 'gradle':
298             yield "target= has no gradle support"
299
300
301 def main():
302
303     global config, options
304
305     anywarns = False
306
307     # Parse command line...
308     parser = ArgumentParser(usage="%(prog)s [options] [APPID [APPID ...]]")
309     common.setup_global_opts(parser)
310     parser.add_argument("appid", nargs='*', help="app-id in the form APPID")
311     options = parser.parse_args()
312
313     config = common.read_config(options)
314
315     # Get all apps...
316     allapps = metadata.read_metadata(xref=True)
317     apps = common.read_app_args(options.appid, allapps, False)
318
319     for appid, app in apps.iteritems():
320         if app.Disabled:
321             continue
322
323         warns = []
324
325         for check_func in [
326                 check_regexes,
327                 check_ucm_tags,
328                 check_char_limits,
329                 check_old_links,
330                 check_checkupdates_ran,
331                 check_useless_fields,
332                 check_empty_fields,
333                 check_categories,
334                 check_duplicates,
335                 check_mediawiki_links,
336                 check_bulleted_lists,
337                 check_builds,
338                 ]:
339             warns += check_func(app)
340
341         if warns:
342             anywarns = True
343             for warn in warns:
344                 print "%s: %s" % (appid, warn)
345
346     if anywarns:
347         sys.exit(1)
348
349
350 if __name__ == "__main__":
351     main()