|
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,124 +61,38 @@ 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 | | - return "" |
82 | | - |
83 | | - # Create the alias repositories which contains different select |
84 | | - # statements These select statements point to the different pip |
85 | | - # whls that are based on a specific version of Python. |
86 | | - selects = {} |
87 | | - no_match_error = "_NO_MATCH_ERROR" |
88 | | - for alias in sorted(aliases, key = lambda x: x.version): |
89 | | - actual = "@{repo}//:{name}".format(repo = alias.repo, name = target_name) |
90 | | - selects.setdefault(actual, []).append(alias.config_setting) |
91 | | - |
92 | | - return render.alias( |
93 | | - name = name, |
94 | | - actual = render.select( |
95 | | - { |
96 | | - tuple(sorted( |
97 | | - conditions, |
98 | | - # Group `is_python` and other conditions for easier reading |
99 | | - # when looking at the generated files. |
100 | | - key = lambda condition: ("is_python" not in condition, condition), |
101 | | - )): target |
102 | | - for target, conditions in sorted(selects.items()) |
103 | | - }, |
104 | | - no_match_error = no_match_error, |
105 | | - # This key_repr is used to render selects.with_or keys |
106 | | - key_repr = lambda x: repr(x[0]) if len(x) == 1 else render.tuple(x), |
107 | | - name = "selects.with_or", |
108 | | - ), |
109 | | - **kwargs |
110 | | - ) |
| 64 | +def _render_pkg_aliases(name, actual, group_name, extra_aliases): |
| 65 | + if len(actual) == 1 and None in actual: |
| 66 | + actual = repr(actual.values()[0]) |
| 67 | + else: |
| 68 | + actual = render.indent(render.dict(actual)).lstrip() |
111 | 69 |
|
112 | | -def _render_pkg_aliases(name, actual): |
113 | | - actual = actual.values()[0] if len(actual) == 1 and None in actual else ":pkg" |
114 | 70 | return """\ |
115 | 71 | pkg_aliases( |
116 | 72 | name = "{name}", |
117 | 73 | actual = {actual}, |
118 | | -)""".format(name = name, actual = repr(actual)) |
| 74 | + group_name = {group_name}, |
| 75 | + extra_aliases = {extra_aliases}, |
| 76 | +)""".format(name = name, actual = actual, group_name = repr(group_name), extra_aliases = repr(extra_aliases)) |
119 | 77 |
|
120 | 78 | def _render_common_aliases(*, name, aliases, extra_aliases = [], group_name = None): |
121 | 79 | lines = [ |
122 | 80 | """\ |
123 | | -load("@bazel_skylib//lib:selects.bzl", "selects") |
124 | 81 | load("@rules_python//python/private/pypi:pkg_aliases.bzl", "pkg_aliases")""", |
125 | 82 | """package(default_visibility = ["//visibility:public"])""", |
126 | 83 | ] |
127 | 84 |
|
128 | | - config_settings = None |
129 | | - if aliases: |
130 | | - config_settings = sorted([v.config_setting for v in aliases if v.config_setting]) |
131 | | - |
132 | | - if config_settings: |
133 | | - error_msg = NO_MATCH_ERROR_MESSAGE_TEMPLATE_V2.format( |
134 | | - config_settings = render.indent( |
135 | | - "\n".join(config_settings), |
136 | | - ).lstrip(), |
137 | | - rules_python = "rules_python", |
138 | | - ) |
139 | | - |
140 | | - lines.append("_NO_MATCH_ERROR = \"\"\"\\\n{error_msg}\"\"\"".format( |
141 | | - error_msg = error_msg, |
142 | | - )) |
143 | | - |
144 | 85 | lines.append( |
145 | 86 | _render_pkg_aliases( |
146 | 87 | name = name, |
147 | 88 | actual = { |
148 | 89 | a.config_setting: a.repo |
149 | 90 | for a in aliases |
150 | 91 | }, |
| 92 | + group_name = group_name, |
| 93 | + extra_aliases = extra_aliases, |
151 | 94 | ), |
152 | 95 | ) |
153 | | - lines.extend( |
154 | | - [ |
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 |
176 | | - ], |
177 | | - ) |
178 | | - if group_name: |
179 | | - lines.extend( |
180 | | - [ |
181 | | - render.alias( |
182 | | - name = "pkg", |
183 | | - actual = repr("//_groups:{}_pkg".format(group_name)), |
184 | | - ), |
185 | | - render.alias( |
186 | | - name = "whl", |
187 | | - actual = repr("//_groups:{}_whl".format(group_name)), |
188 | | - ), |
189 | | - ], |
190 | | - ) |
191 | 96 |
|
192 | 97 | return "\n\n".join(lines) |
193 | 98 |
|
|
0 commit comments