Skip to content

Commit 7f99cb7

Browse files
committed
move the dep parsing to the analysis phase
1 parent 96f65e0 commit 7f99cb7

File tree

3 files changed

+48
-37
lines changed

3 files changed

+48
-37
lines changed

python/private/pypi/generate_whl_library_build_bazel.bzl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ _RENDER = {
2424
"dependencies": render.list,
2525
"dependencies_by_platform": lambda x: render.dict(x, value_repr = render.list),
2626
"entry_points": render.dict,
27+
"extras": render.list,
2728
"group_deps": render.list,
29+
"platforms": render.list,
30+
"requires_dist": render.list,
2831
"srcs_exclude": render.list,
2932
"tags": render.list,
3033
}

python/private/pypi/whl_library.bzl

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ load(":generate_whl_library_build_bazel.bzl", "generate_whl_library_build_bazel"
2424
load(":parse_requirements.bzl", "host_platform")
2525
load(":parse_whl_name.bzl", "parse_whl_name")
2626
load(":patch_whl.bzl", "patch_whl")
27-
load(":pep508_env.bzl", "deps")
2827
load(":pypi_repo_utils.bzl", "pypi_repo_utils")
2928
load(":whl_target_platforms.bzl", "whl_target_platforms")
3029

@@ -354,13 +353,22 @@ def _whl_library_impl(rctx):
354353
)
355354
entry_points[entry_point_without_py] = entry_point_script_name
356355

