Skip to content

Commit ced8cc6

Browse files
authored
refactor: use bazel-skylib for creating hub repo aliases (#1855)
With this PR the code becomes more maintainable and easier to inspect. Since bazel-skylib is already a dependency of rules_python, this is a backwards compatible change. Skipping the CHANGELOG notes because it should not be an externally visible change. Work towards #735.
1 parent 397c1b1 commit ced8cc6

File tree

4 files changed

+95
-33
lines changed

4 files changed

+95
-33
lines changed

python/private/render_pkg_aliases.bzl

+18-8
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ def _render_whl_library_alias(
4242
*,
4343
name,
4444
default_version,
45-
aliases):
45+
aliases,
46+
**kwargs):
4647
"""Render an alias for common targets."""
4748
if len(aliases) == 1 and not aliases[0].version:
4849
alias = aliases[0]
@@ -56,27 +57,36 @@ def _render_whl_library_alias(
5657
# whls that are based on a specific version of Python.
5758
selects = {}
5859
no_match_error = "_NO_MATCH_ERROR"
59-
default = None
6060
for alias in sorted(aliases, key = lambda x: x.version):
6161
actual = "@{repo}//:{name}".format(repo = alias.repo, name = name)
62-
selects[alias.config_setting] = actual
62+
selects.setdefault(actual, []).append(alias.config_setting)
6363
if alias.version == default_version:
64-
default = actual
64+
selects[actual].append("//conditions:default")
6565
no_match_error = None
6666

67-
if default:
68-
selects["//conditions:default"] = default
69-
7067
return render.alias(
7168
name = name,
7269
actual = render.select(
73-
selects,
70+
{
71+
tuple(sorted(
72+
conditions,
73+
# Group `is_python` and other conditions for easier reading
74+
# when looking at the generated files.
75+
key = lambda condition: ("is_python" not in condition, condition),
76+
)): target
77+
for target, conditions in sorted(selects.items())
78+
},
7479
no_match_error = no_match_error,
80+
# This key_repr is used to render selects.with_or keys
81+
key_repr = lambda x: repr(x[0]) if len(x) == 1 else render.tuple(x),
82+
name = "selects.with_or",
7583
),
84+
**kwargs
7685
)
7786

7887
def _render_common_aliases(*, name, aliases, default_version = None):
7988
lines = [
89+
"""load("@bazel_skylib//lib:selects.bzl", "selects")""",
8090
"""package(default_visibility = ["//visibility:public"])""",
8191
]
8292

python/private/text_util.bzl

+22-5
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,18 @@ def _render_alias(name, actual, *, visibility = None):
3535
")",
3636
])
3737

38-
def _render_dict(d, *, value_repr = repr):
38+
def _render_dict(d, *, key_repr = repr, value_repr = repr):
3939
return "\n".join([
4040
"{",
4141
_indent("\n".join([
42-
"{}: {},".format(repr(k), value_repr(v))
42+
"{}: {},".format(key_repr(k), value_repr(v))
4343
for k, v in d.items()
4444
])),
4545
"}",
4646
])
4747

48-
def _render_select(selects, *, no_match_error = None, value_repr = repr):
49-
dict_str = _render_dict(selects, value_repr = value_repr) + ","
48+
def _render_select(selects, *, no_match_error = None, key_repr = repr, value_repr = repr, name = "select"):
49+
dict_str = _render_dict(selects, key_repr = key_repr, value_repr = value_repr) + ","
5050

5151
if no_match_error:
5252
args = "\n".join([
@@ -62,7 +62,7 @@ def _render_select(selects, *, no_match_error = None, value_repr = repr):
6262
"",
6363
])
6464

65-
return "select({})".format(args)
65+
return "{}({})".format(name, args)
6666

6767
def _render_list(items):
6868
if not items:
@@ -80,10 +80,27 @@ def _render_list(items):
8080
"]",
8181
])
8282

