Skip to content

Commit 8bf2bf4

Browse files
Alex Eaglealexeagle
Alex Eagle
authored andcommitted
Allow for requirements files to differ per platform
As a common example, we need a compiled requirements file for linux that differs from mac os
1 parent 12662b6 commit 8bf2bf4

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

docs/pip.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ alias(
140140

141141
| Name | Description | Default Value |
142142
| :-------------: | :-------------: | :-------------: |
143-
| requirements | A 'requirements.txt' pip requirements file. | none |
143+
| requirements | A 'requirements.txt' pip requirements file. | <code>None</code> |
144144
| name | A unique name for the created external repository (default 'pip'). | <code>"pip"</code> |
145145
| kwargs | Keyword arguments passed directly to the <code>pip_repository</code> repository rule. | none |
146146

python/pip.bzl

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ load("//python/pip_install:requirements.bzl", _compile_pip_requirements = "compi
2020
compile_pip_requirements = _compile_pip_requirements
2121
package_annotation = _package_annotation
2222

23-
def pip_install(requirements, name = "pip", **kwargs):
23+
def pip_install(requirements = None, name = "pip", **kwargs):
2424
"""Accepts a `requirements.txt` file and installs the dependencies listed within.
2525
2626
Those dependencies become available in a generated `requirements.bzl` file.

python/pip_install/pip_repository.bzl

+28-5
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,19 @@ exports_files(["requirements.bzl"])
122122
def _pip_repository_impl(rctx):
123123
python_interpreter = _resolve_python_interpreter(rctx)
124124

125-
if rctx.attr.incremental and not rctx.attr.requirements_lock:
126-
fail("Incremental mode requires a requirements_lock attribute be specified.")
125+
os = rctx.os.name.lower()
126+
requirements_txt = rctx.attr.requirements_lock
127+
if os.startswith("mac os") and rctx.attr.requirements_darwin != None:
128+
requirements_txt = rctx.attr.requirements_darwin
129+
elif os.startswith("linux") and rctx.attr.requirements_linux != None:
130+
requirements_txt = rctx.attr.requirements_linux
131+
elif "win" in os:
132+
requirements_txt = rctx.attr.requirements_windows
133+
if rctx.attr.incremental and not requirements_txt:
134+
fail("""\
135+
Incremental mode requires a requirements_lock attribute be specified,
136+
or a platform-specific lockfile using one of the requirements_* attributes.
137+
""")
127138

128139
# Write the annotations file to pass to the wheel maker
129140
annotations = {package: json.decode(data) for (package, data) in rctx.attr.annotations.items()}
@@ -136,9 +147,9 @@ def _pip_repository_impl(rctx):
136147
"-m",
137148
"python.pip_install.parse_requirements_to_bzl",
138149
"--requirements_lock",
139-
rctx.path(rctx.attr.requirements_lock),
150+
rctx.path(requirements_txt),
140151
"--requirements_lock_label",
141-
str(rctx.attr.requirements_lock),
152+
str(requirements_txt),
142153
# pass quiet and timeout args through to child repos.
143154
"--quiet",
144155
str(rctx.attr.quiet),
@@ -158,7 +169,7 @@ def _pip_repository_impl(rctx):
158169
"-m",
159170
"python.pip_install.extract_wheels",
160171
"--requirements",
161-
rctx.path(rctx.attr.requirements),
172+
rctx.path(requirements_txt),
162173
"--annotations",
163174
annotations_file,
164175
]
@@ -282,6 +293,18 @@ pip_repository_attrs = {
282293
allow_single_file = True,
283294
doc = "A 'requirements.txt' pip requirements file.",
284295
),
296+
"requirements_darwin": attr.label(
297+
allow_single_file = True,
298+
doc = "Override the requirements_lock attribute when the host platform is Mac OS",
299+
),
300+
"requirements_linux": attr.label(
301+
allow_single_file = True,
302+
doc = "Override the requirements_lock attribute when the host platform is Linux",
303+
),
304+
"requirements_windows": attr.label(
305+
allow_single_file = True,
306+
doc = "Override the requirements_lock attribute when the host platform is Windows",
307+
),
285308
"requirements_lock": attr.label(
286309
allow_single_file = True,
287310
doc = """

0 commit comments

Comments
 (0)