# LightGraph tutorial

**URL:** <https://discourse.julialang.org/t/lightgraph-tutorial/31070>\
**Category:** General Usage\
**Created:** [November 14, 2019, 1:07am UTC](https://discourse.julialang.org/t/lightgraph-tutorial/31070 "2019-11-14T01:07:09Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![gmouts](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gmouts/32/9896_2.png) [@gmouts](https://discourse.julialang.org/u/gmouts)\
**Post date:** [November 14, 2019, 1:07am UTC](https://discourse.julialang.org/t/lightgraph-tutorial/31070/1 "2019-11-14T01:07:10Z")

</div>

I am trying to replicate [this LightGraph tutorial](https://nbviewer.jupyter.org/github/JuliaGraphs/JuliaGraphsTutorials/blob/master/DAG-Julia-Pkgs.ipynb). Right at the start it has this snippet of code:

```julia
pkgs = readdir(Pkg.dir("METADATA"))
filterfunc = p -> isdir(joinpath(Pkg.dir("METADATA"), p)) && p ∉ [".git",".test"]
pkgs = filter(filterfunc, pkgs)

```

Julia tells me that `Pkg.dir` is deprecated and it returns nothing. What is the first line of this code supposed to do and how can I replicate this on Julia 1.2?

---

<div class="post-metadata">

**Author:** ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)\
**Post date:** [November 14, 2019, 2:28am UTC](https://discourse.julialang.org/t/lightgraph-tutorial/31070/2 "2019-11-14T02:28:29Z")

</div>

METADATA was the old way that the old package system kept track of registered packages. IIRC, it contained a folder for each registered package, and I think those first lines of code are just getting all of the package names by reading the contents of the folder.

There are a bunch of old Pkg features that tutorial uses that are no longer relevant, but Pkg3 (the new package manager) can probably be leveraged to do the same work. Alas, I don’t know enough to help much, but what you want to do is

1. Get a list of all packages in the General registry (that’s analogous to the old METADATA)
2. Parse their Project.toml files to get their dependences

Good luck!

---

<div class="post-metadata">

**Author:** ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)\
**Post date:** [November 14, 2019, 8:27am UTC](https://discourse.julialang.org/t/lightgraph-tutorial/31070/3 "2019-11-14T08:27:52Z")

</div>

It’s always worth looking in the Issues for a project to see if there are any known problems and work rounds.

In this case, [there are known problems](https://github.com/JuliaGraphs/JuliaGraphsTutorials/issues/12) but no work rounds as yet. 😕

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [November 14, 2019, 10:18am UTC](https://discourse.julialang.org/t/lightgraph-tutorial/31070/4 "2019-11-14T10:18:23Z")

</div>

This is unlikely to be the proper way to do it but it should give you the corresponding list of packages:

```julia
using Pkg.TOML: parsefile
registry = parsefile(joinpath(first(DEPOT_PATH), "registries", "General", "Registry.toml"))
pkgs = [package["name"] for package in values(registry["packages"])]

```

As already mentioned this is only the first step and there are more things that have changed with the package manager. However, all necessary information to build the package DAG is available in the same directory and can be obtained using `parsefile` on the appropriate TOML files in there. An earlier discussion may also be helpful to get further: [Replacement for Pkg.dependents() in 1.0?](https://discourse.julialang.org/t/replacement-for-pkg-dependents-in-1-0/24131)

---

<div class="post-metadata">

**Author:** ![gmouts](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gmouts/32/9896_2.png) [@gmouts](https://discourse.julialang.org/u/gmouts)\
**Post date:** [November 14, 2019, 12:33pm UTC](https://discourse.julialang.org/t/lightgraph-tutorial/31070/5 "2019-11-14T12:33:51Z")

</div>

Thanks, that helped a lot. I parsed the Deps.toml files of the packages and I have constructed the DAG. However, I have included dependencies that have been dropped because I didn’t find an easy way to exclude them.

By the way, through the graph I found that the 10 most “important” packages (not included in julia by default) are in order: Tokenize, CSTParser, JuliaFormatter, DocumentFormat, AutomaticDocstrings, SourceWalk, MacroTools, SimpleTraits, ZygoteRules and Unrolled. 🙂

---

<div class="post-metadata">

**Author:** ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)\
**Post date:** [November 14, 2019, 8:47pm UTC](https://discourse.julialang.org/t/lightgraph-tutorial/31070/6 "2019-11-14T20:47:24Z")

</div>

That’s great! You should totally make a PR to update the notebook!

---

<div class="post-metadata">

**Author:** ![gmouts](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gmouts/32/9896_2.png) [@gmouts](https://discourse.julialang.org/u/gmouts)\
**Post date:** [November 14, 2019, 11:16pm UTC](https://discourse.julialang.org/t/lightgraph-tutorial/31070/7 "2019-11-14T23:16:47Z")

</div>

Unfortunately I don’t know how to deal with notebooks and I don’t have time to learn now. I’m dumping my code here and anyone who feels like updating the example should feel free to use it. 🙂

```julia
using Pkg.TOML: parsefile
using LightGraphs

registry = parsefile(joinpath(first(DEPOT_PATH), "registries", "General", "Registry.toml"))
pkgs = [package["name"] for package in values(registry["packages"])]

pkgdict = Dict{String,Int}()
pkgdictinv = Dict{Int,String}()
for (i,pkg) in enumerate(pkgs)
  push!(pkgdict, pkg => i)
  push!(pkgdictinv, i => pkg )
end

DAG = DiGraph(length(pkgs))

pth = joinpath(first(DEPOT_PATH), "registries", "General")
lttrfolders = readdir(pth)
pathfilterfunc = p -> isdir(joinpath(pth, p)) && p[1] != '.'
lttrfolders = filter(pathfilterfunc, lttrfolders)
for lttr in lttrfolders
  ps = readdir(joinpath(pth, lttr))
  for p in ps
    try
      lns = readlines(joinpath(joinpath(pth, lttr), p, "Deps.toml"))
    catch e
      println(e)
      continue
    end
    linefilterfunc = p -> p != "" && p[1] != '['
    lns = filter(linefilterfunc, lns)
    deps = [split(l," ")[1] for l in lns]
    for dep in deps
      try
        if pkgdict[p] == pkgdict[dep]
          println(p)
        else
          try
            add_edge!(DAG, pkgdict[dep], pkgdict[p])
          catch e
            println(e)
            return
          end
        end
      catch e
        # println(e)
      end
    end
  end
end

```
