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/.
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)\] (.*) \((.*)\)$"
25 class YAMLLintProcess(LintProcess
):
26 def process_line(self
, line
):
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
))
35 "path": os
.path
.relpath(str(abspath
), self
.config
["root"]),
36 "message": str(message
),
43 results
.append(result
.from_config(self
.config
, **res
))
46 def get_yamllint_version():
47 from yamllint
import APP_VERSION
52 def run_process(config
, cmd
):
53 proc
= YAMLLintProcess(config
, cmd
)
57 except KeyboardInterrupt:
61 def gen_yamllint_args(cmdargs
, paths
=None, conf_file
=None):
63 if isinstance(paths
, str):
65 if conf_file
and conf_file
!= "default":
66 return args
+ ["-c", conf_file
] + paths
70 def lint(files
, config
, **lintargs
):
73 log
.debug("Version: {}".format(get_yamllint_version()))
77 os
.path
.join(topsrcdir
, "mach"),
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)
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():
100 config
, gen_yamllint_args(cmdargs
, conf_file
=conf_file
, paths
=paths
)