chiark / gitweb /
fix PEP8 "E401 multiple imports on one line"
[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
26 config = None
27 options = None
28
29 regex_warnings = {
30         'Web Site': [
31             (re.compile(r'.*[^sS]://github\.com/.*'),
32                 "github URLs should always use https:// not http://"),
33             (re.compile(r'.*[^sS]://code\.google\.com/.*'),
34                 "code.google.com URLs should always use https:// not http://"),
35         ],
36         'Source Code': [
37             (re.compile(r'.*[^sS]://github\.com/.*'),
38                 "github URLs should always use https:// (not http://, git://, or git@)"),
39             (re.compile(r'.*code\.google\.com/p/[^/]+[/]*$'),
40                 "/source is missing"),
41             (re.compile(r'.*[^sS]://code\.google\.com/.*'),
42                 "code.google.com URLs should always use https:// not http://"),
43             (re.compile(r'.*[^sS]://dl\.google\.com/.*'),
44                 "dl.google.com URLs should always use https:// not http://"),
45             (re.compile(r'.*[^sS]://gitorious\.org/.*'),
46                 "gitorious URLs should always use https:// (not http://, git://, or git@)"),
47         ],
48         'Repo': [
49             (re.compile(r'.*[^sS]://code\.google\.com/.*'),
50                 "code.google.com URLs should always use https:// not http://"),
51             (re.compile(r'.*[^sS]://dl\.google\.com/.*'),
52                 "dl.google.com URLs should always use https:// not http://"),
53             (re.compile(r'.*[^sS]://github\.com/.*'),
54                 "github URLs should always use https:// (not http://, git://, or git@)"),
55             (re.compile(r'.*[^sS]://gitorious\.org/.*'),
56                 "gitorious URLs should always use https:// (not http://, git://, or git@)"),
57             (re.compile(r'.*[^sS]://[^.]*\.googlecode\.com/svn/?.*'),
58                 "Google Code SVN URLs should always use https:// (not http:// or svn://)"),
59             (re.compile(r'.*[^sS]://svn\.apache\.org/repos/?.*'),
60                 "Apache SVN URLs should always use https:// (not http:// or svn://)"),
61             (re.compile(r'.*[^sS]://svn\.code\.sf\.net/.*'),
62                 "Sourceforge SVN URLs should always use https:// (not http:// or svn://)"),
63         ],
64         'Issue Tracker': [
65             (re.compile(r'.*code\.google\.com/p/[^/]+[/]*$'),
66                 "/issues is missing"),
67             (re.compile(r'.*[^sS]://code\.google\.com/.*'),
68                 "code.google.com URLs should always use https:// not http://"),
69             (re.compile(r'.*github\.com/[^/]+/[^/]+[/]*$'),
70                 "/issues is missing"),
71             (re.compile(r'.*[^sS]://github\.com/.*'),
72                 "github URLs should always use https:// not http://"),
73             (re.compile(r'.*[^sS]://gitorious\.org/.*'),
74                 "gitorious URLs should always use https:// not http://"),
75         ],
76         'Description': [
77             (re.compile(r'[ ]*[*#][^ .]'),
78                 "Invalid bulleted list"),
79             (re.compile(r'^ '),
80                 "Unnecessary leading space"),
81         ],
82
83 }
84
85 regex_pedantic = {
86         'Web Site': [
87             (re.compile(r'.*github\.com/[^/]+/[^/]+\.git'),
88                 "Appending .git is not necessary"),
89             (re.compile(r'.*code\.google\.com/p/[^/]+/[^w]'),
90                 "Possible incorrect path appended to google code project site"),
91         ],
92         'Source Code': [
93             (re.compile(r'.*github\.com/[^/]+/[^/]+\.git'),
94                 "Appending .git is not necessary"),
95             (re.compile(r'.*code\.google\.com/p/[^/]+/source/.*'),
96                 "/source is often enough on its own"),
97         ],
98         'Repo': [
99             (re.compile(r'^http://.*'),
100                 "if https:// is available, use it instead of http://"),
101             (re.compile(r'^svn://.*'),
102                 "if https:// is available, use it instead of svn://"),
103         ],
104         'Issue Tracker': [
105             (re.compile(r'.*code\.google\.com/p/[^/]+/issues/.*'),
106                 "/issues is often enough on its own"),
107             (re.compile(r'.*github\.com/[^/]+/[^/]+/issues/.*'),
108                 "/issues is often enough on its own"),
109         ],
110         'Summary': [
111             (re.compile(r'.*\bandroid\b.*', re.IGNORECASE),
112                 "No need to specify that the app is for Android"),
113             (re.compile(r'.*\b(app|application)\b.*', re.IGNORECASE),
114                 "No need to specify that the app is... an app"),
115             (re.compile(r'.*\b(free software|open source)\b.*', re.IGNORECASE),
116                 "No need to specify that the app is Free Software"),
117             (re.compile(r'.*[a-z0-9][.,!?][ $]'),
118                 "Punctuation should be avoided"),
119         ],
120 }
121
122 def main():
123
124     global config, options, appid, app_count, warn_count
125     appid = None
126
127     app_count = 0
128     warn_count = 0
129
130     def warn(message):
131         global appid, app_count, warn_count
132         if appid:
133             print "%s:" % appid
134             appid = None
135             app_count += 1
136         print '    %s' % message
137         warn_count += 1
138
139     def pwarn(message):
140         if options.pedantic:
141             warn(message)
142
143     # Parse command line...
144     parser = OptionParser(usage="Usage: %prog [options] [APPID [APPID ...]]")
145     parser.add_option("-p", "--pedantic", action="store_true", default=False,
146                       help="Show pedantic warnings that might give false positives")
147     parser.add_option("-v", "--verbose", action="store_true", default=False,
148                       help="Spew out even more information than normal")
149     (options, args) = parser.parse_args()
150
151     config = common.read_config(options)
152
153     # Get all apps...
154     allapps = metadata.read_metadata(xref=False)
155     apps = common.read_app_args(args, allapps, False)
156
157     for app in apps:
158         appid = app['id']
159         lastcommit = ''
160
161         if app['Disabled']:
162             continue
163
164         for build in app['builds']:
165             if 'commit' in build and 'disable' not in build:
166                 lastcommit = build['commit']
167
168         # Potentially incorrect UCM
169         if (app['Update Check Mode'] == 'RepoManifest' and
170                 any(s in lastcommit for s in '.,_-/')):
171             pwarn("Last used commit '%s' looks like a tag, but Update Check Mode is '%s'" % (
172                 lastcommit, app['Update Check Mode']))
173
174         # No proper license
175         if app['License'] in ('Unknown', 'None', ''):
176             warn("License was not set")
177
178         # Summary size limit
179         summ_chars = len(app['Summary'])
180         if summ_chars > config['char_limits']['Summary']:
181             warn("Summary of length %s is over the %i char limit" % (
182                 summ_chars, config['char_limits']['Summary']))
183
184         # Redundant summaries
185         summary = app['Summary']
186         name = str(app['Name'] if app['Name'] else app['Auto Name'])
187         if summary and name:
188             summary_l = summary.lower()
189             name_l = name.lower()
190             if summary_l == name_l:
191                 warn("Summary '%s' is just the app's name" % summary)
192             elif (summary_l in name_l or name_l in summary_l):
193                 pwarn("Summary '%s' probably contains redundant info already in app name '%s'" % (
194                     summary, name))
195
196
197         # Description size limit
198         desc_chars = sum(len(l) for l in app['Description'])
199         if desc_chars > config['char_limits']['Description']:
200             warn("Description of length %s is over the %i char limit" % (
201                 desc_chars, config['char_limits']['Description']))
202
203         # Regex checks in all kinds of fields
204         for f in regex_warnings:
205             for m, r in regex_warnings[f]:
206                 t = metadata.metafieldtype(f)
207                 if t == 'string':
208                     if m.match(app[f]):
209                         warn("%s '%s': %s" % (f, app[f], r))
210                 elif t == 'multiline':
211                     for l in app[f]:
212                         if m.match(l):
213                             warn("%s at line '%s': %s" % (f, l, r))
214
215
216         # Regex pedantic checks in all kinds of fields
217         if options.pedantic:
218             for f in regex_pedantic:
219                 for m, r in regex_pedantic[f]:
220                     if m.match(app[f]):
221                         warn("%s '%s': %s" % (f, app[f], r))
222
223         # Build warnings
224         for build in app['builds']:
225             for n in ['master', 'origin/', 'default', 'trunk']:
226                 if 'commit' in build:
227                     if build['commit'].startswith(n):
228                         warn("Branch '%s' used as commit in build '%s'" % (
229                             n, build['version']))
230                 if 'srclibs' in build:
231                     for srclib in build['srclibs']:
232                         ref = srclib.split('@')[1].split('/')[0]
233                         if ref.startswith(n):
234                             warn("Branch '%s' used as commit in srclib '%s'" % (
235                                 n, srclib))
236
237         if not appid:
238             print
239
240     logging.info("Found a total of %i warnings in %i apps." % (warn_count, app_count))
241
242 if __name__ == "__main__":
243     main()
244