Skip to content

Instantly share code, notes, and snippets.

@BrianHicks
Created May 22, 2012 15:39
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save BrianHicks/2769821 to your computer and use it in GitHub Desktop.
Save BrianHicks/2769821 to your computer and use it in GitHub Desktop.
Graph taskwarrior tasks with graphviz

Graph taskwarrior tasks with graphviz

Dependencies

Running

graphdeps <filter> produces "deps.png" in the working directory. Just graphdeps will graph all tasks. You can change the default behavior by specifying a more specific query.

#!/usr/bin/env python
'graph dependencies in projects'
import json
from subprocess import Popen, PIPE
import sys
HEADER = "digraph dependencies {"
FOOTER = "}"
JSON_START = '['
JSON_END = ']'
def call_taskwarrior(cmd):
'call taskwarrior, returning output and error'
tw = Popen(['task'] + cmd.split(), stdout=PIPE, stderr=PIPE)
return tw.communicate()
def get_json(query):
'call taskwarrior, returning objects from json'
result, err = call_taskwarrior('export %s' % query)
return json.loads(JSON_START + result + JSON_END)
def call_dot(instr):
'call dot, returning stdout and stdout'
dot = Popen('dot -T png'.split(), stdout=PIPE, stderr=PIPE, stdin=PIPE)
return dot.communicate(instr)
if __name__ == '__main__':
query = sys.argv[1:]
print 'Calling TaskWarrior'
data = get_json(' '.join(query))
# first pass: labels
lines = [HEADER]
print 'Printing Labels'
for datum in data:
lines.append('"%s"[label="%s"];' % (datum['uuid'], datum['description']))
# second pass: dependencies
print 'Resolving Dependencies'
for datum in data:
for dep in datum.get('depends', '').split(','):
if dep != '':
lines.append('"%s" -> "%s";' % (dep, datum['uuid']))
lines.append(FOOTER)
print '\n'.join(lines)
print 'Calling dot'
png, err = call_dot('\n'.join(lines))
if err != '':
print 'Error calling dot:'
print err.strip()
print 'Writing to deps.png'
with open('deps.png', 'w') as f:
f.write(png)
@bcomnes
Copy link

bcomnes commented Jan 13, 2013

Awesome!

@matthiasbeyer
Copy link

Hi. What python version did you use to call this? I get errors with both 2.7 and 3.6 because of some syntactic errors...

@fmakowski
Copy link

Hey, debugged and fixed that script, available in my fork. Cheers!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment