How to save the output from Pkg.status() as a string variable?

If I do something like version = Pkg.status(), then version is nothing. How do I save the output from Pkg.status() to a string variable?

All I need is just the package name with the version number in a human readable format, so Project.toml is not helpful. I notice a similar question here, but from what I understand the answer provided there let us save the output to a text file, and I wonder if there is a more direct way without saving it to a text file.

From the help page of Pkg.status()

See Pkg.project and Pkg.dependencies to get the project/manifest status as a Julia object instead of printing it.

julia> proj = Pkg.project()
Pkg.API.ProjectInfo(nothing, nothing, nothing, false, Dict{String, Base.UUID}("OffsetArrays" => UUID("6fe1bfb0-de20-5000-8ca7-80f57d26f881"), "HDF5" => UUID("f67ccb44-e63f-5c2f-98bd-6dc0ccc4ba2f"), "MortgageCalculators" => UUID("cbda39b7-a5b8-49f7-bf50-e22af4f2d7a9"), "Revise" => UUID("295af30f-e4ad-537b-8983-00126c2a3abe"), "SIMD" => UUID("fdea26ae-647d-5447-a871-4b548cad5224"), "UnicodePlots" => UUID("b8865327-cd53-5732-bb35-84acbb429228"), "BenchmarkTools" => UUID("6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"), "StaticStrings" => UUID("4db0a0c5-418a-4e1d-8806-cb305fe13294"), "FixedPointNumbers" => UUID("53c48c17-4a7d-5ca2-90c5-79b7896eea93")), "/root/.julia/environments/v1.9/Project.toml")

