Build a dependency graph in python

Question:

I was wondering if python has some built-in library (or any library on the net..)
That will create for for me a graph of dependencies ?
I have a file formatted like that

A::Requires         = ""
B::Requires     = A
C::Requires     = B
H::Requires     = A

AA::Requires         = ""
BB::Requires         = AA
C::Requires     = B

CC::Requires    = BB

Ideally I would like to have something like a tree like that:

A
 +-B
   +-C
 +-H

AA
 +-BB
   +-CC

So basically A lib where I will provide a tuple (A,B) or (A,H) and it will build the tree for me?
If such a lib doesn’t exist, what would be the easier way to accomplish something like that?

Thank you

Asked By: Johny19

||

Answers:

Try with one of the several ones:

graph-tool is very difficult to install (it needs a lot of memory for compilation, I think it was around 5GB of RAM and around 12 hours of compilation).

networkx is pretty decent.

igraph quote from their page: igraph is a free software package for creating and manipulating undirected and directed graphs. It includes implementations for classic graph theory problems like minimum spanning trees and network flow, and also implements algorithms for some recent network analysis methods, like community structure search.

I’ve been using all of them. It really depends on what exactly do you need. If you need them for something as simple as dependencies, then it really is not important which one you are going to use, though, I would recomend you to avoud graph-tool if you need it for something shorter and lighter.

Answered By: Belphegor

Assuming your input from above is given as a string in raw:

import networkx as nx
import re

regex = re.compile(r'^([A-Z]+)::Requiress+=s([A-Z"]+)$')

G = nx.DiGraph()
roots = set()
for l in raw.splitlines():
    if len(l):
        target, prereq = regex.match(l).groups()
        if prereq == '""':
            roots.add(target)
        else:
            G.add_edge(prereq, target)

Now print the tree(s):

for s in roots:
    print s
    spacer = {s: 0}
    for prereq, target in nx.dfs_edges(G, s):
        spacer[target] = spacer[prereq] + 2
        print '{spacer}+-{t}'.format(
                                     spacer=' ' * spacer[prereq],
                                     t=target)
    print ''

this prints:

A
+-H
+-B
  +-C

AA
+-BB
  +-CC

This requires that all roots are presented through root::Requires = "" in order for them to be identified as such.

Answered By: tzelleke

Graphviz is great for building documentation of dependencies in an automated manner.

There’s a useful Python library too called pygraphviz

I use this to build up a dependency map then output in both text form and as a visual that automatically exports to PDF.

I found and am trying https://github.com/thebjorn/pydeps

It is easy to install and easy to use. I am currently learning how to customize the resulting diagram.

Installation

pip install pydeps

Use

pydeps <path to python app>

Heads up

You need to have Graphviz installed in your OS and in your path (you hsould be able to run dot in your terminal without issues).

Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.