rules_python.patch 2.8 KB

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