forked from bazel-contrib/rules_python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpip.bzl
More file actions
195 lines (176 loc) · 6.04 KB
/
Copy pathpip.bzl
File metadata and controls
195 lines (176 loc) · 6.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# Copyright 2017 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Import pip requirements into Bazel."""
def _pip_import_impl(repository_ctx, binary_name):
"""Core implementation of pip_import."""
# Add an empty top-level BUILD file.
# This is because Bazel requires BUILD files along all paths accessed
# via //this/sort/of:path and we wouldn't be able to load our generated
# requirements.bzl without it.
repository_ctx.file("BUILD", "")
# To see the output, pass: quiet=False
result = repository_ctx.execute([
binary_name,
repository_ctx.path(repository_ctx.attr._script),
"--name",
repository_ctx.attr.name,
"--input",
repository_ctx.path(repository_ctx.attr.requirements),
"--output",
repository_ctx.path("requirements.bzl"),
"--directory",
repository_ctx.path(""),
])
if result.return_code:
fail("pip_import failed: %s (%s)" % (result.stdout, result.stderr))
def _pip_system_import_impl(repository_ctx):
"""System python implementation."""
_pip_import_impl(repository_ctx, "python")
def _pip3_import_impl(repository_ctx):
"""Python 3 implementation."""
_pip_import_impl(repository_ctx, "python3")
pip_import = repository_rule(
attrs = {
"requirements": attr.label(
mandatory = True,
allow_single_file = True,
),
"_script": attr.label(
executable = True,
default = Label("//tools:piptool.par"),
cfg = "host",
),
},
implementation = _pip_system_import_impl,
)
"""A rule for importing <code>requirements.txt</code> dependencies into Bazel.
This rule imports a <code>requirements.txt</code> file using the system
<code>python</code>, and generates a new <code>requirements.bzl</code> file.
This is used via the <code>WORKSPACE</code> pattern:
<pre><code>pip_import(
name = "foo",
requirements = ":requirements.txt",
)
load("@foo//:requirements.bzl", "pip_install")
pip_install()
</code></pre>
You can then reference imported dependencies from your <code>BUILD</code>
file with:
<pre><code>load("@foo//:requirements.bzl", "requirement")
py_library(
name = "bar",
...
deps = [
"//my/other:dep",
requirement("futures"),
requirement("mock"),
],
)
</code></pre>
Or alternatively:
<pre><code>load("@foo//:requirements.bzl", "all_requirements")
py_binary(
name = "baz",
...
deps = [
":foo",
] + all_requirements,
)
</code></pre>
Args:
requirements: The label of a requirements.txt file.
"""
pip3_import = repository_rule(
attrs = {
"requirements": attr.label(
mandatory = True,
allow_single_file = True,
),
"_script": attr.label(
executable = True,
default = Label("//tools:piptool.par"),
cfg = "host",
),
},
implementation = _pip3_import_impl,
)
"""A rule for importing <code>requirements.txt</code> dependencies into Bazel.
This rule imports a <code>requirements.txt</code> file using the system
<code>python3</code>, and generates a new <code>requirements.bzl</code> file.
This is used via the <code>WORKSPACE</code> pattern:
<pre><code>pip3_import(
name = "foo",
requirements = ":requirements.txt",
)
load("@foo//:requirements.bzl", "pip_install")
pip_install()
</code></pre>
You can then reference imported dependencies from your <code>BUILD</code>
file with:
<pre><code>load("@foo//:requirements.bzl", "requirement")
py_library(
name = "bar",
...
deps = [
"//my/other:dep",
requirement("futures"),
requirement("mock"),
],
)
</code></pre>
Or alternatively:
<pre><code>load("@foo//:requirements.bzl", "all_requirements")
py_binary(
name = "baz",
...
deps = [
":foo",
] + all_requirements,
)
</code></pre>
Args:
requirements: The label of a requirements.txt file.
"""
def pip_repositories():
"""Pull in dependencies needed to use the packaging rules."""
# At the moment this is a placeholder, in that it does not actually pull in
# any dependencies. However, it does do some validation checking.
#
# As a side effect of migrating our canonical workspace name from
# "@io_bazel_rules_python" to "@rules_python" (#203), users who still
# imported us by the old name would get a confusing error about a
# repository dependency cycle in their workspace. (The cycle is likely
# related to the fact that our repo name is hardcoded into the template
# in piptool.py.)
#
# To produce a more informative error message in this situation, we
# fail-fast here if we detect that we're not being imported by the new
# name. (I believe we have always had the requirement that we're imported
# by the canonical name, because of the aforementioned hardcoding.)
#
# Users who, against best practice, do not call pip_repositories() in their
# workspace will not benefit from this check.
if "rules_python" not in native.existing_rules():
message = "=" * 79 + """\n\
It appears that you are trying to import rules_python without using its
canonical name, "@rules_python". This does not work. Please change your
WORKSPACE file to import this repo with `name = "rules_python"` instead.
"""
if "io_bazel_rules_python" in native.existing_rules():
message += """\n\
Note that the previous name of "@io_bazel_rules_python" is no longer used.
See https://github.com/bazelbuild/rules_python/issues/203 for context.
"""
message += "=" * 79
fail(message)