@@ -37,6 +37,8 @@ different name. Then we can load it from defs.bzl and export it there under
37
37
the original name.
38
38
"""
39
39
40
+ load ("@bazel_tools//tools/python:toolchain.bzl" , _py_runtime_pair = "py_runtime_pair" )
41
+
40
42
# The implementation of the macros and tagging mechanism follows the example
41
43
# set by rules_cc and rules_java.
42
44
@@ -64,6 +66,8 @@ def py_library(**attrs):
64
66
Args:
65
67
**attrs: Rule attributes
66
68
"""
69
+ if attrs .get ("srcs_version" ) in ("PY2" , "PY2ONLY" ):
70
+ fail ("Python 2 is no longer supported: https://github.com/bazelbuild/rules_python/issues/886" )
67
71
68
72
# buildifier: disable=native-python
69
73
native .py_library (** _add_tags (attrs ))
@@ -74,6 +78,10 @@ def py_binary(**attrs):
74
78
Args:
75
79
**attrs: Rule attributes
76
80
"""
81
+ if attrs .get ("python_version" ) == "PY2" :
82
+ fail ("Python 2 is no longer supported: https://github.com/bazelbuild/rules_python/issues/886" )
83
+ if attrs .get ("srcs_version" ) in ("PY2" , "PY2ONLY" ):
84
+ fail ("Python 2 is no longer supported: https://github.com/bazelbuild/rules_python/issues/886" )
77
85
78
86
# buildifier: disable=native-python
79
87
native .py_binary (** _add_tags (attrs ))
@@ -84,6 +92,10 @@ def py_test(**attrs):
84
92
Args:
85
93
**attrs: Rule attributes
86
94
"""
95
+ if attrs .get ("python_version" ) == "PY2" :
96
+ fail ("Python 2 is no longer supported: https://github.com/bazelbuild/rules_python/issues/886" )
97
+ if attrs .get ("srcs_version" ) in ("PY2" , "PY2ONLY" ):
98
+ fail ("Python 2 is no longer supported: https://github.com/bazelbuild/rules_python/issues/886" )
87
99
88
100
# buildifier: disable=native-python
89
101
native .py_test (** _add_tags (attrs ))
@@ -94,6 +106,78 @@ def py_runtime(**attrs):
94
106
Args:
95
107
**attrs: Rule attributes
96
108
"""
109
+ if attrs .get ("python_version" ) == "PY2" :
110
+ fail ("Python 2 is no longer supported: see https://github.com/bazelbuild/rules_python/issues/886" )
97
111
98
112
# buildifier: disable=native-python
99
113
native .py_runtime (** _add_tags (attrs ))
114
+
115
+ # NOTE: This doc is copy/pasted from the builtin py_runtime_pair rule so our
116
+ # doc generator gives useful API docs.
117
+ def py_runtime_pair (name , py2_runtime = None , py3_runtime = None , ** attrs ):
118
+ """A toolchain rule for Python.
119
+
120
+ This used to wrap up to two Python runtimes, one for Python 2 and one for Python 3.
121
+ However, Python 2 is no longer supported, so it now only wraps a single Python 3
122
+ runtime.
123
+
124
+ Usually the wrapped runtimes are declared using the `py_runtime` rule, but any
125
+ rule returning a `PyRuntimeInfo` provider may be used.
126
+
127
+ This rule returns a `platform_common.ToolchainInfo` provider with the following
128
+ schema:
129
+
130
+ ```python
131
+ platform_common.ToolchainInfo(
132
+ py2_runtime = None,
133
+ py3_runtime = <PyRuntimeInfo or None>,
134
+ )
135
+ ```
136
+
137
+ Example usage:
138
+
139
+ ```python
140
+ # In your BUILD file...
141
+
142
+ load("@rules_python//python:defs.bzl", "py_runtime_pair")
143
+
144
+ py_runtime(
145
+ name = "my_py3_runtime",
146
+ interpreter_path = "/system/python3",
147
+ python_version = "PY3",
148
+ )
149
+
150
+ py_runtime_pair(
151
+ name = "my_py_runtime_pair",
152
+ py3_runtime = ":my_py3_runtime",
153
+ )
154
+
155
+ toolchain(
156
+ name = "my_toolchain",
157
+ target_compatible_with = <...>,
158
+ toolchain = ":my_py_runtime_pair",
159
+ toolchain_type = "@rules_python//python:toolchain_type",
160
+ )
161
+ ```
162
+
163
+ ```python
164
+ # In your WORKSPACE...
165
+
166
+ register_toolchains("//my_pkg:my_toolchain")
167
+ ```
168
+
169
+ Args:
170
+ name: str, the name of the target
171
+ py2_runtime: optional Label; must be unset or None; an error is raised
172
+ otherwise.
173
+ py3_runtime: Label; a target with `PyRuntimeInfo` for Python 3.
174
+ **attrs: Extra attrs passed onto the native rule
175
+ """
176
+ if attrs .get ("py2_runtime" ):
177
+ fail ("PYthon 2 is no longer supported: see https://github.com/bazelbuild/rules_python/issues/886" )
178
+ _py_runtime_pair (
179
+ name = name ,
180
+ py2_runtime = py2_runtime ,
181
+ py3_runtime = py3_runtime ,
182
+ ** attrs
183
+ )
0 commit comments