Reza Afzalan
Reza Afzalan

Reputation: 5756

Is there any way to build package dependency tree in julia-lang?

Using npm list will show a tree of installed packages, versions and relations:

enter image description here

Although Julia package management is differ (e.g normally no duplicate copy of a package exists), But is there any way to:

Upvotes: 10

Views: 1876

Answers (3)

Matěj Račinský
Matěj Račinský

Reputation: 1804

There is a package that does this: https://github.com/peng1999/PkgDependency.jl It's quite straightforward to use

pkg> add PkgDependency
julia> using PkgDependency
julia> PkgDependency.tree("HTTP")
HTTP v0.9.13 
    NetworkOptions v1.2.0
    IniFile v0.5.0
    URIs v1.3.0
    MbedTLS v1.0.3
        MbedTLS_jll v2.16.8+1
            JLLWrappers v1.3.0
                Preferences v1.2.2
                    TOML v1.0.3
            Artifacts v1.3.0

It relies on a new (>1.4) API, which can be used on its own to list (direct) dependencies, but it's quite ugly:

julia> using Pkg

julia> Pkg.dependencies()[Pkg.project().dependencies["HTTP"]].dependencies
Dict{String, Base.UUID} with 5 entries:
  "Dates"   => UUID("ade2ca70-3891-5945-98fb-dc099432e06a")
  "Base64"  => UUID("2a0f44e3-6c83-55bd-87e4-b1978d98bd5f")
  "Sockets" => UUID("6462fe0b-24de-5631-8697-dd941f90decc")
  "IniFile" => UUID("83e8ac13-25f8-5344-8a64-a9f2b223428f")
  "MbedTLS" => UUID("739be429-bea8-5141-9913-cc70e7f3736d")

If you only want the list for dependencies of the currently active project, then Pkg.project().dependencies is enough.

I've opened a Pkg.jl issue to discuss this here.

Upvotes: 4

Reza Afzalan
Reza Afzalan

Reputation: 5756

simple code bellow will printout the requires-by result for a package:

whorequires(pkgname) = whorequires(pkgname, 0);
function whorequires(pkgname, i)
  deps = Pkg.dependents(pkgname);
  [print('-') for j=1:i];
  println(pkgname);
  length(deps) > 0 && [whorequires(dep,i+1) for dep in deps];
end

julia> whorequires("JuliaParser");
JuliaParser
-CodeTools
--Atom
-ASTInterpreter
--Gallium
-Jewel

EDIT (to cover issue commented by @amrods)

whorequires(pkgname) = whorequires(pkgname, 0, Dict{ASCIIString,Vector{ASCIIString}}());
function whorequires(pkgname, i, m)
  [print('-') for j=1:i];
  if (haskey(m,pkgname))
    println("$pkgname *[duplicated]* is required by ", length(m[pkgname]), " packages");
  else
    println(pkgname);
    m[pkgname] = Pkg.dependents(pkgname);
    if (length(m[pkgname]) > 0)
        for dep in m[pkgname]
            whorequires(dep,i+1,m);
        end
    end
  end
end

Upvotes: 3

daycaster
daycaster

Reputation: 2707

I don't think there's a simple function, but it shouldn't be too hard to do with these two functions:

julia> Pkg.dependents("Cairo")
10-element Array{AbstractString,1}:
 "Tk"
 "Gtk"
 "Mamba"
 "Drawing"
 "GtkUtilities"
 "ProfileView"
 "Brim"
 "Winston"
 "EcologicalNetwork"
 "VennEuler"

julia> Pkg.installed()
Dict{ASCIIString,VersionNumber} with 119 entries:
  "Libz"             => v"0.0.2"
  "Gtk"              => v"0.9.3"
  "Interact"         => v"0.3.0"
  "ForwardDiff"      => v"0.1.4"
  "Benchmark"        => v"0.1.0"
  "AxisAlgorithms"   => v"0.1.4"
  "Cairo"            => v"0.2.31+"
  "HttpParser"       => v"0.1.1"
  "DataFrames"       => v"0.6.10"
  "Requests"         => v"0.3.4"
  "QuartzImageIO"    => v"0.1.0+"
  "Markdown"         => v"0.3.0"
  "Requires"         => v"0.2.2"
  "ArgParse"         => v"0.3.0"
  ⋮                  => ⋮

Upvotes: 4

Related Questions