0

I'm having this strange error when compile and run my code. What I want is to generate a graph in which some nodes are colored according to their labels. This is the code:

#!/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()

I run with this command:

$ python secdepgraph_2.py serverclient.bc.color.dot

... and I get this output with error:

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

What am I doing wrong? Thank you for help...

1 Answer 1

4

You only have two command line arguments:

["secdepgraph_2.py", "serverclient.bc.color.dot"]

Remember lists are 0-based, so the first is sys.argv[0] and the second is sys.argv[1]

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.