Skip to content

Commit 492b18d

Browse files
jkinkeadBrian Sardo
authored and
Brian Sardo
committed
Add a pip3_import target for Python 3 compatibility.
1 parent 965d4b4 commit 492b18d

File tree

1 file changed

+39
-19
lines changed

1 file changed

+39
-19
lines changed

python/pip.bzl

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

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

1919
# Add an empty top-level BUILD file.
2020
# This is because Bazel requires BUILD files along all paths accessed
2121
# via //this/sort/of:path and we wouldn't be able to load our generated
2222
# requirements.bzl without it.
2323
repository_ctx.file("BUILD", "")
2424

25-
# To see the output, pass: quiet=False
26-
result = repository_ctx.execute([
27-
"python",
28-
repository_ctx.path(repository_ctx.attr._script),
29-
"--name",
30-
repository_ctx.attr.name,
31-
"--input",
32-
repository_ctx.path(repository_ctx.attr.requirements),
33-
"--output",
34-
repository_ctx.path("requirements.bzl"),
35-
"--directory",
36-
repository_ctx.path(""),
37-
])
25+
# To see the output, pass: quiet=False
26+
result = repository_ctx.execute([
27+
binary_name, repository_ctx.path(repository_ctx.attr._script),
28+
"--name", repository_ctx.attr.name,
29+
"--input", repository_ctx.path(repository_ctx.attr.requirements),
30+
"--output", repository_ctx.path("requirements.bzl"),
31+
"--directory", repository_ctx.path(""),
32+
])
3833

3934
if result.return_code:
4035
fail("pip_import failed: %s (%s)" % (result.stdout, result.stderr))
4136

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+
4245
pip_import = repository_rule(
4346
attrs = {
4447
"requirements": attr.label(
@@ -51,15 +54,32 @@ pip_import = repository_rule(
5154
cfg = "host",
5255
),
5356
},
54-
implementation = _pip_import_impl,
57+
implementation = _pip2_import_impl,
58+
)
59+
60+
pip3_import = repository_rule(
61+
attrs = {
62+
"requirements": attr.label(
63+
allow_files = True,
64+
mandatory = True,
65+
single_file = True,
66+
),
67+
"_script": attr.label(
68+
executable = True,
69+
default = Label("//tools:piptool.par"),
70+
cfg = "host",
71+
),
72+
},
73+
implementation = _pip3_import_impl,
5574
)
5675

57-
"""A rule for importing <code>requirements.txt</code> dependencies into Bazel.
76+
"""Rules for importing <code>requirements.txt</code> dependencies into Bazel.
5877
59-
This rule imports a <code>requirements.txt</code> file and generates a new
78+
These rules import a <code>requirements.txt</code> file and generate a new
6079
<code>requirements.bzl</code> file. This is used via the <code>WORKSPACE</code>
6180
pattern:
62-
<pre><code>pip_import(
81+
<pre><code># Use pip3_import to force Python 3.
82+
pip_import(
6383
name = "foo",
6484
requirements = ":requirements.txt",
6585
)

0 commit comments

Comments
 (0)