Skip to content

Commit 1fd9953

Browse files
committed
move to templating part of the args
1 parent 1663af5 commit 1fd9953

File tree

5 files changed

+399
-365
lines changed

5 files changed

+399
-365
lines changed

python/uv/private/BUILD.bazel

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@
1515
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
1616
load("//python:py_binary.bzl", "py_binary")
1717

18+
exports_files(
19+
srcs = ["pip_compile.py"],
20+
# only because this is used from a macro to template
21+
visibility = ["//visibility:public"],
22+
)
23+
1824
filegroup(
1925
name = "distribution",
2026
srcs = glob(["**"]),

python/uv/private/lock.bzl

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"""A simple macro to lock the requirements.
1616
"""
1717

18+
load("@bazel_skylib//rules:expand_template.bzl", "expand_template")
1819
load("@bazel_skylib//rules:native_binary.bzl", "native_binary")
1920
load("@bazel_skylib//rules:write_file.bzl", "write_file")
2021
load("//python:py_binary.bzl", "py_binary")
@@ -27,7 +28,7 @@ _REQUIREMENTS_TARGET_COMPATIBLE_WITH = [] if BZLMOD_ENABLED else ["@platforms//:
2728
_RunLockInfo = provider(
2829
doc = "Information for running the underlying Sphinx command directly",
2930
fields = {
30-
"locker": """
31+
"cmd": """
3132
:type: Target
3233
3334
The locker binary to run.
@@ -46,30 +47,29 @@ def _impl(ctx):
4647
ctx.files.src_outs[0].path,
4748
])
4849
args.add("--output-file", ctx.outputs.out)
49-
args.add_all(ctx.attr.args)
5050

5151
# TODO @aignas 2025-03-02: add the following deps to _RunLockInfo
5252
srcs = ctx.files.srcs + ctx.files.src_outs
5353
args.add_all(ctx.files.srcs)
5454

5555
ctx.actions.run(
56-
executable = ctx.executable._locker,
56+
executable = ctx.executable.cmd,
5757
mnemonic = "RulesPythonLock",
5858
inputs = srcs,
5959
outputs = [
6060
ctx.outputs.out,
6161
],
6262
arguments = [args],
6363
tools = [
64-
ctx.executable._locker,
64+
ctx.executable.cmd,
6565
],
6666
progress_message = "Locking requirements using uv",
6767
env = ctx.attr.env,
6868
)
6969

7070
return [
7171
DefaultInfo(files = depset([ctx.outputs.out])),
72-
_RunLockInfo(locker = ctx.executable._locker),
72+
_RunLockInfo(cmd = ctx.executable.cmd),
7373
]
7474

7575
_lock = rule(
@@ -78,15 +78,15 @@ _lock = rule(
7878
""",
7979
attrs = {
8080
"args": attr.string_list(),
81-
"env": attr.string_dict(),
82-
"out": attr.output(mandatory = True),
83-
"src_outs": attr.label_list(mandatory = True, allow_files = True),
84-
"srcs": attr.label_list(mandatory = True, allow_files = True),
85-
"_locker": attr.label(
81+
"cmd": attr.label(
8682
default = "//python/uv/private:pip_compile",
8783
executable = True,
8884
cfg = "target",
8985
),
86+
"env": attr.string_dict(),
87+
"out": attr.output(mandatory = True),
88+
"src_outs": attr.label_list(mandatory = True, allow_files = True),
89+
"srcs": attr.label_list(mandatory = True, allow_files = True),
9090
},
9191
)
9292

@@ -130,6 +130,26 @@ def lock(*, name, srcs, out, args = [], **kwargs):
130130
]
131131
args += user_args
132132

133+
expand_template(
134+
name = name + "_locker_src",
135+
out = name + "_locker.py",
136+
template = "//python/uv/private:pip_compile.py",
137+
substitutions = {
138+
" args = []": " args = " + repr(args),
139+
},
140+
tags = ["manual"],
141+
)
142+
143+
py_binary(
144+
name = name + "_locker",
145+
srcs = [name + "_locker.py"],
146+
data = [
147+
"//python/uv:current_toolchain",
148+
],
149+
tags = ["manual"],
150+
deps = ["//python/runfiles"],
151+
)
152+
133153
_lock(
134154
name = name,
135155
srcs = srcs,
@@ -145,6 +165,7 @@ def lock(*, name, srcs, out, args = [], **kwargs):
145165
],
146166
args = args,
147167
target_compatible_with = _REQUIREMENTS_TARGET_COMPATIBLE_WITH,
168+
cmd = name + "_locker",
148169
)
149170

150171
run_args = []

python/uv/private/pip_compile.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,30 +19,33 @@ def _run() -> None:
1919
# Let `uv` know that it was spawned by this Python interpreter
2020
env["UV_INTERNAL__PARENT_INTERPRETER"] = sys.executable
2121
args = []
22-
args += sys.argv[1:]
22+
sys_args = sys.argv[1:]
2323

24-
src_out = args[1] if args[0] == "--src-out" else None
24+
src_out = sys_args[1] if sys_args[0] == "--src-out" else None
2525
if src_out:
26-
args = args[2:]
26+
sys_args = sys_args[2:]
2727

28-
if args[0] != "--output-file":
28+
if sys_args[0] != "--output-file":
2929
raise ValueError(
30-
f"The first arg should be to declare the output file, got:\n{args}"
30+
f"The first arg should be to declare the output file, got:\n{sys_args}"
3131
)
3232
else:
33-
out = args[1]
33+
out = sys_args[1]
3434

35+
srcs = sys_args[2:]
36+
37+
# this is set under bazel run
3538
workspace = env.get("BUILD_WORKSPACE_DIRECTORY")
3639
if workspace:
37-
args[1] = Path(workspace) / out
40+
dst = Path(workspace) / out
3841
elif src_out:
3942
src = Path(src_out)
4043
dst = Path(out)
4144
import shutil
4245

4346
shutil.copy(src, dst)
4447

45-
uv_args = ["pip", "compile"] + args
48+
uv_args = ["pip", "compile"] + args + srcs + ["--output-file", str(dst)]
4649

4750
if sys.platform == "win32":
4851
import subprocess

0 commit comments

Comments
 (0)