|
13 | 13 | # limitations under the License.
|
14 | 14 | """Implementation of py_runtime rule."""
|
15 | 15 |
|
| 16 | +def _py_runtime_impl(ctx): |
| 17 | + _ = ctx # @unused |
| 18 | + fail("not implemented") |
| 19 | + |
16 | 20 | # Bind to the name "py_runtime" to preserve the kind/rule_class it shows up
|
17 | 21 | # as elsewhere.
|
18 |
| -py_runtime = _builtins.native.py_runtime |
| 22 | +py_runtime = rule( |
| 23 | + implementation = _py_runtime_impl, |
| 24 | + doc = """ |
| 25 | +Represents a Python runtime used to execute Python code. |
| 26 | +
|
| 27 | +A `py_runtime` target can represent either a *platform runtime* or an *in-build |
| 28 | +runtime*. A platform runtime accesses a system-installed interpreter at a known |
| 29 | +path, whereas an in-build runtime points to an executable target that acts as |
| 30 | +the interpreter. In both cases, an "interpreter" means any executable binary or |
| 31 | +wrapper script that is capable of running a Python script passed on the command |
| 32 | +line, following the same conventions as the standard CPython interpreter. |
| 33 | +
|
| 34 | +A platform runtime is by its nature non-hermetic. It imposes a requirement on |
| 35 | +the target platform to have an interpreter located at a specific path. An |
| 36 | +in-build runtime may or may not be hermetic, depending on whether it points to |
| 37 | +a checked-in interpreter or a wrapper script that accesses the system |
| 38 | +interpreter. |
| 39 | +
|
| 40 | +# Example |
| 41 | +
|
| 42 | +``` |
| 43 | +py_runtime( |
| 44 | + name = "python-2.7.12", |
| 45 | + files = glob(["python-2.7.12/**"]), |
| 46 | + interpreter = "python-2.7.12/bin/python", |
| 47 | +) |
| 48 | +
|
| 49 | +py_runtime( |
| 50 | + name = "python-3.6.0", |
| 51 | + interpreter_path = "/opt/pyenv/versions/3.6.0/bin/python", |
| 52 | +) |
| 53 | +``` |
| 54 | +""", |
| 55 | + fragments = ["py"], |
| 56 | + attrs = { |
| 57 | + "files": attr.label_list( |
| 58 | + allow_files = True, |
| 59 | + doc = """ |
| 60 | +For an in-build runtime, this is the set of files comprising this runtime. |
| 61 | +These files will be added to the runfiles of Python binaries that use this |
| 62 | +runtime. For a platform runtime this attribute must not be set. |
| 63 | +""", |
| 64 | + ), |
| 65 | + "interpreter": attr.label( |
| 66 | + allow_single_file = True, |
| 67 | + doc = """ |
| 68 | +For an in-build runtime, this is the target to invoke as the interpreter. For a |
| 69 | +platform runtime this attribute must not be set. |
| 70 | +""", |
| 71 | + ), |
| 72 | + "interpreter_path": attr.string(doc = """ |
| 73 | +For a platform runtime, this is the absolute path of a Python interpreter on |
| 74 | +the target platform. For an in-build runtime this attribute must not be set. |
| 75 | +"""), |
| 76 | + "coverage_tool": attr.label( |
| 77 | + allow_files = False, |
| 78 | + doc = """ |
| 79 | +This is a target to use for collecting code coverage information from `py_binary` |
| 80 | +and `py_test` targets. |
| 81 | +
|
| 82 | +If set, the target must either produce a single file or be an executable target. |
| 83 | +The path to the single file, or the executable if the target is executable, |
| 84 | +determines the entry point for the python coverage tool. The target and its |
| 85 | +runfiles will be added to the runfiles when coverage is enabled. |
| 86 | +
|
| 87 | +The entry point for the tool must be loadable by a Python interpreter (e.g. a |
| 88 | +`.py` or `.pyc` file). It must accept the command line arguments |
| 89 | +of coverage.py (https://coverage.readthedocs.io), at least including |
| 90 | +the `run` and `lcov` subcommands. |
| 91 | +""", |
| 92 | + ), |
| 93 | + "python_version": attr.string( |
| 94 | + values = ["PY2", "PY3", "_INTERNAL_SENTINEL"], |
| 95 | + doc = """ |
| 96 | +Whether this runtime is for Python major version 2 or 3. Valid values are `"PY2"` |
| 97 | +and `"PY3"`. |
| 98 | +
|
| 99 | +The default value is controlled by the `--incompatible_py3_is_default` flag. |
| 100 | +However, in the future this attribute will be mandatory and have no default |
| 101 | +value. |
| 102 | + """, |
| 103 | + ), |
| 104 | + "stub_shebang": attr.string( |
| 105 | + # TODO(b/254866025): Have PyRuntimeInfo and this use a shared |
| 106 | + # constant |
| 107 | + default = "#!/usr/bin/env python3", |
| 108 | + doc = """ |
| 109 | +"Shebang" expression prepended to the bootstrapping Python stub script |
| 110 | +used when executing `py_binary` targets. |
| 111 | +
|
| 112 | +See https://github.com/bazelbuild/bazel/issues/8685 for |
| 113 | +motivation. |
| 114 | +
|
| 115 | +Does not apply to Windows. |
| 116 | +""", |
| 117 | + ), |
| 118 | + }, |
| 119 | +) |
0 commit comments