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