Bug 1460402 - Update the codespell support to use pip instead of the package r?ahal draft
authorSylvestre Ledru <sledru@mozilla.com>
Wed, 09 May 2018 21:56:43 +0200
changeset 794011 c546ee5a34880a8057c70180f00280a119254317
parent 794010 d1523f128f639284a8f4be34b78285929d7fed92
child 794012 d11eb01a7b68b6a9c80b76ad4446d8deb4b24419
push id109563
push userbmo:sledru@mozilla.com
push dateFri, 11 May 2018 08:19:13 +0000
reviewersahal
bugs1460402
milestone62.0a1
Bug 1460402 - Update the codespell support to use pip instead of the package r?ahal MozReview-Commit-ID: J4wS7s9sWFe
tools/lint/spell/__init__.py
tools/lint/spell/codespell_requirements.txt
--- a/tools/lint/spell/__init__.py
+++ b/tools/lint/spell/__init__.py
@@ -11,32 +11,39 @@ import re
 
 # Py3/Py2 compatibility.
 try:
     from json.decoder import JSONDecodeError
 except ImportError:
     JSONDecodeError = ValueError
 
 from mozlint import result
+from mozlint.util import pip
 from mozprocess import ProcessHandlerMixin
 
+here = os.path.abspath(os.path.dirname(__file__))
+CODESPELL_REQUIREMENTS_PATH = os.path.join(here, 'codespell_requirements.txt')
 
 CODESPELL_NOT_FOUND = """
-Unable to locate codespell, please ensure it is installed and in
-your PATH or set the CODESPELL environment variable.
+Could not find codespell! Install codespell and try again.
+
+    $ pip install -U --require-hashes -r {}
+""".strip().format(CODESPELL_REQUIREMENTS_PATH)
+
 
-https://github.com/lucasdemarchi/codespell or your system's package manager.
-""".strip()
+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)
 
 results = []
 
 CODESPELL_FORMAT_REGEX = re.compile(r'(.*):(.*): (.*) ==> (.*)$')
 
-here = os.path.abspath(os.path.dirname(__file__))
-
 
 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):
@@ -86,16 +93,20 @@ def get_codespell_binary():
     try:
         return which.which('codespell')
     except which.WhichError:
         return None
 
 
 def lint(paths, config, fix=None, **lintargs):
 
+    if not pip.reinstall_program(CODESPELL_REQUIREMENTS_PATH):
+        print(CODESPELL_INSTALL_ERROR)
+        return 1
+
     binary = get_codespell_binary()
 
     if not binary:
         print(CODESPELL_NOT_FOUND)
         if 'MOZ_AUTOMATION' in os.environ:
             return 1
         return []
 
@@ -105,17 +116,18 @@ def lint(paths, config, fix=None, **lint
                 '--disable-colors',
                 # Silence some warnings:
                 # 1: disable warnings about wrong encoding
                 # 2: disable warnings about binary file
                 # 4: shut down warnings about automatic fixes
                 #    that were disabled in dictionary.
                 '--quiet-level=4',
                 '--ignore-words=' + exclude_list,
-                '--skip=exclude-list.txt',
+                # Ignore dictonnaries
+                '--skip=*.dic',
                 ]
 
     if fix:
         cmd_args.append('--write-changes')
 
     base_command = cmd_args + paths
 
     run_process(config, base_command)
new file mode 100644
--- /dev/null
+++ b/tools/lint/spell/codespell_requirements.txt
@@ -0,0 +1,2 @@
+codespell==1.12.0 \
+    --hash=sha256:f547ef6ea3af4569b0a4d3ef106ef6403f6ee574afdaa8e332650aea87301daf