chiark / gitweb /
3f95117a4a022d1fcd877c6018799581eef0015a
[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     '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         (re.compile(r'.*code\.google\.com/p/[^/]+/[^w]'),
102          "Possible incorrect path appended to google code project site"),
103         ],
104     'Source Code': [
105         (re.compile(r'.*github\.com/[^/]+/[^/]+\.git'),
106          "Appending .git is not necessary"),
107         (re.compile(r'.*code\.google\.com/p/[^/]+/source/.*'),
108          "/source is often enough on its own"),
109         ],
110     'Repo': [
111         (re.compile(r'^http://.*'),
112          "use https:// if available"),
113         (re.compile(r'^svn://.*'),
114          "use https:// if available"),
115         ],
116     'Issue Tracker': [
117         (re.compile(r'.*code\.google\.com/p/[^/]+/issues/.*'),
118          "/issues is often enough on its own"),
119         (re.compile(r'.*github\.com/[^/]+/[^/]+/issues/.*'),
120          "/issues is often enough on its own"),
121         ],
122     'Summary': [
123         (re.compile(r'.*\b(free software|open source)\b.*', re.IGNORECASE),
124          "No need to specify that the app is Free Software"),
125         (re.compile(r'.*[a-z0-9][.,!?][ $]'),
126          "Punctuation should be avoided"),
127         ],
128     }
129
130
131 def main():
132
133     global config, options, curid, count
134     curid = None
135
136     count = Counter()
137
138     def warn(message):
139         global curid, count
140         if curid:
141             print "%s:" % curid
142             curid = None
143             count['app'] += 1
144         print '    %s' % message
145         count['warn'] += 1
146
147     def pwarn(message):
148         if options.pedantic:
149             warn(message)
150
151     # Parse command line...
152     parser = OptionParser(usage="Usage: %prog [options] [APPID [APPID ...]]")
153     parser.add_option("-v", "--verbose", action="store_true", default=False,
154                       help="Spew out even more information than normal")
155     parser.add_option("-q", "--quiet", action="store_true", default=False,
156                       help="Restrict output to warnings and errors")
157     parser.add_option("-p", "--pedantic", action="store_true", default=False,
158                       help="Show pedantic warnings that might give false positives")
159     (options, args) = parser.parse_args()
160
161     config = common.read_config(options)
162
163     # Get all apps...
164     allapps = metadata.read_metadata(xref=False)
165     apps = common.read_app_args(args, allapps, False)
166
167     for appid, app in apps.iteritems():
168         curid = appid
169         lastcommit = ''
170
171         if app['Disabled']:
172             continue
173
174         for build in app['builds']:
175             if build['commit'] and not build['disable']:
176                 lastcommit = build['commit']
177
178         # Potentially incorrect UCM
179         if (app['Update Check Mode'] == 'RepoManifest' and
180                 any(s in lastcommit for s in '.,_-/')):
181             pwarn("Last used commit '%s' looks like a tag, but Update Check Mode is '%s'" % (
182                 lastcommit, app['Update Check Mode']))
183
184         # Summary size limit
185         summ_chars = len(app['Summary'])
186         if summ_chars > config['char_limits']['Summary']:
187             warn("Summary of length %s is over the %i char limit" % (
188                 summ_chars, config['char_limits']['Summary']))
189
190         # Redundant info
191         if app['Web Site'] and app['Source Code']:
192             if app['Web Site'].lower() == app['Source Code'].lower():
193                 warn("Website '%s' is just the app's source code link" % app['Web Site'])
194                 app['Web Site'] = ''
195
196         name = app['Name'] or app['Auto Name']
197         if app['Summary'] and name:
198             if app['Summary'].lower() == name.lower():
199                 warn("Summary '%s' is just the app's name" % app['Summary'])
200
201         if app['Summary'] and app['Description'] and len(app['Description']) == 1:
202             if app['Summary'].lower() == app['Description'][0].lower():
203                 warn("Description '%s' is just the app's summary" % app['Summary'])
204
205         # Description size limit
206         desc_chars = sum(len(l) for l in app['Description'])
207         if desc_chars > config['char_limits']['Description']:
208             warn("Description of length %s is over the %i char limit" % (
209                 desc_chars, config['char_limits']['Description']))
210
211         # Regex checks in all kinds of fields
212         for f in regex_warnings:
213             for m, r in regex_warnings[f]:
214                 t = metadata.metafieldtype(f)
215                 if t == 'string':
216                     if m.match(app[f]):
217                         warn("%s '%s': %s" % (f, app[f], r))
218                 elif t == 'multiline':
219                     for l in app[f]:
220                         if m.match(l):
221                             warn("%s at line '%s': %s" % (f, l, r))
222
223         # Regex pedantic checks in all kinds of fields
224         if options.pedantic:
225             for f in regex_pedantic:
226                 for m, r in regex_pedantic[f]:
227                     if m.match(app[f]):
228                         warn("%s '%s': %s" % (f, app[f], r))
229
230         # Build warnings
231         for build in app['builds']:
232             if build['disable']:
233                 continue
234             for s in ['master', 'origin', 'HEAD', 'default', 'trunk']:
235                 if build['commit'] and build['commit'].startswith(s):
236                     warn("Branch '%s' used as commit in build '%s'" % (
237                         s, build['version']))
238                 for srclib in build['srclibs']:
239                     ref = srclib.split('@')[1].split('/')[0]
240                     if ref.startswith(s):
241                         warn("Branch '%s' used as commit in srclib '%s'" % (
242                             s, srclib))
243             for s in ['git clone', 'git svn clone', 'svn checkout', 'svn co', 'hg clone']:
244                 for flag in ['init', 'prebuild', 'build']:
245                     if not build[flag]:
246                         continue
247                     if s in build[flag]:
248                         # TODO: This should not be pedantic!
249                         pwarn("'%s' used in %s '%s'" % (s, flag, build[flag]))
250
251         if not curid:
252             print
253
254     logging.info("Found a total of %i warnings in %i apps." % (count['warn'], count['app']))
255
256 if __name__ == "__main__":
257     main()