Skip to content

Instantly share code, notes, and snippets.

@epruesse
Forked from ericdill/render_env.py
Last active July 12, 2018 19:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save epruesse/a9fc0844ad0f497d9cae6e1fe5950784 to your computer and use it in GitHub Desktop.
Save epruesse/a9fc0844ad0f497d9cae6e1fe5950784 to your computer and use it in GitHub Desktop.
Render the dependency graph for your conda environment (needs graphviz!)
import json
import glob
from os.path import join, basename, dirname
from os import getcwd
from sys import argv
# install this with "conda install -c conda-forge python-graphviz"
try:
import graphviz as gv
except ImportError:
print("Missing graphviz python module. Install using:")
print()
print("conda install -c conda-forge python-graphviz")
print()
exit(1)
from subprocess import check_output, CalledProcessError
# get path to conda environment
if len(argv) == 0:
path = getcwd()
else:
try:
path = check_output(["conda", "..activate", "bash"] + argv[1:])
path = dirname(path).decode('ascii')
except CalledProcessError:
exit(1)
fname = 'env-%s' % basename(path)
fmt = "svg"
dg = gv.Digraph(filename=fname, format=fmt)
for json_file in glob.glob(join(path, 'conda-meta', '*.json')):
print('reading', json_file)
j = json.load(open(json_file))
dg.node(j['name'])
for dep in j.get('depends', []):
dg.edge(j['name'], dep.split(' ')[0])
dg.render()
print("Wrote {}.{}".format(fname, fmt))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment