chiark / gitweb /
Merge branch 'changelog' of https://gitlab.com/krt/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
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         # TODO enable in August 2015, when Google Code goes read-only
35         # (re.compile(r'.*://code\.google\.com/.*'),
36         # "code.google.com will be soon switching down, perhaps it moved to github.com?"),
37     ],
38     'Source Code': [
39         (re.compile(r'.*[^sS]://github\.com/.*'),
40          "github URLs should always use https:// (not http://, git://, or git@)"),
41         (re.compile(r'.*[^sS]://dl\.google\.com/.*'),
42          "dl.google.com URLs should always use https:// not http://"),
43         (re.compile(r'.*[^sS]://gitorious\.org/.*'),
44          "gitorious URLs should always use https:// (not http://, git://, or git@)"),
45         # TODO enable in August 2015, when Google Code goes read-only
46         # (re.compile(r'.*://code\.google\.com/.*'),
47         # "code.google.com will be soon switching down, perhaps it moved to github.com?"),
48     ],
49     'Repo': [
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         # TODO enable in August 2015, when Google Code goes read-only
63         # (re.compile(r'.*://code\.google\.com/.*'),
64         # "code.google.com will be soon switching down, perhaps it moved to github.com?"),
65     ],
66     'Issue Tracker': [
67         (re.compile(r'.*github\.com/[^/]+/[^/]+[/]*$'),
68          "/issues is missing"),
69         (re.compile(r'.*[^sS]://github\.com/.*'),
70          "github URLs should always use https:// not http://"),
71         (re.compile(r'.*[^sS]://gitorious\.org/.*'),
72          "gitorious URLs should always use https:// not http://"),
73         # TODO enable in August 2015, when Google Code goes read-only
74         # (re.compile(r'.*://code\.google\.com/.*'),
75         # "code.google.com will be soon switching down, perhaps it moved to github.com?"),
76     ],
77     'Changelog': [
78         (re.compile(r'.*[^sS]://code\.google\.com/.*'),
79          "code.google.com URLs should always use https:// not http://"),
80         (re.compile(r'.*[^sS]://github\.com/.*'),
81          "github URLs should always use https:// not http://"),
82         (re.compile(r'.*[^sS]://gitorious\.org/.*'),
83          "gitorious URLs should always use https:// not http://"),
84     ],
85     'License': [
86         (re.compile(r'^(|None|Unknown)$'),
87          "No license specified"),
88     ],
89     'Summary': [
90         (re.compile(r'^$'),
91          "Summary yet to be filled"),
92         (re.compile(r'.*\b(free software|open source)\b.*', re.IGNORECASE),
93          "No need to specify that the app is Free Software"),
94         (re.compile(r'.*((your|for).*android|android.*(app|device|client|port|version))', re.IGNORECASE),
95          "No need to specify that the app is for Android"),
96     ],
97     'Description': [
98         (re.compile(r'^No description available$'),
99          "Description yet to be filled"),
100         (re.compile(r'\s*[*#][^ .]'),
101          "Invalid bulleted list"),
102         (re.compile(r'^\s'),
103          "Unnecessary leading space"),
104         (re.compile(r'.*\s$'),
105          "Unnecessary trailing space"),
106     ],
107 }
108
109 regex_pedantic = {
110     'Web Site': [
111         (re.compile(r'.*github\.com/[^/]+/[^/]+\.git'),
112          "Appending .git is not necessary"),
113     ],
114     'Source Code': [
115         (re.compile(r'.*github\.com/[^/]+/[^/]+\.git'),
116          "Appending .git is not necessary"),
117     ],
118     'Repo': [
119         (re.compile(r'^http://.*'),
120          "use https:// if available"),
121         (re.compile(r'^svn://.*'),
122          "use https:// if available"),
123     ],
124     'Issue Tracker': [
125         (re.compile(r'.*github\.com/[^/]+/[^/]+/issues/.*'),
126          "/issues is often enough on its own"),
127     ],
128     'Changelog': [
129         (re.compile(r'.*commit.*', re.IGNORECASE),
130          "Not every commit log is suitable as change log"),
131     ],
132     'Summary': [
133         (re.compile(r'.*[a-z0-9][.!?][ $]'),
134          "Punctuation should be avoided"),
135     ],
136 }
137
138
139 def main():
140
141     global config, options, curid, count
142     curid = None
143
144     count = Counter()
145
146     def warn(message):
147         global curid, count
148         if curid:
149             print "%s:" % curid
150             curid = None
151             count['app'] += 1
152         print '    %s' % message
153         count['warn'] += 1
154
155     def pwarn(message):
156         if options.pedantic:
157             warn(message)
158
159     # Parse command line...
160     parser = OptionParser(usage="Usage: %prog [options] [APPID [APPID ...]]")
161     parser.add_option("-v", "--verbose", action="store_true", default=False,
162                       help="Spew out even more information than normal")
163     parser.add_option("-q", "--quiet", action="store_true", default=False,
164                       help="Restrict output to warnings and errors")
165     parser.add_option("-p", "--pedantic", action="store_true", default=False,
166                       help="Show pedantic warnings that might give false positives")
167     (options, args) = parser.parse_args()
168
169     config = common.read_config(options)
170
171     # Get all apps...
172     allapps = metadata.read_metadata(xref=False)
173     apps = common.read_app_args(args, allapps, False)
174
175     for appid, app in apps.iteritems():
176         if app['Disabled']:
177             continue
178
179         curid = appid
180         count['app_total'] += 1
181
182         curbuild = None
183         for build in app['builds']:
184             if not curbuild or int(build['vercode']) > int(curbuild['vercode']):
185                 curbuild = build
186
187         # Potentially incorrect UCM
188         if (curbuild and curbuild['commit']
189                 and app['Update Check Mode'] == 'RepoManifest'
190                 and curbuild['commit'] != 'unknown - see disabled'
191                 and any(s in curbuild['commit'] for s in '.,_-/')):
192             pwarn("Last used commit '%s' looks like a tag, but Update Check Mode is '%s'" % (
193                 curbuild['commit'], app['Update Check Mode']))
194
195         # Dangerous auto updates
196         if curbuild and app['Auto Update Mode'] != 'None':
197             for flag in ['target', 'srclibs', 'scanignore']:
198                 if curbuild[flag]:
199                     pwarn("Auto Update Mode is enabled but '%s' is manually set at '%s'" % (flag, curbuild[flag]))
200
201         # Summary size limit
202         summ_chars = len(app['Summary'])
203         if summ_chars > config['char_limits']['Summary']:
204             warn("Summary of length %s is over the %i char limit" % (
205                 summ_chars, config['char_limits']['Summary']))
206
207         # Redundant info
208         if app['Web Site'] and app['Source Code']:
209             if app['Web Site'].lower() == app['Source Code'].lower():
210                 warn("Website '%s' is just the app's source code link" % app['Web Site'])
211
212         # "None" still a category
213         if 'None' in app['Categories']:
214             warn("Category 'None' is is still present")
215         elif not app['Categories']:
216             warn("Categories are not set")
217
218         name = app['Name'] or app['Auto Name']
219         if app['Summary'] and name:
220             if app['Summary'].lower() == name.lower():
221                 warn("Summary '%s' is just the app's name" % app['Summary'])
222
223         desc = app['Description']
224         if app['Summary'] and desc and len(desc) == 1:
225             if app['Summary'].lower() == desc[0].lower():
226                 warn("Description '%s' is just the app's summary" % app['Summary'])
227
228         # Description size limit
229         desc_charcount = sum(len(l) for l in desc)
230         if desc_charcount > config['char_limits']['Description']:
231             warn("Description of length %s is over the %i char limit" % (
232                 desc_charcount, config['char_limits']['Description']))
233
234         if (not desc[0] or not desc[-1]
235                 or any(not desc[l - 1] and not desc[l] for l in range(1, len(desc)))):
236             warn("Description has an extra empty line")
237
238         # Regex checks in all kinds of fields
239         for f in regex_warnings:
240             for m, r in regex_warnings[f]:
241                 t = metadata.metafieldtype(f)
242                 if t == 'string':
243                     if m.match(app[f]):
244                         warn("%s '%s': %s" % (f, app[f], r))
245                 elif t == 'multiline':
246                     for l in app[f]:
247                         if m.match(l):
248                             warn("%s at line '%s': %s" % (f, l, r))
249
250         # Regex pedantic checks in all kinds of fields
251         if options.pedantic:
252             for f in regex_pedantic:
253                 for m, r in regex_pedantic[f]:
254                     if m.match(app[f]):
255                         warn("%s '%s': %s" % (f, app[f], r))
256
257         # Build warnings
258         for build in app['builds']:
259             if build['disable']:
260                 continue
261             for s in ['master', 'origin', 'HEAD', 'default', 'trunk']:
262                 if build['commit'] and build['commit'].startswith(s):
263                     warn("Branch '%s' used as commit in build '%s'" % (
264                         s, build['version']))
265                 for srclib in build['srclibs']:
266                     ref = srclib.split('@')[1].split('/')[0]
267                     if ref.startswith(s):
268                         warn("Branch '%s' used as commit in srclib '%s'" % (
269                             s, srclib))
270             for s in ['git clone', 'git svn clone', 'svn checkout', 'svn co', 'hg clone']:
271                 for flag in ['init', 'prebuild', 'build']:
272                     if not build[flag]:
273                         continue
274                     if s in build[flag]:
275                         # TODO: This should not be pedantic!
276                         pwarn("'%s' used in %s '%s'" % (s, flag, build[flag]))
277
278         if not curid:
279             print
280
281     logging.info("Found a total of %i warnings in %i apps out of %i total." % (
282         count['warn'], count['app'], count['app_total']))
283
284 if __name__ == "__main__":
285     main()