Bug 1845017 - Disable the TestPHCExhaustion test r=glandium
[gecko.git] / tools / lint / yamllint_ / __init__.py
blob2244fabd3fc70556136e2adb6767a5a646028c24
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("(.*):(.*):(.*): \[(error|warning)\] (.*) \((.*)\)$")
20 results = []
23 class YAMLLintProcess(LintProcess):
24 def process_line(self, line):
25 try:
26 match = YAMLLINT_FORMAT_REGEX.match(line)
27 abspath, line, col, level, message, code = match.groups()
28 except AttributeError:
29 print("Unable to match yaml regex against output: {}".format(line))
30 return
32 res = {
33 "path": os.path.relpath(str(abspath), self.config["root"]),
34 "message": str(message),
35 "level": "error",
36 "lineno": line,
37 "column": col,
38 "rule": code,
41 results.append(result.from_config(self.config, **res))
44 def get_yamllint_version():
45 from yamllint import APP_VERSION
47 return APP_VERSION
50 def run_process(config, cmd):
51 proc = YAMLLintProcess(config, cmd)
52 proc.run()
53 try:
54 proc.wait()
55 except KeyboardInterrupt:
56 proc.kill()
59 def gen_yamllint_args(cmdargs, paths=None, conf_file=None):
60 args = cmdargs[:]
61 if isinstance(paths, str):
62 paths = [paths]
63 if conf_file and conf_file != "default":
64 return args + ["-c", conf_file] + paths
65 return args + paths
68 def lint(files, config, **lintargs):
69 log = lintargs["log"]
71 log.debug("Version: {}".format(get_yamllint_version()))
73 cmdargs = [
74 sys.executable,
75 os.path.join(topsrcdir, "mach"),
76 "python",
77 "--",
78 "-m",
79 "yamllint",
80 "-f",
81 "parsable",
83 log.debug("Command: {}".format(" ".join(cmdargs)))
85 config = config.copy()
86 config["root"] = lintargs["root"]
88 # Run any paths with a .yamllint file in the directory separately so
89 # it gets picked up. This means only .yamllint files that live in
90 # directories that are explicitly included will be considered.
91 paths_by_config = defaultdict(list)
92 for f in files:
93 conf_files = get_ancestors_by_name(".yamllint", f, config["root"])
94 paths_by_config[conf_files[0] if conf_files else "default"].append(f)
96 for conf_file, paths in paths_by_config.items():
97 run_process(
98 config, gen_yamllint_args(cmdargs, conf_file=conf_file, paths=paths)
101 return results