2018-01-27 09:35:31 +00:00
|
|
|
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
|
|
|
from __future__ import absolute_import, print_function
|
|
|
|
|
|
|
|
import os
|
|
|
|
import signal
|
|
|
|
import re
|
|
|
|
|
2019-07-11 14:03:39 +00:00
|
|
|
# py2-compat
|
2018-01-27 09:35:31 +00:00
|
|
|
try:
|
|
|
|
from json.decoder import JSONDecodeError
|
|
|
|
except ImportError:
|
|
|
|
JSONDecodeError = ValueError
|
|
|
|
|
2019-07-11 14:03:39 +00:00
|
|
|
from mozfile import which
|
2018-01-27 09:35:31 +00:00
|
|
|
from mozlint import result
|
2018-05-09 19:56:43 +00:00
|
|
|
from mozlint.util import pip
|
2018-01-27 09:35:31 +00:00
|
|
|
from mozprocess import ProcessHandlerMixin
|
|
|
|
|
2018-05-09 19:56:43 +00:00
|
|
|
here = os.path.abspath(os.path.dirname(__file__))
|
|
|
|
CODESPELL_REQUIREMENTS_PATH = os.path.join(here, 'codespell_requirements.txt')
|
2018-01-27 09:35:31 +00:00
|
|
|
|
|
|
|
CODESPELL_NOT_FOUND = """
|
2018-05-09 19:56:43 +00:00
|
|
|
Could not find codespell! Install codespell and try again.
|
|
|
|
|
|
|
|
$ pip install -U --require-hashes -r {}
|
|
|
|
""".strip().format(CODESPELL_REQUIREMENTS_PATH)
|
|
|
|
|
2018-01-27 09:35:31 +00:00
|
|
|
|
2018-05-09 19:56:43 +00:00
|
|
|
CODESPELL_INSTALL_ERROR = """
|
|
|
|
Unable to install correct version of codespell
|
|
|
|
Try to install it manually with:
|
|
|
|
$ pip install -U --require-hashes -r {}
|
|
|
|
""".strip().format(CODESPELL_REQUIREMENTS_PATH)
|
2018-01-27 09:35:31 +00:00
|
|
|
|
|
|
|
results = []
|
|
|
|
|
|
|
|
CODESPELL_FORMAT_REGEX = re.compile(r'(.*):(.*): (.*) ==> (.*)$')
|
|
|
|
|
|
|
|
|
|
|
|
class CodespellProcess(ProcessHandlerMixin):
|
|
|
|
def __init__(self, config, *args, **kwargs):
|
|
|
|
self.config = config
|
|
|
|
kwargs['processOutputLine'] = [self.process_line]
|
|
|
|
ProcessHandlerMixin.__init__(self, *args, **kwargs)
|
|
|
|
|
|
|
|
def process_line(self, line):
|
|
|
|
try:
|
|
|
|
match = CODESPELL_FORMAT_REGEX.match(line)
|
|
|
|
abspath, line, typo, correct = match.groups()
|
|
|
|
except AttributeError:
|
|
|
|
print('Unable to match regex against output: {}'.format(line))
|
|
|
|
return
|
|
|
|
|
|
|
|
# Ignore false positive like aParent (which would be fixed to apparent)
|
|
|
|
# See https://github.com/lucasdemarchi/codespell/issues/314
|
|
|
|
m = re.match(r'^[a-z][A-Z][a-z]*', typo)
|
|
|
|
if m:
|
|
|
|
return
|
2018-08-29 13:36:46 +00:00
|
|
|
res = {'path': abspath,
|
2018-01-27 09:35:31 +00:00
|
|
|
'message': typo + " ==> " + correct,
|
2018-08-28 19:24:55 +00:00
|
|
|
'level': 'error',
|
2018-01-27 09:35:31 +00:00
|
|
|
'lineno': line,
|
|
|
|
}
|
|
|
|
results.append(result.from_config(self.config, **res))
|
|
|
|
|
|
|
|
def run(self, *args, **kwargs):
|
|
|
|
orig = signal.signal(signal.SIGINT, signal.SIG_IGN)
|
|
|
|
ProcessHandlerMixin.run(self, *args, **kwargs)
|
|
|
|
signal.signal(signal.SIGINT, orig)
|
|
|
|
|
|
|
|
|
|
|
|
def run_process(config, cmd):
|
|
|
|
proc = CodespellProcess(config, cmd)
|
|
|
|
proc.run()
|
|
|
|
try:
|
|
|
|
proc.wait()
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
proc.kill()
|
|
|
|
|
|
|
|
|
|
|
|
def get_codespell_binary():
|
|
|
|
"""
|
|
|
|
Returns the path of the first codespell binary available
|
|
|
|
if not found returns None
|
|
|
|
"""
|
|
|
|
binary = os.environ.get('CODESPELL')
|
|
|
|
if binary:
|
|
|
|
return binary
|
|
|
|
|
2019-07-11 14:03:39 +00:00
|
|
|
return which('codespell')
|
2018-01-27 09:35:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
def lint(paths, config, fix=None, **lintargs):
|
|
|
|
|
2018-05-09 19:56:43 +00:00
|
|
|
if not pip.reinstall_program(CODESPELL_REQUIREMENTS_PATH):
|
|
|
|
print(CODESPELL_INSTALL_ERROR)
|
|
|
|
return 1
|
|
|
|
|
2018-01-27 09:35:31 +00:00
|
|
|
binary = get_codespell_binary()
|
|
|
|
|
|
|
|
if not binary:
|
|
|
|
print(CODESPELL_NOT_FOUND)
|
|
|
|
if 'MOZ_AUTOMATION' in os.environ:
|
|
|
|
return 1
|
|
|
|
return []
|
|
|
|
|
|
|
|
config['root'] = lintargs['root']
|
2018-04-18 19:17:40 +00:00
|
|
|
exclude_list = os.path.join(here, 'exclude-list.txt')
|
2018-01-27 09:35:31 +00:00
|
|
|
cmd_args = [binary,
|
|
|
|
'--disable-colors',
|
|
|
|
# Silence some warnings:
|
|
|
|
# 1: disable warnings about wrong encoding
|
|
|
|
# 2: disable warnings about binary file
|
2018-03-08 09:51:42 +00:00
|
|
|
# 4: shut down warnings about automatic fixes
|
|
|
|
# that were disabled in dictionary.
|
2018-05-12 10:03:11 +00:00
|
|
|
'--quiet-level=7',
|
2018-04-18 19:17:40 +00:00
|
|
|
'--ignore-words=' + exclude_list,
|
2018-05-09 19:56:43 +00:00
|
|
|
# Ignore dictonnaries
|
|
|
|
'--skip=*.dic',
|
2018-01-27 09:35:31 +00:00
|
|
|
]
|
|
|
|
|
2018-04-19 21:41:57 +00:00
|
|
|
if fix:
|
|
|
|
cmd_args.append('--write-changes')
|
2018-01-27 09:35:31 +00:00
|
|
|
|
|
|
|
base_command = cmd_args + paths
|
|
|
|
|
|
|
|
run_process(config, base_command)
|
|
|
|
return results
|