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