From ae307ea2e9cee65c347b53cd92d1556e99db7fe4 Mon Sep 17 00:00:00 2001 From: Philippe Proulx Date: Thu, 26 Nov 2015 19:55:09 -0500 Subject: [PATCH] Add dotlinks.py Signed-off-by: Philippe Proulx --- tools/dotlinks.py | 76 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100755 tools/dotlinks.py diff --git a/tools/dotlinks.py b/tools/dotlinks.py new file mode 100755 index 0000000..de9b30b --- /dev/null +++ b/tools/dotlinks.py @@ -0,0 +1,76 @@ +#!/usr/bin/env python3 + +# The MIT License (MIT) +# +# Copyright (c) 2015 Philippe Proulx +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. + +import re +import os +import sys +import json +import graphviz + + +def _get_section_infos(json_path): + with open(json_path) as f: + c = f.read() + + return json.loads(c) + + +def dotlinks(): + section_infos = _get_section_infos(sys.argv[1]) + digraph = graphviz.Digraph(format='png', engine='dot') + digraph.attr('node', fontname='Terminus', fontsize='8') + + for sid, section_info in section_infos.items(): + color = '' + style = '' + in_links_count = len(section_info['in-links']) + out_links_count = len(section_info['out-links']) + + if in_links_count == 0 and out_links_count == 0: + color = '#e62739' + elif in_links_count == 0: + color = '#fae596' + elif out_links_count == 0: + color = '#6ed3cf' + + if color: + style = 'filled' + + digraph.node(sid, style=style, color=color) + + for sid, section_info in section_infos.items(): + out_links = section_info['out-links'] + + for out_link in out_links: + if 'section' in out_link: + dest = out_link['section'] + digraph.edge(sid, dest) + + digraph.render(filename='linkgraph') + + return True + + +if __name__ == '__main__': + sys.exit(0 if dotlinks() else 1) -- 2.34.1