| 1 |
diff --git a/python/pip_install/pip_repository.bzl b/python/pip_install/pip_repository.bzl |
| 2 |
index c3007e1..f8a9234 100644 |
| 3 |
--- a/python/pip_install/pip_repository.bzl |
| 4 |
+++ b/python/pip_install/pip_repository.bzl |
| 5 |
@@ -39,7 +39,8 @@ def _resolve_python_interpreter(rctx): |
| 6 |
if "/" not in python_interpreter: |
| 7 |
python_interpreter = rctx.which(python_interpreter) |
| 8 |
if not python_interpreter: |
| 9 |
- fail("python interpreter not found") |
| 10 |
+ print("WARNING: python interpreter not found. Python targets will not be functional") |
| 11 |
+ return "" |
| 12 |
return python_interpreter |
| 13 |
|
| 14 |
def _parse_optional_attrs(rctx, args): |
| 15 |
@@ -93,13 +94,49 @@ def _parse_optional_attrs(rctx, args): |
| 16 |
|
| 17 |
return args |
| 18 |
|
| 19 |
+def _generate_stub_requirements_bzl(rctx): |
| 20 |
+ contents = """\ |
| 21 |
+def requirement(name): |
| 22 |
+ return "@{repo}//:empty" |
| 23 |
+""".format(repo=rctx.attr.name) |
| 24 |
+ rctx.file("requirements.bzl", contents) |
| 25 |
+ |
| 26 |
_BUILD_FILE_CONTENTS = """\ |
| 27 |
package(default_visibility = ["//visibility:public"]) |
| 28 |
|
| 29 |
# Ensure the `requirements.bzl` source can be accessed by stardoc, since users load() from it |
| 30 |
exports_files(["requirements.bzl"]) |
| 31 |
+ |
| 32 |
+py_library( |
| 33 |
+ name = "empty", |
| 34 |
+ srcs = [], |
| 35 |
+) |
| 36 |
""" |
| 37 |
|
| 38 |
+def _python_version_info(rctx, python_interpreter, info_index): |
| 39 |
+ cmd = [ |
| 40 |
+ python_interpreter, |
| 41 |
+ "-c", |
| 42 |
+ "from __future__ import print_function; import sys; print(sys.version_info[{}])".format(info_index) |
| 43 |
+ ] |
| 44 |
+ result = rctx.execute(cmd) |
| 45 |
+ if result.stderr or not result.stdout: |
| 46 |
+ print("WARNING: Failed to get version info from {}".format(python_interpreter)) |
| 47 |
+ return None |
| 48 |
+ return int(result.stdout.strip()) |
| 49 |
+ |
| 50 |
+def _python_version_supported(rctx, python_interpreter): |
| 51 |
+ major_version = _python_version_info(rctx, python_interpreter, 0) |
| 52 |
+ minor_version = _python_version_info(rctx, python_interpreter, 1) |
| 53 |
+ if major_version == None or minor_version == None: |
| 54 |
+ print("WARNING: Failed to get Python version of {}".format(python_interpreter)) |
| 55 |
+ return False |
| 56 |
+ if (major_version != 3 or minor_version < 6): |
| 57 |
+ print("WARNING: {} is of version {}.{}. This version is unsupported.".format(python_interpreter, major_version, minor_version)) |
| 58 |
+ return False |
| 59 |
+ return True |
| 60 |
+ |
| 61 |
+ |
| 62 |
def _pip_repository_impl(rctx): |
| 63 |
python_interpreter = _resolve_python_interpreter(rctx) |
| 64 |
|
| 65 |
@@ -109,6 +146,11 @@ def _pip_repository_impl(rctx): |
| 66 |
# We need a BUILD file to load the generated requirements.bzl |
| 67 |
rctx.file("BUILD.bazel", _BUILD_FILE_CONTENTS) |
| 68 |
|
| 69 |
+ # Check if python interpreter has minimum required version. |
| 70 |
+ if not python_interpreter or not _python_version_supported(rctx, python_interpreter): |
| 71 |
+ _generate_stub_requirements_bzl(rctx) |
| 72 |
+ return |
| 73 |
+ |
| 74 |
pypath = _construct_pypath(rctx) |
| 75 |
|
| 76 |
if rctx.attr.incremental: |