Get flake8 working as an aspect

This commit is contained in:
Reid 'arrdem' McKenzie 2021-08-30 00:29:43 -06:00
parent 3a1ffe6d6a
commit 81386229c6
12 changed files with 112 additions and 8 deletions

3
BUILD Normal file
View file

@ -0,0 +1,3 @@
package(default_visibility = ["//visibility:public"])
exports_files(["setup.cfg"])

View file

@ -18,7 +18,7 @@ index-servers = pypi
shitlist = pip,pkg_resources,setuptools
[flake8]
extend-ignore = E203,E501,F405,F403,E731
extend-ignore = E203,E501,F405,F403,E731,E306
[pypi]
repository = https://pypi.python.org/pypi

View file

@ -3,5 +3,8 @@ py_binary(
main = "__main__.py",
deps = [
py_requirement("autoflake"),
]
],
visibility = [
"//visibility:public"
],
)

View file

@ -1,7 +1,12 @@
exports_files(["flake8.cfg"], visibility=["//visibility:public"])
py_binary(
name = "flake8",
main = "__main__.py",
deps = [
py_requirement("flake8"),
],
visibility = [
"//visibility:public"
],
)

65
tools/flake8/flake8.bzl Normal file
View file

@ -0,0 +1,65 @@
"""Linting for Python using Aspects."""
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:
report = ctx.actions.declare_file(ctx.label.name + ".report")
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 = {
'_flake8': attr.label(default="//tools/flake8"),
'_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]),
},
)

View file

@ -3,5 +3,9 @@ py_binary(
main = "__main__.py",
deps = [
py_requirement("isort"),
]
],
visibility = [
"//visibility:public"
],
)

View file

@ -3,5 +3,8 @@ py_binary(
main = "__main__.py",
deps = [
py_requirement("openapi-spec-validator"),
]
],
visibility = [
"//visibility:public"
],
)

View file

@ -14,6 +14,9 @@ load("@rules_zapp//zapp:zapp.bzl",
"zapp_binary",
)
load("//tools/flake8:flake8.bzl",
"flake8",
)
def py_requirement(*args, **kwargs):
"""A re-export of requirement()"""
@ -208,6 +211,12 @@ def py_project(name=None,
],
)
# if lib_srcs:
# flake8(
# name = "flake8",
# deps = [lib_name],
# )
if main:
py_binary(
name=name,

View file

@ -8,5 +8,8 @@ py_binary(
py_requirement("sphinxcontrib-openapi"),
py_requirement("sphinxcontrib-programoutput"),
py_requirement("livereload"),
]
],
visibility = [
"//visibility:public"
],
)

View file

@ -3,5 +3,8 @@ py_binary(
main = "__main__.py",
deps = [
py_requirement("unify"),
]
],
visibility = [
"//visibility:public"
],
)

View file

@ -5,5 +5,8 @@ py_binary(
py_requirement("beautifulsoup4"),
py_requirement("click"),
py_requirement("lxml"),
]
],
visibility = [
"//visibility:public"
],
)

View file

@ -4,5 +4,8 @@ py_binary(
main = "__main__.py",
deps = [
py_requirement("yamllint"),
]
],
visibility = [
"//visibility:public"
],
)