13
13
# limitations under the License.
14
14
"""Import pip requirements into Bazel."""
15
15
16
- def _pip_import_impl (repository_ctx ):
16
+ def _pip_import_impl (repository_ctx , binary_name ):
17
17
"""Core implementation of pip_import."""
18
18
19
19
# Add an empty top-level BUILD file.
@@ -24,7 +24,7 @@ def _pip_import_impl(repository_ctx):
24
24
25
25
# To see the output, pass: quiet=False
26
26
result = repository_ctx .execute ([
27
- "python" , repository_ctx .path (repository_ctx .attr ._script ),
27
+ binary_name , repository_ctx .path (repository_ctx .attr ._script ),
28
28
"--name" , repository_ctx .attr .name ,
29
29
"--input" , repository_ctx .path (repository_ctx .attr .requirements ),
30
30
"--output" , repository_ctx .path ("requirements.bzl" ),
@@ -34,6 +34,14 @@ def _pip_import_impl(repository_ctx):
34
34
if result .return_code :
35
35
fail ("pip_import failed: %s (%s)" % (result .stdout , result .stderr ))
36
36
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
+
37
45
pip_import = repository_rule (
38
46
attrs = {
39
47
"requirements" : attr .label (
@@ -47,15 +55,32 @@ pip_import = repository_rule(
47
55
cfg = "host" ,
48
56
),
49
57
},
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 ,
51
75
)
52
76
53
- """A rule for importing <code>requirements.txt</code> dependencies into Bazel.
77
+ """Rules for importing <code>requirements.txt</code> dependencies into Bazel.
54
78
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
56
80
<code>requirements.bzl</code> file. This is used via the <code>WORKSPACE</code>
57
81
pattern:
58
- <pre><code>pip_import(
82
+ <pre><code># Use pip3_import to force Python 3.
83
+ pip_import(
59
84
name = "foo",
60
85
requirements = ":requirements.txt",
61
86
)
0 commit comments