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