Skip to content

Commit a7de3d4

Browse files
committed
refactor legacy aliases
1 parent 354f7a2 commit a7de3d4

File tree

5 files changed

+76
-55
lines changed

5 files changed

+76
-55
lines changed

examples/bzlmod/MODULE.bazel.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

python/private/pypi/pkg_aliases.bzl

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ This is used in bzlmod and non-bzlmod setups."""
1818

1919
load(
2020
":labels.bzl",
21-
#"DATA_LABEL",
22-
#"DIST_INFO_LABEL",
21+
"DATA_LABEL",
22+
"DIST_INFO_LABEL",
2323
#"PY_LIBRARY_IMPL_LABEL",
2424
"PY_LIBRARY_PUBLIC_LABEL",
2525
#"WHEEL_FILE_IMPL_LABEL",
26-
#"WHEEL_FILE_PUBLIC_LABEL",
26+
"WHEEL_FILE_PUBLIC_LABEL",
2727
)
2828

2929
def pkg_aliases(
@@ -44,3 +44,27 @@ def pkg_aliases(
4444
name = name,
4545
actual = ":" + PY_LIBRARY_PUBLIC_LABEL,
4646
)
47+
48+
target_names = {
49+
x: x
50+
for x in [
51+
PY_LIBRARY_PUBLIC_LABEL,
52+
WHEEL_FILE_PUBLIC_LABEL,
53+
DATA_LABEL,
54+
DIST_INFO_LABEL,
55+
]
56+
}
57+
58+
if type(actual) == type({}):
59+
fail("TODO")
60+
61+
repo = actual
62+
63+
for name, target_name in target_names.items():
64+
native.alias(
65+
name = name,
66+
actual = "@{repo}//:{target_name}".format(
67+
repo = repo,
68+
target_name = target_name,
69+
),
70+
)

python/private/pypi/render_pkg_aliases.bzl

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,7 @@ def _render_whl_library_alias(
7878
**kwargs):
7979
"""Render an alias for common targets."""
8080
if len(aliases) == 1 and not aliases[0].version:
81-
alias = aliases[0]
82-
return render.alias(
83-
name = name,
84-
actual = repr("@{repo}//:{name}".format(
85-
repo = alias.repo,
86-
name = target_name,
87-
)),
88-
**kwargs
89-
)
81+
return ""
9082

9183
# Create the alias repositories which contains different select
9284
# statements These select statements point to the different pip
@@ -118,7 +110,9 @@ def _render_whl_library_alias(
118110
)
119111

120112
def _render_pkg_aliases(name, actual):
121-
return """pkg_aliases(
113+
actual = actual.values()[0] if len(actual) == 1 and None in actual else ":pkg"
114+
return """\
115+
pkg_aliases(
122116
name = "{name}",
123117
actual = {actual},
124118
)""".format(name = name, actual = repr(actual))
@@ -150,28 +144,35 @@ load("@rules_python//python/private/pypi:pkg_aliases.bzl", "pkg_aliases")""",
150144
lines.append(
151145
_render_pkg_aliases(
152146
name = name,
153-
actual = ":pkg",
147+
actual = {
148+
a.config_setting: a.repo
149+
for a in aliases
150+
},
154151
),
155152
)
156153
lines.extend(
157154
[
158-
_render_whl_library_alias(
159-
name = name,
160-
aliases = aliases,
161-
target_name = target_name,
162-
visibility = ["//_groups:__subpackages__"] if name.startswith("_") else None,
163-
)
164-
for target_name, name in (
165-
{
166-
PY_LIBRARY_PUBLIC_LABEL: PY_LIBRARY_IMPL_LABEL if group_name else PY_LIBRARY_PUBLIC_LABEL,
167-
WHEEL_FILE_PUBLIC_LABEL: WHEEL_FILE_IMPL_LABEL if group_name else WHEEL_FILE_PUBLIC_LABEL,
168-
DATA_LABEL: DATA_LABEL,
169-
DIST_INFO_LABEL: DIST_INFO_LABEL,
170-
} | {
171-
x: x
172-
for x in extra_aliases
173-
}
174-
).items()
155+
line
156+
for line in [
157+
_render_whl_library_alias(
158+
name = name,
159+
aliases = aliases,
160+
target_name = target_name,
161+
visibility = ["//_groups:__subpackages__"] if name.startswith("_") else None,
162+
)
163+
for target_name, name in (
164+
{
165+
PY_LIBRARY_PUBLIC_LABEL: PY_LIBRARY_IMPL_LABEL if group_name else PY_LIBRARY_PUBLIC_LABEL,
166+
WHEEL_FILE_PUBLIC_LABEL: WHEEL_FILE_IMPL_LABEL if group_name else WHEEL_FILE_PUBLIC_LABEL,
167+
DATA_LABEL: DATA_LABEL,
168+
DIST_INFO_LABEL: DIST_INFO_LABEL,
169+
} | {
170+
x: x
171+
for x in extra_aliases
172+
}
173+
).items()
174+
]
175+
if line
175176
],
176177
)
177178
if group_name:

tests/pypi/pkg_aliases/pkg_aliases_test.bzl

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,28 @@ def _test_empty(env):
3232
),
3333
)
3434

35+
# buildifier: disable=unsorted-dict-items
3536
want = [
36-
# buildifier: disable=unsorted-dict-items
3737
{
3838
"name": "foo",
3939
"actual": ":pkg",
4040
},
41+
{
42+
"name": "pkg",
43+
"actual": "@repo//:pkg",
44+
},
45+
{
46+
"name": "whl",
47+
"actual": "@repo//:whl",
48+
},
49+
{
50+
"name": "data",
51+
"actual": "@repo//:data",
52+
},
53+
{
54+
"name": "dist_info",
55+
"actual": "@repo//:dist_info",
56+
},
4157
]
4258

4359
env.expect.that_collection(actual).contains_exactly(want)

tests/pypi/render_pkg_aliases/render_pkg_aliases_test.bzl

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -81,27 +81,7 @@ package(default_visibility = ["//visibility:public"])
8181
8282
pkg_aliases(
8383
name = "foo",
84-
actual = ":pkg",
85-
)
86-
87-
alias(
88-
name = "pkg",
89-
actual = "@pypi_foo//:pkg",
90-
)
91-
92-
alias(
93-
name = "whl",
94-
actual = "@pypi_foo//:whl",
95-
)
96-
97-
alias(
98-
name = "data",
99-
actual = "@pypi_foo//:data",
100-
)
101-
102-
alias(
103-
name = "dist_info",
104-
actual = "@pypi_foo//:dist_info",
84+
actual = "pypi_foo",
10585
)"""
10686

10787
env.expect.that_dict(actual).contains_exactly({want_key: want_content})

0 commit comments

Comments
 (0)