10 Given a directory, will look into lib*.pkg files to produce a dependency graph
11 of libraries (and DA if there are some)
14 __author__ = "L. Aphecetche aphecetc_at_in2p3_dot_fr"
15 __date__ = "April 2008"
18 #_______________________________________________________________________________
20 """Describe usage of script
22 print "Usage: %s [-h | --help] [-d | --debug] [--noda] directory_to_scan" % sys.argv[0]
25 #_______________________________________________________________________________
26 def getSourceFiles(lib):
27 """Extract the list of classes from a libXXX.pkg file
34 if re.search('Ali',l) and re.search('.cxx',l):
35 l = re.sub('SRCS',' ',l)
36 l = re.sub(':=',' ',l)
38 l = re.sub("\\\\",' ',l)
39 l = re.sub(".cxx",' ',l)
45 #_______________________________________________________________________________
46 def getIncludeFiles(srcfile):
47 """Extract the list of included classes from a class
53 f = open("%s.cxx" % srcfile)
55 print "Could not open file %s.cxx" % srcfile
60 if re.search("^#",line) and re.search("#include",line) and re.search('Ali',line):
61 line = re.sub("#include",' ',line)
64 line = re.sub("\"",' ',line)
70 #_______________________________________________________________________________
72 """Extract a unique list from list
79 #_______________________________________________________________________________
80 def findLibrary(file,allfiles):
81 """Find in which library a given class is defined
83 for k,v in allfiles.items():
85 a,f = os.path.split(f)
90 #_______________________________________________________________________________
92 """From libMUONxxx.pkg to xxx
95 if re.search("libMUON",libname):
96 s = re.sub("libMUON","",s)
97 s = re.sub("\.pkg","",s)
100 #_______________________________________________________________________________
101 #_______________________________________________________________________________
102 #_______________________________________________________________________________
109 opts, args = getopt.getopt(sys.argv[1:],"hd",["help", "debug","noda"])
110 except getopt.GetoptError:
111 print "Error in options"
115 if o in ( "-d","--debug" ):
117 elif o in ( "-h","--help" ):
123 assert False, "unhandled option"
127 # find the libraries defined in this directory (looking for libXXXX.pkg files)
130 for file in os.listdir(dir):
131 if re.search('^lib',file) and re.search('.pkg$',file):
132 libraries.append(file)
134 # append fake libraries for DAs
136 libraries.append("libMUONTRKda.pkg")
137 libraries.append("libMUONTRGda.pkg")
139 # srcfiles is a dictionary :
140 # srcfiles[libXXX.pkg] -> { list of classes (inferred from list of .cxx files) }
144 # allfiles is a dictonary :
145 # allfiles[libXXX.pkg] -> { list of all included files of that library }
148 for lib in libraries:
149 if not re.search("da",lib):
150 # handle the special case of DAs which are not part of libs, really
151 srcfiles[lib] = getSourceFiles(lib)
154 l = re.sub("lib","",l)
155 l = re.sub("\.pkg","",l)
156 srcfiles[lib] = [ l ]
158 for src in srcfiles[lib]:
159 files.extend(getIncludeFiles(src))
160 allfiles[lib] = unique(files)
163 for lib in libraries:
165 for f in allfiles[lib]:
166 l = findLibrary(f,srcfiles)
167 print " ",f,"(",l,")"
170 # deps is a dictionary
171 # deps[libXXX.pkg] -> { list of libraries libXXX.pkg directly depends upon }
175 for lib,files in allfiles.items():
178 l = findLibrary(f,srcfiles)
181 deps[lib] = unique(d)
185 print lib, " depends on "
190 ofile = "%s.dot" % os.path.splitext(os.path.basename(sys.argv[0]))[0]
194 f.write("digraph G {")
195 f.write("rankdir=BT;")
197 for l,d in deps.items():
199 if re.search("MUON",dl):
200 f.write("node [shape=box,style=filled,fillcolor=yellow];")
202 f.write("node [shape=ellipse,style=filled,fillcolor=lightgray];")
203 f.write("%s -> %s;\n" %(shorten(l),shorten(dl)))
207 print "You should now do :"
208 print "tred %s > %s.bis" % ( ofile, ofile )
209 print "dot -Tpng %s.bis > %s.png" % (ofile,ofile)
211 if __name__ == "__main__":