-
-
Notifications
You must be signed in to change notification settings - Fork 97
Expand file tree
/
Copy pathdefs.bzl
More file actions
78 lines (68 loc) · 2.35 KB
/
Copy pathdefs.bzl
File metadata and controls
78 lines (68 loc) · 2.35 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
"""Wrapper macro that injects debugpy as a wrapper entrypoint."""
load("@aspect_rules_py//py:defs.bzl", _py_binary = "py_binary")
def _debug_main_impl(ctx):
"""Generate a debugpy wrapper that runs the real entrypoint."""
main_file = ctx.file.main.short_path
if main_file.endswith(".py"):
main_file = main_file[:-3]
module = main_file.replace("/", ".")
ctx.actions.expand_template(
template = ctx.file._template,
output = ctx.outputs.out,
substitutions = {
"%%MAIN_MODULE%%": module,
},
)
return [DefaultInfo(files = depset([ctx.outputs.out]))]
_debug_main = rule(
implementation = _debug_main_impl,
attrs = {
"main": attr.label(
allow_single_file = [".py"],
mandatory = True,
doc = "The real application entrypoint to wrap.",
),
"_template": attr.label(
default = ":_debug_main.py.tpl",
allow_single_file = True,
),
"out": attr.output(mandatory = True),
},
)
def py_debuggable_binary(name, srcs = [], deps = [], debug_deps = [], **kwargs):
"""A py_binary that wraps the entrypoint with debugpy in debug mode.
In debug mode (the default), the binary's main is replaced with a
debugpy wrapper that starts a DAP listener before importing the real
application. The IDE can attach to the listener before any app code
runs.
In prod mode, debug_deps and the debug wrapper are stripped.
Args:
name: Target name.
srcs: Source files.
deps: Production dependencies — always included.
debug_deps: Debug-only dependencies (e.g. debugpy) — included unless mode=prod.
**kwargs: Forwarded to py_binary.
"""
main = kwargs.pop("main", None)
debug_main_name = "_{}_debug_main".format(name)
_debug_main(
name = debug_main_name,
main = main,
out = debug_main_name + ".py",
)
_py_binary(
name = name,
srcs = srcs + select({
"//:is_prod": [],
"//conditions:default": [debug_main_name],
}),
main = select({
"//:is_prod": main,
"//conditions:default": debug_main_name + ".py",
}),
deps = deps + select({
"//:is_prod": [],
"//conditions:default": debug_deps,
}),
**kwargs
)