source/tools/flake8/flake8.bzl

72 lines
2 KiB
Python
Raw Normal View History

2021-08-30 06:29:43 +00:00
"""Linting for Python using Aspects."""
2021-08-30 06:40:02 +00:00
# Hacked up from https://github.com/bazelbuild/rules_rust/blob/main/rust/private/clippy.bzl
#
# Usage:
# bazel build --aspects="//tools/flake8:flake8.bzl%flake8_aspect" --output_groups=flake8_checks <target|pattern>
#
# Note that the build directive can be inserted to .bazelrc to make it part of the default behavior
2021-08-30 06:29:43 +00:00
def _flake8_aspect_impl(target, ctx):
if hasattr(ctx.rule.attr, 'srcs'):
flake8 = ctx.attr._flake8.files_to_run
config = ctx.attr._config.files.to_list()[0]
files = []
for src in ctx.rule.attr.srcs:
for f in src.files.to_list():
if f.extension == "py":
files.append(f)
if files:
2021-09-03 04:10:12 +00:00
report = ctx.actions.declare_file(ctx.label.name + ".flake.report")
2021-08-30 06:29:43 +00:00
else:
return []
args = ["--config", config.path, "--tee", "--output-file", report.path]
for f in files:
args.append(f.path)
ctx.actions.run(
executable = flake8,
inputs = files,
tools = ctx.attr._config.files.to_list() + ctx.attr._flake8.files.to_list(),
arguments = args,
outputs = [report],
mnemonic = "Flake8",
)
return [
OutputGroupInfo(flake8_checks = depset([report]))
]
return []
flake8_aspect = aspect(
implementation = _flake8_aspect_impl,
attr_aspects = ['deps'],
attrs = {
2021-08-30 06:40:02 +00:00
'_flake8': attr.label(default=":flake8"),
2021-08-30 06:29:43 +00:00
'_config': attr.label(
default="//:setup.cfg",
executable=False,
allow_single_file=True
),
}
)
def _flake8_rule_impl(ctx):
ready_targets = [dep for dep in ctx.attr.deps if "flake8_checks" in dir(dep[OutputGroupInfo])]
files = depset([], transitive = [dep[OutputGroupInfo].flake8_checks for dep in ready_targets])
return [DefaultInfo(files = files)]
flake8 = rule(
implementation = _flake8_rule_impl,
attrs = {
'deps' : attr.label_list(aspects = [flake8_aspect]),
},
)