Skip to content

Commit 9fe1cde

Browse files
rickeylevjanwinkler1
authored andcommitted
add run_toml2json and attrs to pip.parse extension
1 parent 79b98fa commit 9fe1cde

2 files changed

Lines changed: 46 additions & 0 deletions

File tree

python/private/pypi/extension.bzl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,12 @@ The list of labels to use as SRCS for the marker evaluation code. This ensures t
744744
code will be re-evaluated when any of files in the default changes.
745745
""",
746746
),
747+
"_toml2json": attr.label(
748+
default = "//tools/private/toml2json:toml2json.py",
749+
),
750+
"_tool_python_interpreter": attr.label(
751+
default = "@python_3_14_host//:BUILD.bazel",
752+
),
747753
}, **ATTRS)
748754
attrs.update(AUTH_ATTRS)
749755

python/private/pypi/pypi_repo_utils.bzl

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,45 @@ def _execute_checked_stdout(mrctx, *, python, srcs, **kwargs):
162162
**_execute_prep(mrctx, python = python, srcs = srcs, **kwargs)
163163
)
164164

165+
def _run_toml2json(mrctx, toml_label, attr, logger = None):
166+
"""Parses a TOML file into a dictionary using toml2json tool.
167+
168+
Args:
169+
mrctx: The module_ctx or repository_ctx object.
170+
toml_label: Label pointing to the TOML file.
171+
attr: Attributes struct (e.g. from a repository rule). Must contain
172+
`_tool_python_interpreter` and `_toml2json`.
173+
logger: Optional logger instance for informational messages.
174+
175+
Returns:
176+
A dictionary representation of the TOML file content.
177+
"""
178+
if not toml_label:
179+
fail("toml_label cannot be None")
180+
181+
python_interpreter = _resolve_python_interpreter(
182+
mrctx,
183+
python_interpreter_target = attr._tool_python_interpreter,
184+
)
185+
186+
toml_path = mrctx.path(toml_label)
187+
if not toml_path.exists:
188+
fail("toml file does not exist: {} (from label {})".format(toml_path, toml_label))
189+
190+
# Use the shared toml2json tool
191+
toml2json_tool = mrctx.path(ctx.attr._toml2json)
192+
193+
stdout = _execute_checked_stdout(
194+
mrctx,
195+
logger = logger,
196+
op = "Toml2Json: {}".format(toml_label),
197+
python = python_interpreter,
198+
arguments = [str(toml2json_tool), str(toml_path)],
199+
srcs = [toml2json_tool, toml_path],
200+
)
201+
202+
return json.decode(stdout)
203+
165204
def _find_namespace_package_files(rctx, install_dir):
166205
"""Finds all `__init__.py` files that belong to namespace packages.
167206
@@ -202,4 +241,5 @@ pypi_repo_utils = struct(
202241
execute_checked_stdout = _execute_checked_stdout,
203242
find_namespace_package_files = _find_namespace_package_files,
204243
resolve_python_interpreter = _resolve_python_interpreter,
244+
run_toml2json = _run_toml2json,
205245
)

0 commit comments

Comments
 (0)