graph-include-web 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #! /usr/bin/python2
  2. import os.path
  3. import sys
  4. import shlex
  5. import re
  6. from headerutils import *
  7. def pretty_name (name):
  8. return name.replace(".","_").replace("-","_").replace("/","_").replace("+","_");
  9. include_files = list()
  10. edges = 0
  11. one_c = False
  12. clink = list()
  13. noterm = False
  14. def build_inclist (output, filen):
  15. global edges
  16. global one_c
  17. global clink
  18. global noterm
  19. inc = build_include_list (filen)
  20. if one_c and filen[-2:] == ".c":
  21. pn = "all_c"
  22. else:
  23. pn = pretty_name(filen)
  24. for nm in inc:
  25. if pn == "all_c":
  26. if nm not in clink:
  27. if len(build_include_list(nm)) != 0 or not noterm:
  28. output.write (pretty_name(nm) + " -> " + pn + ";\n")
  29. edges = edges + 1
  30. if nm not in include_files:
  31. include_files.append(nm)
  32. clink.append (nm)
  33. else:
  34. output.write (pretty_name(nm) + " -> " + pn + ";\n")
  35. edges = edges + 1
  36. if nm not in include_files:
  37. include_files.append(nm)
  38. return len(inc) == 0
  39. dotname = "graph.dot"
  40. graphname = "graph.png"
  41. def build_dot_file (file_list):
  42. global one_c
  43. output = open(dotname, "w")
  44. output.write ("digraph incweb {\n");
  45. if one_c:
  46. output.write ("all_c [shape=box];\n");
  47. for x in file_list:
  48. if x[-2:] == ".h":
  49. include_files.append (x)
  50. elif os.path.exists (x):
  51. build_inclist (output, x)
  52. if not one_c:
  53. output.write (pretty_name (x) + "[shape=box];\n")
  54. for x in include_files:
  55. term = build_inclist (output, x)
  56. if term:
  57. output.write (pretty_name(x) + " [style=filled];\n")
  58. output.write ("}\n");
  59. files = list()
  60. dohelp = False
  61. edge_thresh = 0
  62. for arg in sys.argv[1:]:
  63. if arg[0:2] == "-o":
  64. dotname = arg[2:]+".dot"
  65. graphname = arg[2:]+".png"
  66. elif arg[0:2] == "-h":
  67. dohelp = True
  68. elif arg[0:2] == "-a":
  69. one_c = True
  70. if arg[0:3] == "-at":
  71. noterm = True
  72. elif arg[0:2] == "-f":
  73. if not os.path.exists (arg[2:]):
  74. print "Option " + arg +" doesn't specify a proper file"
  75. dohelp = True
  76. else:
  77. sfile = open (arg[2:], "r")
  78. srcdata = sfile.readlines()
  79. sfile.close()
  80. for x in srcdata:
  81. files.append(x.rstrip())
  82. elif arg[0:2] == "-n":
  83. edge_thresh = int (arg[2:])
  84. elif arg[0:1] == "-":
  85. print "Unrecognized option " + arg
  86. dohelp = True
  87. else:
  88. files.append (arg)
  89. if len(sys.argv) == 1:
  90. dohelp = True
  91. if dohelp:
  92. print "Generates a graph of the include web for specified files."
  93. print "Usage: [-finput_file] [-h] [-ooutput] [file1 ... [filen]]"
  94. print " -finput_file : Input file containing a list of files to process."
  95. print " -ooutput : Specifies output to output.dot and output.png."
  96. print " defaults to graph.dot and graph.png."
  97. print " -nnum : Specifies the # of edges beyond which sfdp is invoked. def=0."
  98. print " -a : Aggregate all .c files to 1 file. Shows only include web."
  99. print " -at : Aggregate, but don't include terminal.h to .c links."
  100. print " -h : Print this help."
  101. else:
  102. print files
  103. build_dot_file (files)
  104. if edges > edge_thresh:
  105. os.system ("sfdp -Tpng " + dotname + " -o" + graphname)
  106. else:
  107. os.system ("dot -Tpng " + dotname + " -o" + graphname)