Skip to content

Commit f0b1c76

Browse files
committed
use param file
1 parent aafaf3b commit f0b1c76

File tree

2 files changed

+65
-10
lines changed

2 files changed

+65
-10
lines changed

python/uv/private/lock.bzl

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,33 @@ load("@bazel_skylib//rules:diff_test.bzl", "diff_test")
1919
load("@bazel_skylib//rules:expand_template.bzl", "expand_template")
2020
load("//python:py_binary.bzl", "py_binary")
2121
load("//python/private:bzlmod_enabled.bzl", "BZLMOD_ENABLED") # buildifier: disable=bzl-visibility
22+
load("//python/private:text_util.bzl", "render") # buildifier: disable=bzl-visibility
2223

2324
visibility(["//..."])
2425

2526
_REQUIREMENTS_TARGET_COMPATIBLE_WITH = [] if BZLMOD_ENABLED else ["@platforms//:incompatible"]
2627

27-
def _impl(ctx):
28-
args = ctx.actions.args()
28+
_LockRunInfo = provider(
29+
doc = "Information about source tree for Sphinx to build.",
30+
fields = {
31+
"args": """
32+
:type: Args
2933
30-
# TODO @aignas 2025-03-02: create an executable file here that is using a
31-
# python and uv toolchains.
34+
TODO
35+
""",
36+
},
37+
)
3238

39+
def _lock_impl(ctx):
40+
args = ctx.actions.args()
3341
if ctx.files.maybe_out:
3442
args.add_all([
3543
"--src-out",
3644
ctx.files.maybe_out[0].path,
3745
])
3846
args.add("--output-file", ctx.outputs.out)
3947
args.add_all(ctx.files.srcs)
48+
args.use_param_file("--file=%s", use_always = True)
4049

4150
ctx.actions.run(
4251
executable = ctx.executable.cmd,
@@ -51,10 +60,13 @@ def _impl(ctx):
5160
env = ctx.attr.env,
5261
)
5362

54-
return [DefaultInfo(files = depset([ctx.outputs.out]))]
63+
return [
64+
DefaultInfo(files = depset([ctx.outputs.out])),
65+
_LockRunInfo(args = args),
66+
]
5567

5668
_lock = rule(
57-
implementation = _impl,
69+
implementation = _lock_impl,
5870
doc = """\
5971
""",
6072
attrs = {
@@ -71,6 +83,37 @@ _lock = rule(
7183
},
7284
)
7385

86+
def _run_lock_impl(ctx):
87+
provider = ctx.attr.lock[_LockRunInfo]
88+
89+
executable = ctx.actions.declare_file(ctx.label.name)
90+
ctx.actions.expand_template(
91+
template = ctx.file._template,
92+
output = executable,
93+
substitutions = {
94+
" args = []": " args = " + render.indent(render.list(provider.args)),
95+
},
96+
is_executable = True,
97+
)
98+
fail(provider)
99+
100+
_run_lock = rule(
101+
implementation = _run_lock_impl,
102+
doc = """\
103+
""",
104+
attrs = {
105+
"lock": attr.label(
106+
mandatory = True,
107+
providers = [_LockRunInfo],
108+
),
109+
"_template": attr.label(
110+
default = "//python/uv/private:pip_compile.py",
111+
allow_single_file = True,
112+
),
113+
},
114+
executable = True,
115+
)
116+
74117
def lock(*, name, srcs, out, args = [], **kwargs):
75118
"""Pin the requirements based on the src files.
76119
@@ -142,6 +185,7 @@ def lock(*, name, srcs, out, args = [], **kwargs):
142185
"//python/uv:current_toolchain",
143186
] + srcs + ([maybe_out] if maybe_out else []),
144187
args = run_args,
188+
python_version = kwargs.get("python_version"),
145189
tags = ["manual"],
146190
deps = ["//python/runfiles"],
147191
)
@@ -164,6 +208,11 @@ def lock(*, name, srcs, out, args = [], **kwargs):
164208
cmd = locker_target,
165209
)
166210

211+
_run_lock(
212+
name = name + ".run2",
213+
lock = name,
214+
)
215+
167216
if maybe_out:
168217
diff_test(
169218
name = name + "_test",

python/uv/private/pip_compile.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@
44
import sys
55
from pathlib import Path
66

7-
from python import runfiles
8-
97

108
def _run() -> None:
11-
rfiles = runfiles.Create()
12-
uv_path = rfiles.Rlocation("_main/python/uv/current_toolchain/uv")
9+
uv_path = ""
10+
if not uv_path:
11+
from python import runfiles
12+
13+
rfiles = runfiles.Create()
14+
uv_path = rfiles.Rlocation("_main/python/uv/current_toolchain/uv")
15+
1316
if not uv_path:
1417
raise RuntimeError("cannot find uv: {}".format(uv_path))
1518

@@ -20,6 +23,9 @@ def _run() -> None:
2023
env["UV_INTERNAL__PARENT_INTERPRETER"] = sys.executable
2124
args = []
2225
sys_args = sys.argv[1:]
26+
if sys_args[0].startswith("--file"):
27+
# TODO @aignas 2025-03-10: use argparse.ArgumentParser instead
28+
sys_args = Path(sys_args[0].partition("=")[-1]).read_text().strip().split("\n")
2329

2430
src_out = sys_args[1] if sys_args[0] == "--src-out" else None
2531
if src_out:

0 commit comments

Comments
 (0)