Skip to content

Commit 4e068dd

Browse files
authored
only attempt to use Pkg server for package downloads if the server tracks a registry containing that package (#2689)
1 parent 67a65d2 commit 4e068dd

File tree

3 files changed

+38
-30
lines changed

3 files changed

+38
-30
lines changed

src/Operations.jl

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,9 @@ function download_source(ctx::Context; readonly=true)
669669
widths = [textwidth(pkg.name) for (pkg, _) in pkgs_to_install]
670670
max_name = maximum(widths; init=0)
671671

672+
# Check what registries the current pkg server tracks
673+
server_registry_info = Registry.pkg_server_registry_info()
674+
672675
@sync begin
673676
jobs = Channel{eltype(pkgs_to_install)}(ctx.num_concurrent_downloads)
674677
results = Channel(ctx.num_concurrent_downloads)
@@ -688,9 +691,19 @@ function download_source(ctx::Context; readonly=true)
688691
end
689692
try
690693
archive_urls = Pair{String,Bool}[]
691-
if (server = pkg_server()) !== nothing
692-
url = "$server/package/$(pkg.uuid)/$(pkg.tree_hash)"
693-
push!(archive_urls, url => true)
694+
# Check if the current package is available in one of the registries being tracked by the pkg server
695+
# In that case, download from the package server
696+
if server_registry_info !== nothing
697+
server, registry_info = server_registry_info
698+
for reg in ctx.registries
699+
if reg.uuid in keys(registry_info)
700+
if haskey(reg, pkg.uuid)
701+
url = "$server/package/$(pkg.uuid)/$(pkg.tree_hash)"
702+
push!(archive_urls, url => true)
703+
break
704+
end
705+
end
706+
end
694707
end
695708
for repo_url in urls
696709
url = get_archive_url_for_version(repo_url, pkg.tree_hash)

src/Registry/Registry.jl

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -53,26 +53,10 @@ const DEFAULT_REGISTRIES =
5353
uuid = UUID("23338594-aafe-5451-b93e-139f81909106"),
5454
url = "https://github.com/JuliaRegistries/General.git")]
5555

56-
# Use the pattern
57-
#
58-
# registry_urls = nothing
59-
# for ...
60-
# url, registry_urls = pkg_server_registry_url(uuid, registry_urls)
61-
# end
62-
#
63-
# to query the pkg server at most once for registries.
64-
pkg_server_registry_url(uuid::UUID, ::Nothing) =
65-
pkg_server_registry_url(uuid, pkg_server_registry_urls())
66-
67-
pkg_server_registry_url(uuid::UUID, registry_urls::Dict{UUID, String}) =
68-
get(registry_urls, uuid, nothing), registry_urls
69-
70-
pkg_server_registry_url(::Nothing, registry_urls) = nothing, registry_urls
71-
72-
function pkg_server_registry_urls()
73-
registry_urls = Dict{UUID, String}()
56+
function pkg_server_registry_info()
57+
registry_info = Dict{UUID, Base.SHA1}()
7458
server = pkg_server()
75-
server === nothing && return registry_urls
59+
server === nothing && return nothing
7660
tmp_path = tempname()
7761
download_ok = false
7862
try
@@ -85,13 +69,24 @@ function pkg_server_registry_urls()
8569
open(tmp_path) do io
8670
for line in eachline(io)
8771
if (m = match(r"^/registry/([^/]+)/([^/]+)$", line)) !== nothing
88-
uuid = UUID(m.captures[1])
89-
hash = String(m.captures[2])
90-
registry_urls[uuid] = "$server/registry/$uuid/$hash"
72+
uuid = UUID(m.captures[1]::SubString{String})
73+
hash = Base.SHA1(m.captures[2]::SubString{String})
74+
registry_info[uuid] = hash
9175
end
9276
end
9377
end
9478
Base.rm(tmp_path, force=true)
79+
return server, registry_info
80+
end
81+
82+
function pkg_server_registry_urls()
83+
server_registry_info = pkg_server_registry_info()
84+
registry_urls = Dict{UUID, String}()
85+
server_registry_info === nothing && return registry_urls
86+
server, registry_info = server_registry_info
87+
for (uuid, hash) in registry_info
88+
registry_urls[uuid] = "$server/registry/$uuid/$hash"
89+
end
9590
return registry_urls
9691
end
9792

@@ -166,12 +161,12 @@ function download_registries(io::IO, regs::Vector{RegistrySpec}, depot::String=d
166161
populate_known_registries_with_urls!(regs)
167162
regdir = joinpath(depot, "registries")
168163
isdir(regdir) || mkpath(regdir)
169-
registry_urls = nothing
164+
registry_urls = pkg_server_registry_urls()
170165
for reg in regs
171166
if reg.path !== nothing && reg.url !== nothing
172167
Pkg.Types.pkgerror("ambiguous registry specification; both url and path is set.")
173168
end
174-
url, registry_urls = pkg_server_registry_url(reg.uuid, registry_urls)
169+
url = get(registry_urls, reg.uuid, nothing)
175170
if url !== nothing && registry_read_from_tarball()
176171
tmp = tempname()
177172
try
@@ -347,14 +342,14 @@ update(regs::Vector{String}; kwargs...) = update([RegistrySpec(name = name) for
347342
function update(regs::Vector{RegistrySpec} = RegistrySpec[]; io::IO=stderr_f(), force::Bool=true)
348343
isempty(regs) && (regs = reachable_registries(; depots=depots1()))
349344
errors = Tuple{String, String}[]
350-
registry_urls = nothing
345+
registry_urls = pkg_server_registry_urls()
351346
for reg in unique(r -> r.uuid, find_installed_registries(io, regs); seen=Set{UUID}())
352347
let reg=reg
353348
regpath = pathrepr(reg.path)
354349
if reg.tree_info !== nothing
355350
printpkgstyle(io, :Updating, "registry at " * regpath)
356351
old_hash = reg.tree_info
357-
url, registry_urls = pkg_server_registry_url(reg.uuid, registry_urls)
352+
url = get(registry_urls, reg.uuid, nothing)
358353
if url !== nothing
359354
check_registry_state(reg)
360355
end

test/utils.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ const REGISTRY_DIR = joinpath(REGISTRY_DEPOT, "registries", "General")
2020
const GENERAL_UUID = UUID("23338594-aafe-5451-b93e-139f81909106")
2121

2222
function init_reg()
23-
url, _ = Pkg.Registry.pkg_server_registry_url(GENERAL_UUID, nothing)
2423
mkpath(REGISTRY_DIR)
2524
if Pkg.Registry.registry_use_pkg_server()
25+
url = Pkg.Registry.pkg_server_registry_urls()[GENERAL_UUID]
2626
@info "Downloading General registry from $url"
2727
Pkg.PlatformEngines.download_verify_unpack(url, nothing, REGISTRY_DIR, ignore_existence = true, io = stderr)
2828
tree_info_file = joinpath(REGISTRY_DIR, ".tree_info.toml")

0 commit comments

Comments
 (0)