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("(.*):(.*):(.*): \[(error|warning)\] (.*) \((.*)\)$")
23 class YAMLLintProcess(LintProcess
):
24 def process_line(self
, line
):
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
))
33 "path": os
.path
.relpath(str(abspath
), self
.config
["root"]),
34 "message": str(message
),
41 results
.append(result
.from_config(self
.config
, **res
))
44 def get_yamllint_version():
45 from yamllint
import APP_VERSION
50 def run_process(config
, cmd
):
51 proc
= YAMLLintProcess(config
, cmd
)
55 except KeyboardInterrupt:
59 def gen_yamllint_args(cmdargs
, paths
=None, conf_file
=None):
61 if isinstance(paths
, str):
63 if conf_file
and conf_file
!= "default":
64 return args
+ ["-c", conf_file
] + paths
68 def lint(files
, config
, **lintargs
):
71 log
.debug("Version: {}".format(get_yamllint_version()))
75 os
.path
.join(topsrcdir
, "mach"),
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)
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():
98 config
, gen_yamllint_args(cmdargs
, conf_file
=conf_file
, paths
=paths
)