aboutsummaryrefslogtreecommitdiff
path: root/third_party/rules_python.patch
blob: 3b2b5b8d0dfcfe93e2edb8a10dffcb37fe88d5bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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: