chiark / gitweb /
Re-add https enforcings to Repo:
[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 optparse import OptionParser
21 import re
22 import logging
23 import common
24 import metadata
25 from collections import Counter
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('gitorious.org'),
38     enforce_https('apache.org'),
39     enforce_https('google.com'),
40     enforce_https('svn.code.sf.net'),
41     enforce_https('googlecode.com'),
42 ]
43
44 http_warnings = https_enforcings + [
45     (re.compile(r'.*/$'),
46      "HTTP links shouldn't end with a slash"),
47     # TODO enable in August 2015, when Google Code goes read-only
48     # (re.compile(r'.*://code\.google\.com/.*'),
49     #  "code.google.com will be soon switching down, perhaps the project moved to github.com?"),
50 ]
51
52 regex_warnings = {
53     'Web Site': http_warnings + [
54     ],
55     'Source Code': http_warnings + [
56     ],
57     'Repo': https_enforcings + [
58     ],
59     'Issue Tracker': http_warnings + [
60         (re.compile(r'.*github\.com/[^/]+/[^/]+[/]*$'),
61          "/issues is missing"),
62         # TODO enable in August 2015, when Google Code goes read-only
63         # (re.compile(r'.*://code\.google\.com/.*'),
64         # "code.google.com will be soon switching down, perhaps it moved to github.com?"),
65     ],
66     'Changelog': http_warnings + [
67     ],
68     'License': [
69         (re.compile(r'^(|None|Unknown)$'),
70          "No license specified"),
71     ],
72     'Summary': [
73         (re.compile(r'^$'),
74          "Summary yet to be filled"),
75         (re.compile(r'.*\b(free software|open source)\b.*', re.IGNORECASE),
76          "No need to specify that the app is Free Software"),
77         (re.compile(r'.*((your|for).*android|android.*(app|device|client|port|version))', re.IGNORECASE),
78          "No need to specify that the app is for Android"),
79     ],
80     'Description': [
81         (re.compile(r'^No description available$'),
82          "Description yet to be filled"),
83         (re.compile(r'\s*[*#][^ .]'),
84          "Invalid bulleted list"),
85         (re.compile(r'^\s'),
86          "Unnecessary leading space"),
87         (re.compile(r'.*\s$'),
88          "Unnecessary trailing space"),
89     ],
90 }
91
92 regex_pedantic = {
93     'Web Site': [
94         (re.compile(r'.*github\.com/[^/]+/[^/]+\.git'),
95          "Appending .git is not necessary"),
96     ],
97     'Source Code': [
98         (re.compile(r'.*github\.com/[^/]+/[^/]+\.git'),
99          "Appending .git is not necessary"),
100     ],
101     'Repo': [
102     ],
103     'Issue Tracker': [
104         (re.compile(r'.*github\.com/[^/]+/[^/]+/issues/.*'),
105          "/issues is often enough on its own"),
106     ],
107     'Summary': [
108         (re.compile(r'.*[a-z0-9][.!?][ $]'),
109          "Punctuation should be avoided"),
110     ],
111 }
112
113
114 def main():
115
116     global config, options, curid, count
117     curid = None
118
119     count = Counter()
120
121     def warn(message):
122         global curid, count
123         if curid:
124             print "%s:" % curid
125             curid = None
126             count['app'] += 1
127         print '    %s' % message
128         count['warn'] += 1
129
130     def pwarn(message):
131         if options.pedantic:
132             warn(message)
133
134     # Parse command line...
135     parser = OptionParser(usage="Usage: %prog [options] [APPID [APPID ...]]")
136     parser.add_option("-v", "--verbose", action="store_true", default=False,
137                       help="Spew out even more information than normal")
138     parser.add_option("-q", "--quiet", action="store_true", default=False,
139                       help="Restrict output to warnings and errors")
140     parser.add_option("-p", "--pedantic", action="store_true", default=False,
141                       help="Show pedantic warnings that might give false positives")
142     (options, args) = parser.parse_args()
143
144     config = common.read_config(options)
145
146     # Get all apps...
147     allapps = metadata.read_metadata(xref=False)
148     apps = common.read_app_args(args, allapps, False)
149
150     for appid, app in apps.iteritems():
151         if app['Disabled']:
152             continue
153
154         curid = appid
155         count['app_total'] += 1
156
157         curbuild = None
158         for build in app['builds']:
159             if not curbuild or int(build['vercode']) > int(curbuild['vercode']):
160                 curbuild = build
161
162         # Potentially incorrect UCM
163         if (curbuild and curbuild['commit']
164                 and app['Update Check Mode'] == 'RepoManifest'
165                 and curbuild['commit'] != 'unknown - see disabled'
166                 and any(s in curbuild['commit'] for s in '.,_-/')):
167             pwarn("Last used commit '%s' looks like a tag, but Update Check Mode is '%s'" % (
168                 curbuild['commit'], app['Update Check Mode']))
169
170         # Dangerous auto updates
171         if curbuild and app['Auto Update Mode'] != 'None':
172             for flag in ['target', 'srclibs', 'scanignore']:
173                 if curbuild[flag]:
174                     pwarn("Auto Update Mode is enabled but '%s' is manually set at '%s'" % (flag, curbuild[flag]))
175
176         # Summary size limit
177         summ_chars = len(app['Summary'])
178         if summ_chars > config['char_limits']['Summary']:
179             warn("Summary of length %s is over the %i char limit" % (
180                 summ_chars, config['char_limits']['Summary']))
181
182         # Redundant info
183         if app['Web Site'] and app['Source Code']:
184             if app['Web Site'].lower() == app['Source Code'].lower():
185                 warn("Website '%s' is just the app's source code link" % app['Web Site'])
186
187         # "None" still a category
188         if 'None' in app['Categories']:
189             warn("Category 'None' is is still present")
190         elif not app['Categories']:
191             warn("Categories are not set")
192
193         name = app['Name'] or app['Auto Name']
194         if app['Summary'] and name:
195             if app['Summary'].lower() == name.lower():
196                 warn("Summary '%s' is just the app's name" % app['Summary'])
197
198         desc = app['Description']
199         if app['Summary'] and desc and len(desc) == 1:
200             if app['Summary'].lower() == desc[0].lower():
201                 warn("Description '%s' is just the app's summary" % app['Summary'])
202
203         # Description size limit
204         desc_charcount = sum(len(l) for l in desc)
205         if desc_charcount > config['char_limits']['Description']:
206             warn("Description of length %s is over the %i char limit" % (
207                 desc_charcount, config['char_limits']['Description']))
208
209         if (not desc[0] or not desc[-1]
210                 or any(not desc[l - 1] and not desc[l] for l in range(1, len(desc)))):
211             warn("Description has an extra empty line")
212
213         # Regex checks in all kinds of fields
214         for f in regex_warnings:
215             for m, r in regex_warnings[f]:
216                 t = metadata.metafieldtype(f)
217                 if t == 'string':
218                     if m.match(app[f]):
219                         warn("%s '%s': %s" % (f, app[f], r))
220                 elif t == 'multiline':
221                     for l in app[f]:
222                         if m.match(l):
223                             warn("%s at line '%s': %s" % (f, l, r))
224
225         # Regex pedantic checks in all kinds of fields
226         if options.pedantic:
227             for f in regex_pedantic:
228                 for m, r in regex_pedantic[f]:
229                     if m.match(app[f]):
230                         warn("%s '%s': %s" % (f, app[f], r))
231
232         # Build warnings
233         for build in app['builds']:
234             if build['disable']:
235                 continue
236             for s in ['master', 'origin', 'HEAD', 'default', 'trunk']:
237                 if build['commit'] and build['commit'].startswith(s):
238                     warn("Branch '%s' used as commit in build '%s'" % (
239                         s, build['version']))
240                 for srclib in build['srclibs']:
241                     ref = srclib.split('@')[1].split('/')[0]
242                     if ref.startswith(s):
243                         warn("Branch '%s' used as commit in srclib '%s'" % (
244                             s, srclib))
245             for s in ['git clone', 'git svn clone', 'svn checkout', 'svn co', 'hg clone']:
246                 for flag in ['init', 'prebuild', 'build']:
247                     if not build[flag]:
248                         continue
249                     if s in build[flag]:
250                         # TODO: This should not be pedantic!
251                         pwarn("'%s' used in %s '%s'" % (s, flag, build[flag]))
252
253         if not curid:
254             print
255
256     logging.info("Found a total of %i warnings in %i apps out of %i total." % (
257         count['warn'], count['app'], count['app_total']))
258
259 if __name__ == "__main__":
260     main()