diff --git a/python/pip_install/pip_repository.bzl b/python/pip_install/pip_repository.bzl index c3007e1..f8a9234 100644 --- a/python/pip_install/pip_repository.bzl +++ b/python/pip_install/pip_repository.bzl @@ -39,7 +39,8 @@ def _resolve_python_interpreter(rctx): if "/" not in python_interpreter: python_interpreter = rctx.which(python_interpreter) if not python_interpreter: - fail("python interpreter not found") + print("WARNING: python interpreter not found. Python targets will not be functional") + return "" return python_interpreter def _parse_optional_attrs(rctx, args): @@ -93,13 +94,49 @@ def _parse_optional_attrs(rctx, args): return args +def _generate_stub_requirements_bzl(rctx): + contents = """\ +def requirement(name): + return "@{repo}//:empty" +""".format(repo=rctx.attr.name) + rctx.file("requirements.bzl", contents) + _BUILD_FILE_CONTENTS = """\ package(default_visibility = ["//visibility:public"]) # Ensure the `requirements.bzl` source can be accessed by stardoc, since users load() from it exports_files(["requirements.bzl"]) + +py_library( + name = "empty", + srcs = [], +) """ +def _python_version_info(rctx, python_interpreter, info_index): + cmd = [ + python_interpreter, + "-c", + "from __future__ import print_function; import sys; print(sys.version_info[{}])".format(info_index) + ] + result = rctx.execute(cmd) + if result.stderr or not result.stdout: + print("WARNING: Failed to get version info from {}".format(python_interpreter)) + return None + return int(result.stdout.strip()) + +def _python_version_supported(rctx, python_interpreter): + major_version = _python_version_info(rctx, python_interpreter, 0) + minor_version = _python_version_info(rctx, python_interpreter, 1) + if major_version == None or minor_version == None: + print("WARNING: Failed to get Python version of {}".format(python_interpreter)) + return False + if (major_version != 3 or minor_version < 6): + print("WARNING: {} is of version {}.{}. This version is unsupported.".format(python_interpreter, major_version, minor_version)) + return False + return True + + def _pip_repository_impl(rctx): python_interpreter = _resolve_python_interpreter(rctx) @@ -109,6 +146,11 @@ def _pip_repository_impl(rctx): # We need a BUILD file to load the generated requirements.bzl rctx.file("BUILD.bazel", _BUILD_FILE_CONTENTS) + # Check if python interpreter has minimum required version. + if not python_interpreter or not _python_version_supported(rctx, python_interpreter): + _generate_stub_requirements_bzl(rctx) + return + pypath = _construct_pypath(rctx) if rctx.attr.incremental: