Skip to content

Commit ac5d58c

Browse files
committed
simplify design a bit
1 parent 66d216d commit ac5d58c

File tree

4 files changed

+19
-48
lines changed

4 files changed

+19
-48
lines changed

python/bin/BUILD.bazel

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ label_flag(
2727

2828
generate_repl_main(
2929
name = "repl_py",
30-
src = "repl_stub.py",
30+
src = "repl_stub",
3131
out = "repl.py",
3232
)
3333

@@ -50,9 +50,13 @@ py_binary(
5050
name = "repl",
5151
srcs = [":repl.py"],
5252
visibility = ["//visibility:public"],
53+
data = [
54+
":repl_stub",
55+
],
5356
deps = [
5457
":repl_dep",
5558
":repl_stub_dep",
59+
"//python/runfiles",
5660
],
5761
)
5862

python/private/repl.bzl

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
11
def _generate_repl_main_impl(ctx):
2-
args = ctx.actions.args()
3-
args.add_all([
4-
ctx.file._template,
5-
ctx.file.src,
6-
ctx.outputs.out,
7-
])
2+
stub_repo = ctx.attr.src.label.repo_name or ctx.workspace_name
3+
stub_path = "/".join([stub_repo, ctx.file.src.short_path])
84

9-
ctx.actions.run(
10-
executable = ctx.executable._generator,
11-
inputs = [
12-
ctx.file._template,
13-
ctx.file.src,
14-
],
15-
outputs = [ctx.outputs.out],
16-
arguments = [args],
5+
ctx.actions.expand_template(
6+
template = ctx.file._template,
7+
output = ctx.outputs.out,
8+
substitutions = {
9+
"%stub_path%": stub_path,
10+
},
1711
)
1812

1913
generate_repl_main = rule(

python/private/repl_main_generator.py

Lines changed: 0 additions & 30 deletions
This file was deleted.

python/private/repl_template.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import os
2+
import runpy
23
from pathlib import Path
34

5+
from python.runfiles import runfiles
46

5-
def repl_stub():
6-
pass # %REPLACE_WHOLE_LINE_WITH_STUB%
7+
8+
STUB_PATH = "%stub_path%"
79

810

911
def start_repl():
@@ -19,7 +21,8 @@ def start_repl():
1921
compiled_code = compile(source_code, filename=startup_file, mode="exec")
2022
eval(compiled_code, {})
2123

22-
repl_stub()
24+
bazel_runfiles = runfiles.Create()
25+
runpy.run_path(bazel_runfiles.Rlocation(STUB_PATH), run_name="__main__")
2326

2427

2528
if __name__ == "__main__":

0 commit comments

Comments
 (0)