Bug 1387862 - Add initial support for ./mach lint -l yaml r=ahal r=dustin draft
authorJustin Wood <Callek@gmail.com>
Sun, 06 Aug 2017 13:23:48 -0400
changeset 644104 a71a4b6382e6a6d50f000436ecf01ad4de99ec2c
parent 644103 95ff969360640a86c843d19fb6dfbb972e819cdb
child 644105 3b3cdf18616cbdbc7da58c1f9a3fac5d9cb6a589
push id73304
push userCallek@gmail.com
push dateThu, 10 Aug 2017 12:43:29 +0000
reviewersahal, dustin
bugs1387862
milestone57.0a1
Bug 1387862 - Add initial support for ./mach lint -l yaml r=ahal r=dustin We should have CI Lint YAML files in the tree. MozReview-Commit-ID: HYVWXzNnnzG
tools/lint/yaml.yml
tools/lint/yamllint_/__init__.py
tools/lint/yamllint_/yamllint_requirements.txt
new file mode 100644
--- /dev/null
+++ b/tools/lint/yaml.yml
@@ -0,0 +1,5 @@
+yamllint:
+    description: YAML linter
+    extensions: ['yml', 'yaml']
+    type: external
+    payload: yamllint_:lint
copy from tools/lint/flake8_/__init__.py
copy to tools/lint/yamllint_/__init__.py
--- a/tools/lint/flake8_/__init__.py
+++ b/tools/lint/yamllint_/__init__.py
@@ -1,109 +1,81 @@
 # 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/.
 
-import json
+import re
 import os
 import signal
 import subprocess
 
 import which
 from mozprocess import ProcessHandlerMixin
 
 from mozlint import result
 
 
 here = os.path.abspath(os.path.dirname(__file__))
-FLAKE8_REQUIREMENTS_PATH = os.path.join(here, 'flake8_requirements.txt')
-
-FLAKE8_NOT_FOUND = """
-Could not find flake8! Install flake8 and try again.
-
-    $ pip install -U --require-hashes -r {}
-""".strip().format(FLAKE8_REQUIREMENTS_PATH)
+YAMLLINT_REQUIREMENTS_PATH = os.path.join(here, 'yamllint_requirements.txt')
 
 
-FLAKE8_INSTALL_ERROR = """
-Unable to install correct version of flake8
+YAMLLINT_INSTALL_ERROR = """
+Unable to install correct version of yamllint
 Try to install it manually with:
     $ pip install -U --require-hashes -r {}
-""".strip().format(FLAKE8_REQUIREMENTS_PATH)
+""".strip().format(YAMLLINT_REQUIREMENTS_PATH)
 
-LINE_OFFSETS = {
-    # continuation line under-indented for hanging indent
-    'E121': (-1, 2),
-    # continuation line missing indentation or outdented
-    'E122': (-1, 2),
-    # continuation line over-indented for hanging indent
-    'E126': (-1, 2),
-    # continuation line over-indented for visual indent
-    'E127': (-1, 2),
-    # continuation line under-indented for visual indent
-    'E128': (-1, 2),
-    # continuation line unaligned for hanging indend
-    'E131': (-1, 2),
-    # expected 1 blank line, found 0
-    'E301': (-1, 2),
-    # expected 2 blank lines, found 1
-    'E302': (-2, 3),
-}
-"""Maps a flake8 error to a lineoffset tuple.
-
-The offset is of the form (lineno_offset, num_lines) and is passed
-to the lineoffset property of `ResultContainer`.
-"""
+YAMLLINT_FORMAT_REGEX = re.compile(r'(.*):(.*):(.*): \[(error|warning)\] (.*) \((.*)\)$')
 
 results = []
 
 
-class Flake8Process(ProcessHandlerMixin):
+class YAMLLintProcess(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):
-        # Escape slashes otherwise JSON conversion will not work
-        line = line.replace('\\', '\\\\')
         try:
-            res = json.loads(line)
-        except ValueError:
-            print('Non JSON output from linter, will not be processed: {}'.format(line))
+            match = YAMLLINT_FORMAT_REGEX.match(line)
+            abspath, line, col, level, message, code = match.groups()
+        except AttributeError:
+            print('Unable to match yaml regex against output: {}'.format(line))
             return
 
-        if 'code' in res:
-            if res['code'].startswith('W'):
-                res['level'] = 'warning'
-
-            if res['code'] in LINE_OFFSETS:
-                res['lineoffset'] = LINE_OFFSETS[res['code']]
+        res = {'path': os.path.relpath(abspath, self.config['root']),
+               'message': message,
+               'level': level,
+               'lineno': line,
+               'column': col,
+               'rule': code,
+               }
 
         results.append(result.from_config(self.config, **res))
 
     def run(self, *args, **kwargs):
-        # flake8 seems to handle SIGINT poorly. Handle it here instead
+        # protect against poor SIGINT handling. Handle it here instead
         # so we can kill the process without a cryptic traceback.
         orig = signal.signal(signal.SIGINT, signal.SIG_IGN)
         ProcessHandlerMixin.run(self, *args, **kwargs)
         signal.signal(signal.SIGINT, orig)
 
 
-def get_flake8_binary():
+def get_yamllint_binary():
     """
-    Returns the path of the first flake8 binary available
+    Returns the path of the first yamllint binary available
     if not found returns None
     """
-    binary = os.environ.get('FLAKE8')
+    binary = os.environ.get('YAMLLINT')
     if binary:
         return binary
 
     try:
-        return which.which('flake8')
+        return which.which('yamllint')
     except which.WhichError:
         return None
 
 
 def _run_pip(*args):
     """
     Helper function that runs pip with subprocess
     """
