|
1 | 1 | """Implementation of the rules to expose a REPL.""" |
2 | 2 |
|
3 | | -load("//python:py_binary.bzl", "py_binary") |
| 3 | +load("//python:py_binary.bzl", _py_binary="py_binary") |
4 | 4 |
|
5 | 5 | def _generate_repl_main_impl(ctx): |
6 | 6 | stub_repo = ctx.attr.stub.label.repo_name or ctx.workspace_name |
7 | 7 | stub_path = "/".join([stub_repo, ctx.file.stub.short_path]) |
8 | 8 |
|
| 9 | + # Point the generated main file at the stub. |
9 | 10 | ctx.actions.expand_template( |
10 | 11 | template = ctx.file._template, |
11 | 12 | output = ctx.outputs.out, |
@@ -33,17 +34,40 @@ _generate_repl_main = rule( |
33 | 34 | doc = "The template to use for generating `out`.", |
34 | 35 | ), |
35 | 36 | }, |
| 37 | + doc = """\ |
| 38 | +Generates a "main" script for a py_binary target that starts a Python REPL. |
| 39 | +
|
| 40 | +The template is designed to take care of the majority of the logic. The user |
| 41 | +customizes the exact shell that will be started via the stub. The stub is a |
| 42 | +simple shell script that imports the desired shell and then executes it. |
| 43 | +""", |
36 | 44 | ) |
37 | 45 |
|
38 | 46 | def py_repl_binary(name, stub, deps = [], data = [], **kwargs): |
39 | | - """A |
| 47 | + """A py_binary target that executes a REPL when run. |
| 48 | +
|
| 49 | + The stub is the script that ultimately decides which shell the REPL will run. |
| 50 | + It can be as simple as this: |
| 51 | +
|
| 52 | + import code |
| 53 | + code.interact() |
| 54 | +
|
| 55 | + Or it can load something like IPython instead. |
| 56 | +
|
| 57 | + Args: |
| 58 | + name: Name of the generated py_binary target. |
| 59 | + stub: The script that invokes the shell. |
| 60 | + deps: The dependencies of the py_binary. |
| 61 | + data: The runtime dependencies of the py_binary. |
| 62 | + **kwargs: Forwarded to the py_binary. |
| 63 | + """ |
40 | 64 | _generate_repl_main( |
41 | 65 | name = "%s_py" % name, |
42 | 66 | stub = stub, |
43 | 67 | out = "%s.py" % name, |
44 | 68 | ) |
45 | 69 |
|
46 | | - py_binary( |
| 70 | + _py_binary( |
47 | 71 | name = name, |
48 | 72 | srcs = [ |
49 | 73 | ":%s.py" % name, |
|
0 commit comments