Skip to content

Commit ce80db6

Browse files
vihangmaignas
andauthored
feat: Support constraints in pip_compile (#2916)
This adds in support to pass in a constraints file to pip-compile. This is extremly useful when you want to uprade an indirect/intermediate dependency to pull in security fixes but don't want to add said dependency to the requirements.in file. --------- Signed-off-by: Vihang Mehta <[email protected]> Co-authored-by: Ignas Anikevicius <[email protected]>
1 parent d60cee2 commit ce80db6

File tree

7 files changed

+38
-17
lines changed

7 files changed

+38
-17
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ END_UNRELEASED_TEMPLATE
111111
and activated with custom flags. See the [Registering custom runtimes]
112112
docs and {obj}`single_version_platform_override()` API docs for more
113113
information.
114+
* (rules) Added support for a using constraints files with `compile_pip_requirements`.
115+
Useful when an intermediate dependency needs to be upgraded to pull in
116+
security patches.
114117

115118
{#v0-0-0-removed}
116119
### Removed

examples/pip_parse/BUILD.bazel

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ py_console_script_binary(
5757
compile_pip_requirements(
5858
name = "requirements",
5959
src = "requirements.in",
60+
constraints = [
61+
"constraints_certifi.txt",
62+
"constraints_urllib3.txt",
63+
],
6064
requirements_txt = "requirements_lock.txt",
6165
requirements_windows = "requirements_windows.txt",
6266
)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
certifi>=2025.1.31
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
urllib3>1.26.18

examples/pip_parse/requirements_lock.txt

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@ babel==2.13.1 \
1212
--hash=sha256:33e0952d7dd6374af8dbf6768cc4ddf3ccfefc244f9986d4074704f2fbd18900 \
1313
--hash=sha256:7077a4984b02b6727ac10f1f7294484f737443d7e2e66c5e4380e41a3ae0b4ed
1414
# via sphinx
15-
certifi==2024.7.4 \
16-
--hash=sha256:5a1e7645bc0ec61a09e26c36f6106dd4cf40c6db3a1fb6352b0244e7fb057c7b \
17-
--hash=sha256:c198e21b1289c2ab85ee4e67bb4b4ef3ead0892059901a8d5b622f24a1101e90
18-
# via requests
15+
certifi==2025.4.26 \
16+
--hash=sha256:0a816057ea3cdefcef70270d2c515e4506bbc954f417fa5ade2021213bb8f0c6 \
17+
--hash=sha256:30350364dfe371162649852c63336a15c70c6510c2ad5015b21c2345311805f3
18+
# via
19+
# -c ./constraints_certifi.txt
20+
# requests
1921
chardet==4.0.0 \
2022
--hash=sha256:0d6f53a15db4120f2b08c94f11e7d93d2c911ee118b6b30a04ec3ee8310179fa \
2123
--hash=sha256:f864054d66fd9118f2e67044ac8981a54775ec5b67aed0441892edb553d21da5
@@ -218,10 +220,12 @@ sphinxcontrib-serializinghtml==1.1.9 \
218220
# via
219221
# -r requirements.in
220222
# sphinx
221-
urllib3==1.26.18 \
222-
--hash=sha256:34b97092d7e0a3a8cf7cd10e386f401b3737364026c45e622aa02903dffe0f07 \
223-
--hash=sha256:f8ecc1bba5667413457c529ab955bf8c67b45db799d159066261719e328580a0
224-
# via requests
223+
urllib3==1.26.20 \
224+
--hash=sha256:0ed14ccfbf1c30a9072c7ca157e4319b70d65f623e91e7b32fadb2853431016e \
225+
--hash=sha256:40c2dc0c681e47eb8f90e7e27bf6ff7df2e677421fd46756da1161c39ca70d32
226+
# via
227+
# -c ./constraints_urllib3.txt
228+
# requests
225229
yamllint==1.28.0 \
226230
--hash=sha256:89bb5b5ac33b1ade059743cf227de73daa34d5e5a474b06a5e17fc16583b0cf2 \
227231
--hash=sha256:9e3d8ddd16d0583214c5fdffe806c9344086721f107435f68bad990e5a88826b

examples/pip_parse/requirements_windows.txt

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@ babel==2.13.1 \
1212
--hash=sha256:33e0952d7dd6374af8dbf6768cc4ddf3ccfefc244f9986d4074704f2fbd18900 \
1313
--hash=sha256:7077a4984b02b6727ac10f1f7294484f737443d7e2e66c5e4380e41a3ae0b4ed
1414
# via sphinx
15-
certifi==2024.7.4 \
16-
--hash=sha256:5a1e7645bc0ec61a09e26c36f6106dd4cf40c6db3a1fb6352b0244e7fb057c7b \
17-
--hash=sha256:c198e21b1289c2ab85ee4e67bb4b4ef3ead0892059901a8d5b622f24a1101e90
18-
# via requests
15+
certifi==2025.4.26 \
16+
--hash=sha256:0a816057ea3cdefcef70270d2c515e4506bbc954f417fa5ade2021213bb8f0c6 \
17+
--hash=sha256:30350364dfe371162649852c63336a15c70c6510c2ad5015b21c2345311805f3
18+
# via
19+
# -c ./constraints_certifi.txt
20+
# requests
1921
chardet==4.0.0 \
2022
--hash=sha256:0d6f53a15db4120f2b08c94f11e7d93d2c911ee118b6b30a04ec3ee8310179fa \
2123
--hash=sha256:f864054d66fd9118f2e67044ac8981a54775ec5b67aed0441892edb553d21da5
@@ -222,10 +224,12 @@ sphinxcontrib-serializinghtml==1.1.9 \
222224
# via
223225
# -r requirements.in
224226
# sphinx
225-
urllib3==1.26.18 \
226-
--hash=sha256:34b97092d7e0a3a8cf7cd10e386f401b3737364026c45e622aa02903dffe0f07 \
227-
--hash=sha256:f8ecc1bba5667413457c529ab955bf8c67b45db799d159066261719e328580a0
228-
# via requests
227+
urllib3==1.26.20 \
228+
--hash=sha256:0ed14ccfbf1c30a9072c7ca157e4319b70d65f623e91e7b32fadb2853431016e \
229+
--hash=sha256:40c2dc0c681e47eb8f90e7e27bf6ff7df2e677421fd46756da1161c39ca70d32
230+
# via
231+
# -c ./constraints_urllib3.txt
232+
# requests
229233
yamllint==1.28.0 \
230234
--hash=sha256:89bb5b5ac33b1ade059743cf227de73daa34d5e5a474b06a5e17fc16583b0cf2 \
231235
--hash=sha256:9e3d8ddd16d0583214c5fdffe806c9344086721f107435f68bad990e5a88826b

python/private/pypi/pip_compile.bzl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ def pip_compile(
3838
requirements_windows = None,
3939
visibility = ["//visibility:private"],
4040
tags = None,
41+
constraints = [],
4142
**kwargs):
4243
"""Generates targets for managing pip dependencies with pip-compile.
4344
@@ -77,6 +78,7 @@ def pip_compile(
7778
requirements_windows: File of windows specific resolve output to check validate if requirement.in has changes.
7879
tags: tagging attribute common to all build rules, passed to both the _test and .update rules.
7980
visibility: passed to both the _test and .update rules.
81+
constraints: a list of files containing constraints to pass to pip-compile with `--constraint`.
8082
**kwargs: other bazel attributes passed to the "_test" rule.
8183
"""
8284
if len([x for x in [srcs, src, requirements_in] if x != None]) > 1:
@@ -100,7 +102,7 @@ def pip_compile(
100102
visibility = visibility,
101103
)
102104

103-
data = [name, requirements_txt] + srcs + [f for f in (requirements_linux, requirements_darwin, requirements_windows) if f != None]
105+
data = [name, requirements_txt] + srcs + [f for f in (requirements_linux, requirements_darwin, requirements_windows) if f != None] + constraints
104106

105107
# Use the Label constructor so this is expanded in the context of the file
106108
# where it appears, which is to say, in @rules_python
@@ -122,6 +124,8 @@ def pip_compile(
122124
args.append("--requirements-darwin={}".format(loc.format(requirements_darwin)))
123125
if requirements_windows:
124126
args.append("--requirements-windows={}".format(loc.format(requirements_windows)))
127+
for constraint in constraints:
128+
args.append("--constraint=$(location {})".format(constraint))
125129
args.extend(extra_args)
126130

127131
deps = [

0 commit comments

Comments
 (0)