|
| 1 | +"""Internal setup to help the runtime_env toolchain.""" |
| 2 | + |
| 3 | +load("//python/private:repo_utils.bzl", "repo_utils") |
| 4 | + |
| 5 | +def _runtime_env_repo_impl(rctx): |
| 6 | + pyenv = repo_utils.which_unchecked(rctx, "pyenv").binary |
| 7 | + if pyenv != None: |
| 8 | + pyenv_version_file = repo_utils.execute_checked( |
| 9 | + rctx, |
| 10 | + op = "GetPyenvVersionFile", |
| 11 | + arguments = [pyenv, "version-file"], |
| 12 | + ).stdout.strip() |
| 13 | + |
| 14 | + # When pyenv is used, the version file is what decided the |
| 15 | + # version used. Watch it so we compute the correct value if the |
| 16 | + # user changes it. |
| 17 | + rctx.watch(pyenv_version_file) |
| 18 | + |
| 19 | + version = repo_utils.execute_checked( |
| 20 | + rctx, |
| 21 | + op = "GetPythonVersion", |
| 22 | + arguments = [ |
| 23 | + "python3", |
| 24 | + "-I", |
| 25 | + "-c", |
| 26 | + """import sys; print(f"{sys.version_info.major}.{sys.version_info.minor}")""", |
| 27 | + ], |
| 28 | + environment = { |
| 29 | + # Prevent the user's current shell from influencing the result. |
| 30 | + # This envvar won't be present when a test is run. |
| 31 | + # NOTE: This should be None, but Bazel 7 doesn't support None |
| 32 | + # values. Thankfully, pyenv treats empty string the same as missing. |
| 33 | + "PYENV_VERSION": "", |
| 34 | + }, |
| 35 | + ).stdout.strip() |
| 36 | + rctx.file("info.bzl", "PYTHON_VERSION = '{}'\n".format(version)) |
| 37 | + rctx.file("BUILD.bazel", "") |
| 38 | + |
| 39 | +runtime_env_repo = repository_rule( |
| 40 | + implementation = _runtime_env_repo_impl, |
| 41 | +) |
0 commit comments