83+
def _render_tuple(items, *, value_repr = repr):
84+
if not items:
85+
return "tuple()"
86+
87+
if len(items) == 1:
88+
return "({},)".format(value_repr(items[0]))
89+
90+
return "\n".join([
91+
"(",
92+
_indent("\n".join([
93+
"{},".format(value_repr(item))
94+
for item in items
95+
])),
96+
")",
97+
])
98+
8399
render = struct(
84100
alias = _render_alias,
85101
dict = _render_dict,
86102
indent = _indent,
87103
list = _render_list,
88104
select = _render_select,
105+
tuple = _render_tuple,
89106
)

tests/pip_hub_repository/render_pkg_aliases/render_pkg_aliases_test.bzl

+36-20
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ def _test_legacy_aliases(env):
6565

6666
want_key = "foo/BUILD.bazel"
6767
want_content = """\
68+
load("@bazel_skylib//lib:selects.bzl", "selects")
69+
6870
package(default_visibility = ["//visibility:public"])
6971
7072
alias(
@@ -108,6 +110,8 @@ def _test_bzlmod_aliases(env):
108110

109111
want_key = "bar_baz/BUILD.bazel"
110112
want_content = """\
113+
load("@bazel_skylib//lib:selects.bzl", "selects")
114+
111115
package(default_visibility = ["//visibility:public"])
112116
113117
alias(
@@ -117,40 +121,48 @@ alias(
117121
118122
alias(
119123
name = "pkg",
120-
actual = select(
124+
actual = selects.with_or(
121125
{
122-
"//:my_config_setting": "@pypi_32_bar_baz//:pkg",
123-
"//conditions:default": "@pypi_32_bar_baz//:pkg",
126+
(
127+
"//:my_config_setting",
128+
"//conditions:default",
129+
): "@pypi_32_bar_baz//:pkg",
124130
},
125131
),
126132
)
127133
128134
alias(
129135
name = "whl",
130-
actual = select(
136+
actual = selects.with_or(
131137
{
132-
"//:my_config_setting": "@pypi_32_bar_baz//:whl",
133-
"//conditions:default": "@pypi_32_bar_baz//:whl",
138+
(
139+
"//:my_config_setting",
140+
"//conditions:default",
141+
): "@pypi_32_bar_baz//:whl",
134142
},
135143
),
136144
)
137145
138146
alias(
139147
name = "data",
140-
actual = select(
148+
actual = selects.with_or(
141149
{
142-
"//:my_config_setting": "@pypi_32_bar_baz//:data",
143-
"//conditions:default": "@pypi_32_bar_baz//:data",
150+
(
151+
"//:my_config_setting",
152+
"//conditions:default",
153+
): "@pypi_32_bar_baz//:data",
144154
},
145155
),
146156
)
147157
148158
alias(
149159
name = "dist_info",
150-
actual = select(
160+
actual = selects.with_or(
151161
{
152-
"//:my_config_setting": "@pypi_32_bar_baz//:dist_info",
153-
"//conditions:default": "@pypi_32_bar_baz//:dist_info",
162+
(
163+
"//:my_config_setting",
164+
"//conditions:default",
165+
): "@pypi_32_bar_baz//:dist_info",
154166
},
155167
),
156168
)"""
@@ -178,6 +190,8 @@ def _test_bzlmod_aliases_with_no_default_version(env):
178190

179191
want_key = "bar_baz/BUILD.bazel"
180192
want_content = """\
193+
load("@bazel_skylib//lib:selects.bzl", "selects")
194+
181195
package(default_visibility = ["//visibility:public"])
182196
183197
_NO_MATCH_ERROR = \"\"\"\\
@@ -206,7 +220,7 @@ alias(
206220
207221
alias(
208222
name = "pkg",
209-
actual = select(
223+
actual = selects.with_or(
210224
{
211225
"@@//python/config_settings:is_python_3.1": "@pypi_31_bar_baz//:pkg",
212226
"@@//python/config_settings:is_python_3.2": "@pypi_32_bar_baz//:pkg",
@@ -217,7 +231,7 @@ alias(
217231
218232
alias(
219233
name = "whl",
220-
actual = select(
234+
actual = selects.with_or(
221235
{
222236
"@@//python/config_settings:is_python_3.1": "@pypi_31_bar_baz//:whl",
223237
"@@//python/config_settings:is_python_3.2": "@pypi_32_bar_baz//:whl",
@@ -228,7 +242,7 @@ alias(
228242
229243
alias(
230244
name = "data",
231-
actual = select(
245+
actual = selects.with_or(
232246
{
233247
"@@//python/config_settings:is_python_3.1": "@pypi_31_bar_baz//:data",
234248
"@@//python/config_settings:is_python_3.2": "@pypi_32_bar_baz//:data",
@@ -239,7 +253,7 @@ alias(
239253
240254
alias(
241255
name = "dist_info",
242-
actual = select(
256+
actual = selects.with_or(
243257
{
244258
"@@//python/config_settings:is_python_3.1": "@pypi_31_bar_baz//:dist_info",
245259
"@@//python/config_settings:is_python_3.2": "@pypi_32_bar_baz//:dist_info",
@@ -273,6 +287,8 @@ def _test_bzlmod_aliases_for_non_root_modules(env):
273287

274288
want_key = "bar_baz/BUILD.bazel"
275289
want_content = """\
290+
load("@bazel_skylib//lib:selects.bzl", "selects")
291+
276292
package(default_visibility = ["//visibility:public"])
277293
278294
_NO_MATCH_ERROR = \"\"\"\\
@@ -301,7 +317,7 @@ alias(
301317
302318
alias(
303319
name = "pkg",
304-
actual = select(
320+
actual = selects.with_or(
305321
{
306322
"@@//python/config_settings:is_python_3.1": "@pypi_31_bar_baz//:pkg",
307323
"@@//python/config_settings:is_python_3.2": "@pypi_32_bar_baz//:pkg",
@@ -312,7 +328,7 @@ alias(
312328
313329
alias(
314330
name = "whl",
315-
actual = select(
331+
actual = selects.with_or(
316332
{
317333
"@@//python/config_settings:is_python_3.1": "@pypi_31_bar_baz//:whl",
318334
"@@//python/config_settings:is_python_3.2": "@pypi_32_bar_baz//:whl",
@@ -323,7 +339,7 @@ alias(
323339
324340
alias(
325341
name = "data",
326-
actual = select(
342+
actual = selects.with_or(
327343
{
328344
"@@//python/config_settings:is_python_3.1": "@pypi_31_bar_baz//:data",
329345
"@@//python/config_settings:is_python_3.2": "@pypi_32_bar_baz//:data",
@@ -334,7 +350,7 @@ alias(
334350
335351
alias(
336352
name = "dist_info",
337-
actual = select(
353+
actual = selects.with_or(
338354
{
339355
"@@//python/config_settings:is_python_3.1": "@pypi_31_bar_baz//:dist_info",
340356
"@@//python/config_settings:is_python_3.2": "@pypi_32_bar_baz//:dist_info",

tests/private/text_util/render_tests.bzl

+19
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,25 @@ def _test_render_alias(env):
5454

5555
_tests.append(_test_render_alias)
5656

57+
def _test_render_tuple_dict(env):
58+
got = render.dict(
59+
{
60+
("foo", "bar"): "baz",
61+
("foo",): "bar",
62+
},
63+
key_repr = render.tuple,
64+
)
65+
env.expect.that_str(got).equals("""\
66+
{
67+
(
68+
"foo",
69+
"bar",
70+
): "baz",
71+
("foo",): "bar",
72+
}""")
73+
74+
_tests.append(_test_render_tuple_dict)
75+
5776
def render_test_suite(name):
5877
"""Create the test suite.
5978

0 commit comments

Comments
 (0)