chiark / gitweb /
123a6441edba45f0b8a45e01531aa987b638b425
[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     'License': [
77         (re.compile(r'^(|None|Unknown)$'),
78          "No license specified"),
79         ],
80     'Description': [
81         (re.compile(r'^No description available$'),
82          "Description yet to be filled"),
83         (re.compile(r'[ ]*[*#][^ .]'),
84          "Invalid bulleted list"),
85         (re.compile(r'^ '),
86          "Unnecessary leading space"),
87         ],
88 }
89
90 regex_pedantic = {
91     'Web Site': [
92         (re.compile(r'.*github\.com/[^/]+/[^/]+\.git'),
93          "Appending .git is not necessary"),
94         (re.compile(r'.*code\.google\.com/p/[^/]+/[^w]'),
95          "Possible incorrect path appended to google code project site"),
96         ],
97     'Source Code': [
98         (re.compile(r'.*github\.com/[^/]+/[^/]+\.git'),
99          "Appending .git is not necessary"),
100         (re.compile(r'.*code\.google\.com/p/[^/]+/source/.*'),
101          "/source is often enough on its own"),
102         ],
103     'Repo': [
104         (re.compile(r'^http://.*'),
105          "use https:// if available"),
106         (re.compile(r'^svn://.*'),
107          "use https:// if available"),
108         ],
109     'Issue Tracker': [
110         (re.compile(r'.*code\.google\.com/p/[^/]+/issues/.*'),
111          "/issues is often enough on its own"),
112         (re.compile(r'.*github\.com/[^/]+/[^/]+/issues/.*'),
113          "/issues is often enough on its own"),
114         ],
115     'Summary': [
116         (re.compile(r'^[a-z]'),
117          "No capitalization was done"),
118         (re.compile(r'.*\bandroid\b.*', re.IGNORECASE),
119          "No need to specify that the app is for Android"),
120         (re.compile(r'.*\b(app|application)\b.*', re.IGNORECASE),
121          "No need to specify that the app is... an app"),
122         (re.compile(r'.*\b(free software|open source)\b.*', re.IGNORECASE),
123          "No need to specify that the app is Free Software"),
124         (re.compile(r'.*[a-z0-9][.,!?][ $]'),
125          "Punctuation should be avoided"),
126         ],
127     }
128
129
130 def main():
131
132     global config, options, appid, app_count, warn_count
133     appid = None
134
135     app_count = 0
136     warn_count = 0
137
138     def warn(message):
139         global appid, app_count, warn_count
140         if appid:
141             print "%s:" % appid
142             appid = None
143             app_count += 1
144         print '    %s' % message
145         warn_count += 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 app in apps:
168         appid = app['id']
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 summaries
191         summary = app['Summary']
192         name = app['Name'] or app['Auto Name']
193         if summary and name:
194             summary_l = summary.lower()
195             name_l = name.lower()
196             if summary_l == name_l:
197                 warn("Summary '%s' is just the app's name" % summary)
198             elif (summary_l in name_l or name_l in summary_l):
199                 pwarn("Summary '%s' probably contains redundant info already in app name '%s'" % (
200                     summary, name))
201
202         # Description size limit
203         desc_chars = sum(len(l) for l in app['Description'])
204         if desc_chars > config['char_limits']['Description']:
205             warn("Description of length %s is over the %i char limit" % (
206                 desc_chars, config['char_limits']['Description']))
207
208         # Regex checks in all kinds of fields
209         for f in regex_warnings:
210             for m, r in regex_warnings[f]:
211                 t = metadata.metafieldtype(f)
212                 if t == 'string':
213                     if m.match(app[f]):
214                         warn("%s '%s': %s" % (f, app[f], r))
215                 elif t == 'multiline':
216                     for l in app[f]:
217                         if m.match(l):
218                             warn("%s at line '%s': %s" % (f, l, r))
219
220         # Regex pedantic checks in all kinds of fields
221         if options.pedantic:
222             for f in regex_pedantic:
223                 for m, r in regex_pedantic[f]:
224                     if m.match(app[f]):
225                         warn("%s '%s': %s" % (f, app[f], r))
226
227         # Build warnings
228         for build in app['builds']:
229             for s in ['master', 'origin/', 'default', 'trunk']:
230                 if build['commit'] and build['commit'].startswith(s):
231                     warn("Branch '%s' used as commit in build '%s'" % (
232                         s, build['version']))
233                 for srclib in build['srclibs']:
234                     ref = srclib.split('@')[1].split('/')[0]
235                     if ref.startswith(s):
236                         warn("Branch '%s' used as commit in srclib '%s'" % (
237                             s, srclib))
238             for s in ['git clone', 'svn checkout', 'svn co', 'hg clone']:
239                 for flag in ['init', 'prebuild', 'build']:
240                     if not build[flag]:
241                         continue
242                     if s in build[flag]:
243                         # TODO: This should not be pedantic!
244                         pwarn("'%s' used in %s '%s'" % (s, flag, build[flag]))
245
246         if not appid:
247             print
248
249     logging.info("Found a total of %i warnings in %i apps." % (warn_count, app_count))
250
251 if __name__ == "__main__":
252     main()