Skip to content

Commit

Permalink
tweak to be backwardscompatible
Browse files Browse the repository at this point in the history
  • Loading branch information
KristofferC committed May 26, 2021
1 parent 310edd2 commit 3e29422
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 35 deletions.
40 changes: 18 additions & 22 deletions src/Registry/Registry.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module Registry

import ..Pkg
using ..Pkg: depots1, printpkgstyle, DEFAULT_IO, isdir_nothrow, pathrepr, pkg_server,
GitTools, OFFLINE_MODE, UPDATED_REGISTRY_THIS_SESSION
using ..Pkg: depots1, printpkgstyle, stderr_f, isdir_nothrow, pathrepr, pkg_server,
GitTools
using ..Pkg.PlatformEngines: download_verify_unpack, download, download_verify, exe7z
using UUIDs, LibGit2, TOML

Expand Down Expand Up @@ -40,7 +40,7 @@ Pkg.Registry.add(RegistrySpec(url = "https://github.com/JuliaRegistries/General.
"""
add(reg::Union{String,RegistrySpec}; kwargs...) = add([reg]; kwargs...)
add(regs::Vector{String}; kwargs...) = add(RegistrySpec[RegistrySpec(name = name) for name in regs]; kwargs...)
function add(regs::Vector{RegistrySpec}; io::IO=DEFAULT_IO[])
function add(regs::Vector{RegistrySpec}; io::IO=stderr_f())
if isempty(regs)
download_default_registries(io, only_if_empty = false)
else
Expand Down Expand Up @@ -146,7 +146,7 @@ function registry_use_pkg_server()
end

registry_read_from_tarball() =
registry_use_pkg_server() && get(ENV, "JULIA_PKG_UNPACK_REGISTRY", "") != "true"
registry_use_pkg_server() && !(get(ENV, "JULIA_PKG_UNPACK_REGISTRY", "") == "true")

function check_registry_state(reg)
reg_currently_uses_pkg_server = reg.tree_info !== nothing
Expand Down Expand Up @@ -184,13 +184,10 @@ function download_registries(io::IO, regs::Vector{RegistrySpec}, depot::String=d
reg_unc = uncompress_registry(tmp)
reg.name = TOML.parse(reg_unc["Registry.toml"])["name"]::String
end
regpath = joinpath(regdir, reg.name)
Base.rm(regpath; recursive=true, force=true)
mkpath(regpath)
mv(tmp, joinpath(regpath, reg.name * ".tar.gz"); force=true)
mv(tmp, joinpath(regdir, reg.name * ".tar.gz"); force=true)
hash = pkg_server_url_hash(url)
reg_info = Dict("uuid" => string(reg.uuid), "git-tree-sha1" => string(hash), "filename" => reg.name * ".tar.gz")
open(joinpath(regpath, ".registry_info.toml"), "w") do io
reg_info = Dict("uuid" => string(reg.uuid), "git-tree-sha1" => string(hash), "path" => reg.name * ".tar.gz")
open(joinpath(regdir, reg.name * ".toml"), "w") do io
TOML.print(io, reg_info)
end
else
Expand Down Expand Up @@ -276,7 +273,7 @@ Pkg.Registry.rm(RegistrySpec(uuid = "23338594-aafe-5451-b93e-139f81909106"))
"""
rm(reg::Union{String,RegistrySpec}; kwargs...) = rm([reg]; kwargs...)
rm(regs::Vector{String}; kwargs...) = rm([RegistrySpec(name = name) for name in regs]; kwargs...)
function rm(regs::Vector{RegistrySpec}; io::IO=DEFAULT_IO[])
function rm(regs::Vector{RegistrySpec}; io::IO=stderr_f())
for registry in find_installed_registries(io, regs)
printpkgstyle(io, :Removing, "registry `$(registry.name)` from $(Base.contractuser(registry.path))")
Base.rm(registry.path; force=true, recursive=true)
Expand Down Expand Up @@ -341,10 +338,7 @@ Pkg.Registry.update(RegistrySpec(uuid = "23338594-aafe-5451-b93e-139f81909106"))
"""
update(reg::Union{String,RegistrySpec}; kwargs...) = update([reg]; kwargs...)
update(regs::Vector{String}; kwargs...) = update([RegistrySpec(name = name) for name in regs]; kwargs...)
function update(regs::Vector{RegistrySpec} = RegistrySpec[]; io::IO=DEFAULT_IO[], force::Bool=true)
OFFLINE_MODE[] && return
!force && UPDATED_REGISTRY_THIS_SESSION[] && return

function update(regs::Vector{RegistrySpec} = RegistrySpec[]; io::IO=stderr_f(), force::Bool=true)
isempty(regs) && (regs = reachable_registries(; depots=depots1()))
errors = Tuple{String, String}[]
registry_urls = nothing
Expand All @@ -369,12 +363,15 @@ function update(regs::Vector{RegistrySpec} = RegistrySpec[]; io::IO=DEFAULT_IO[]
catch err
@error "could not download $url" exception=err
end
Base.rm(reg.path; recursive=true, force=true)
mkpath(reg.path)
mv(tmp, joinpath(reg.path, reg.name * ".tar.gz"))
# If we have an uncompressed Pkg server registry, remove it and get the compressed version
if isdir(reg.path)
Base.rm(reg.path; recursive=true, force=true)
end
registry_path = dirname(reg.path)
mv(tmp, joinpath(registry_path, reg.name * ".tar.gz"); force=true)
hash = pkg_server_url_hash(url)
reg_info = Dict("uuid" => string(reg.uuid), "git-tree-sha1" => string(hash), "filename" => reg.name * ".tar.gz")
open(joinpath(reg.path, ".registry_info.toml"), "w") do io
reg_info = Dict("uuid" => string(reg.uuid), "git-tree-sha1" => string(hash), "path" => reg.name * ".tar.gz")
open(joinpath(registry_path, reg.name * ".toml"), "w") do io
TOML.print(io, reg_info)
end
else
Expand Down Expand Up @@ -443,7 +440,6 @@ function update(regs::Vector{RegistrySpec} = RegistrySpec[]; io::IO=DEFAULT_IO[]
end
@error warn_str
end
UPDATED_REGISTRY_THIS_SESSION[] = true
return
end

Expand All @@ -461,7 +457,7 @@ Display information about available registries.
Pkg.Registry.status()
```
"""
function status(io::IO=DEFAULT_IO[])
function status(io::IO=stderr_f())
regs = reachable_registries()
regs = unique(r -> r.uuid, regs; seen=Set{Union{UUID,Nothing}}())
printpkgstyle(io, Symbol("Registry Status"), "")
Expand Down
34 changes: 24 additions & 10 deletions src/Registry/registry_instance.jl
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ end
const REGISTRY_CACHE = Dict{String, Tuple{Base.SHA1, Bool, RegistryInstance}}()

function get_cached_registry(path, tree_info::Base.SHA1, compressed::Bool)
if !isdir(path)
if !ispath(path)
delete!(REGISTRY_CACHE, path)
return nothing
end
Expand All @@ -241,9 +241,10 @@ end

function RegistryInstance(path::AbstractString)
compressed_file = nothing
if isfile(joinpath(path, ".registry_info.toml"))
d_reg_info = parsefile(nothing, path, ".registry_info.toml")
compressed_file = d_reg_info["filename"]::String
if isfile(path)
@assert splitext(path)[2] == ".toml"
d_reg_info = parsefile(nothing, dirname(path), basename(path))
compressed_file = d_reg_info["path"]::String
tree_info = Base.SHA1(d_reg_info["git-tree-sha1"]::String)
else
tree_info_file = joinpath(path, ".tree_info.toml")
Expand All @@ -262,7 +263,7 @@ function RegistryInstance(path::AbstractString)
end

in_memory_registry = if compressed_file !== nothing
uncompress_registry(joinpath(path, compressed_file))
uncompress_registry(joinpath(dirname(path), compressed_file))
else
nothing
end
Expand Down Expand Up @@ -329,11 +330,24 @@ function reachable_registries(; depots::Union{String, Vector{String}}=Base.DEPOT
isdir(d) || continue
reg_dir = joinpath(d, "registries")
isdir(reg_dir) || continue
for name in readdir(reg_dir)
file_unpacked = joinpath(reg_dir, name, "Registry.toml")
file_packed = joinpath(reg_dir, name, ".registry_info.toml")
if isfile(file_unpacked) || isfile(file_packed)
push!(registries, RegistryInstance(joinpath(reg_dir, name)))
reg_paths = readdir(reg_dir; join=true)
candidate_registries = String[]
# All folders could be registries
append!(candidate_registries, filter(isdir, reg_paths))
if registry_read_from_tarball()
compressed_registries = filter(endswith(".toml"), reg_paths)
# if we are reading compressed registries, ignore compressed registries
# with the same name
compressed_registry_names = Set([splitext(basename(file))[1] for file in compressed_registries])
filter!(x -> !(basename(x) in compressed_registry_names), candidate_registries)
append!(candidate_registries, compressed_registries)
end

for candidate in candidate_registries
candidate = joinpath(reg_dir, candidate)
# candidate can be either a folder or a TOML file
if isfile(joinpath(candidate, "Registry.toml")) || isfile(candidate)
push!(registries, RegistryInstance(candidate))
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions test/registry.jl
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ if Pkg.Registry.registry_use_pkg_server()

# This should not uncompress the registry
Registry.add(RegistrySpec(uuid = UUID("23338594-aafe-5451-b93e-139f81909106")))
@test isfile(joinpath(DEPOT_PATH[1], "registries", "General", "General.tar.gz")) != something(unpack, false)
@test isfile(joinpath(DEPOT_PATH[1], "registries", "General.tar.gz")) != something(unpack, false)
Pkg.add("Example")

# Write some bad git-tree-sha1 here so that Pkg.update will have to update the registry
Expand All @@ -340,11 +340,11 @@ if Pkg.Registry.registry_use_pkg_server()
git-tree-sha1 = "179182faa6a80b3cf24445e6f55c954938d57941"
""")
else
write(joinpath(DEPOT_PATH[1], "registries", "General", ".registry_info.toml"),
write(joinpath(DEPOT_PATH[1], "registries", "General.toml"),
"""
git-tree-sha1 = "179182faa6a80b3cf24445e6f55c954938d57941"
uuid = "23338594-aafe-5451-b93e-139f81909106"
filename = "General.tar.gz"
path = "General.tar.gz"
""")
end
Pkg.update()
Expand Down

0 comments on commit 3e29422

Please sign in to comment.