Skip to content

Commit 2c29ae2

Browse files
committed
feat: uv lock rule instead of genrule
This change implements the uv pip compile as a rule. In order to also make things easier to debug we provide a runnable rule that has the same arguments and updates the source tree output file automatically. The main design is to have a regular lock rule and then it returns a custom provider that has all of the recipe ingredients to construct an executable rule. The execution depends on having bash or powershell, however the powershell script is not yet complete and requires some help from the community. Work towards #1975.
1 parent 5a8f6c4 commit 2c29ae2

File tree

11 files changed

+421
-90
lines changed

11 files changed

+421
-90
lines changed

docs/BUILD.bazel

+5-1
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,11 @@ lock(
169169
name = "requirements",
170170
srcs = ["pyproject.toml"],
171171
out = "requirements.txt",
172-
upgrade = True,
172+
args = [
173+
"--emit-index-url",
174+
"--universal",
175+
#"--upgrade",
176+
],
173177
visibility = ["//private:__pkg__"],
174178
)
175179

examples/BUILD.bazel

+3
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,8 @@ lock(
2121
name = "bzlmod_requirements_3_9",
2222
srcs = ["bzlmod/requirements.in"],
2323
out = "bzlmod/requirements_lock_3_9.txt",
24+
args = [
25+
"--emit-index-url",
26+
],
2427
python_version = "3.9.19",
2528
)

examples/bzlmod/requirements_lock_3_9.txt

+6-11
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,7 @@ chardet==4.0.0 \
2626
colorama==0.4.6 \
2727
--hash=sha256:08695f5cb7ed6e0531a20572697297273c47b8cae5a63ffc6d6ed5c201be6e44 \
2828
--hash=sha256:4f1d9991f5acc0ca119f9d443620b77f9d6b33703e51011c16baf57afb285fc6
29-
# via
30-
# -r examples/bzlmod/requirements.in
31-
# pylint
32-
# sphinx
29+
# via -r examples/bzlmod/requirements.in
3330
dill==0.3.6 \
3431
--hash=sha256:a07ffd2351b8c678dfc4a856a3005f8067aea51d6ba6c700796a4d9e280f39f0 \
3532
--hash=sha256:e5db55f3687856d8fbdab002ed78544e1c4559a130302693d839dfe8f93f2373
@@ -46,7 +43,7 @@ imagesize==1.4.1 \
4643
--hash=sha256:0d8d18d08f840c19d0ee7ca1fd82490fdc3729b7ac93f49870406ddde8ef8d8b \
4744
--hash=sha256:69150444affb9cb0d5cc5a92b3676f0b2fb7cd9ae39e947a5e11a36b4497cd4a
4845
# via sphinx
49-
importlib-metadata==8.4.0 ; python_version < '3.10' \
46+
importlib-metadata==8.4.0 \
5047
--hash=sha256:66f342cc6ac9818fc6ff340576acd24d65ba0b3efabb2b4ac08b598965a4a2f1 \
5148
--hash=sha256:9a547d3bc3608b025f93d403fdd1aae741c24fbb8314df4b155675742ce303c5
5249
# via sphinx
@@ -265,9 +262,7 @@ s3cmd==2.1.0 \
265262
setuptools==65.6.3 \
266263
--hash=sha256:57f6f22bde4e042978bcd50176fdb381d7c21a9efa4041202288d3737a0c6a54 \
267264
--hash=sha256:a7620757bf984b58deaf32fc8a4577a9bbc0850cf92c20e1ce41c38c19e5fb75
268-
# via
269-
# babel
270-
# yamllint
265+
# via yamllint
271266
six==1.16.0 \
272267
--hash=sha256:1e61c37477a1626458e36f7b1d82aa5c9b094fa4802892072e49de9c60c4c926 \
273268
--hash=sha256:8abb2f1d86890a2dfb989f9a77cfcfd3e47c2a354b01111771326f8aa26e0254
@@ -316,15 +311,15 @@ tabulate==0.9.0 \
316311
--hash=sha256:0095b12bf5966de529c0feb1fa08671671b3368eec77d7ef7ab114be2c068b3c \
317312
--hash=sha256:024ca478df22e9340661486f85298cff5f6dcdba14f3813e8830015b9ed1948f
318313
# via -r examples/bzlmod/requirements.in
319-
tomli==2.0.1 ; python_version < '3.11' \
314+
tomli==2.0.1 \
320315
--hash=sha256:939de3e7a6161af0c887ef91b7d41a53e7c5a1ca976325f429cb46ea9bc30ecc \
321316
--hash=sha256:de526c12914f0c550d15924c62d72abc48d6fe7364aa87328337a31007fe8a4f
322317
# via pylint
323318
tomlkit==0.11.6 \
324319
--hash=sha256:07de26b0d8cfc18f871aec595fda24d95b08fef89d147caa861939f37230bf4b \
325320
--hash=sha256:71b952e5721688937fb02cf9d354dbcf0785066149d2855e44531ebdd2b65d73
326321
# via pylint
327-
typing-extensions==4.12.2 ; python_version < '3.10' \
322+
typing-extensions==4.12.2 \
328323
--hash=sha256:04e5ca0351e0f3f85c6853954072df659d0d13fac324d0072316b67d7794700d \
329324
--hash=sha256:1a7ead55c7e559dd4dee8856e3a88b41225abfe1ce8df57b7c13915fe121ffb8
330325
# via
@@ -480,7 +475,7 @@ yamllint==1.28.0 \
480475
--hash=sha256:89bb5b5ac33b1ade059743cf227de73daa34d5e5a474b06a5e17fc16583b0cf2 \
481476
--hash=sha256:9e3d8ddd16d0583214c5fdffe806c9344086721f107435f68bad990e5a88826b
482477
# via -r examples/bzlmod/requirements.in
483-
zipp==3.20.0 ; python_version < '3.10' \
478+
zipp==3.20.0 \
484479
--hash=sha256:0145e43d89664cfe1a2e533adc75adafed82fe2da404b4bbb6b026c0157bdb31 \
485480
--hash=sha256:58da6168be89f0be59beb194da1250516fdaa062ccebd30127ac65d30045e10d
486481
# via importlib-metadata

