-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpytype.bzl
66 lines (53 loc) · 1.96 KB
/
pytype.bzl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# cribbed from
# https://github.com/bazelbuild/examples/tree/main/rules/test_rule/line_length.bzl
# some inspiration from
# https://github.com/bazel-contrib/bazel-mypy-integration
# TODO this effectively leaves the pytype output directory in the
# bazel sandbox directory so it starts from scratch every time. I
# almost got this working running pytype-single directly and making
# the .pyi an output but got stuck on non-hermetic dependencies
# (site-packages, etc), I think it's a tiny patch to pytype to support
# this and plan to take it up with them
def _run_pytype(f):
return 'pytype -v2 {f} || err=1'.format(f=f)
def _pytype_lib_test_impl(ctx):
srcs = ctx.files.srcs
runfiles = ctx.runfiles(files = srcs)
transitive_runfiles = []
for runfiles_attr in (
ctx.attr.srcs,
ctx.attr.deps):
for target in runfiles_attr:
transitive_runfiles.append(target[DefaultInfo].default_runfiles)
runfiles = runfiles.merge_all(transitive_runfiles)
cmds = [ _run_pytype(f.path) for f in srcs ]
script = "\n".join(["err=0"] + cmds + ["exit $err"])
ctx.actions.write(
output = ctx.outputs.executable,
content = script,
)
return [DefaultInfo(runfiles = runfiles)]
_pytype_lib_test = rule(
implementation = _pytype_lib_test_impl,
attrs = {
"deps": attr.label_list(),
"srcs": attr.label_list(allow_files = [".py"]),
},
test = True,
)
def pytype_lib_test(name, **kwargs):
if 'tags' not in kwargs:
kwargs['tags'] = []
kwargs['tags'].append('pytype_test')
_pytype_lib_test(name=name, **kwargs)
def pytype_library(name, srcs, deps=None, data=None, **kwargs):
native.py_library(name=name,
srcs=srcs,
deps=deps,
data=data,
**kwargs)
# don't propagate data
pytype_lib_test(name=(name + '_pytype_test'),
srcs=srcs,
deps=deps,
**kwargs)