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