| 1 |
#!/usr/bin/env python3 |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
# you may not use this file except in compliance with the License. |
| 5 |
# You may obtain a copy of the License at |
| 6 |
# |
| 7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 9 |
# Unless required by applicable law or agreed to in writing, software |
| 10 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
# See the License for the specific language governing permissions and |
| 13 |
# limitations under the License. |
| 14 |
# |
| 15 |
# Authors: |
| 16 |
# Chandan Singh <[email protected]> |
| 17 |
# |
| 18 |
'''Print dependency graph of given element(s) in DOT format. |
| 19 |
|
| 20 |
This script must be run from the same directory where you would normally |
| 21 |
run `bst` commands. |
| 22 |
|
| 23 |
When `--format` option is specified, the output will also be rendered in the |
| 24 |
given format. A file with name `bst-graph.{format}` will be created in the same |
| 25 |
directory. To use this option, you must have the `graphviz` command line tool |
| 26 |
installed. |
| 27 |
''' |
| 28 |
|
| 29 |
import argparse |
| 30 |
import subprocess |
| 31 |
import re |
| 32 |
import urllib.parse |
| 33 |
|
| 34 |
from graphviz import Digraph |
| 35 |
from ruamel.yaml import YAML |
| 36 |
|
| 37 |
def parse_args(): |
| 38 |
'''Handle parsing of command line arguments. |
| 39 |
|
| 40 |
Returns: |
| 41 |
A argparse.Namespace object |
| 42 |
''' |
| 43 |
parser = argparse.ArgumentParser(description=__doc__) |
| 44 |
parser.add_argument( |
| 45 |
'element', nargs='*', |
| 46 |
help='Name of the element' |
| 47 |
) |
| 48 |
parser.add_argument( |
| 49 |
'--format', |
| 50 |
help='Redner the graph in given format (`pdf`, `png`, `svg` etc)' |
| 51 |
) |
| 52 |
parser.add_argument( |
| 53 |
'--view', action='store_true', |
| 54 |
help='Open the rendered graph with the default application' |
| 55 |
) |
| 56 |
return parser.parse_args() |
| 57 |
|
| 58 |
|
| 59 |
def unique_node_name(s): |
| 60 |
'''Generate unique node name for `s`. |
| 61 |
|
| 62 |
Graphviz node names cannot contain colons or backslashes so we use |
| 63 |
url-encoding to generate the unique node name. (A cryptographic hash could |
| 64 |
be used instead but that would make the graphviz file less readable.) |
| 65 |
|
| 66 |
Args: |
| 67 |
s: element name |
| 68 |
|
| 69 |
Returns: |
| 70 |
A string containing the unique node name |
| 71 |
''' |
| 72 |
return urllib.parse.quote_plus(s) |
| 73 |
|
| 74 |
|
| 75 |
def parse_graph(lines): |
| 76 |
'''Return nodes and edges of the parsed grpah. |
| 77 |
|
| 78 |
Args: |
| 79 |
lines: List of lines in format 'NAME|BUILD-DEPS|RUNTIME-DEPS' |
| 80 |
|
| 81 |
Returns: |
| 82 |
Tuple of format (nodes,build_deps,runtime_deps) |
| 83 |
Each member of build_deps and runtime_deps is also a tuple. |
| 84 |
''' |
| 85 |
parser = YAML(typ="safe") |
| 86 |
nodes = set() |
| 87 |
build_deps = set() |
| 88 |
runtime_deps = set() |
| 89 |
for line in lines: |
| 90 |
line = line.strip() |
| 91 |
if not line: |
| 92 |
continue |
| 93 |
# It is safe to split on '|' as it is not a valid character for |
| 94 |
# element names. |
| 95 |
name, build_dep, runtime_dep = line.split('|') |
| 96 |
|
| 97 |
build_dep = parser.load(build_dep) |
| 98 |
runtime_dep = parser.load(runtime_dep) |
| 99 |
|
| 100 |
nodes.add(name) |
| 101 |
[build_deps.add((name, dep)) for dep in build_dep if dep] |
| 102 |
[runtime_deps.add((name, dep)) for dep in runtime_dep if dep] |
| 103 |
|
| 104 |
return nodes, build_deps, runtime_deps |
| 105 |
|
| 106 |
|
| 107 |
def generate_graph(nodes, build_deps, runtime_deps): |
| 108 |
'''Generate graph from given nodes and edges. |
| 109 |
|
| 110 |
Args: |
| 111 |
nodes: set of nodes |
| 112 |
build_deps: set of tuples of build depdencies |
| 113 |
runtime_deps: set of tuples of runtime depdencies |
| 114 |
|
| 115 |
Returns: |
| 116 |
A graphviz.Digraph object |
| 117 |
''' |
| 118 |
graph = Digraph() |
| 119 |
for name in nodes: |
| 120 |
graph.node(unique_node_name(name), label=name) |
| 121 |
for source, target in build_deps: |
| 122 |
graph.edge(unique_node_name(source), unique_node_name(target), label='build-dep') |
| 123 |
for source, target in runtime_deps: |
| 124 |
graph.edge(unique_node_name(source), unique_node_name(target), label='runtime-dep') |
| 125 |
return graph |
| 126 |
|
| 127 |
|
| 128 |
def main(): |
| 129 |
args = parse_args() |
| 130 |
cmd = ['bst', 'show', '--format', '%{name}|%{build-deps}|%{runtime-deps}||'] |
| 131 |
if 'element' in args: |
| 132 |
cmd += args.element |
| 133 |
graph_lines = subprocess.check_output(cmd, universal_newlines=True) |
| 134 |
# NOTE: We generate nodes and edges before giving them to graphviz as |
| 135 |
# the library does not de-deuplicate them. |
| 136 |
nodes, build_deps, runtime_deps = parse_graph(re.split(r"\|\|", graph_lines)) |
| 137 |
graph = generate_graph(nodes, build_deps, runtime_deps) |
| 138 |
|
| 139 |
print(graph.source) |
| 140 |
|
| 141 |
if args.format: |
| 142 |
graph.render(cleanup=True, |
| 143 |
filename='bst-graph', |
| 144 |
format=args.format, |
| 145 |
view=args.view) |
| 146 |
|
| 147 |
|
| 148 |
if __name__ == '__main__': |
| 149 |
main() |