Look up UUID of package by name in the General registry

How can I look up UUID of a package by name in the General registry (or return nothing if there is no such package?)

julia> import TOML

julia> function lookup(name)
           # Look up Registry.toml files in depots
           tomlfiles = String[]
           for d in DEPOT_PATH
               regs = joinpath(d, "registries")
               if isdir(regs)
                   for r in readdir(regs)
                       toml = joinpath(regs, r, "Registry.toml")
                       if isfile(toml)
                           push!(tomlfiles, toml)
                       end
                   end
               end
           end
           
           # Look up uuids in toml files
           uuids = Base.UUID[]
           for f in tomlfiles
               toml = TOML.parsefile(f)
               if haskey(toml, "packages")
                   for (k, v) in toml["packages"]
                       if v["name"] == name
                           push!(uuids, Base.UUID(k))
                       end
                   end
               end
           end
           return uuids
       end
lookup (generic function with 1 method)

julia> lookup("Literate")
1-element Vector{Base.UUID}:
 UUID("98b081ad-f1c9-55d3-8b20-4c87d4299306")
2 Likes

I think the question was opposite, how to do something like this:

julia> lookup("Literate")
"98b081ad-f1c9-55d3-8b20-4c87d4299306"
1 Like

Of course, I inverted the condition somehow. Fixed now.

1 Like

Thanks for the nice solution.

Incidentally, I may need to adjust my thinking a bit. I was somehow implicitly assuming that this data is stored in a data structure handled by Pkg, which consequently may/should provide an API to extract it.

But in fact, this data is in a TOML file, and when I ask Pkg to do something similar, it parses the same TOML file — I can access this data directly. I have to admit that this did not occur to me.

Incidentally, is the registry TOML format documented somewhere? It is pretty self-explanatory, just being curious.

On Pkg master (might not be on Julia master) you can do:

julia> reg = Pkg.Registry.RegistryInstance(joinpath(homedir(), ".julia/registries/General"))
Registry: "General" at "/Users/kristoffercarlsson/.julia/registries/General":
  uuid: 23338594-aafe-5451-b93e-139f81909106
  repo: https://github.com/JuliaRegistries/General.git
  git-tree-sha1: c3b2b9b7518793da48dd6cf56a9337be3dd5195f
  packages: 4754


julia> Pkg.Registry.uuids_from_name(reg, "Literate")
1-element Vector{Base.UUID}:
 UUID("98b081ad-f1c9-55d3-8b20-4c87d4299306")

(of course, these are internal functions).

3 Likes