Skip to content

Commit 832007c

Browse files
committed
simplify and reuse code
1 parent df80ed8 commit 832007c

File tree

1 file changed

+58
-52
lines changed

1 file changed

+58
-52
lines changed

python/uv/private/uv.bzl

Lines changed: 58 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,32 @@ for a particular version.
135135
},
136136
)
137137

138+
def _build_config(config, *, platform, compatible_with, target_settings, urls = [], sha256 = "", **values):
139+
"""Set the value in the config if the value is provided"""
140+
for key, value in values.items():
141+
if not value:
142+
continue
143+
144+
config[key] = value
145+
146+
config.setdefault("platforms", {})
147+
if platform and not (compatible_with or target_settings or urls):
148+
config["platforms"].pop(platform)
149+
elif platform:
150+
if compatible_with or target_settings:
151+
config["platforms"][platform] = struct(
152+
name = platform.replace("-", "_").lower(),
153+
compatible_with = compatible_with,
154+
target_settings = target_settings,
155+
)
156+
if urls:
157+
config.setdefault("urls", {})[platform] = struct(
158+
sha256 = sha256,
159+
urls = urls,
160+
)
161+
elif compatible_with or target_settings:
162+
fail("`platform` name must be specified when specifying `compatible_with` or `target_settings`")
163+
138164
def parse_modules(module_ctx, uv_repository = None):
139165
"""Parse the modules to get the config for 'uv' toolchains.
140166
@@ -145,67 +171,47 @@ def parse_modules(module_ctx, uv_repository = None):
145171
Returns:
146172
A dictionary for each version of the `uv` to configure.
147173
"""
148-
config = {
149-
"platforms": {},
150-
}
174+
config = {}
151175

152176
for mod in module_ctx.modules:
153177
for default_attr in mod.tags.default:
154-
if default_attr.version:
155-
config["version"] = default_attr.version
156-
157-
if default_attr.base_url:
158-
config["base_url"] = default_attr.base_url
159-
160-
if default_attr.manifest_filename:
161-
config["manifest_filename"] = default_attr.manifest_filename
162-
163-
if default_attr.platform and not (default_attr.compatible_with or default_attr.target_settings):
164-
config["platforms"].pop(default_attr.platform)
165-
elif default_attr.platform:
166-
config["platforms"].setdefault(
167-
default_attr.platform,
168-
struct(
169-
name = default_attr.platform.replace("-", "_").lower(),
170-
compatible_with = default_attr.compatible_with,
171-
target_settings = default_attr.target_settings,
172-
),
173-
)
174-
elif default_attr.compatible_with or default_attr.target_settings:
175-
fail("TODO: unsupported")
178+
_build_config(
179+
config,
180+
version = default_attr.version,
181+
base_url = default_attr.base_url,
182+
manifest_filename = default_attr.manifest_filename,
183+
platform = default_attr.platform,
184+
compatible_with = default_attr.compatible_with,
185+
target_settings = default_attr.target_settings,
186+
)
176187

177188
versions = {}
178189
for mod in module_ctx.modules:
179190
last_version = None
180191
for config_attr in mod.tags.configure:
181192
last_version = config_attr.version or last_version or config["version"]
182-
specific_config = versions.setdefault(last_version, {
183-
"base_url": config.get("base_url", ""),
184-
"manifest_filename": config["manifest_filename"],
185-
"platforms": {k: v for k, v in config["platforms"].items()}, # make a copy
186-
})
187-
if config_attr.platform and not (config_attr.compatible_with or config_attr.target_settings or config_attr.urls):
188-
specific_config["platforms"].pop(config_attr.platform)
189-
elif config_attr.platform:
190-
if config_attr.compatible_with or config_attr.target_settings:
191-
specific_config["platforms"][config_attr.platform] = struct(
192-
name = config_attr.platform.replace("-", "_").lower(),
193-
compatible_with = config_attr.compatible_with,
194-
target_settings = config_attr.target_settings,
195-
)
196-
if config_attr.urls:
197-
specific_config.setdefault("urls", {})[config_attr.platform] = struct(
198-
sha256 = config_attr.sha256,
199-
urls = config_attr.urls,
200-
)
201-
elif config_attr.compatible_with or config_attr.target_settings:
202-
fail("TODO: unsupported")
203-
204-
if config_attr.base_url:
205-
specific_config["base_url"] = config_attr.base_url
206-
207-
if config_attr.manifest_filename:
208-
specific_config["manifest_filename"] = config_attr.manifest_filename
193+
if not last_version:
194+
fail("version must be specified")
195+
196+
specific_config = versions.setdefault(
197+
last_version,
198+
{
199+
"base_url": config.get("base_url", ""),
200+
"manifest_filename": config["manifest_filename"],
201+
"platforms": dict(config["platforms"]), # copy
202+
},
203+
)
204+
205+
_build_config(
206+
specific_config,
207+
base_url = config_attr.base_url,
208+
manifest_filename = config_attr.manifest_filename,
209+
platform = config_attr.platform,
210+
compatible_with = config_attr.compatible_with,
211+
target_settings = config_attr.target_settings,
212+
sha256 = config_attr.sha256,
213+
urls = config_attr.urls,
214+
)
209215

210216
versions = {
211217
version: config

0 commit comments

Comments
 (0)