Skip to content

Commit d74caaf

Browse files
committed
allow custom shebang
1 parent 9dfa3ab commit d74caaf

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

python/private/py_console_script_binary.bzl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ def py_console_script_binary(
5252
entry_points_txt = None,
5353
script = None,
5454
binary_rule = py_binary,
55+
shebang = None,
5556
**kwargs):
5657
"""Generate a py_binary for a console_script entry_point.
5758
@@ -68,6 +69,8 @@ def py_console_script_binary(
6869
binary_rule: {any}`rule callable`, The rule/macro to use to instantiate
6970
the target. It's expected to behave like {any}`py_binary`.
7071
Defaults to {any}`py_binary`.
72+
shebang: [`str`], The shebang to use for the entry point python file.
73+
Defaults to empty string.
7174
**kwargs: Extra parameters forwarded to `binary_rule`.
7275
"""
7376
main = "rules_python_entry_point_{}.py".format(name)
@@ -81,6 +84,7 @@ def py_console_script_binary(
8184
out = main,
8285
console_script = script,
8386
console_script_guess = name,
87+
shebang = shebang,
8488
visibility = ["//visibility:private"],
8589
)
8690

python/private/py_console_script_gen.bzl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ def _py_console_script_gen_impl(ctx):
4242
args = ctx.actions.args()
4343
args.add("--console-script", ctx.attr.console_script)
4444
args.add("--console-script-guess", ctx.attr.console_script_guess)
45+
args.add("--shebang", ctx.attr.shebang)
4546
args.add(entry_points_txt)
4647
args.add(ctx.outputs.out)
4748

@@ -81,6 +82,10 @@ py_console_script_gen = rule(
8182
doc = "Output file location.",
8283
mandatory = True,
8384
),
85+
"shebang": attr.string(
86+
doc = "The shebang to use for the entry point python file.",
87+
default = "",
88+
),
8489
"_tool": attr.label(
8590
default = ":py_console_script_gen_py",
8691
executable = True,

python/private/py_console_script_gen.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
_ENTRY_POINTS_TXT = "entry_points.txt"
4545

4646
_TEMPLATE = """\
47+
{shebang}
48+
4749
import sys
4850
4951
# See @rules_python//python/private:py_console_script_gen.py for explanation
@@ -87,13 +89,16 @@ def run(
8789
out: pathlib.Path,
8890
console_script: str,
8991
console_script_guess: str,
92+
shebang: str = "#!/usr/bin/env python3",
9093
):
9194
"""Run the generator
9295
9396
Args:
9497
entry_points: The entry_points.txt file to be parsed.
9598
out: The output file.
9699
console_script: The console_script entry in the entry_points.txt file.
100+
console_script_guess: The string used for guessing the console_script if it is not provided.
101+
shebang: The shebang to use for the entry point python file. Defaults to "#!/usr/bin/env python3".
97102
"""
98103
config = EntryPointsParser()
99104
config.read(entry_points)
@@ -136,6 +141,7 @@ def run(
136141
with open(out, "w") as f:
137142
f.write(
138143
_TEMPLATE.format(
144+
shebang=shebang,
139145
module=module,
140146
attr=attr,
141147
entry_point=entry_point,
@@ -154,6 +160,11 @@ def main():
154160
required=True,
155161
help="The string used for guessing the console_script if it is not provided.",
156162
)
163+
parser.add_argument(
164+
"--shebang",
165+
default="#!/usr/bin/env python3",
166+
help="The shebang to use for the entry point python file.",
167+
)
157168
parser.add_argument(
158169
"entry_points",
159170
metavar="ENTRY_POINTS_TXT",
@@ -173,6 +184,7 @@ def main():
173184
out=args.out,
174185
console_script=args.console_script,
175186
console_script_guess=args.console_script_guess,
187+
shebang=args.shebang,
176188
)
177189

178190

0 commit comments

Comments
 (0)