chiark / gitweb /
Merge branch 'master' of https://gitlab.com/eighthave/fdroidserver
[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'.*[a-z0-9][.,!?][ $]'),
110                 "Punctuation should be avoided"),
111         ],
112 }
113
114 def main():
115
116     global config, options, appid, app_count, warn_count
117     appid = None
118
119     app_count = 0
120     warn_count = 0
121
122     def warn(message):
123         global appid, app_count, warn_count
124         if appid:
125             print "%s:" % appid
126             appid = None
127             app_count += 1
128         print '    %s' % message
129         warn_count += 1
130
131     def pwarn(message):
132         if options.pedantic:
133             warn(message)
134
135     # Parse command line...
136     parser = OptionParser(usage="Usage: %prog [options] [APPID [APPID ...]]")
137     parser.add_option("-p", "--pedantic", action="store_true", default=False,
138                       help="Show pedantic warnings that might give false positives")
139     parser.add_option("-v", "--verbose", action="store_true", default=False,
140                       help="Spew out even more information than normal")
141     (options, args) = parser.parse_args()
142
143     config = common.read_config(options)
144
145     # Get all apps...
146     allapps = metadata.read_metadata(xref=False)
147     apps = common.read_app_args(args, allapps, False)
148
149     for app in apps:
150         appid = app['id']
151         lastcommit = ''
152
153         if app['Disabled']:
154             continue
155
156         for build in app['builds']:
157             if 'commit' in build and 'disable' not in build:
158                 lastcommit = build['commit']
159
160         # Potentially incorrect UCM
161         if (app['Update Check Mode'] == 'RepoManifest' and
162                 any(s in lastcommit for s in '.,_-/')):
163             pwarn("Last used commit '%s' looks like a tag, but Update Check Mode is '%s'" % (
164                 lastcommit, app['Update Check Mode']))
165
166         # No proper license
167         if app['License'] in ('Unknown','None',''):
168             warn("License was not set")
169
170         # Summary size limit
171         summ_chars = len(app['Summary'])
172         if summ_chars > config['char_limits']['Summary']:
173             warn("Summary of length %s is over the %i char limit" % (
174                 summ_chars, config['char_limits']['Summary']))
175
176         # Redundant summaries
177         summary = app['Summary']
178         name = str(app['Name'] if app['Name'] else app['Auto Name'])
179         if summary and name:
180             summary_l = summary.lower()
181             name_l = name.lower()
182             if summary_l == name_l:
183                 warn("Summary '%s' is just the app's name" % summary)
184             elif (summary_l in name_l or name_l in summary_l):
185                 pwarn("Summary '%s' probably contains redundant info already in app name '%s'" % (
186                     summary, name))
187
188         # Invalid lists
189         desc_chars = 0
190         for line in app['Description']:
191             if re.match(r'[ ]*[*#][^ .]', line):
192                 warn("Invalid bulleted list: '%s'" % line)
193             desc_chars += len(line)
194
195         # Description size limit
196         if desc_chars > config['char_limits']['Description']:
197             warn("Description of length %s is over the %i char limit" % (
198                 desc_chars, config['char_limits']['Description']))
199
200         # Regex checks in all kinds of fields
201         for f in regex_warnings:
202             for m, r in regex_warnings[f]:
203                 if m.match(app[f]):
204                     warn("%s '%s': %s" % (f, app[f], r))
205
206         # Regex pedantic checks in all kinds of fields
207         if options.pedantic:
208             for f in regex_pedantic:
209                 for m, r in regex_pedantic[f]:
210                     if m.match(app[f]):
211                         warn("%s '%s': %s" % (f, app[f], r))
212
213         # Build warnings
214         for build in app['builds']:
215             for n in ['master', 'origin/', 'default', 'trunk']:
216                 if 'commit' in build:
217                     if build['commit'].startswith(n):
218                         warn("Branch '%s' used as commit in build '%s'" % (
219                             n, build['version']))
220                 if 'srclibs' in build:
221                     for srclib in build['srclibs']:
222                         ref = srclib.split('@')[1].split('/')[0]
223                         if ref.startswith(n):
224                             warn("Branch '%s' used as commit in srclib '%s'" % (
225                                 n, srclib))
226
227         if not appid:
228             print
229
230     logging.info("Found a total of %i warnings in %i apps." % (warn_count, app_count))
231
232 if __name__ == "__main__":
233     main()
234