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