Skip to content

Commit 7b222cf

Browse files
authored
Add python_interpreter attr to pip rules (bazel-contrib#252)
This adds a `python_interpreter` attribute to `pip_import` and `whl_library` that can be used to select the system command used to run Python's packaging tools. This provides the basis for invoking pip under Python 3 to install PY3 dependencies. Example usage: ``` pip_import( name = 'pip_deps', requirements = '//:requirements.txt', python_interpreter = 'python3.7', ) ``` The par files have been regenerated. (This required a little bootstrapping since piptool.py needs to be modified to accept the flag before pip.bzl is modified to pass it.)
1 parent 13bf8b7 commit 7b222cf

File tree

6 files changed

+38
-4
lines changed

6 files changed

+38
-4
lines changed

docs/pip.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
## pip_import
66

77
<pre>
8-
pip_import(<a href="#pip_import-name">name</a>, <a href="#pip_import-requirements">requirements</a>)
8+
pip_import(<a href="#pip_import-name">name</a>, <a href="#pip_import-python_interpreter">python_interpreter</a>, <a href="#pip_import-requirements">requirements</a>)
99
</pre>
1010

1111
A rule for importing `requirements.txt` dependencies into Bazel.
@@ -67,6 +67,16 @@ py_binary(
6767
</p>
6868
</td>
6969
</tr>
70+
<tr id="pip_import-python_interpreter">
71+
<td><code>python_interpreter</code></td>
72+
<td>
73+
String; optional
74+
<p>
75+
The command to run the Python interpreter used to invoke pip and unpack the
76+
wheels.
77+
</p>
78+
</td>
79+
</tr>
7080
<tr id="pip_import-requirements">
7181
<td><code>requirements</code></td>
7282
<td>

docs/whl.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
## whl_library
66

77
<pre>
8-
whl_library(<a href="#whl_library-name">name</a>, <a href="#whl_library-extras">extras</a>, <a href="#whl_library-requirements">requirements</a>, <a href="#whl_library-whl">whl</a>)
8+
whl_library(<a href="#whl_library-name">name</a>, <a href="#whl_library-extras">extras</a>, <a href="#whl_library-python_interpreter">python_interpreter</a>, <a href="#whl_library-requirements">requirements</a>, <a href="#whl_library-whl">whl</a>)
99
</pre>
1010

1111
A rule for importing `.whl` dependencies into Bazel.
@@ -53,6 +53,15 @@ This rule defines `@foo//:pkg` as a `py_library` target.
5353
</p>
5454
</td>
5555
</tr>
56+
<tr id="whl_library-python_interpreter">
57+
<td><code>python_interpreter</code></td>
58+
<td>
59+
String; optional
60+
<p>
61+
The command to run the Python interpreter used when unpacking the wheel.
62+
</p>
63+
</td>
64+
</tr>
5665
<tr id="whl_library-requirements">
5766
<td><code>requirements</code></td>
5867
<td>

packaging/piptool.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ def pip_main(argv):
8686
parser = argparse.ArgumentParser(
8787
description='Import Python dependencies into Bazel.')
8888

89+
parser.add_argument('--python_interpreter', action='store',
90+
help=('The Python interpreter to use when extracting '
91+
'wheels.'))
92+
8993
parser.add_argument('--name', action='store',
9094
help=('The namespace of the import.'))
9195

@@ -177,10 +181,12 @@ def whl_library(wheel):
177181
if "{repo_name}" not in native.existing_rules():
178182
whl_library(
179183
name = "{repo_name}",
184+
python_interpreter = "{python_interpreter}",
180185
whl = "@{name}//:{path}",
181186
requirements = "@{name}//:requirements.bzl",
182187
extras = [{extras}]
183188
)""".format(name=args.name, repo_name=wheel.repository_name(),
189+
python_interpreter=args.python_interpreter,
184190
path=wheel.basename(),
185191
extras=','.join([
186192
'"%s"' % extra

python/pip.bzl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@ def _pip_import_impl(repository_ctx):
2424

2525
# To see the output, pass: quiet=False
2626
result = repository_ctx.execute([
27-
"python",
27+
repository_ctx.attr.python_interpreter,
2828
repository_ctx.path(repository_ctx.attr._script),
29+
"--python_interpreter",
30+
repository_ctx.attr.python_interpreter,
2931
"--name",
3032
repository_ctx.attr.name,
3133
"--input",
@@ -41,6 +43,10 @@ def _pip_import_impl(repository_ctx):
4143

4244
pip_import = repository_rule(
4345
attrs = {
46+
"python_interpreter": attr.string(default = "python", doc = """
47+
The command to run the Python interpreter used to invoke pip and unpack the
48+
wheels.
49+
"""),
4450
"requirements": attr.label(
4551
mandatory = True,
4652
allow_single_file = True,

python/whl.bzl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def _whl_impl(repository_ctx):
1717
"""Core implementation of whl_library."""
1818

1919
args = [
20-
"python",
20+
repository_ctx.attr.python_interpreter,
2121
repository_ctx.path(repository_ctx.attr._script),
2222
"--whl",
2323
repository_ctx.path(repository_ctx.attr.whl),
@@ -40,6 +40,9 @@ whl_library = repository_rule(
4040
"extras": attr.string_list(doc = """
4141
A subset of the "extras" available from this <code>.whl</code> for which
4242
<code>requirements</code> has the dependencies.
43+
"""),
44+
"python_interpreter": attr.string(default = "python", doc = """
45+
The command to run the Python interpreter used when unpacking the wheel.
4346
"""),
4447
"requirements": attr.string(doc = """
4548
The name of the <code>pip_import</code> repository rule from which to load this

tools/piptool.par

574 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)