"Fossies" - the Fresh Open Source Software Archive

Member "ncc-2.8/scripts/gengraph.py" (19 Nov 2004, 4687 Bytes) of package /linux/privat/old/ncc-2.8.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 #!/usr/bin/env python
    2 #
    3 """Usage: %s [options] <nccout> <function> ...
    4 valid options:
    5 \t -h | --help: usage statement
    6 \t -V | --version: print version and exit
    7 \t -D | --dot: additional options to dot
    8 \t -d | --depth: max depth of graph
    9 \t -i | --ignore: functions to ignore
   10 \t -l | --location: show file names of function declarations
   11 \t -s | --show: show function but ignore sub-functions
   12 
   13 Reads a ncc-generated file and produces a call graph in dot format.
   14 Sample usage:
   15 gengraph.py -i "exit strlen" nccout main |dot -Tsvg -o func.svg
   16 """
   17 #
   18 # Author: Jose Vasconcellos <jvasco@bellatlantic.net>
   19 # Copyright (C) 2004 Jose Vasconcellos
   20 #
   21 # gengraph.py is free software; you can redistribute it and/or modify
   22 # it under the terms of the GNU General Public License as published by
   23 # the Free Software Foundation; either version 2, or (at your option)
   24 # any later version.
   25 
   26 # gengraph.py is distributed in the hope that it will be useful,
   27 # but WITHOUT ANY WARRANTY; without even the implied warranty of
   28 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   29 # GNU General Public License for more details.
   30 #
   31 # Tested with Python version 2.3 and ncc-1.9
   32 
   33 import sys
   34 import os
   35 import string
   36 import getopt
   37 
   38 # globals
   39 location = 0
   40 maxdepth = 1000        # large number
   41 dot = None
   42 ignore = []
   43 show = []
   44 shown = []
   45 
   46 cfile = None    #current file
   47 cfunc = None    #current func
   48 rfunc = {}
   49 
   50 # funcs: function dictionary
   51 # this dictionary maps a function to a list containing dependencies
   52 funcs = {}
   53 
   54 ####################
   55 
   56 def usage():
   57     print __doc__ % os.path.basename(sys.argv[0])
   58     sys.exit(1)
   59 
   60 ####################
   61 
   62 def addfunc(f, d):
   63     if f in funcs:
   64         # already exists, just append
   65         funcs[f].append(d)
   66     else:
   67         # new function entry; 1st item holds file name
   68         funcs[f] = [None, d]
   69 
   70 def addfile(f, p):
   71     if f in funcs:
   72         # existing entry, put file name in 1st entry in list
   73         funcs[f][0] = p
   74     else:
   75         # new function entry; create list with file name
   76         funcs[f] = [p]
   77 
   78 ####################
   79 
   80 def word(w):
   81     return w.rstrip(" ()\t\n")
   82 
   83 ####################
   84 
   85 def gengraph(s, d=0):
   86     if s:
   87         shown.append(s)
   88     if s in funcs:
   89         l = funcs[s]
   90         for f in l[1:]:
   91             f1 = f
   92             edge = ""
   93             if f1[0] == '*':
   94                 edge = "[color=blue]"
   95                 while rfunc.has_key(f1):
   96                     f1 = rfunc[f1]
   97             print '"%s" -> "%s" %s;' % (s, f1, edge)
   98             if d < maxdepth and f1 == f and f not in shown:
   99                 gengraph(f,d+1)
  100 
  101 ####################
  102 # start of main program
  103 
  104 # process command line options
  105 try:
  106     opts, args = getopt.getopt(sys.argv[1:], "hvVD:d:i:l:s:", \
  107     ["help","version","depth","dot","ignore","location","show"])
  108 
  109 except getopt.GetoptError:
  110     # print help information and exit:
  111     usage()
  112 
  113 for o, a in opts:
  114     if o in ("-h", "--help"):
  115     usage()
  116     elif o in ("-V", "--version"):
  117         print "$Id: gengraph.py 5 2004-11-18 03:21:58Z Jose $"
  118     sys.exit(1)
  119     elif o in ("-i", "--ignore"):
  120     ignore = string.split(a)
  121     elif o in ("-D", "--dot"):
  122     dot = a
  123     elif o in ("-d", "--depth"):
  124     maxdepth = int(a)
  125     elif o in ("-l", "--location"):
  126     location = 1
  127     elif o in ("-s", "--show"):
  128     show = string.split(a)
  129 
  130 if len(args) < 2:
  131     usage()
  132 
  133 infile = open(args[0])
  134 start = args[1:]
  135 
  136 # process lines in nccout file
  137 # and populate dictionary
  138 for line in infile:
  139     if len(line) == 0:
  140         pass
  141     elif line[0] == 'D':
  142         w = word(line[3:])
  143         if w not in ignore:
  144             cfunc = w
  145         cfile = None
  146     elif line[0] == 'F':
  147         if cfunc and cfunc not in show:
  148             w = word(line[3:])
  149             if w not in ignore:
  150                 addfunc(cfunc, w)
  151         cfile = None
  152     elif line[0] == 'P':
  153         cfile = line[3:]
  154         cfunc = None
  155     elif line[0] == 'Y':
  156         addfile(word(line[3:]), cfile)
  157         cfunc = None
  158     elif line[0] == 'R':
  159         l = line[3:].split()
  160         rfunc[word(l[0])] = word(l[1])
  161         cfunc = None
  162         cfile = None
  163 
  164 # generate graph
  165 print "digraph gengraph {"
  166 if dot:
  167     print dot
  168 
  169 # traverse graph
  170 for s in start:
  171     gengraph(s)
  172 
  173 if location:
  174     pass        #todo
  175 else:
  176     # mark special nodes
  177     for s in shown:
  178         if s in show:
  179             print '"%s" [ style="bold", color="red", shape="box" ];' % s
  180         if s[0] == '*':
  181             print '"%s" [ shape="hexagon" ];' % s
  182         elif s.find('/') >= 0:
  183             print '"%s" [ shape="octagon" ];' % s
  184     # mark start nodes
  185     for s in start:
  186         print '"%s" [ style="filled,bold", fillcolor="#dddddd", shape="box" ];' % s
  187 
  188 print "}"
  189 
  190 # vim: set ai sw=4 et :