编译并运行我的代码时,我遇到了这个奇怪的错误。 我想要的是生成一个图表,其中一些节点根据其标签着色。这是代码:
#!/usr/bin/env python
# Code V1.1
# Reduce NetDepGraph
import os
import pydot
import sys
import networkx as nx
from networkx import dfs_edges
import matplotlib.pyplot as plot
from itertools import islice
def is_secure(nodo):
insecure_functions = [
"write",
"read",
"send",
"recv",
"recvfrom",
"sendto"
]
try:
x = int(nodo)
return True
except:
pass
for funcao in insecure_functions:
if nodo.lower().find(funcao) != -1:
return False
return True
def colore_subgrafo(nodo):
print("colorindo nodo {}".format(nodo['label']))
if nodo['label'].find("write") != -1 or nodo['label'].find("send") != -1 or nodo['label'].find("sendto") != -1:
nodo["color"] = "blue"
nodo["style"] = "filled"
return "blue"
else:
nodo["color"] = "green"
nodo["style"] = "filled"
return "green"
def getAttributes(idNode , grafo):
for nodo,attributes in grafo.nodes(data=True):
if idNode == nodo:
return (nodo,attributes)
def colore_vizinhos(grafo,lista,cor):
for n in lista:
no = getAttributes(n,grafo)
(node,attributes) = no
attributes['color'] = cor
attributes["style"] = "filled"
sucessores = list( vizinho for eu,vizinho in grafo.out_edges(node))
if len(sucessores) != 0 :
colore_vizinhos(grafo,sucessores,cor)
return 0
def removeNodesInsignificantes(grafo):
nodes = ["perror","unreachable"]
for nodo,attributes in grafo.nodes(data=True):
for n in nodes:
if attributes['label'].find(n) != -1:
suc = list( vizinho for eu,vizinho in grafo.out_edges(nodo))
pre = list( vizinho for eu,vizinho in grafo.in_edges(nodo))
all_n = suc+pre+[nodo]
grafo.remove_nodes_from(all_n)
if attributes['label'].find("") and grafo.edges(nodo) <= 1:
grafo.remove_node(nodo)
def remove_flow_through(graph):
for n,node in graph.nodes(data=True):
pred = graph.predecessors(n)
succ = graph.successors(n)
if node['label'] == " " or node['label'] == "unrecheable":
if len(pred) == len(succ) == 1:
graph.remove_node(n)
graph.add_edge(pred[0], succ[0])
def main():
grafodot = pydot.graph_from_dot_file( sys.argv[1] )
grafo = nx.DiGraph( nx.drawing.from_pydot( grafodot ) )
i = 0
for nodo,attributes in grafo.nodes(data=True):
if not is_secure( attributes['label'] ):
cor_pai = colore_subgrafo(attributes)
sucessores = list( vizinho for eu,vizinho in grafo.out_edges(nodo))
colore_vizinhos(grafo,sucessores,cor_pai)
# while True:
# prev_len = len(grafo)
# remove_flow_through(grafo)
# if len(grafo) == prev_len:
# break
removeNodesInsignificantes(grafo)
desenhaPng = nx.to_pydot(grafo, strict=False)
desenhaPng.write_png('{}.png'.format(sys.argv[2]))
if __name__ == "__main__":
main()
我使用此命令运行:
$ python secdepgraph_2.py serverclient.bc.color.dot
...我得到这个输出错误:
colorindo nodo "Const:recv"
colorindo nodo "Call recv"
colorindo nodo "Const:tttrecv"
colorindo nodo "Call send"
colorindo nodo "Const:tttsend"
colorindo nodo "Call tttrecv"
colorindo nodo "Call tttsend"
colorindo nodo "Net Write: tttsend 6"
colorindo nodo "Net Read: tttrecv 3"
colorindo nodo "Const:send"
colorindo nodo "Net Write: send 7"
Traceback (most recent call last):
File "secdepgraph_2.py", line 112, in <module>
main()
File "secdepgraph_2.py", line 108, in main
desenhaPng.write_png('{}.png'.format(sys.argv[2]))
IndexError: list index out of range
我做错了什么? 谢谢你的帮助...
答案 0 :(得分:3)
您只有两个命令行参数:
["secdepgraph_2.py", "serverclient.bc.color.dot"]
记住列表是从0开始的,因此第一个是sys.argv[0]
,第二个是sys.argv[1]