private/BUILD.bazel

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ load("@rules_multirun//:defs.bzl", "multirun")
44

55
multirun(
66
name = "requirements.update",
7+
testonly = True,
78
commands = [
89
"//tools/publish:{}.update".format(r)
910
for r in [

python/uv/private/BUILD.bazel

+22-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@
1313
# limitations under the License.
1414

1515
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
16+
load("//python/private:bzlmod_enabled.bzl", "BZLMOD_ENABLED") # buildifier: disable=bzl-visibility
17+
18+
exports_files(
19+
srcs = [
20+
"copy.py",
21+
"pip_compile.py",
22+
],
23+
# only because this is used from a macro to template
24+
visibility = ["//visibility:public"],
25+
)
1626

1727
filegroup(
1828
name = "distribution",
@@ -31,9 +41,9 @@ bzl_library(
3141
srcs = ["lock.bzl"],
3242
visibility = ["//python/uv:__subpackages__"],
3343
deps = [
34-
"//python:py_binary_bzl",
44+
"//python:py_test_bzl",
3545
"//python/private:bzlmod_enabled_bzl",
36-
"@bazel_skylib//rules:write_file",
46+
"@bazel_skylib//rules:expand_template",
3747
],
3848
)
3949

@@ -81,3 +91,13 @@ bzl_library(
8191
"//python/private:text_util_bzl",
8292
],
8393
)
94+
95+
filegroup(
96+
name = "pip_compile_template",
97+
srcs = select({
98+
"@platforms//os:windows": ["pip_compile.ps1"],
99+
"//conditions:default": ["pip_compile.sh"],
100+
}),
101+
target_compatible_with = [] if BZLMOD_ENABLED else ["@platforms//:incompatible"],
102+
visibility = ["//visibility:public"],
103+
)

python/uv/private/copy.py

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import sys
2+
from difflib import unified_diff
3+
from os import environ
4+
from pathlib import Path
5+
6+
7+
def main():
8+
src = ""
9+
dst = ""
10+
11+
src = Path(src)
12+
assert src.exists(), f"the {src} file does not exist"
13+
14+
if "BUILD_WORKSPACE_DIRECTORY" not in environ:
15+
dst = Path(dst)
16+
a = dst.read_text() if dst.exists() else "\n"
17+
b = src.read_text()
18+
19+
diff = unified_diff(
20+
a.splitlines(),
21+
b.splitlines(),
22+
str(dst),
23+
str(src),
24+
lineterm="",
25+
)
26+
diff = "\n".join(list(diff))
27+
print(diff)
28+
print(
29+
"""\
30+
31+
===============================================================================
32+
The in source file copy is out of date, please run the failed test target with
33+
'bazel run' to fix the error message update the source file copy.
34+
===============================================================================
35+
"""
36+
)
37+
return 1 if diff else 0
38+
39+
print(f"cp <bazel-sandbox>/{src}", file=sys.stderr)
40+
print(f" -> <workspace>/{dst}", file=sys.stderr)
41+
build_workspace = Path(environ["BUILD_WORKSPACE_DIRECTORY"])
42+
43+
dst = build_workspace / dst
44+
dst.write_text(src.read_text())
45+
print("Success!", file=sys.stderr)
46+
return 0
47+
48+
49+
if __name__ == "__main__":
50+
sys.exit(main())

0 commit comments

Comments
 (0)