|
| 1 | +"""Implementation of the rules to expose a REPL.""" |
| 2 | + |
| 3 | +load("//python:py_binary.bzl", _py_binary = "py_binary") |
| 4 | + |
| 5 | +def _generate_repl_main_impl(ctx): |
| 6 | + stub_repo = ctx.attr.stub.label.repo_name or ctx.workspace_name |
| 7 | + stub_path = "/".join([stub_repo, ctx.file.stub.short_path]) |
| 8 | + |
| 9 | + out = ctx.actions.declare_file(ctx.label.name + ".py") |
| 10 | + |
| 11 | + # Point the generated main file at the stub. |
| 12 | + ctx.actions.expand_template( |
| 13 | + template = ctx.file._template, |
| 14 | + output = out, |
| 15 | + substitutions = { |
| 16 | + "%stub_path%": stub_path, |
| 17 | + }, |
| 18 | + ) |
| 19 | + |
| 20 | + return [DefaultInfo(files = depset([out]))] |
| 21 | + |
| 22 | +_generate_repl_main = rule( |
| 23 | + implementation = _generate_repl_main_impl, |
| 24 | + attrs = { |
| 25 | + "stub": attr.label( |
| 26 | + mandatory = True, |
| 27 | + allow_single_file = True, |
| 28 | + doc = ("The stub responsible for actually invoking the final shell. " + |
| 29 | + "See the \"Customizing the REPL\" docs for details."), |
| 30 | + ), |
| 31 | + "_template": attr.label( |
| 32 | + default = "//python/private:repl_template.py", |
| 33 | + allow_single_file = True, |
| 34 | + doc = "The template to use for generating `out`.", |
| 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 | +
|
| 44 | +The target's name is used for the output filename (with a .py extension). |
| 45 | +""", |
| 46 | +) |
| 47 | + |
| 48 | +def py_repl_binary(name, stub, deps = [], data = [], **kwargs): |
| 49 | + """A py_binary target that executes a REPL when run. |
| 50 | +
|
| 51 | + The stub is the script that ultimately decides which shell the REPL will run. |
| 52 | + It can be as simple as this: |
| 53 | +
|
| 54 | + import code |
| 55 | + code.interact() |
| 56 | +
|
| 57 | + Or it can load something like IPython instead. |
| 58 | +
|
| 59 | + Args: |
| 60 | + name: Name of the generated py_binary target. |
| 61 | + stub: The script that invokes the shell. |
| 62 | + deps: The dependencies of the py_binary. |
| 63 | + data: The runtime dependencies of the py_binary. |
| 64 | + **kwargs: Forwarded to the py_binary. |
| 65 | + """ |
| 66 | + _generate_repl_main( |
| 67 | + name = "%s_py" % name, |
| 68 | + stub = stub, |
| 69 | + ) |
| 70 | + |
| 71 | + _py_binary( |
| 72 | + name = name, |
| 73 | + srcs = [ |
| 74 | + ":%s_py" % name, |
| 75 | + ], |
| 76 | + main = "%s_py.py" % name, |
| 77 | + data = data + [ |
| 78 | + stub, |
| 79 | + ], |
| 80 | + deps = deps + [ |
| 81 | + "//python/runfiles", |
| 82 | + ], |
| 83 | + **kwargs |
| 84 | + ) |
0 commit comments