357-
# TODO @aignas 2025-02-24: move this to pkg_aliases layer to have this in
358-
# the analysis phase. This means that we need to get the target platform abi
359-
# from the python version/versions we are setting the package up for. We can
360-
# potentially get this from the python toolchain interpreter.
361-
package_deps = deps(
362-
# TODO @aignas 2025-02-24: get the data here by parsing the METADATA
363-
# file manually without involving python interpreter at all.
356+
build_file_contents = generate_whl_library_build_bazel(
357+
whl_name = whl_path.basename,
358+
# TODO @aignas 2025-03-23: load the dep_template from the hub repository
359+
dep_template = rctx.attr.dep_template or "@{}{{name}}//:{{target}}".format(rctx.attr.repo_prefix),
360+
# TODO @aignas 2025-03-23: store the `group_name` per package in the hub repo
361+
group_name = rctx.attr.group_name,
362+
group_deps = rctx.attr.group_deps,
363+
# TODO @aignas 2025-03-23: store the pip_data_exclude in the hub repo.
364+
data_exclude = rctx.attr.pip_data_exclude,
365+
tags = [
366+
"pypi_name=" + metadata["name"],
367+
"pypi_version=" + metadata["version"],
368+
],
369+
entry_points = entry_points,
370+
# TODO @aignas 2025-03-23: store the annotation in the hub repo.
371+
annotation = None if not rctx.attr.annotation else struct(**json.decode(rctx.read(rctx.attr.annotation))),
364372
name = metadata["name"],
365373
requires_dist = metadata["requires_dist"],
366374
# target the host platform if the target platform is not specified in the rule.
@@ -381,28 +389,6 @@ def _whl_library_impl(rctx):
381389
# deps. This would be again, internal only stuff.
382390
host_python_version = metadata["python_version"],
383391
)
384-
385-
build_file_contents = generate_whl_library_build_bazel(
386-
name = whl_path.basename,
387-
# TODO @aignas 2025-03-23: load the dep_template from the hub repository
388-
dep_template = rctx.attr.dep_template or "@{}{{name}}//:{{target}}".format(rctx.attr.repo_prefix),
389-
# TODO @aignas 2025-03-23: replace `dependencies` and
390-
# `dependencies_by_platform` with `requires_dist`.
391-
dependencies = package_deps.deps,
392-
dependencies_by_platform = package_deps.deps_select,
393-
# TODO @aignas 2025-03-23: store the `group_name` per package in the hub repo
394-
group_name = rctx.attr.group_name,
395-
group_deps = rctx.attr.group_deps,
396-
# TODO @aignas 2025-03-23: store the pip_data_exclude in the hub repo.
397-
data_exclude = rctx.attr.pip_data_exclude,
398-
tags = [
399-
"pypi_name=" + metadata["name"],
400-
"pypi_version=" + metadata["version"],
401-
],
402-
entry_points = entry_points,
403-
# TODO @aignas 2025-03-23: store the annotation in the hub repo.
404-
annotation = None if not rctx.attr.annotation else struct(**json.decode(rctx.read(rctx.attr.annotation))),
405-
)
406392
rctx.file("BUILD.bazel", build_file_contents)
407393

408394
return

python/private/pypi/whl_library_targets.bzl

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,12 @@ load(
2929
"WHEEL_FILE_IMPL_LABEL",
3030
"WHEEL_FILE_PUBLIC_LABEL",
3131
)
32+
load(":pep508_env.bzl", "deps")
3233

3334
def whl_library_targets(
3435
*,
3536
name,
37+
whl_name,
3638
dep_template,
3739
data_exclude = [],
3840
srcs_exclude = [],
@@ -41,14 +43,16 @@ def whl_library_targets(
4143
DIST_INFO_LABEL: ["site-packages/*.dist-info/**"],
4244
DATA_LABEL: ["data/**"],
4345
},
44-
dependencies = [],
45-
dependencies_by_platform = {},
4646
group_deps = [],
4747
group_name = "",
4848
data = [],
4949
copy_files = {},
5050
copy_executables = {},
5151
entry_points = {},
52+
requires_dist = [],
53+
platforms,
54+
extras = [],
55+
host_python_version,
5256
native = native,
5357
rules = struct(
5458
copy_file = copy_file,
@@ -58,14 +62,12 @@ def whl_library_targets(
5862
"""Create all of the whl_library targets.
5963
6064
Args:
61-
name: {type}`str` The file to match for including it into the `whl`
65+
name: {type}`str` the name of the distribution.
66+
whl_name: {type}`str` The file to match for including it into the `whl`
6267
filegroup. This may be also parsed to generate extra metadata.
6368
dep_template: {type}`str` The dep_template to use for dependency
6469
interpolation.
6570
tags: {type}`list[str]` The tags set on the `py_library`.
66-
dependencies: {type}`list[str]` A list of dependencies.
67-
dependencies_by_platform: {type}`dict[str, list[str]]` A list of
68-
dependencies by platform key.
6971
filegroups: {type}`dict[str, list[str]]` A dictionary of the target
7072
names and the glob matches.
7173
group_name: {type}`str` name of the dependency group (if any) which
@@ -87,9 +89,29 @@ def whl_library_targets(
8789
data: {type}`list[str]` A list of labels to include as part of the `data` attribute in `py_library`.
8890
entry_points: {type}`dict[str, str]` The mapping between the script
8991
name and the python file to use. DEPRECATED.
92+
requires_dist: TODO
93+
platforms: TODO
94+
extras: TODO
95+
host_python_version: TODO
9096
native: {type}`native` The native struct for overriding in tests.
9197
rules: {type}`struct` A struct with references to rules for creating targets.
9298
"""
99+
100+
# TODO @aignas 2025-02-24: move this to pkg_aliases layer to have this in
101+
# the analysis phase. This means that we need to get the target platform abi
102+
# from the python version/versions we are setting the package up for. We can
103+
# potentially get this from the python toolchain interpreter.
104+
package_deps = deps(
105+
name = name,
106+
requires_dist = requires_dist,
107+
platforms = platforms,
108+
extras = extras,
109+
host_python_version = host_python_version,
110+
)
111+
112+
dependencies = package_deps.deps
113+
dependencies_by_platform = package_deps.deps_select
114+
93115
dependencies = sorted([normalize_name(d) for d in dependencies])
94116
dependencies_by_platform = {
95117
platform: sorted([normalize_name(d) for d in deps])
@@ -203,7 +225,7 @@ def whl_library_targets(
203225
if hasattr(native, "filegroup"):
204226
native.filegroup(
205227
name = whl_file_label,
206-
srcs = [name],
228+
srcs = [whl_name],
207229
data = _deps(
208230
deps = dependencies,
209231
deps_by_platform = dependencies_by_platform,

0 commit comments

Comments
 (0)