8000 feat: set no_match_error if the default_version is unset · bazel-contrib/rules_python@52bdd01 · GitHub
[go: up one dir, main page]

Skip to content

Commit 52bdd01

Browse files
committed
feat: set no_match_error if the default_version is unset
1 parent bd84917 commit 52bdd01

File tree

2 files changed

+123
-29
lines changed

2 files changed

+123
-29
lines changed

python/private/render_pkg_aliases.bzl

Lines changed: 57 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,54 @@ This is used in bzlmod and non-bzlmod setups."""
1919
load("//python/private:normalize_name.bzl", "normalize_name")
2020
load(":version_label.bzl", "version_label")
2121

22-
_DEFAULT = """\
22+
_ALIAS = """\
2323
alias(
2424
name = "{name}",
25-
actual = "@{repo_name}_{dep}//:{target}",
25+
actual = "{actual}",
2626
)"""
2727

2828
_SELECT = """\
2929
alias(
3030
name = "{name}",
31-
actual = select({{{selects}}}),
31+
actual = select({args}),
3232
)"""
3333

34+
def _indent(text, indent = " " * 4):
35+
return "\n".join([indent + line for line in text.split("\n")])
36+
37+
def _render_select(name, selects, no_match_error = None):
38+
dict_str = "\n".join([
39+
"{",
40+
_indent("\n".join([
41+
"{}: {},".format(repr(k), repr(v))
42+
for k, v in selects.items()
43+
])),
44+
"},",
45+
])
46+
args = _indent(dict_str)
47+
48+
if no_match_error:
49+
args = "\n".join([
50+
"",
51+
_indent(args),
52+
_indent(
53+
"no_match_error = \"{error_str}\",".format(
54+
selects = args,
55+
error_str = no_match_error,
56+
),
57+
indent = " " * 8,
58+
),
59+
_indent(""),
60+
])
61+
else:
62+
# A single arg is present, so strip spaces and delimiters surrounding the dict
63+
args = args.strip(", ")
64+
65+
return _SELECT.format(
66+
name = name,
67+
args = args,
68+
)
69+
3470
def _render_alias(
3571
*,
3672
name,
@@ -47,11 +83,13 @@ def _render_alias(
4783
generated.
4884
"""
4985
if versions == None:
50-
return _DEFAULT.format(
86+
return _ALIAS.format(
5187
name = name,
52-
repo_name = repo_name,
53-
dep = dep,
54-
target = target,
88+
actual = "@{repo_name}_{dep}//:{target}".format(
89+
repo_name = repo_name,
90+
dep = dep,
91+
target = target,
92+
),
5593
)
5694

5795
# Create the alias repositories which contains different select
@@ -71,35 +109,30 @@ def _render_alias(
71109
)
72110
selects[condition] = actual
73111

112+
if not default_version:
113+
return _render_select(
114+
name,
115+
selects,
116+
no_match_error = "PyPI package is only available for versions: {}".format(
117+
",".join(versions),
118+
),
119+
)
120+
74121
default_actual = "@{repo_name}_{version}_{dep}//:{target}".format(
75122
repo_name = repo_name,
76123
version = version_label(default_version),
77124
dep = dep,
78125
target = target,
79126
)
80127
selects["//conditions:default"] = default_actual
81-
82-
return _SELECT.format(
83-
name = name,
84-
selects = "\n{} ".format(
85-
"".join([
86-
" {}: {},\n".format(repr(k), repr(v))
87-
for k, v in selects.items()
88-
]),
89-
),
90-
)
128+
return _render_select(name, selects)
91129

92130
def _render_common_aliases(repo_name, name, versions = None, default_version = None, rules_python = None):
93131
return "\n\n".join([
94132
"""package(default_visibility = ["//visibility:public"])""",
95-
_render_alias(
133+
_ALIAS.format(
96134
name = name,
97-
repo_name = repo_name,
98-
dep = name,
99-
target = "pkg",
100-
versions = versions,
101-
default_version = default_version,
102-
rules_python = rules_python,
135+
actual = ":pkg",
103136
),
104137
] + [
105138
_render_alias(

tests/pip_hub_repository/render_pkg_aliases/render_pkg_aliases_test.bzl

Lines changed: 66 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ package(default_visibility = ["//visibility:public"])
3131
3232
alias(
3333
name = "foo",
34-
actual = "@pypi_foo//:pkg",
34+
actual = ":pkg",
3535
)
3636
3737
alias(
@@ -87,10 +87,7 @@ package(default_visibility = ["//visibility:public"])
8787
8888
alias(
8989
name = "bar_baz",
90-
actual = select({
91-
"@@rules_python//python/config_settings:is_python_3.2.3": "@pypi_32_bar_baz//:pkg",
92-
"//conditions:default": "@pypi_32_bar_baz//:pkg",
93-
}),
90+
actual = ":pkg",
9491
)
9592
9693
alias(
@@ -130,6 +127,70 @@ alias(
130127

131128
_tests.append(_test_bzlmod_aliases)
132129

130+
def _test_bzlmod_aliases_with_no_default_version(env):
131+
actual = render_pkg_aliases(
132+
default_version = None,
133+
repo_name = "pypi",
134+
rules_python = "rules_python",
135+
whl_map = {
136+
"bar-baz": ["3.2.3"],
137+
},
138+
)
139+
140+
want = {
141+
"bar_baz/BUILD.bazel": """\
142+
package(default_visibility = ["//visibility:public"])
143+
144+
alias(
145+
name = "bar_baz",
146+
actual = ":pkg",
147+
)
148+
149+
alias(
150+
name = "pkg",
151+
actual = select(
152+
{
153+
"@@rules_python//python/config_settings:is_python_3.2.3": "@pypi_32_bar_baz//:pkg",
154+
},
155+
no_match_error = "PyPI package is only available for versions: 3.2.3",
156+
),
157+
)
158+
159+
alias(
160+
name = "whl",
161+
actual = select(
162+
{
163+
"@@rules_python//python/config_settings:is_python_3.2.3": "@pypi_32_bar_baz//:whl",
164+
},
165+
no_match_error = "PyPI package is only available for versions: 3.2.3",
166+
),
167+
)
168+
169+
alias(
170+
name = "data",
171+
actual = select(
172+
{
173+
"@@rules_python//python/config_settings:is_python_3.2.3": "@pypi_32_bar_baz//:data",
174+
},
175+
no_match_error = "PyPI package is only available for versions: 3.2.3",
176+
),
177+
)
178+
179+
alias(
180+
name = "dist_info",
181+
actual = select(
182+
{
183+
"@@rules_python//python/config_settings:is_python_3.2.3": "@pypi_32_bar_baz//:dist_info",
184+
},
185+
no_match_error = "PyPI package is only available for versions: 3.2.3",
186+
),
187+
)""",
188+
}
189+
190+
env.expect.that_dict(actual).contains_exactly(want)
191+
192+
_tests.append(_test_bzlmod_aliases_with_no_default_version)
193+
133194
def _test_bzlmod_aliases_are_created_for_all_wheels(env):
134195
actual = render_pkg_aliases(
135196
default_version = "3.2.3",

0 commit comments

Comments
 (0)
0