|
22 | 22 | ":generate_group_library_build_bazel.bzl",
|
23 | 23 | "generate_group_library_build_bazel",
|
24 | 24 | ) # buildifier: disable=bzl-visibility
|
25 |
| -load( |
26 |
| - ":labels.bzl", |
27 |
| - "DATA_LABEL", |
28 |
| - "DIST_INFO_LABEL", |
29 |
| - "PY_LIBRARY_IMPL_LABEL", |
30 |
| - "PY_LIBRARY_PUBLIC_LABEL", |
31 |
| - "WHEEL_FILE_IMPL_LABEL", |
32 |
| - "WHEEL_FILE_PUBLIC_LABEL", |
33 |
| -) |
34 | 25 | load(":parse_whl_name.bzl", "parse_whl_name")
|
35 | 26 | load(":whl_target_platforms.bzl", "whl_target_platforms")
|
36 | 27 |
|
@@ -70,117 +61,32 @@ If the value is missing, then the "default" Python version is being used,
|
70 | 61 | which has a "null" version value and will not match version constraints.
|
71 | 62 | """
|
72 | 63 |
|
73 |
| -def _render_whl_library_alias( |
74 |
| - *, |
75 |
| - name, |
76 |
| - aliases, |
77 |
| - target_name, |
78 |
| - **kwargs): |
79 |
| - """Render an alias for common targets.""" |
80 |
| - 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 |
| - ) |
90 |
| - |
91 |
| - # Create the alias repositories which contains different select |
92 |
| - # statements These select statements point to the different pip |
93 |
| - # whls that are based on a specific version of Python. |
94 |
| - selects = {} |
95 |
| - no_match_error = "_NO_MATCH_ERROR" |
96 |
| - for alias in sorted(aliases, key = lambda x: x.version): |
97 |
| - actual = "@{repo}//:{name}".format(repo = alias.repo, name = target_name) |
98 |
| - selects.setdefault(actual, []).append(alias.config_setting) |
| 64 | +def _repr_actual(aliases): |
| 65 | + if len(aliases) == 1 and not aliases[0].version and not aliases[0].config_setting: |
| 66 | + return repr(aliases[0].repo) |
99 | 67 |
|
100 |
| - return render.alias( |
101 |
| - name = name, |
102 |
| - actual = render.select( |
103 |
| - { |
104 |
| - tuple(sorted( |
105 |
| - conditions, |
106 |
| - # Group `is_python` and other conditions for easier reading |
107 |
| - # when looking at the generated files. |
108 |
| - key = lambda condition: ("is_python" not in condition, condition), |
109 |
| - )): target |
110 |
| - for target, conditions in sorted(selects.items()) |
111 |
| - }, |
112 |
| - no_match_error = no_match_error, |
113 |
| - # This key_repr is used to render selects.with_or keys |
114 |
| - key_repr = lambda x: repr(x[0]) if len(x) == 1 else render.tuple(x), |
115 |
| - name = "selects.with_or", |
116 |
| - ), |
117 |
| - **kwargs |
118 |
| - ) |
| 68 | + actual = {} |
| 69 | + for alias in aliases: |
| 70 | + actual[alias.config_setting or ("//_config:is_python_" + alias.version)] = alias.repo |
| 71 | + return render.indent(render.dict(actual)).lstrip() |
119 | 72 |
|
120 | 73 | def _render_common_aliases(*, name, aliases, extra_aliases = [], group_name = None):
|
121 |
| - lines = [ |
122 |
| - """load("@bazel_skylib//lib:selects.bzl", "selects")""", |
123 |
| - """package(default_visibility = ["//visibility:public"])""", |
124 |
| - ] |
125 |
| - |
126 |
| - config_settings = None |
127 |
| - if aliases: |
128 |
| - config_settings = sorted([v.config_setting for v in aliases if v.config_setting]) |
129 |
| - |
130 |
| - if config_settings: |
131 |
| - error_msg = NO_MATCH_ERROR_MESSAGE_TEMPLATE_V2.format( |
132 |
| - config_settings = render.indent( |
133 |
| - "\n".join(config_settings), |
134 |
| - ).lstrip(), |
135 |
| - rules_python = "rules_python", |
136 |
| - ) |
| 74 | + return """\ |
| 75 | +load("@rules_python//python/private/pypi:pkg_aliases.bzl", "pkg_aliases") |
137 | 76 |
|
138 |
| - lines.append("_NO_MATCH_ERROR = \"\"\"\\\n{error_msg}\"\"\"".format( |
139 |
| - error_msg = error_msg, |
140 |
| - )) |
| 77 | +package(default_visibility = ["//visibility:public"]) |
141 | 78 |
|
142 |
| - lines.append( |
143 |
| - render.alias( |
144 |
| - name = name, |
145 |
| - actual = repr(":pkg"), |
146 |
| - ), |
147 |
| - ) |
148 |
| - lines.extend( |
149 |
| - [ |
150 |
| - _render_whl_library_alias( |
151 |
| - name = name, |
152 |
| - aliases = aliases, |
153 |
| - target_name = target_name, |
154 |
| - visibility = ["//_groups:__subpackages__"] if name.startswith("_") else None, |
155 |
| - ) |
156 |
| - for target_name, name in ( |
157 |
| - { |
158 |
| - PY_LIBRARY_PUBLIC_LABEL: PY_LIBRARY_IMPL_LABEL if group_name else PY_LIBRARY_PUBLIC_LABEL, |
159 |
| - WHEEL_FILE_PUBLIC_LABEL: WHEEL_FILE_IMPL_LABEL if group_name else WHEEL_FILE_PUBLIC_LABEL, |
160 |
| - DATA_LABEL: DATA_LABEL, |
161 |
| - DIST_INFO_LABEL: DIST_INFO_LABEL, |
162 |
| - } | { |
163 |
| - x: x |
164 |
| - for x in extra_aliases |
165 |
| - } |
166 |
| - ).items() |
167 |
| - ], |
| 79 | +pkg_aliases( |
| 80 | + name = "{name}", |
| 81 | + actual = {actual}, |
| 82 | + group_name = {group_name}, |
| 83 | + extra_aliases = {extra_aliases}, |
| 84 | +)""".format( |
| 85 | + name = name, |
| 86 | + actual = _repr_actual(aliases), |
| 87 | + group_name = repr(group_name), |
| 88 | + extra_aliases = repr(extra_aliases), |
168 | 89 | )
|
169 |
| - if group_name: |
170 |
| - lines.extend( |
171 |
| - [ |
172 |
| - render.alias( |
173 |
| - name = "pkg", |
174 |
| - actual = repr("//_groups:{}_pkg".format(group_name)), |
175 |
| - ), |
176 |
| - render.alias( |
177 |
| - name = "whl", |
178 |
| - actual = repr("//_groups:{}_whl".format(group_name)), |
179 |
| - ), |
180 |
| - ], |
181 |
| - ) |
182 |
| - |
183 |
| - return "\n\n".join(lines) |
184 | 90 |
|
185 | 91 | def render_pkg_aliases(*, aliases, requirement_cycles = None, extra_hub_aliases = {}):
|
186 | 92 | """Create alias declarations for each PyPI package.
|
@@ -222,7 +128,7 @@ def render_pkg_aliases(*, aliases, requirement_cycles = None, extra_hub_aliases
|
222 | 128 | "{}/BUILD.bazel".format(normalize_name(name)): _render_common_aliases(
|
223 | 129 | name = normalize_name(name),
|
224 | 130 | aliases = pkg_aliases,
|
225 |
| - extra_aliases = extra_hub_aliases.get(name, []), |
| 131 | + extra_aliases = extra_hub_aliases.get(normalize_name(name), []), |
226 | 132 | group_name = whl_group_mapping.get(normalize_name(name)),
|
227 | 133 | ).strip()
|
228 | 134 | for name, pkg_aliases in aliases.items()
|
@@ -256,21 +162,17 @@ def whl_alias(*, repo, version = None, config_setting = None, filename = None, t
|
256 | 162 | if not repo:
|
257 | 163 | fail("'repo' must be specified")
|
258 | 164 |
|
259 |
| - if version: |
260 |
| - config_setting = config_setting or ("//_config:is_python_" + version) |
261 |
| - config_setting = str(config_setting) |
262 |
| - |
263 | 165 | if target_platforms:
|
264 | 166 | for p in target_platforms:
|
265 | 167 | if not p.startswith("cp"):
|
266 | 168 | fail("target_platform should start with 'cp' denoting the python version, got: " + p)
|
267 | 169 |
|
268 | 170 | return struct(
|
269 |
| - repo = repo, |
270 |
| - version = version, |
271 | 171 | config_setting = config_setting,
|
272 | 172 | filename = filename,
|
| 173 | + repo = repo, |
273 | 174 | target_platforms = target_platforms,
|
| 175 | + version = version, |
274 | 176 | )
|
275 | 177 |
|
276 | 178 | def render_multiplatform_pkg_aliases(*, aliases, **kwargs):
|
|
0 commit comments