8000 Add a pip3_import target for Python 3 compatibility. · raaaar/rules_python@aa8db2d · GitHub
[go: up one dir, main page]

Skip to content

Commit aa8db2d

Browse files
committed
Add a pip3_import target for Python 3 compatibility.
1 parent f414af5 commit aa8db2d

File tree

1 file changed

+31
-6
lines changed

1 file changed

+31
-6
lines changed

python/pip.bzl

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# limitations under the License.
1414
"""Import pip requirements into Bazel."""
1515

16-
def _pip_import_impl(repository_ctx):
16+
def _pip_import_impl(repository_ctx, binary_name):
1717
"""Core implementation of pip_import."""
1818

1919
# Add an empty top-level BUILD file.
@@ -24,7 +24,7 @@ def _pip_import_impl(repository_ctx):
2424

2525
# To see the output, pass: quiet=False
2626
result = repository_ctx.execute([
27-
"python", repository_ctx.path(repository_ctx.attr._script),
27+
binary_name, repository_ctx.path(repository_ctx.attr._script),
2828
"--name", repository_ctx.attr.name,
2929
"--input", repository_ctx.path(repository_ctx.attr.requirements),
3030
"--output", repository_ctx.path("requirements.bzl"),
@@ -34,6 +34,14 @@ def _pip_import_impl(repository_ctx):
3434
if result.return_code:
3535
fail("pip_import failed: %s (%s)" % (result.stdout, result.stderr))
3636

37+
def _pip2_import_impl(repository_ctx):
38+
"""System python implementation. Assumes this links to python 2."""
39+
_pip_import_impl(repository_ctx, "python")
40+
41+
def _pip3_import_impl(repository_ctx):
42+
"""Python 3 implementation."""
43+
_pip_import_impl(repository_ctx, "python3")
44+
3745
pip_import = repository_rule(
3846
attrs = {
3947
"requirements": attr.label(
@@ -47,15 +55,32 @@ pip_import = repository_rule(
4755
cfg = "host",
4856
),
4957
},
50-
implementation = _pip_import_impl,
58+
implementation = _pip2_import_impl,
59+
)
60+
61+
pip3_import = repository_rule(
62+
attrs = {
63+
"requirements": attr.label(
64+
allow_files = True,
65+
mandatory = True,
66+
single_file = True,
67+
),
68+
"_script": attr.label(
69+
executable = True,
70+
default = Label("//tools:piptool.par"),
71+
cfg = "host",
72+
),
73+
},
74+
implementation = _pip3_import_impl,
5175
)
5276

53-
"""A rule for importing <code>requirements.txt</code> dependencies into Bazel.
77+
"""Rules for importing <code>requirements.txt</code> dependencies into Bazel.
5478
55-
This rule imports a <code>requirements.txt</code> file and generates a new
79+
These rules import a <code>requirements.txt</code> file and generate a new
5680
<code>requirements.bzl</code> file. This is used via the <code>WORKSPACE</code>
5781
pattern:
58-
<pre><code>pip_import(
82+
<pre><code># Use pip3_import to force Python 3.
83+
pip_import(
5984
name = "foo",
6085
requirements = ":requirements.txt",
6186
)

0 commit comments

Comments
 (0)
0