julia> proj.dependencies
Dict{String, Base.UUID} with 9 entries:
  "OffsetArrays"        => UUID("6fe1bfb0-de20-5000-8ca7-80f57d26f881…
  "HDF5"                => UUID("f67ccb44-e63f-5c2f-98bd-6dc0ccc4ba2f…
  "MortgageCalculators" => UUID("cbda39b7-a5b8-49f7-bf50-e22af4f2d7a9…
  "Revise"              => UUID("295af30f-e4ad-537b-8983-00126c2a3abe…
  "SIMD"                => UUID("fdea26ae-647d-5447-a871-4b548cad5224…
  "UnicodePlots"        => UUID("b8865327-cd53-5732-bb35-84acbb429228…
  "BenchmarkTools"      => UUID("6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf…
  "StaticStrings"       => UUID("4db0a0c5-418a-4e1d-8806-cb305fe13294…
  "FixedPointNumbers"   => UUID("53c48c17-4a7d-5ca2-90c5-79b7896eea93…

julia> Pkg.dependencies()
Dict{Base.UUID, Pkg.API.PackageInfo} with 98 entries:
  UUID("fb4d412d-6eee-5… => PackageInfo("FixedPointDecimals", v"0.4.2…
  UUID("7cb0a576-ebde-5… => PackageInfo("MPICH_jll", v"4.1.2+0", "8a5…
  UUID("477f73a3-ac25-5… => PackageInfo("libaec_jll", v"1.0.6+1", "ed…
  UUID("fdea26ae-647d-5… => PackageInfo("SIMD", v"3.4.4", "8b20084a97…
  UUID("3da002f7-5984-5… => PackageInfo("ColorTypes", v"0.11.4", "eb7…
  UUID("a8cc5b0e-0ffa-5… => PackageInfo("Crayons", v"4.1.1", "249fe38…
  UUID("92d709cd-6900-4… => PackageInfo("IrrationalConstants", v"0.2.…
  UUID("a63ad114-7e13-5… => PackageInfo("Mmap", nothing, nothing, fal…
  UUID("79e6a3ab-5dfb-5… => PackageInfo("Adapt", v"3.5.0", "0310e08cb…
  UUID("6fe1bfb0-de20-5… => PackageInfo("OffsetArrays", v"1.12.9", "8…
  UUID("76f85450-5226-5… => PackageInfo("LibGit2", nothing, nothing, …
  UUID("2a0f44e3-6c83-5… => PackageInfo("Base64", nothing, nothing, f…
  UUID("05823500-19ac-5… => PackageInfo("OpenLibm_jll", v"0.8.1+0", n…
  UUID("cbda39b7-a5b8-4… => PackageInfo("MortgageCalculators", v"1.0.…
  UUID("295af30f-e4ad-5… => PackageInfo("Revise", v"3.5.9", "a38e7d70…
  UUID("56f22d72-fd6d-9… => PackageInfo("Artifacts", nothing, nothing…
  UUID("682c06a0-de6a-5… => PackageInfo("JSON", v"0.21.4", "31e996f0a…
  UUID("b77e0a4c-d291-5… => PackageInfo("InteractiveUtils", nothing, …
  UUID("21216c6a-2e73-6… => PackageInfo("Preferences", v"1.3.0", "47e…
  UUID("62fd8b95-f654-4… => PackageInfo("TensorCore", v"0.1.1", "1feb…
  UUID("7b1f6079-737a-5… => PackageInfo("FileWatching", nothing, noth…
  UUID("bea87d4a-7f5b-5… => PackageInfo("SuiteSparse_jll", v"5.10.1+6…
  UUID("e66e0078-7015-5… => PackageInfo("CompilerSupportLibraries_jll…
  UUID("37e2e46d-f89d-5… => PackageInfo("LinearAlgebra", nothing, not…
  UUID("458c3c95-2e84-5… => PackageInfo("OpenSSL_jll", v"3.0.9+0", "c…
  UUID("aa1ae85d-cabe-5… => PackageInfo("JuliaInterpreter", v"0.9.26"…
  UUID("ade2ca70-3891-5… => PackageInfo("Dates", nothing, nothing, fa…
  UUID("276daf66-3868-5… => PackageInfo("SpecialFunctions", v"2.3.0",…
  ⋮                      => ⋮
1 Like

What about:

io = IOBuffer()
Pkg.status(; io=io)
st = String(take!(io))

See also: https://github.com/ufechner7/PkgHelpers.jl/blob/main/src/PkgHelpers.jl

1 Like

looking up the dictionary from Pkg.dependencies() is my current approach though I need to manually specify the package I’m checking. Pkg.project().dependencies is essentially just the Project.toml file and the UUID are not really helpful for my purpose. I’m just wondering if there’s a simple IO method where I can direct the output from Pkg.status() (something exactly what I need) to a string variable, something I thought was trivial but apparently not the case.

@ufechner7 just gave that to you above.

julia> io = IOBuffer()
IOBuffer(data=UInt8[...], readable=true, writable=true, seekable=true, append=false, size=0, maxsize=Inf, ptr=1, mark=-1)

julia> Pkg.status(; io=io)

julia> st = String(take!(io))
"Status `~/.julia/environments/v1.9/Project.toml`\n  [6e4b80f9] BenchmarkTools v1.3.2\n  [53c48c17] FixedPointNumbers v0.8.4\n⌃ [f67ccb44] HDF5 v0.16.16\n  [cbda39b7] MortgageCalculators v1.0.0-DEV `https://github.com/mkitti/MortgageCalculators.jl#main`\n⌃ [6fe1bfb0] OffsetArrays v1.12.9\n⌃ [295af30f] Revise v3.5.9\n⌃ [fdea26ae] SIMD v3.4.4\n⌃ [4db0a0c5] StaticStrings v0.2.3\n  [b8865327] UnicodePlots v3.6.0\nInfo Packages marked with ⌃ have new versions available and may be upgradable.\n"

julia> println(st)
Status `~/.julia/environments/v1.9/Project.toml`
  [6e4b80f9] BenchmarkTools v1.3.2
  [53c48c17] FixedPointNumbers v0.8.4
⌃ [f67ccb44] HDF5 v0.16.16
  [cbda39b7] MortgageCalculators v1.0.0-DEV `https://github.com/mkitti/MortgageCalculators.jl#main`
⌃ [6fe1bfb0] OffsetArrays v1.12.9
⌃ [295af30f] Revise v3.5.9
⌃ [fdea26ae] SIMD v3.4.4
⌃ [4db0a0c5] StaticStrings v0.2.3
  [b8865327] UnicodePlots v3.6.0
Info Packages marked with ⌃ have new versions available and may be upgradable.
1 Like

In case you also wanted to preserve the color output, use IOContext.

julia> io = IOBuffer()
IOBuffer(data=UInt8[...], readable=true, writable=true, seekable=true, append=false, size=0, maxsize=Inf, ptr=1, mark=-1)

julia> ioc = IOContext(io, :color => true)
IOContext(IOBuffer(data=UInt8[...], readable=true, writable=true, seekable=true, append=false, size=0, maxsize=Inf, ptr=1, mark=-1))

julia> Pkg.status(; io=ioc)

julia> st = String(take!(io))
"\e[32m\e[1mStatus\e[22m\e[39m `~/.julia/environments/v1.9/Project.toml`\n  \e[90m[6e4b80f9] \e[39mBenchmarkTools v1.3.2\n  \e[90m[53c48c17] \e[39mFixedPointNumbers v0.8.4\n\e[32m⌃\e[39m \e[90m[f67ccb44] \e[39mHDF5 v0.16.16\n  \e[90m[cbda39b7] \e[39mMortg" ⋯ 204 bytes ⋯ "0m[fdea26ae] \e[39mSIMD v3.4.4\n\e[32m⌃\e[39m \e[90m[4db0a0c5] \e[39mStaticStrings v0.2.3\n  \e[90m[b8865327] \e[39mUnicodePlots v3.6.0\n\e[36m\e[1mInfo\e[22m\e[39m Packages marked with \e[32m⌃\e[39m have new versions available and may be upgradable.\n"

julia> println(st)
Status `~/.julia/environments/v1.9/Project.toml`
  [6e4b80f9] BenchmarkTools v1.3.2
  [53c48c17] FixedPointNumbers v0.8.4
⌃ [f67ccb44] HDF5 v0.16.16
  [cbda39b7] MortgageCalculators v1.0.0-DEV `https://github.com/mkitti/MortgageCalculators.jl#main`
⌃ [6fe1bfb0] OffsetArrays v1.12.9
⌃ [295af30f] Revise v3.5.9
⌃ [fdea26ae] SIMD v3.4.4
⌃ [4db0a0c5] StaticStrings v0.2.3
  [b8865327] UnicodePlots v3.6.0
Info Packages marked with ⌃ have new versions available and may be upgradable.

2 Likes