@@ -111,66 +83,72 @@ def _run_pip(*args):
         subprocess.check_output(['pip'] + list(args),
                                 stderr=subprocess.STDOUT)
         return True
     except subprocess.CalledProcessError as e:
         print(e.output)
         return False
 
 
-def reinstall_flake8():
+def reinstall_yamllint():
     """
-    Try to install flake8 at the target version, returns True on success
+    Try to install yamllint at the target version, returns True on success
     otherwise prints the otuput of the pip command and returns False
     """
     if _run_pip('install', '-U',
                 '--require-hashes', '-r',
-                FLAKE8_REQUIREMENTS_PATH):
+                YAMLLINT_REQUIREMENTS_PATH):
         return True
 
     return False
 
 
 def run_process(config, cmd):
-    proc = Flake8Process(config, cmd)
+    proc = YAMLLintProcess(config, cmd)
     proc.run()
     try:
         proc.wait()
     except KeyboardInterrupt:
         proc.kill()
 
 
+def gen_yamllint_args(cmdargs, paths=None, conf_file=None):
+    args = cmdargs[:]
+    if isinstance(paths, basestring):
+        paths = [paths]
+    if conf_file:
+        return args + ['-c', conf_file] + paths
+    return args + paths
+
+
 def lint(files, config, **lintargs):
 
-    if not reinstall_flake8():
-        print(FLAKE8_INSTALL_ERROR)
+    if not reinstall_yamllint():
+        print(YAMLLINT_INSTALL_ERROR)
         return 1
 
-    binary = get_flake8_binary()
+    binary = get_yamllint_binary()
 
     cmdargs = [
         binary,
-        '--format', '{"path":"%(path)s","lineno":%(row)s,'
-                    '"column":%(col)s,"rule":"%(code)s","message":"%(text)s"}',
+        '-f', 'parsable'
     ]
 
-    # Run any paths with a .flake8 file in the directory separately so
-    # it gets picked up. This means only .flake8 files that live in
+    config = config.copy()
+    config['root'] = lintargs['root']
+
+    # Run any paths with a .yamllint file in the directory separately so
+    # it gets picked up. This means only .yamllint files that live in
     # directories that are explicitly included will be considered.
-    # See bug 1277851
     no_config = []
     for f in files:
-        if not os.path.isfile(os.path.join(f, '.flake8')):
+        yamllint_config = os.path.join(f, '.yamllint')
+        if not os.path.isfile(yamllint_config):
             no_config.append(f)
             continue
-        run_process(config, cmdargs+[f])
-
-    # XXX For some reason passing in --exclude results in flake8 not using
-    # the local .flake8 file. So for now only pass in --exclude if there
-    # is no local config.
-    exclude = lintargs.get('exclude')
-    if exclude:
-        cmdargs += ['--exclude', ','.join(lintargs['exclude'])]
+        run_process(config,
+                    gen_yamllint_args(cmdargs, conf_file=yamllint_config, paths=f))
 
     if no_config:
-        run_process(config, cmdargs+no_config)
+        run_process(config,
+                    gen_yamllint_args(cmdargs, paths=no_config))
 
     return results
new file mode 100644
--- /dev/null
+++ b/tools/lint/yamllint_/yamllint_requirements.txt
@@ -0,0 +1,21 @@
+yamllint==1.8.1 \
+    --hash=sha256:806b21828ca92fd6e9f20d8eccfa53035e38041854bb4d1b666f271a7788dbcf \
+    --hash=sha256:048743567ca9511e19222233ebb53795586a2cede07b79e801577e0a9b4f173c
+PyYAML==3.12 \
+    --hash=sha256:3262c96a1ca437e7e4763e2843746588a965426550f3797a79fca9c6199c431f \
+    --hash=sha256:16b20e970597e051997d90dc2cddc713a2876c47e3d92d59ee198700c5427736 \
+    --hash=sha256:e863072cdf4c72eebf179342c94e6989c67185842d9997960b3e69290b2fa269 \
+    --hash=sha256:bc6bced57f826ca7cb5125a10b23fd0f2fff3b7c4701d64c439a300ce665fff8 \
+    --hash=sha256:c01b880ec30b5a6e6aa67b09a2fe3fb30473008c85cd6a67359a1b15ed6d83a4 \
+    --hash=sha256:827dc04b8fa7d07c44de11fabbc888e627fa8293b695e0f99cb544fdfa1bf0d1 \
+    --hash=sha256:592766c6303207a20efc445587778322d7f73b161bd994f227adaa341ba212ab \
+    --hash=sha256:592766c6303207a20efc445587778322d7f73b161bd994f227adaa341ba212ab \
+    --hash=sha256:5f84523c076ad14ff5e6c037fe1c89a7f73a3e04cf0377cb4d017014976433f3 \
+    --hash=sha256:0c507b7f74b3d2dd4d1322ec8a94794927305ab4cebbe89cc47fe5e81541e6e8 \
+    --hash=sha256:b4c423ab23291d3945ac61346feeb9a0dc4184999ede5e7c43e1ffb975130ae6 \
+    --hash=sha256:ca233c64c6e40eaa6c66ef97058cdc80e8d0157a443655baa1b2966e812807ca \
+    --hash=sha256:4474f8ea030b5127225b8894d626bb66c01cda098d47a2b0d3429b6700af9fd8 \
+    --hash=sha256:326420cbb492172dec84b0f65c80942de6cedb5233c413dd824483989c000608 \
+    --hash=sha256:5ac82e411044fb129bae5cfbeb3ba626acb2af31a8d17d175004b70862a741a7
+pathspec==0.5.3 \
+    --hash=sha256:54478a66a360f4ebe4499c9235e4206fca5dec837b8e272d1ce37e0a626cc64d