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