Backed out 2 changesets (bug 1943998) for causing wd failures @ phases.py CLOSED...
[gecko.git] / tools / lint / yamllint_ / __init__.py
blobd0087fbbc40a03e43e2df62d5615c412076c9bf2
1 # This Source Code Form is subject to the terms of the Mozilla Public
2 # License, v. 2.0. If a copy of the MPL was not distributed with this
3 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 import os
6 import re
7 import sys
8 from collections import defaultdict
10 from mozbuild.base import MozbuildObject
12 topsrcdir = MozbuildObject.from_environment().topsrcdir
14 from mozlint import result
15 from mozlint.pathutils import get_ancestors_by_name
16 from mozlint.util.implementation import LintProcess
18 YAMLLINT_FORMAT_REGEX = re.compile(
19 r"(.*):(.*):(.*): \[(error|warning)\] (.*) \((.*)\)$"
22 results = []
25 class YAMLLintProcess(LintProcess):
26 def process_line(self, line):
27 try:
28 match = YAMLLINT_FORMAT_REGEX.match(line)
29 abspath, line, col, level, message, code = match.groups()
30 except AttributeError:
31 print("Unable to match yaml regex against output: {}".format(line))
32 return
34 res = {
35 "path": os.path.relpath(str(abspath), self.config["root"]),
36 "message": str(message),
37 "level": "error",
38 "lineno": line,
39 "column": col,
40 "rule": code,
43 results.append(result.from_config(self.config, **res))
46 def get_yamllint_version():
47 from yamllint import APP_VERSION
49 return APP_VERSION
52 def run_process(config, cmd):
53 proc = YAMLLintProcess(config, cmd)
54 proc.run()
55 try:
56 proc.wait()
57 except KeyboardInterrupt:
58 proc.kill()
61 def gen_yamllint_args(cmdargs, paths=None, conf_file=None):
62 args = cmdargs[:]
63 if isinstance(paths, str):
64 paths = [paths]
65 if conf_file and conf_file != "default":
66 return args + ["-c", conf_file] + paths
67 return args + paths
70 def lint(files, config, **lintargs):
71 log = lintargs["log"]
73 log.debug("Version: {}".format(get_yamllint_version()))
75 cmdargs = [
76 sys.executable,
77 os.path.join(topsrcdir, "mach"),
78 "python",
79 "--",
80 "-m",
81 "yamllint",
82 "-f",
83 "parsable",
85 log.debug("Command: {}".format(" ".join(cmdargs)))
87 config = config.copy()
88 config["root"] = lintargs["root"]
90 # Run any paths with a .yamllint file in the directory separately so
91 # it gets picked up. This means only .yamllint files that live in
92 # directories that are explicitly included will be considered.
93 paths_by_config = defaultdict(list)
94 for f in files:
95 conf_files = get_ancestors_by_name(".yamllint", f, config["root"])
96 paths_by_config[conf_files[0] if conf_files else "default"].append(f)
98 for conf_file, paths in paths_by_config.items():
99 run_process(
100 config, gen_yamllint_args(cmdargs, conf_file=conf_file, paths=paths)
103 return results