3 # lint.py - part of the FDroid server tool
4 # Copyright (C) 2013-2014 Daniel Martí <mvdan@mvdan.cc>
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU Affero General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See th
14 # GNU Affero General Public License for more details.
16 # You should have received a copy of the GNU Affero General Public Licen
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 from argparse import ArgumentParser
27 from . import metadata
28 from . import rewritemeta
34 def enforce_https(domain):
35 return (re.compile(r'.*[^sS]://[^/]*' + re.escape(domain) + r'(/.*)?'),
36 domain + " URLs should always use https://")
40 enforce_https('github.com'),
41 enforce_https('gitlab.com'),
42 enforce_https('bitbucket.org'),
43 enforce_https('apache.org'),
44 enforce_https('google.com'),
45 enforce_https('git.code.sf.net'),
46 enforce_https('svn.code.sf.net'),
47 enforce_https('anongit.kde.org'),
48 enforce_https('savannah.nongnu.org'),
49 enforce_https('git.savannah.nongnu.org'),
50 enforce_https('download.savannah.nongnu.org'),
51 enforce_https('savannah.gnu.org'),
52 enforce_https('git.savannah.gnu.org'),
53 enforce_https('download.savannah.gnu.org'),
57 def forbid_shortener(domain):
58 return (re.compile(r'https?://[^/]*' + re.escape(domain) + r'/.*'),
59 _("URL shorteners should not be used"))
62 http_url_shorteners = [
63 forbid_shortener('1url.com'),
64 forbid_shortener('adf.ly'),
65 forbid_shortener('bc.vc'),
66 forbid_shortener('bit.do'),
67 forbid_shortener('bit.ly'),
68 forbid_shortener('bitly.com'),
69 forbid_shortener('budurl.com'),
70 forbid_shortener('buzurl.com'),
71 forbid_shortener('cli.gs'),
72 forbid_shortener('cur.lv'),
73 forbid_shortener('cutt.us'),
74 forbid_shortener('db.tt'),
75 forbid_shortener('filoops.info'),
76 forbid_shortener('goo.gl'),
77 forbid_shortener('is.gd'),
78 forbid_shortener('ity.im'),
79 forbid_shortener('j.mp'),
80 forbid_shortener('l.gg'),
81 forbid_shortener('lnkd.in'),
82 forbid_shortener('moourl.com'),
83 forbid_shortener('ow.ly'),
84 forbid_shortener('para.pt'),
85 forbid_shortener('po.st'),
86 forbid_shortener('q.gs'),
87 forbid_shortener('qr.ae'),
88 forbid_shortener('qr.net'),
89 forbid_shortener('rdlnk.com'),
90 forbid_shortener('scrnch.me'),
91 forbid_shortener('short.nr'),
92 forbid_shortener('sn.im'),
93 forbid_shortener('snipurl.com'),
94 forbid_shortener('su.pr'),
95 forbid_shortener('t.co'),
96 forbid_shortener('tiny.cc'),
97 forbid_shortener('tinyarrows.com'),
98 forbid_shortener('tinyurl.com'),
99 forbid_shortener('tr.im'),
100 forbid_shortener('tweez.me'),
101 forbid_shortener('twitthis.com'),
102 forbid_shortener('twurl.nl'),
103 forbid_shortener('tyn.ee'),
104 forbid_shortener('u.bb'),
105 forbid_shortener('u.to'),
106 forbid_shortener('ur1.ca'),
107 forbid_shortener('urlof.site'),
108 forbid_shortener('v.gd'),
109 forbid_shortener('vzturl.com'),
110 forbid_shortener('x.co'),
111 forbid_shortener('xrl.us'),
112 forbid_shortener('yourls.org'),
113 forbid_shortener('zip.net'),
114 forbid_shortener('✩.ws'),
115 forbid_shortener('➡.ws'),
118 http_checks = https_enforcings + http_url_shorteners + [
119 (re.compile(r'.*github\.com/[^/]+/[^/]+\.git'),
120 _("Appending .git is not necessary")),
121 (re.compile(r'.*://[^/]*(github|gitlab|bitbucket|rawgit)[^/]*/([^/]+/){1,3}master'),
122 _("Use /HEAD instead of /master to point at a file in the default branch")),
126 'WebSite': http_checks,
127 'SourceCode': http_checks,
128 'Repo': https_enforcings,
129 'IssueTracker': http_checks + [
130 (re.compile(r'.*github\.com/[^/]+/[^/]+/*$'),
131 _("/issues is missing")),
132 (re.compile(r'.*gitlab\.com/[^/]+/[^/]+/*$'),
133 _("/issues is missing")),
135 'Donate': http_checks + [
136 (re.compile(r'.*flattr\.com'),
137 _("Flattr donation methods belong in the FlattrID flag")),
138 (re.compile(r'.*liberapay\.com'),
139 _("Liberapay donation methods belong in the LiberapayID flag")),
141 'Changelog': http_checks,
144 _("Unnecessary leading space")),
145 (re.compile(r'.*\s$'),
146 _("Unnecessary trailing space")),
149 (re.compile(r'.*\b(free software|open source)\b.*', re.IGNORECASE),
150 _("No need to specify that the app is Free Software")),
151 (re.compile(r'.*((your|for).*android|android.*(app|device|client|port|version))', re.IGNORECASE),
152 _("No need to specify that the app is for Android")),
153 (re.compile(r'.*[a-z0-9][.!?]( |$)'),
154 _("Punctuation should be avoided")),
156 _("Unnecessary leading space")),
157 (re.compile(r'.*\s$'),
158 _("Unnecessary trailing space")),
160 'Description': https_enforcings + http_url_shorteners + [
161 (re.compile(r'\s*[*#][^ .]'),
162 _("Invalid bulleted list")),
164 _("Unnecessary leading space")),
165 (re.compile(r'.*\s$'),
166 _("Unnecessary trailing space")),
170 locale_pattern = re.compile(r'^[a-z]{2,3}(-[A-Z][A-Z])?$')
173 def check_regexes(app):
174 for f, checks in regex_checks.items():
177 t = metadata.fieldtype(f)
178 if t == metadata.TYPE_MULTILINE:
179 for l in v.splitlines():
181 yield "%s at line '%s': %s" % (f, l, r)
186 yield "%s '%s': %s" % (f, v, r)
189 def get_lastbuild(builds):
193 if not build.disable:
194 vercode = int(build.versionCode)
195 if lowest_vercode == -1 or vercode < lowest_vercode:
196 lowest_vercode = vercode
197 if not lastbuild or int(build.versionCode) > int(lastbuild.versionCode):
202 def check_ucm_tags(app):
203 lastbuild = get_lastbuild(app.builds)
204 if (lastbuild is not None
206 and app.UpdateCheckMode == 'RepoManifest'
207 and not lastbuild.commit.startswith('unknown')
208 and lastbuild.versionCode == app.CurrentVersionCode
209 and not lastbuild.forcevercode
210 and any(s in lastbuild.commit for s in '.,_-/')):
211 yield _("Last used commit '{commit}' looks like a tag, but Update Check Mode is '{ucm}'")\
212 .format(commit=lastbuild.commit, ucm=app.UpdateCheckMode)
215 def check_char_limits(app):
216 limits = config['char_limits']
218 if len(app.Summary) > limits['summary']:
219 yield _("Summary of length {length} is over the {limit} char limit")\
220 .format(length=len(app.Summary), limit=limits['summary'])
222 if len(app.Description) > limits['description']:
223 yield _("Description of length {length} is over the {limit} char limit")\
224 .format(length=len(app.Description), limit=limits['description'])
227 def check_old_links(app):
237 if any(s in app.Repo for s in usual_sites):
238 for f in ['WebSite', 'SourceCode', 'IssueTracker', 'Changelog']:
240 if any(s in v for s in old_sites):
241 yield _("App is in '{repo}' but has a link to {url}")\
242 .format(repo=app.Repo, url=v)
245 def check_useless_fields(app):
246 if app.UpdateCheckName == app.id:
247 yield _("Update Check Name is set to the known app id - it can be removed")
250 filling_ucms = re.compile(r'^(Tags.*|RepoManifest.*)')
253 def check_checkupdates_ran(app):
254 if filling_ucms.match(app.UpdateCheckMode):
255 if not app.AutoName and not app.CurrentVersion and app.CurrentVersionCode == '0':
256 yield _("UCM is set but it looks like checkupdates hasn't been run yet")
259 def check_empty_fields(app):
260 if not app.Categories:
261 yield _("Categories are not set")
264 all_categories = set([
275 "Science & Education",
285 def check_categories(app):
286 for categ in app.Categories:
287 if categ not in all_categories:
288 yield _("Category '%s' is not valid" % categ)
291 def check_duplicates(app):
292 if app.Name and app.Name == app.AutoName:
293 yield _("Name '%s' is just the auto name - remove it") % app.Name
296 for f in ['Source Code', 'Web Site', 'Issue Tracker', 'Changelog']:
302 yield _("Duplicate link in '{field}': {url}").format(field=f, url=v)
306 name = app.Name or app.AutoName
307 if app.Summary and name:
308 if app.Summary.lower() == name.lower():
309 yield _("Summary '%s' is just the app's name") % app.Summary
311 if app.Summary and app.Description and len(app.Description) == 1:
312 if app.Summary.lower() == app.Description[0].lower():
313 yield _("Description '%s' is just the app's summary") % app.Summary
316 for l in app.Description.splitlines():
320 yield _("Description has a duplicate line")
324 desc_url = re.compile(r'(^|[^[])\[([^ ]+)( |\]|$)')
327 def check_mediawiki_links(app):
328 wholedesc = ' '.join(app.Description)
329 for um in desc_url.finditer(wholedesc):
331 for m, r in http_checks:
333 yield _("URL {url} in Description: {error}").format(url=url, error=r)
336 def check_bulleted_lists(app):
337 validchars = ['*', '#']
340 for l in app.Description.splitlines():
345 if l[0] == lchar and l[1] == ' ':
347 if lcount > 2 and lchar not in validchars:
348 yield _("Description has a list (%s) but it isn't bulleted (*) nor numbered (#)") % lchar
355 def check_builds(app):
356 supported_flags = set(metadata.build_flags)
357 # needed for YAML and JSON
358 for build in app.builds:
360 if build.disable.startswith('Generated by import.py'):
361 yield _("Build generated by `fdroid import` - remove disable line once ready")
363 for s in ['master', 'origin', 'HEAD', 'default', 'trunk']:
364 if build.commit and build.commit.startswith(s):
365 yield _("Branch '{branch}' used as commit in build '{versionName}'")\
366 .format(branch=s, versionName=build.versionName)
367 for srclib in build.srclibs:
368 ref = srclib.split('@')[1].split('/')[0]
369 if ref.startswith(s):
370 yield _("Branch '{branch}' used as commit in srclib '{srclib}'")\
371 .format(branch=s, srclib=srclib)
372 for key in build.keys():
373 if key not in supported_flags:
374 yield _('%s is not an accepted build field') % key
377 def check_files_dir(app):
378 dir_path = os.path.join('metadata', app.id)
379 if not os.path.isdir(dir_path):
382 for name in os.listdir(dir_path):
383 path = os.path.join(dir_path, name)
384 if not (os.path.isfile(path) or name == 'signatures' or locale_pattern.match(name)):
385 yield _("Found non-file at %s") % path
389 used = {'signatures', }
390 for build in app.builds:
391 for fname in build.patch:
392 if fname not in files:
393 yield _("Unknown file '{filename}' in build '{versionName}'")\
394 .format(filename=fname, versionName=build.versionName)
398 for name in files.difference(used):
399 if locale_pattern.match(name):
401 yield _("Unused file at %s") % os.path.join(dir_path, name)
404 def check_format(app):
405 if options.format and not rewritemeta.proper_format(app):
406 yield _("Run rewritemeta to fix formatting")
409 def check_license_tag(app):
410 '''Ensure all license tags are in https://spdx.org/license-list'''
411 if app.License.rstrip('+') not in SPDX:
412 yield _('Invalid license tag "%s"! Use only tags from https://spdx.org/license-list') \
416 def check_extlib_dir(apps):
417 dir_path = os.path.join('build', 'extlib')
418 unused_extlib_files = set()
419 for root, dirs, files in os.walk(dir_path):
421 unused_extlib_files.add(os.path.join(root, name)[len(dir_path) + 1:])
425 for build in app.builds:
426 for path in build.extlibs:
427 if path not in unused_extlib_files:
428 yield _("{appid}: Unknown extlib {path} in build '{versionName}'")\
429 .format(appid=app.id, path=path, versionName=build.versionName)
433 for path in unused_extlib_files.difference(used):
434 if any(path.endswith(s) for s in [
436 'source.txt', 'origin.txt', 'md5.txt',
437 'LICENSE', 'LICENSE.txt',
438 'COPYING', 'COPYING.txt',
439 'NOTICE', 'NOTICE.txt',
442 yield _("Unused extlib at %s") % os.path.join(dir_path, path)
445 def check_for_unsupported_metadata_files(basedir=""):
446 """Checks whether any non-metadata files are in metadata/"""
451 formats = config['accepted_formats']
452 for f in glob.glob(basedir + 'metadata/*') + glob.glob(basedir + 'metadata/.*'):
456 exists = exists or os.path.exists(f + '.' + t)
458 print(_('"%s/" has no matching metadata file!') % f)
460 elif not os.path.splitext(f)[1][1:] in formats:
461 print('"' + f.replace(basedir, '')
462 + '" is not a supported file format: (' + ','.join(formats) + ')')
470 global config, options
472 # Parse command line...
473 parser = ArgumentParser(usage="%(prog)s [options] [APPID [APPID ...]]")
474 common.setup_global_opts(parser)
475 parser.add_argument("-f", "--format", action="store_true", default=False,
476 help=_("Also warn about formatting issues, like rewritemeta -l"))
477 parser.add_argument("appid", nargs='*', help=_("applicationId in the form APPID"))
478 metadata.add_metadata_arguments(parser)
479 options = parser.parse_args()
480 metadata.warnings_action = options.W
482 config = common.read_config(options)
485 allapps = metadata.read_metadata(xref=True)
486 apps = common.read_app_args(options.appid, allapps, False)
488 anywarns = check_for_unsupported_metadata_files()
490 apps_check_funcs = []
491 if len(options.appid) == 0:
492 # otherwise it finds tons of unused extlibs
493 apps_check_funcs.append(check_extlib_dir)
494 for check_func in apps_check_funcs:
495 for warn in check_func(apps.values()):
499 for appid, app in apps.items():
508 check_checkupdates_ran,
509 check_useless_fields,
513 check_mediawiki_links,
514 check_bulleted_lists,
521 for check_func in app_check_funcs:
522 for warn in check_func(app):
524 print("%s: %s" % (appid, warn))
530 # A compiled, public domain list of official SPDX license tags from:
531 # https://github.com/sindresorhus/spdx-license-list/blob/v3.0.1/spdx-simple.json
532 # The deprecated license tags have been removed from the list, they are at the
533 # bottom, starting after the last license tags that start with Z.
534 # This is at the bottom, since its a long list of data
536 "PublicDomain", # an F-Droid addition, until we can enforce a better option
576 "BSD-2-Clause-FreeBSD",
577 "BSD-2-Clause-NetBSD",
579 "BSD-3-Clause-Clear",
580 "BSD-3-Clause-No-Nuclear-License",
581 "BSD-3-Clause-No-Nuclear-License-2014",
582 "BSD-3-Clause-No-Nuclear-Warranty",
586 "BSD-3-Clause-Attribution",
602 "CNRI-Python-GPL-Compatible",
736 "MPL-2.0-no-copyleft-exception",
860 "zlib-acknowledgement",
866 if __name__ == "__main__":