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