你好,我在Python中迭代一个点状文件,使用的是 pydot 包。我想给图中的所有节点和边着色,这些节点是特定节点的父节点,而不是起始根。
实际图表如下
假设给定的节点是_ZL3WTFv,则需要输出。
Python code is following
import pydot
import sys
def Clean(inp):
return inp.replace('"{', '').replace('}"', '')
def GetNode(path):
graph = pydot.graph_from_dot_file(path)
graph = graph[0]
nodesList = graph.get_nodes()
for e in nodesList:
tempAttr = e.get_attributes()
print('Checking', Clean(tempAttr['label']))
if Clean(tempAttr['label']) == '_ZL3WTFv':
#how to mark all nodes & paths to the top level parents from here e.g. _ZL3WTFv
print('Node Found!')
break
if len(sys.argv) > 1:
GetNode(sys.argv[1])
else:
print('Dot file path Missing!')
graphviz dot文件的代码如下
digraph "Call graph" {
label="Call graph";
Node0x163ebe0 [shape=record,label="{external node}"];
Node0x163ebe0 -> Node0x163b270;
Node0x163ebe0 -> Node0x1605360;
Node0x163ebe0 -> Node0x16052c0;
Node0x163ebe0 -> Node0x160e2a0;
Node0x163ebe0 -> Node0x160e600;
Node0x163ebe0 -> Node0x160eb00;
Node0x160eb00 [shape=record,label="{_GLOBAL__sub_I_test.cpp}"];
Node0x160eb00 -> Node0x163b200;
Node0x163b200 [shape=record,label="{__cxx_global_var_init}"];
Node0x163b200 -> Node0x163b270;
Node0x163b200 -> Node0x16052c0;
Node0x163b270 [shape=record,label="{_ZNSt8ios_base4InitC1Ev}"];
Node0x163b270 -> Node0x163e480;
Node0x1605360 [shape=record,label="{_ZNSt8ios_base4InitD1Ev}"];
Node0x1605360 -> Node0x163e480;
Node0x16052c0 [shape=record,label="{__cxa_atexit}"];
Node0x16052c0 -> Node0x163e480;
Node0x160e2a0 [shape=record,label="{main}"];
Node0x160e2a0 -> Node0x160e310;
Node0x160e310 [shape=record,label="{_ZL1Av}"];
Node0x160e310 -> Node0x160e3b0;
Node0x160e310 -> Node0x160e450;
Node0x160e3b0 [shape=record,label="{_ZL1Bv}"];
Node0x160e3b0 -> Node0x160e3e0;
Node0x160e450 [shape=record,label="{_ZL1Gv}"];
Node0x160e450 -> Node0x160e530;
Node0x160e3e0 [shape=record,label="{_ZL1Cv}"];
Node0x160e3e0 -> Node0x160e5d0;
Node0x160e3e0 -> Node0x160e670;
Node0x160e530 [shape=record,label="{_ZL1Mv}"];
Node0x160e530 -> Node0x160e850;
Node0x160e5d0 [shape=record,label="{_ZL3WTFv}"];
Node0x160e5d0 -> Node0x160e600;
Node0x160e670 [shape=record,label="{_ZL1Xv}"];
Node0x160e670 -> Node0x160e750;
Node0x160e600 [shape=record,label="{_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc}"];
Node0x160e600 -> Node0x163e480;
Node0x160e750 [shape=record,label="{_ZL1Yv}"];
Node0x160e750 -> Node0x160e5d0;
Node0x160e850 [shape=record,label="{_ZL1Nv}"];
Node0x160e850 -> Node0x160e8f0;
Node0x160e8f0 [shape=record,label="{_ZL1Ov}"];
Node0x160e8f0 -> Node0x160e990;
Node0x160e990 [shape=record,label="{_ZL1Pv}"];
Node0x160e990 -> Node0x160ea30;
Node0x160ea30 [shape=record,label="{_ZL1Sv}"];
Node0x160ea30 -> Node0x160e5d0;