13
13
# limitations under the License.
14
14
"""Import pip requirements into Bazel."""
15
15
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."""
18
18
19
19
# Add an empty top-level BUILD file.
20
20
# This is because Bazel requires BUILD files along all paths accessed
21
21
# via //this/sort/of:path and we wouldn't be able to load our generated
22
22
# requirements.bzl without it.
23
23
repository_ctx .file ("BUILD" , "" )
24
24
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
+ ])
38
33
39
34
if result .return_code :
40
35
fail ("pip_import failed: %s (%s)" % (result .stdout , result .stderr ))
41
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
+
42
45
pip_import = repository_rule (
43
46
attrs = {
44
47
"requirements" : attr .label (
@@ -51,15 +54,32 @@ pip_import = repository_rule(
51
54
cfg = "host" ,
52
55
),
53
56
},
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 ,
55
74
)
56
75
57
- """A rule for importing <code>requirements.txt</code> dependencies into Bazel.
76
+ """Rules for importing <code>requirements.txt</code> dependencies into Bazel.
58
77
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
60
79
<code>requirements.bzl</code> file. This is used via the <code>WORKSPACE</code>
61
80
pattern:
62
- <pre><code>pip_import(
81
+ <pre><code># Use pip3_import to force Python 3.
82
+ pip_import(
63
83
name = "foo",
64
84
requirements = ":requirements.txt",
65
85
)
0 commit comments