Skip to content

Commit 06ca1b4

Browse files
committed
feat(uv): handle credential helpers and .netrc
This allows one to download the uv binaries from private mirrors. Work towards bazel-contrib#1975.
1 parent a2ff7da commit 06ca1b4

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ END_UNRELEASED_TEMPLATE
9797
* (pypi) `RULES_PYTHON_ENABLE_PIPSTAR` environment variable: when `1`, the Starlark
9898
implementation of wheel METADATA parsing is used (which has improved multi-platform
9999
build support).
100+
* (uv) Handle `.netrc` and `auth_patterns` auth when downloading `uv`. Work towards
101+
[#1975](https://github.com/bazel-contrib/rules_python/issues/1975).
100102

101103
{#v0-0-0-removed}
102104
### Removed

python/uv/private/uv.bzl

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ EXPERIMENTAL: This is experimental and may be removed without notice
1818
A module extension for working with uv.
1919
"""
2020

21+
load("//python/private:auth.bzl", "AUTH_ATTRS", "get_auth")
2122
load(":toolchain_types.bzl", "UV_TOOLCHAIN_TYPE")
2223
load(":uv_repository.bzl", "uv_repository")
2324
load(":uv_toolchains_repo.bzl", "uv_toolchains_repo")
@@ -77,7 +78,7 @@ The version of uv to configure the sources for. If this is not specified it will
7778
last version used in the module or the default version set by `rules_python`.
7879
""",
7980
),
80-
}
81+
} | AUTH_ATTRS
8182

8283
default = tag_class(
8384
doc = """\
@@ -133,7 +134,7 @@ for a particular version.
133134
},
134135
)
135136

136-
def _configure(config, *, platform, compatible_with, target_settings, urls = [], sha256 = "", override = False, **values):
137+
def _configure(config, *, platform, compatible_with, target_settings, auth_patterns, urls = [], sha256 = "", override = False, **values):
137138
"""Set the value in the config if the value is provided"""
138139
for key, value in values.items():
139140
if not value:
@@ -144,6 +145,7 @@ def _configure(config, *, platform, compatible_with, target_settings, urls = [],
144145

145146
config[key] = value
146147

148+
config.setdefault("auth_patterns", {}).update(auth_patterns)
147149
config.setdefault("platforms", {})
148150
if not platform:
149151
if compatible_with or target_settings or urls:
@@ -189,8 +191,10 @@ def process_modules(
189191

190192
# default values to apply for version specific config
191193
defaults = {
194+
"auth_patterns": {},
192195
"base_url": "",
193196
"manifest_filename": "",
197+
"netrc": "",
194198
"platforms": {
195199
# The structure is as follows:
196200
# "platform_name": struct(
@@ -216,6 +220,8 @@ def process_modules(
216220
compatible_with = tag.compatible_with,
217221
target_settings = tag.target_settings,
218222
override = mod.is_root,
223+
netrc = tag.netrc,
224+
auth_patterns = tag.auth_patterns,
219225
)
220226

221227
for key in [
@@ -253,8 +259,10 @@ def process_modules(
253259
specific_config = versions.setdefault(
254260
last_version,
255261
{
262+
"auth_patterns": defaults["auth_patterns"],
256263
"base_url": defaults["base_url"],
257264
"manifest_filename": defaults["manifest_filename"],
265+
"netrc": defaults["netrc"],
258266
# shallow copy is enough as the values are structs and will
259267
# be replaced on modification
260268
"platforms": dict(defaults["platforms"]),
@@ -271,6 +279,8 @@ def process_modules(
271279
sha256 = tag.sha256,
272280
urls = tag.urls,
273281
override = mod.is_root,
282+
netrc = tag.netrc,
283+
auth_patterns = tag.auth_patterns,
274284
)
275285

276286
if not versions:
@@ -313,6 +323,10 @@ def process_modules(
313323
),
314324
manifest_filename = config["manifest_filename"],
315325
platforms = sorted(platforms),
326+
attr = struct(
327+
netrc = config["netrc"],
328+
auth_patterns = config["auth_patterns"],
329+
),
316330
)
317331

318332
for platform_name, platform in platforms.items():
@@ -327,6 +341,8 @@ def process_modules(
327341
platform = platform_name,
328342
urls = urls[platform_name].urls,
329343
sha256 = urls[platform_name].sha256,
344+
auth_patterns = config["auth_patterns"],
345+
netrc = config["netrc"],
330346
)
331347

332348
toolchain_names.append(toolchain_name)
@@ -363,7 +379,7 @@ def _overlap(first_collection, second_collection):
363379

364380
return False
365381

366-
def _get_tool_urls_from_dist_manifest(module_ctx, *, base_url, manifest_filename, platforms):
382+
def _get_tool_urls_from_dist_manifest(module_ctx, *, base_url, manifest_filename, platforms, attr):
367383
"""Download the results about remote tool sources.
368384
369385
This relies on the tools using the cargo packaging to infer the actual
@@ -432,9 +448,11 @@ def _get_tool_urls_from_dist_manifest(module_ctx, *, base_url, manifest_filename
432448
]
433449
"""
434450
dist_manifest = module_ctx.path(manifest_filename)
451+
urls = [base_url + "/" + manifest_filename]
435452
result = module_ctx.download(
436-
base_url + "/" + manifest_filename,
453+
url = urls,
437454
output = dist_manifest,
455+
auth = get_auth(module_ctx, urls, ctx_attr = attr),
438456
)
439457
if not result.success:
440458
fail(result)
@@ -454,11 +472,13 @@ def _get_tool_urls_from_dist_manifest(module_ctx, *, base_url, manifest_filename
454472

455473
checksum_fname = checksum["name"]
456474
checksum_path = module_ctx.path(checksum_fname)
475+
urls = ["{}/{}".format(base_url, checksum_fname)]
457476
downloads[checksum_path] = struct(
458477
download = module_ctx.download(
459-
"{}/{}".format(base_url, checksum_fname),
478+
url = urls,
460479
output = checksum_path,
461480
block = False,
481+
auth = get_auth(module_ctx, urls, ctx_attr = attr),
462482
),
463483
archive_fname = fname,
464484
platforms = checksum["target_triples"],

python/uv/private/uv_repository.bzl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ EXPERIMENTAL: This is experimental and may be removed without notice
1818
Create repositories for uv toolchain dependencies
1919
"""
2020

21+
load("//python/private:auth.bzl", "AUTH_ATTRS", "get_auth")
22+
2123
UV_BUILD_TMPL = """\
2224
# Generated by repositories.bzl
2325
load("@rules_python//python/uv:uv_toolchain.bzl", "uv_toolchain")
@@ -43,6 +45,7 @@ def _uv_repo_impl(repository_ctx):
4345
url = repository_ctx.attr.urls,
4446
sha256 = repository_ctx.attr.sha256,
4547
stripPrefix = strip_prefix,
48+
auth = get_auth(repository_ctx, repository_ctx.attr.urls),
4649
)
4750

4851
binary = "uv.exe" if is_windows else "uv"
@@ -70,5 +73,5 @@ uv_repository = repository_rule(
7073
"sha256": attr.string(mandatory = False),
7174
"urls": attr.string_list(mandatory = True),
7275
"version": attr.string(mandatory = True),
73-
},
76+
} | AUTH_ATTRS,
7477
)

0 commit comments

Comments
 (0)