1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | import re import ntpath import glob import subprocess # In order to get this tool to run correctly, you'll need python installed (duh!) and graphviz basedir = "/home/ronald-dev/allBashScript/" ''' This function will scan a bash file, and look for any dependency to another bash file @param String The fileName @return Array The dependency filenames ''' def readDependencies(fileName): f = open(fileName, 'r') content = f.read() occurences = {} # {file1.sh:1, file2.sh:1, file3.sh:1} for m in re.finditer('[\w|\/]*\.sh', content): word = ntpath.basename(content[m.start(): m.end()]) # print str(m.start()) + " to " + str(m.end()) + ": " + word if word not in occurences: isComment = False index = m.start() while index > 0 and content[index] != "\n": if content[index] == """ or content[index] == "'" or content[index] == "#" : isComment = True break else: index = index-1 if isComment == False: occurences[word]=1 f.close() return occurences.keys() ''' Scan one bash file and convert it dependencies to GraphViz notation @param String The fileName @param Array The dependency filenames @return String GraphViz notation "bash01.sh" -> "bash02.sh";[NEWLINE] etc ''' def toGraphvizNotation(fileName, arrayOfDependencies): string = "" filename = ntpath.basename(fileName) for dependency in arrayOfDependencies: string += ' "' + filename + '" -> "' + dependency + "";\n" return string ''' Scans all bash file, with extension .sh @param String The directory to scan @return Array Array of filenames ''' def readAllBashFiles(directory): return glob.glob(directory+"*.sh") ''' Scan multiple bash files and convert their dependencies to GraphViz notation @param String The directory to scan @return String GraphViz notation "bash01.sh" -> "bash02.sh";[NEWLINE] etc ''' def generateGraphVizFile(basedir): bashfiles = readAllBashFiles(basedir) #print bashfiles graphVizFileContent = "" for bashfile in bashfiles: graphVizFileContent += toGraphvizNotation(bashfile, readDependencies(bashfile)) graphVizFileContent = "digraph G { \n"+ graphVizFileContent+ "}" # print graphVizFileContent; return graphVizFileContent ''' Write to a content to a file @param String The filename to be written to @return String The content ''' def writeGraphvizFile(filename, graphVizFileContent): f = open(filename, 'w') f.write(graphVizFileContent) f.close() writeGraphvizFile("/tmp/bashdependencies.dot", generateGraphVizFile(basedir)) subprocess.call(["/usr/bin/dot", "-T", "svg", "-o", "/tmp/allBashScript.svg", "/tmp/bashdependencies.dot"]) subprocess.call(["eog", "/tmp/allBashScript.svg"]) |
-
Archives
- March 2019
- March 2018
- June 2017
- May 2017
- November 2016
- September 2016
- July 2016
- June 2016
- May 2016
- April 2016
- March 2016
- February 2016
- January 2016
- December 2015
- November 2015
- October 2015
- September 2015
- August 2015
- July 2015
- June 2015
- April 2015
- December 2014
- October 2014
- September 2014
- May 2014
- April 2014
- March 2014
- January 2014
- November 2013
- October 2013
- October 2012
- September 2012
- August 2012
- July 2012
- June 2012
- March 2012
- February 2012
- January 2012
- December 2011
- November 2011
- October 2011
- September 2011
- August 2011
-
Meta