summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthew Sotoudeh <masotoudeh@ucdavis.edu>2021-05-07 19:13:32 -0700
committerGitHub <noreply@github.com>2021-05-07 19:13:32 -0700
commitcb4f346641afd090dc088c01df46dc403e600773 (patch)
tree4c7ca5cfc1f59848b821ebb73efac3c491fe8914
parentbf2b329302f283d2d71eabf924d3b23186cb833e (diff)
Add a Warn-Only mode that falls back on the system Python (#12)
Adds a new flag. BAZEL_PYTHON_WARN_ONLY=true will cause it to print a debug message and then carry on with the system Python if no bazel-python installation is found. This allows the user to do a 'fast build' in cases where Python is not especially important (e.g., build just the C++ server of a project).
-rw-r--r--README.md7
-rw-r--r--bazel_python.bzl34
-rwxr-xr-xpywrapper.sh11
3 files changed, 41 insertions, 11 deletions
diff --git a/README.md b/README.md
index a48de6b..8a11e8b 100644
--- a/README.md
+++ b/README.md
@@ -112,6 +112,13 @@ the repository for which you want to compute coverage. To use it, after running
`bazel test //...` you should be able to run `bazel run coverage_report` to
produce an `htmlcov` directory with the coverage report.
+#### Selectively Disabling Bazel-Python
+In some scenarios, it may suffice to use the system Python. For example, if a
+backend server only needs Python for one or two setup operations. In this case,
+one may pass `--define BAZEL_PYTHON_ONLY_WARN=true` to instruct Bazel-Python to
+fallback on the system Python installation if a version-specific one is not
+found. It will still print a debug message, but will not fail the build.
+
## Known Issues
#### Missing Modules
If you get errors about missing modules (e.g., `pytest not found`), please
diff --git a/bazel_python.bzl b/bazel_python.bzl
index 60950be..ccde609 100644
--- a/bazel_python.bzl
+++ b/bazel_python.bzl
@@ -73,21 +73,35 @@ def _bazel_python_venv_impl(ctx):
Also installs requirements specified by @ctx.attr.requirements_file.
"""
- if "BAZEL_PYTHON_DIR" not in ctx.var:
- fail("You must run setup_python.sh for " + ctx.attr.python_version)
- python_parent_dir = ctx.var.get("BAZEL_PYTHON_DIR")
python_version = ctx.attr.python_version
- python_dir = python_parent_dir + "/" + python_version
+ use_system = False
+ only_warn = ctx.var.get("BAZEL_PYTHON_ONLY_WARN", "false").lower() == "true"
+ if "BAZEL_PYTHON_DIR" not in ctx.var:
+ if only_warn:
+ print("A bazel-python installation was not found. Falling back to the system python. For reproducibility, please run setup_python.sh for " + python_version)
+ use_system = True
+ else:
+ fail("You must run setup_python.sh for " + python_version)
+ if use_system:
+ python_dir = ""
+ else:
+ python_parent_dir = ctx.var.get("BAZEL_PYTHON_DIR")
+ python_dir = python_parent_dir + "/" + python_version
# TODO: Fail if python_dir does not exist.
venv_dir = ctx.actions.declare_directory("bazel_python_venv_installed")
inputs = []
- command = """
- export PATH={py_dir}/bin:$PATH
- export PATH={py_dir}/include:$PATH
- export PATH={py_dir}/lib:$PATH
- export PATH={py_dir}/share:$PATH
- export PYTHON_PATH={py_dir}:{py_dir}/bin:{py_dir}/include:{py_dir}/lib:{py_dir}/share
+ if use_system:
+ command = ""
+ else:
+ command = """
+ export PATH={py_dir}/bin:$PATH
+ export PATH={py_dir}/include:$PATH
+ export PATH={py_dir}/lib:$PATH
+ export PATH={py_dir}/share:$PATH
+ export PYTHON_PATH={py_dir}:{py_dir}/bin:{py_dir}/include:{py_dir}/lib:{py_dir}/share
+ """
+ command += """
python3 -m venv {out_dir} || exit 1
source {out_dir}/bin/activate || exit 1
"""
diff --git a/pywrapper.sh b/pywrapper.sh
index 6a7e99d..0059881 100755
--- a/pywrapper.sh
+++ b/pywrapper.sh
@@ -1,4 +1,13 @@
#!/bin/bash
-source bazel_python_venv_installed/bin/activate || exit 1
+# If python is run from the 'main' workspace, then we will have
+# bazel_python_venv_installed available right in the current directory. But if
+# it's run from a dependency (e.g., GRPC) then it will be under
+# bazel_out/.../[mainworkspace]. This searches for the first matching path then
+# exits, so it should be reasonably fast in most cases.
+# https://unix.stackexchange.com/questions/68414/only-find-first-few-matched-files-using-find
+venv_path=$((find . -path "*/bazel_python_venv_installed/bin/activate" & ) | head -n 1)
+# If venv_path was not found it will be empty and the below will throw an
+# error, alerting Bazel something went wrong.
+source $venv_path || exit 1
python $@
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback