chiark / gitweb /
lint: take forcevercode into account for UCM warnings
[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
31 def enforce_https(domain):
32     return (re.compile(r'.*[^sS]://[^/]*' + re.escape(domain) + r'/.*'),
33             domain + " URLs should always use https://")
34
35 https_enforcings = [
36     enforce_https('github.com'),
37     enforce_https('gitlab.com'),
38     enforce_https('gitorious.org'),
39     enforce_https('apache.org'),
40     enforce_https('google.com'),
41     enforce_https('svn.code.sf.net'),
42     enforce_https('googlecode.com'),
43 ]
44
45 http_warnings = https_enforcings + [
46     (re.compile(r'.*github\.com/[^/]+/[^/]+\.git'),
47      "Appending .git is not necessary"),
48     # TODO enable in August 2015, when Google Code goes read-only
49     # (re.compile(r'.*://code\.google\.com/.*'),
50     #  "code.google.com will be soon switching down, perhaps the project moved to github.com?"),
51 ]
52
53 regex_warnings = {
54     'Web Site': http_warnings + [
55     ],
56     'Source Code': http_warnings + [
57     ],
58     'Repo': https_enforcings + [
59     ],
60     'Issue Tracker': http_warnings + [
61         (re.compile(r'.*github\.com/[^/]+/[^/]+[/]*$'),
62          "/issues is missing"),
63     ],
64     'Changelog': http_warnings + [
65     ],
66     'License': [
67         (re.compile(r'^(|None|Unknown)$'),
68          "No license specified"),
69     ],
70     'Summary': [
71         (re.compile(r'^$'),
72          "Summary yet to be filled"),
73         (re.compile(r'.*\b(free software|open source)\b.*', re.IGNORECASE),
74          "No need to specify that the app is Free Software"),
75         (re.compile(r'.*((your|for).*android|android.*(app|device|client|port|version))', re.IGNORECASE),
76          "No need to specify that the app is for Android"),
77         (re.compile(r'.*[a-z0-9][.!?]( |$)'),
78          "Punctuation should be avoided"),
79     ],
80     'Description': [
81         (re.compile(r'^No description available$'),
82          "Description yet to be filled"),
83         (re.compile(r'\s*[*#][^ .]'),
84          "Invalid bulleted list"),
85         (re.compile(r'^\s'),
86          "Unnecessary leading space"),
87         (re.compile(r'.*\s$'),
88          "Unnecessary trailing space"),
89     ],
90 }
91
92
93 def main():
94
95     global config, options, curid, count
96     curid = None
97
98     count = Counter()
99
100     def warn(message):
101         global curid, count
102         if curid:
103             print "%s:" % curid
104             curid = None
105             count['app'] += 1
106         print '    %s' % message
107         count['warn'] += 1
108
109     # Parse command line...
110     parser = OptionParser(usage="Usage: %prog [options] [APPID [APPID ...]]")
111     parser.add_option("-v", "--verbose", action="store_true", default=False,
112                       help="Spew out even more information than normal")
113     parser.add_option("-q", "--quiet", action="store_true", default=False,
114                       help="Restrict output to warnings and errors")
115     (options, args) = parser.parse_args()
116
117     config = common.read_config(options)
118
119     # Get all apps...
120     allapps = metadata.read_metadata(xref=False)
121     apps = common.read_app_args(args, allapps, False)
122
123     for appid, app in apps.iteritems():
124         if app['Disabled']:
125             continue
126
127         curid = appid
128         count['app_total'] += 1
129
130         curbuild = None
131         for build in app['builds']:
132             if not curbuild or int(build['vercode']) > int(curbuild['vercode']):
133                 curbuild = build
134
135         # Incorrect UCM
136         if (curbuild and curbuild['commit']
137                 and app['Update Check Mode'] == 'RepoManifest'
138                 and not curbuild['commit'].startswith('unknown')
139                 and curbuild['vercode'] == app['Current Version Code']
140                 and not curbuild['forcevercode']
141                 and any(s in curbuild['commit'] for s in '.,_-/')):
142             warn("Last used commit '%s' looks like a tag, but Update Check Mode is '%s'" % (
143                 curbuild['commit'], app['Update Check Mode']))
144
145         # Summary size limit
146         summ_chars = len(app['Summary'])
147         if summ_chars > config['char_limits']['Summary']:
148             warn("Summary of length %s is over the %i char limit" % (
149                 summ_chars, config['char_limits']['Summary']))
150
151         # Redundant info
152         if app['Web Site'] and app['Source Code']:
153             if app['Web Site'].lower() == app['Source Code'].lower():
154                 warn("Website '%s' is just the app's source code link" % app['Web Site'])
155
156         # "None" still a category
157         if 'None' in app['Categories']:
158             warn("Category 'None' is is still present")
159         elif not app['Categories']:
160             warn("Categories are not set")
161
162         if app['Name'] and app['Name'] == app['Auto Name']:
163             warn("Name '%s' is just the auto name" % app['Name'])
164
165         name = app['Name'] or app['Auto Name']
166         if app['Summary'] and name:
167             if app['Summary'].lower() == name.lower():
168                 warn("Summary '%s' is just the app's name" % app['Summary'])
169
170         desc = app['Description']
171         if app['Summary'] and desc and len(desc) == 1:
172             if app['Summary'].lower() == desc[0].lower():
173                 warn("Description '%s' is just the app's summary" % app['Summary'])
174
175         # Description size limit
176         desc_charcount = sum(len(l) for l in desc)
177         if desc_charcount > config['char_limits']['Description']:
178             warn("Description of length %s is over the %i char limit" % (
179                 desc_charcount, config['char_limits']['Description']))
180
181         if (not desc[0] or not desc[-1]
182                 or any(not desc[l - 1] and not desc[l] for l in range(1, len(desc)))):
183             warn("Description has an extra empty line")
184
185         # Regex checks in all kinds of fields
186         for f in regex_warnings:
187             for m, r in regex_warnings[f]:
188                 t = metadata.metafieldtype(f)
189                 if t == 'string':
190                     if m.match(app[f]):
191                         warn("%s '%s': %s" % (f, app[f], r))
192                 elif t == 'multiline':
193                     for l in app[f]:
194                         if m.match(l):
195                             warn("%s at line '%s': %s" % (f, l, r))
196
197         # Build warnings
198         for build in app['builds']:
199             if build['disable']:
200                 continue
201             for s in ['master', 'origin', 'HEAD', 'default', 'trunk']:
202                 if build['commit'] and build['commit'].startswith(s):
203                     warn("Branch '%s' used as commit in build '%s'" % (
204                         s, build['version']))
205                 for srclib in build['srclibs']:
206                     ref = srclib.split('@')[1].split('/')[0]
207                     if ref.startswith(s):
208                         warn("Branch '%s' used as commit in srclib '%s'" % (
209                             s, srclib))
210
211         if not curid:
212             print
213
214     logging.info("Found a total of %i warnings in %i apps out of %i total." % (
215         count['warn'], count['app'], count['app_total']))
216
217 if __name__ == "__main__":
218     main()