aboutsummaryrefslogtreecommitdiffstats
path: root/longbow/src/python/site-packages
diff options
context:
space:
mode:
Diffstat (limited to 'longbow/src/python/site-packages')
-rw-r--r--longbow/src/python/site-packages/CMakeLists.txt12
-rwxr-xr-xlongbow/src/python/site-packages/longbow.pth18
-rw-r--r--longbow/src/python/site-packages/longbow/.gitignore1
-rwxr-xr-xlongbow/src/python/site-packages/longbow/ANSITerm.py62
-rwxr-xr-xlongbow/src/python/site-packages/longbow/CoverageReport.py262
-rwxr-xr-xlongbow/src/python/site-packages/longbow/DoxygenReport.py161
-rwxr-xr-xlongbow/src/python/site-packages/longbow/FileUtil.py102
-rwxr-xr-xlongbow/src/python/site-packages/longbow/GCov.py232
-rwxr-xr-xlongbow/src/python/site-packages/longbow/GCovSummary.py42
-rwxr-xr-xlongbow/src/python/site-packages/longbow/Language_C.py204
-rwxr-xr-xlongbow/src/python/site-packages/longbow/LongBow.py96
-rwxr-xr-xlongbow/src/python/site-packages/longbow/NameReport.py818
-rwxr-xr-xlongbow/src/python/site-packages/longbow/StyleReport.py382
-rwxr-xr-xlongbow/src/python/site-packages/longbow/SymbolTable.py87
-rwxr-xr-xlongbow/src/python/site-packages/longbow/VocabularyReport.py162
15 files changed, 2641 insertions, 0 deletions
diff --git a/longbow/src/python/site-packages/CMakeLists.txt b/longbow/src/python/site-packages/CMakeLists.txt
new file mode 100644
index 00000000..f0be88f6
--- /dev/null
+++ b/longbow/src/python/site-packages/CMakeLists.txt
@@ -0,0 +1,12 @@
+install(FILES longbow.pth DESTINATION ${INSTALL_BASE_PYTHON_DIR} COMPONENT library)
+install(FILES longbow/LongBow.py DESTINATION ${INSTALL_PYTHON_DIR} COMPONENT library)
+install(FILES longbow/FileUtil.py DESTINATION ${INSTALL_PYTHON_DIR} COMPONENT library)
+install(FILES longbow/GCov.py DESTINATION ${INSTALL_PYTHON_DIR} COMPONENT library)
+install(FILES longbow/GCovSummary.py DESTINATION ${INSTALL_PYTHON_DIR} COMPONENT library)
+install(FILES longbow/ANSITerm.py DESTINATION ${INSTALL_PYTHON_DIR} COMPONENT library)
+install(FILES longbow/SymbolTable.py DESTINATION ${INSTALL_PYTHON_DIR} COMPONENT library)
+install(FILES longbow/Language_C.py DESTINATION ${INSTALL_PYTHON_DIR} COMPONENT library)
+install(FILES longbow/StyleReport.py DESTINATION ${INSTALL_PYTHON_DIR} COMPONENT library)
+install(FILES longbow/CoverageReport.py DESTINATION ${INSTALL_PYTHON_DIR} COMPONENT library)
+install(FILES longbow/VocabularyReport.py DESTINATION ${INSTALL_PYTHON_DIR} COMPONENT library)
+install(FILES longbow/NameReport.py DESTINATION ${INSTALL_PYTHON_DIR} COMPONENT library)
diff --git a/longbow/src/python/site-packages/longbow.pth b/longbow/src/python/site-packages/longbow.pth
new file mode 100755
index 00000000..9f0d4f64
--- /dev/null
+++ b/longbow/src/python/site-packages/longbow.pth
@@ -0,0 +1,18 @@
+# Copyright (c) 2017 Cisco and/or its affiliates.
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at:
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+#
+#
+# longbow package configuration
+longbow
diff --git a/longbow/src/python/site-packages/longbow/.gitignore b/longbow/src/python/site-packages/longbow/.gitignore
new file mode 100644
index 00000000..0d20b648
--- /dev/null
+++ b/longbow/src/python/site-packages/longbow/.gitignore
@@ -0,0 +1 @@
+*.pyc
diff --git a/longbow/src/python/site-packages/longbow/ANSITerm.py b/longbow/src/python/site-packages/longbow/ANSITerm.py
new file mode 100755
index 00000000..8594c949
--- /dev/null
+++ b/longbow/src/python/site-packages/longbow/ANSITerm.py
@@ -0,0 +1,62 @@
+#! /usr/bin/env python
+# Copyright (c) 2017 Cisco and/or its affiliates.
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at:
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+#
+import os
+import subprocess
+import re
+import sys
+import pprint
+
+ansiRed = "\x1b[31m";
+ansiGreen = "\x1b[32m";
+ansiYellow = "\x1b[33m";
+ansiBlue = "\x1b[34m";
+ansiMagenta = "\x1b[35m";
+ansiCyan = "\x1b[36m";
+ansiReset = "\x1b[0m";
+
+def colorize(color, chars):
+
+ result = chars
+ if color == "red":
+ result = ansiRed + chars + ansiReset
+ elif color == "green":
+ result = ansiGreen + chars + ansiReset
+ elif color == "yellow":
+ result = ansiYellow + chars + ansiReset
+ elif color == "blue":
+ result = ansiBlue + chars + ansiReset
+ elif color == "magenta":
+ result = ansiMagenta + chars + ansiReset
+ elif color == "cyan":
+ result = ansiCyan + chars + ansiReset
+ else:
+ print >> sys.stderr, "Bad color name:", color
+
+ return result
+
+
+def printColorized(color, string):
+ print colorize(color, string)
+ return
+
+
+class ANSITerm:
+ def __init__(self):
+ return
+
+ def printColorized(self, color, string):
+ print colorize(color, string)
diff --git a/longbow/src/python/site-packages/longbow/CoverageReport.py b/longbow/src/python/site-packages/longbow/CoverageReport.py
new file mode 100755
index 00000000..c18ae056
--- /dev/null
+++ b/longbow/src/python/site-packages/longbow/CoverageReport.py
@@ -0,0 +1,262 @@
+#! /usr/bin/env python
+# Copyright (c) 2017 Cisco and/or its affiliates.
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at:
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+#
+import sys
+import os
+import re
+import subprocess
+import difflib
+import csv
+# import argparse
+import pprint
+# sys.path.append("${INSTALL_PYTHON_DIR}")
+# sys.path.append("${DEPENDENCY_PYTHON_DIR}")
+# sys.path.append("../site-packages/longbow/")
+import LongBow
+import GCov
+import GCovSummary
+import FileUtil
+import ANSITerm
+import Language_C
+
+def checkTestExecutable(executableFileName):
+ result = False
+
+ if not os.path.exists(executableFileName):
+ return result
+
+ path = os.path.dirname(executableFileName)
+ pattern = os.path.basename(executableFileName)+'*.gcda'
+ if not Language_C.findFiles(path, pattern):
+ return result
+
+ pattern = os.path.basename(executableFileName)+'*.gcno'
+ if not Language_C.findFiles(path, pattern):
+ return result
+
+ result = True
+ return result
+
+def findTestExecutable(fileName, hints=[]):
+ '''
+Given a file name, look in the canonical places for a corresponding LongBow test file.
+ '''
+ directoryName = os.path.dirname(fileName)
+ if len(directoryName) == 0:
+ directoryName = "."
+
+ file = Language_C.Module(fileName)
+
+ possibleTestFiles = list()
+ for hint in hints:
+ possibleTestFiles.append(hint + "/" + file.getExecutableName())
+ possibleTestFiles.append(hint + "/" + file.getTestExecutableName())
+ possibleTestFiles.append(directoryName + "/" + file.getExecutableName())
+ possibleTestFiles.append(directoryName + "/" + file.getTestExecutableName())
+ possibleTestFiles.append(directoryName + "/test/" + file.getTestExecutableName())
+
+ result = None
+ for possibleTestFile in possibleTestFiles:
+ if checkTestExecutable(possibleTestFile) == True:
+ result = os.path.abspath(possibleTestFile)
+ break
+
+ return result
+
+
+def textSummary(args, filesAndTests, gCovResults, prefix=""):
+
+ summary = GCov.computeSummary(filesAndTests, gCovResults)
+
+ if not args.includeTestSources:
+ summary = GCovSummary.removeTestSourceFiles(summary)
+
+ if len(summary) == 0:
+ return
+
+ if args.explain:
+ pp = pprint.PrettyPrinter(indent=2, width=150)
+ pp.pprint(summary)
+
+ maximumFileLength = max(map(lambda entry: len(entry), summary))
+
+ format = "%s%-" + str(maximumFileLength) + "s %6s"
+ print format % (prefix, "File Path", "Score")
+
+ format = "%s%-" + str(maximumFileLength) + "s %6.2f"
+ for testedFile in sorted(summary.keys()):
+ string = format % (prefix, testedFile, summary[testedFile]["coverage"])
+ if summary[testedFile]["direct"] == "indirect":
+ ANSITerm.printColorized("magenta", string)
+ else:
+ LongBow.scorePrinter(eval(args.distribution), summary[testedFile]["coverage"], string)
+
+ return
+
+
+def textAverage(args, filesAndTests, gcovResults):
+ summary = GCov.computeSummary(filesAndTests, gcovResults)
+
+ if not args.includeTestSources:
+ summary = GCovSummary.removeTestSourceFiles(summary)
+
+ score = GCovSummary.averageCoverage(summary)
+
+ LongBow.scorePrinter(eval(args.distribution), score, "%.2f" % (score))
+ return score
+
+
+def csvSummary(args, filesAndTests, gCovResults):
+ summary = GCov.computeSummary(filesAndTests, gCovResults)
+
+ if not args.includeTestSources:
+ summary = GCovSummary.removeTestSourceFiles(summary)
+
+ if len(summary) > 0:
+ for testedFile in sorted(summary.keys()):
+ outputString = "%s,%.2f" % (testedFile, summary[testedFile]["coverage"])
+ LongBow.scorePrinter(eval(args.distribution), summary[testedFile]["coverage"], outputString)
+
+ return
+
+
+def csvAverage(args, filesAndTests, gcovResults):
+ summary = GCov.computeSummary(filesAndTests, gcovResults)
+
+ if not args.includeTestSources:
+ summary = GCovSummary.removeTestSourceFiles(summary)
+
+ score = GCovSummary.averageCoverage(summary)
+
+ LongBow.scorePrinter(eval(args.distribution), score, "%.2f" % (score))
+ return
+
+
+def textVisualDisplayGcovLine(line):
+ token = line.split(":", 2)
+ if len(token) == 3:
+ if token[0] == "#####":
+ print ANSITerm.colorize("red", token[1] + " " + token[2])
+ elif token[0] == "$$$$$":
+ print ANSITerm.colorize("yellow", token[1] + " " + token[2])
+ else:
+ print ANSITerm.colorize("green", token[1] + " " + token[2])
+
+ return
+
+
+def textVisual(args, filesAndTests, gcovResults):
+
+ summary = GCov.computeSummary(filesAndTests, gcovResults)
+ if args.explain:
+ pp = pprint.PrettyPrinter(indent=2, width=150)
+ pp.pprint(summary)
+ pp.pprint(filesAndTests)
+
+ for entry in filesAndTests:
+ print entry[0]
+ try:
+ gcovLines = summary[entry[0]]["gcovLines"]
+ map(lambda line: textVisualDisplayGcovLine(line.strip()), gcovLines)
+ except KeyError:
+ print >> sys.stderr, "No coverage information for", entry[0]
+
+ return
+
+
+def displaySummary(args, filesAndTests, newGCovResults):
+ if args.output == "text":
+ textSummary(args, filesAndTests, newGCovResults)
+ elif args.output == "csv":
+ csvSummary(args, filesAndTests, newGCovResults)
+ else:
+ print >> sys.stderr, "Unsupported output type"
+ return
+
+
+def displayAverage(args, filesAndTests, gcovResults):
+ if args.output == "text":
+ textAverage(args, filesAndTests, gcovResults)
+ elif args.output == "csv":
+ csvAverage(args, filesAndTests, gcovResults)
+ else:
+ print >> sys.stderr, "Unsupported output type"
+ return
+
+
+def explain(args, filesAndTests, gcovResults):
+
+ pp = pprint.PrettyPrinter(indent=2, width=150)
+ pp.pprint(gcovResults)
+
+ return
+
+def getFilesAndTests(fileNames, testDirs=[]):
+ namesAndPaths = map(lambda fileName: [fileName, os.path.abspath(fileName)], fileNames)
+ filesAndTests = map(lambda nameAndPath: [ nameAndPath[0], findTestExecutable(nameAndPath[1], testDirs) ], namesAndPaths)
+ return filesAndTests
+
+
+def gradeAndPrint(targets, testDirs=[], problemsOnly=False, prefix=""):
+ filesAndTests = getFilesAndTests(targets, testDirs)
+ newGCovResults = map(lambda fileAndTestFile: GCov.getCoverage(fileAndTestFile[1]), filesAndTests)
+
+ summarys = GCov.computeSummary(filesAndTests, newGCovResults)
+ if len(summarys) < 1:
+ print "%sNo GCov Results - Please be sure to run 'make check' first" % prefix
+ return False
+ summarys = GCovSummary.removeTestSourceFiles(summarys)
+
+ paths = summarys.keys()
+ if problemsOnly:
+ paths = filter(lambda key: summarys[key]["coverage"] < 100, paths)
+
+ distribution=[99,90]
+ maximumFileLength = max(map(lambda entry: len(os.path.relpath(entry)), paths))
+ format = "%s%-" + str(maximumFileLength) + "s %6s"
+ print format % (prefix, "File Path", "Score")
+ format = "%s%-" + str(maximumFileLength) + "s %6.2f"
+ for path in sorted(paths):
+ string = format % (prefix, os.path.relpath(path), summarys[path]["coverage"])
+ LongBow.scorePrinter(distribution, summarys[path]["coverage"], string)
+
+ return True
+
+def commandLineMain(args, fileNames, testDir=""):
+
+ testDirs = []
+ if testDir:
+ testDirs.append(testDir)
+ fileNames = map(lambda fileName: os.path.abspath(fileName), fileNames)
+ filesAndTests = map(lambda fileName: [fileName, findTestExecutable(fileName, testDirs)], fileNames)
+
+ filesWithNoTest = filter(lambda fileAndTest: fileAndTest[1] == None, filesAndTests)
+ if len(filesWithNoTest) != 0:
+ outputFormat = "%s has no corresponding test executable or coverage data.\n"
+ map(lambda filesAndTests: sys.stderr.write(outputFormat % (filesAndTests[0])), filesWithNoTest)
+
+ gCovResults = map(lambda fileAndTestFile: GCov.getCoverage(fileAndTestFile[1]), filesAndTests)
+
+ if args.summary is True:
+ displaySummary(args, filesAndTests, gCovResults)
+ elif args.average is True:
+ displayAverage(args, filesAndTests, gCovResults)
+ elif args.visual is True:
+ textVisual(args, filesAndTests, gCovResults)
+ elif args.explain is True:
+ explain(args, filesAndTests, gCovResults)
+
+ return True
diff --git a/longbow/src/python/site-packages/longbow/DoxygenReport.py b/longbow/src/python/site-packages/longbow/DoxygenReport.py
new file mode 100755
index 00000000..46edf047
--- /dev/null
+++ b/longbow/src/python/site-packages/longbow/DoxygenReport.py
@@ -0,0 +1,161 @@
+#! /usr/bin/env python
+# Copyright (c) 2017 Cisco and/or its affiliates.
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at:
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+#
+
+import sys
+import os
+import pprint
+import subprocess
+import difflib
+import csv
+import LongBow
+
+def concatenateContinuationLines(lines):
+ '''
+ Parse doxygen log lines.
+ Lines that are indented by a space are continutations of the previous line.
+ '''
+ result = list()
+ accumulator = ""
+ for line in lines:
+ line = line.rstrip()
+ if line.startswith(" ") == False and line.startswith(" ") == False:
+ if len(accumulator) > 0:
+ result.append(accumulator)
+ accumulator = line
+ else:
+ accumulator = accumulator + " " + line.lstrip()
+
+ result.append(accumulator)
+
+ return result
+
+def parseLine(line):
+ result = None
+ if not line.startswith("<"):
+ fields = line.split(":")
+ if len(fields) >= 4:
+ result = { "fileName" : fields[0].strip(),
+ "lineNumber" : int(fields[1].strip()),
+ "type" : "documentation",
+ "severity" : fields[2].strip(),
+ "message" : " ".join(fields[3:]).strip()}
+ elif line.startswith("error"):
+ print line
+ elif len(line) > 0:
+ print "Consider using doxygen -s:", line
+
+ return result
+
+def canonicalize(lines):
+ lines = concatenateContinuationLines(lines)
+ parsedLines = map(lambda line: parseLine(line), lines)
+ parsedLines = filter(lambda line: line != None, parsedLines)
+ return parsedLines
+
+def organize(entries):
+ result = dict()
+
+ for entry in entries:
+ if not entry["fileName"] in result:
+ result[entry["fileName"]] = dict()
+
+ entryByFile = result[entry["fileName"]]
+
+ if not str(entry["lineNumber"]) in entryByFile:
+ entryByFile[str(entry["lineNumber"])] = list()
+ if not entry in entryByFile[str(entry["lineNumber"])]:
+ entryByFile[str(entry["lineNumber"])].append(entry)
+
+ return result
+
+def textualSummary(distribution, documentation):
+ maxWidth = 0
+ for entry in documentation:
+ if len(entry) > maxWidth:
+ maxWidth = len(entry)
+
+ formatString ="%-" + str(maxWidth) + "s %8d %8d %.2f%%"
+ for entry in documentation:
+ badLines = len(documentation[entry])
+ totalLines = LongBow.countLines(entry)
+ score = float(totalLines - badLines) / float(totalLines) * 100.0
+ LongBow.scorePrinter(distribution, score, formatString % (entry, totalLines, badLines, score))
+ return
+
+def textualAverage(distribution, documentation, format):
+ sum = 0.0
+
+ for entry in documentation:
+ badLines = len(documentation[entry])
+ totalLines = LongBow.countLines(entry)
+ score = float(totalLines - badLines) / float(totalLines) * 100.0
+ sum = sum + score
+
+ if len(documentation) == 0:
+ averageScore = 100.0
+ else:
+ averageScore = sum / float(len(documentation))
+
+ LongBow.scorePrinter(distribution, averageScore, format % averageScore)
+
+def csvSummary(distribution, documentation):
+ formatString ="documentation,%s,%d,%d,%.2f%%"
+ for entry in documentation:
+ badLines = len(documentation[entry])
+ totalLines = LongBow.countLines(entry)
+ score = float(totalLines - badLines) / float(totalLines) * 100.0
+ LongBow.scorePrinter(distribution, score, formatString % (entry, totalLines, badLines, score))
+ return
+
+
+def gradeAndPrint(targets, doxLogfile, problemsOnly=False, prefix=""):
+ with open(doxLogfile, 'r') as f:
+ lines = f.readlines()
+
+ lines = canonicalize(lines)
+
+ result = organize(lines)
+
+ pp = pprint.PretyPrinter(intent=len(prefix))
+
+ distribution=[100, 95]
+ textualSummary(distribution, result)
+ return True
+
+def commandLineMain(args, fileNames):
+ if not args.summary and not args.average:
+ args.summary = True
+
+ with open(args.doxygenlog, 'r') as f:
+ lines = f.readlines()
+
+ lines = canonicalize(lines)
+
+ result = organize(lines)
+
+ pp = pprint.PrettyPrinter(indent=4)
+ #pp.pprint(result)
+
+ distribution = eval(args.distribution)
+ if args.summary:
+ if args.output == "text":
+ textualSummary(distribution, result)
+ else:
+ csvSummary(distribution, result)
+
+ if args.average:
+ textualAverage(distribution, result, "%.2f")
diff --git a/longbow/src/python/site-packages/longbow/FileUtil.py b/longbow/src/python/site-packages/longbow/FileUtil.py
new file mode 100755
index 00000000..ae3113f6
--- /dev/null
+++ b/longbow/src/python/site-packages/longbow/FileUtil.py
@@ -0,0 +1,102 @@
+#! /usr/bin/env python
+# Copyright (c) 2017 Cisco and/or its affiliates.
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at:
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+#
+
+import sys
+import os
+import csv
+import subprocess
+
+def readFileLines(fileName):
+ '''
+ Get the entire file into memory as a list of lines.
+ '''
+ result = None
+
+ with open(fileName, "r") as file:
+ result = file.readlines()
+
+ return result
+
+def readFileString(fileName):
+ '''
+ Get the entire file into memory as a python string.
+ '''
+ result = None
+
+ if fileName != None and len(fileName) > 0:
+ with open (fileName, "r") as file:
+ result = file.read()
+
+ return result
+
+def sourceFileNameToName(sourceFileName):
+ '''
+ Given the path to a source file, return the name without any path components or trailing suffix.
+ '''
+ name = os.path.basename(sourceFileName)
+ return name.split(".")[0]
+
+def canonicalizeFunctionName(functionName):
+ '''
+ Given a function name that contains the initial '_' character,
+ strip it and return a canonicalised form of the same name suitable for a source file.
+ '''
+ if functionName[0] == "_":
+ functionName = functionName[1:]
+ return functionName
+
+def isReservedName(functionName):
+ '''
+ Given a canonicalized name, determine if it is a reserved name according to ISO/IEC 9899:2011 and ANSI Sec. 4.1.2.1,
+ identifiers that begin with an underscore and either an uppercase letter or another underscore are always reserved for any use.
+ '''
+ if functionName[0] == '_' and functionName[1] == '_':
+ return True
+ elif functionName[0] == '_' and functionName[1].isupper():
+ return True
+ return False
+
+def getDarwinTestableFunctions(objectFileName):
+ '''
+ Retrieve a set of local and global function names within a file.
+ '''
+ command = [ "/usr/bin/nm", "-gUm", objectFileName ]
+
+ output = subprocess.check_output(command)
+ lines = output.splitlines()
+
+ external = []
+ internal = []
+ for line in lines:
+ if line:
+ fields = line.split(" ")
+ if (len(fields) > 1) and (fields[1] == "(__TEXT,__text)"):
+ functionName = canonicalizeFunctionName(fields[3])
+
+ if not isReservedName(functionName):
+ if fields[2] == "external":
+ external.append( ( functionName ) )
+ else:
+ internal.append( ( functionName ) )
+ pass
+ pass
+ pass
+ pass
+
+ external.sort()
+ internal.sort()
+ return { "Local": internal, "Global" : external }
diff --git a/longbow/src/python/site-packages/longbow/GCov.py b/longbow/src/python/site-packages/longbow/GCov.py
new file mode 100755
index 00000000..c2705fda
--- /dev/null
+++ b/longbow/src/python/site-packages/longbow/GCov.py
@@ -0,0 +1,232 @@
+#! /usr/bin/env python
+# Copyright (c) 2017 Cisco and/or its affiliates.
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at:
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+#
+import os
+import subprocess
+import re
+import sys
+import pprint
+import FileUtil
+import Language_C
+
+class GCov:
+ def __init__(self):
+ return
+
+def canonicalizeLines(lines):
+ result = []
+ accumulatedLine = ""
+ for line in lines:
+ line = line.strip()
+ if len(line) == 0:
+ if len(accumulatedLine.strip()) > 0:
+ result.append(accumulatedLine.strip())
+ accumulatedLine = ""
+ elif "creating" in line:
+ if len(accumulatedLine.strip()) > 0:
+ result.append(accumulatedLine.strip())
+ accumulatedLine = ""
+ result.append(line)
+ else:
+ accumulatedLine = accumulatedLine + " " + line
+ return result
+
+def executeGCovCommand(testExecutableFileName):
+ currentDirectory = os.getcwd()
+ targetDirectory = os.path.dirname(os.path.abspath(testExecutableFileName))
+ testExecutableBaseName = os.path.basename(testExecutableFileName)
+
+ os.chdir(targetDirectory)
+ objects = Language_C.findFiles("./", testExecutableBaseName+"*.o")
+ if not objects:
+ return
+ objdir = os.path.dirname(objects[0])
+ gcdas = Language_C.findFiles("./", testExecutableBaseName+"*.gcda")
+ if not gcdas:
+ return
+ gcda = gcdas[0]
+ gcnos = Language_C.findFiles("./", testExecutableBaseName+"*.gcno")
+ if not gcnos:
+ return
+ gcno = gcnos[0]
+ proc = subprocess.Popen(['gcov', '-af', '-o='+objdir, '-gcda='+gcda, '-gcno='+gcno, testExecutableBaseName], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+ os.chdir(currentDirectory)
+
+ inputLines = map(lambda line: line.strip(), proc.stdout)
+
+ return canonicalizeLines(inputLines)
+
+def parseFunctionLine(line):
+ # Function 'TestFixture_Global_TearDown' Lines executed:71.43% of 7"
+ search = re.search("Function '(.*)' Lines executed:(.*)% of (.*)", line, re.IGNORECASE)
+
+ result = []
+ if search:
+ functionName = search.group(1)
+ percentage = search.group(2)
+ totalLines = search.group(3)
+ result = { functionName : { "coverage" : float(percentage), "numberOfLines" : int(totalLines) } }
+
+ return result
+
+def parseFileLine(testExecutableDirectoryName, line):
+ # File './../parc_Buffer.c' Lines executed:92.69% of 424
+ search = re.search("File '(.*)' Lines executed:(.*)% of (.*)", line, re.IGNORECASE)
+
+ result = { }
+ if search:
+ baseName = os.path.basename(search.group(1));
+ fileName = os.path.abspath(testExecutableDirectoryName + "/" + baseName)
+ percentage = search.group(2)
+ totalLines = search.group(3)
+ result = { fileName : { "coverage" : float(percentage), "totalLines" : int(totalLines) } }
+
+ return result
+
+def parseCreatingLine(testExecutableDirectoryName, line):
+ search = re.search("(.*):creating '(.*)'", line, re.IGNORECASE)
+
+ result = None
+ if search:
+ baseName = os.path.basename(search.group(1));
+ fileName = os.path.abspath(testExecutableDirectoryName + "/" + baseName)
+ baseName = os.path.basename(search.group(2));
+ gcovFileName = os.path.abspath(testExecutableDirectoryName + "/" + baseName)
+
+ result = { "fileName" : fileName, "gcovFileName" : gcovFileName, "gcovLines" : FileUtil.readFileLines(gcovFileName) }
+
+ return result
+
+
+def computeCoverageFromGCovLines(testExecutableDirectoryName, testExecutableFileName, lines):
+ '''
+ This produces a dictionary consisting of:
+
+ 'testedFiles' : dictionary containing as keys 'functions' and the name of a file that was tested
+
+ The value of the key that is the name of a file that was tested is a dictionary containing the keys,
+ 'coverage', 'gcovFileName', and 'gcovLines'
+
+ 'coverage' is the percentage of code executed
+
+ 'testedFunctions' is a list containing lists consisting of the function name, the percent executed, and the number of lines in the function.
+ '''
+ testedFiles = { }
+ testedFunctions = { }
+ gcovFileNames = []
+ for line in lines:
+ if line.startswith("Function"):
+ element = parseFunctionLine(line)
+ testedFunctions.update(element)
+ elif line.startswith("File"):
+ element = parseFileLine(testExecutableDirectoryName, line)
+ testedFiles.update(element)
+ else:
+ element = parseCreatingLine(testExecutableDirectoryName, line)
+ if element != None:
+ fileName = element["fileName"]
+ del element["fileName"]
+ testedFiles[fileName].update(element)
+ pass
+
+ result = { testExecutableFileName : { "testedFunctions" : testedFunctions, "testedFiles" : testedFiles } }
+
+ return result
+
+
+def noCoverage():
+ result = { "testedFiles" : { }, "testedFunctions" : { } }
+ return result
+
+def getCoverage(testExecutableFileName):
+ '''
+ '''
+ if testExecutableFileName == None:
+ return None
+
+ testExecutableFileName = os.path.abspath(testExecutableFileName)
+ testExecutableDirectoryName = os.path.dirname(testExecutableFileName)
+ gcovLines = executeGCovCommand(testExecutableFileName)
+
+ return computeCoverageFromGCovLines(testExecutableDirectoryName, testExecutableFileName, gcovLines)
+
+
+def selectGreaterCoverage(testedFileA, testedFileB):
+ result = testedFileB
+ if testedFileA["coverage"] >= testedFileB["coverage"]:
+ result = testedFileA
+
+ return result
+
+def computeSummary(filesAndTests, newGCovResults):
+ '''
+ First, for each target file named in the gcov results, find the corresponding testedFile and report the maximum coverage.
+
+ If the target file is not in any of the testedFiles
+
+ { targetFileName : { "coverage": percent, "veracity" : "direct" / "indirect" } }
+ '''
+
+ newGCovResults = filter(lambda entry: entry != None, newGCovResults)
+
+ result = dict()
+ for entry in newGCovResults:
+ for testExecutableName in entry:
+ testExecutableCSourceName = Language_C.Module(testExecutableName).getCSourceName()
+
+ for testedFileName in entry[testExecutableName]["testedFiles"]:
+ testedFile = entry[testExecutableName]["testedFiles"][testedFileName]
+
+ if Language_C.Module(testedFileName).getTestExecutableName() == os.path.basename(testExecutableName):
+ result[testedFileName] = testedFile
+ result[testedFileName]["direct"] = "direct"
+ elif testedFileName in result:
+ bestCoverage = selectGreaterCoverage(testedFile, result[testedFileName])
+ if result[testedFileName] != bestCoverage:
+ result[testedFileName] = bestCoverage
+ result[testedFileName]["direct"] = "indirect"
+ else:
+ result[testedFileName] = testedFile
+ result[testedFileName]["direct"] = "indirect"
+
+ return result
+
+def computeAverage(filesAndTests, gcovResults):
+ summary = computeSuperSummary(filesAndTests, gcovResults)
+
+ filesToAverage = removeTestSourceFiles(summary)
+
+ score = 0.0
+
+ if len(filesToAverage) > 0:
+ sum = reduce(lambda x, y: x + y, map(lambda entry: summary[entry]["coverage"], filesToAverage))
+ score = sum / float(len(filesToAverage))
+
+ return score
+
+
+if __name__ == '__main__':
+ pp = pprint.PrettyPrinter(indent=4, width=132)
+ if True:
+ gcovResult = getCoverage("/Users/gscott/Documents/workspace/Distillery/Libparc/parc/algol/test/test_parc_JSON")
+ else:
+ lines = sys.stdin.readlines()
+ lines = canonicalizeLines(lines)
+ pp.pprint(lines)
+
+ gcovResult = computeCoverageFromGCovLines("/Users/gscott/Documents/workspace/Distillery/Libparc/parc/algol/test/", lines)
+
+ pp.pprint(gcovResult)
diff --git a/longbow/src/python/site-packages/longbow/GCovSummary.py b/longbow/src/python/site-packages/longbow/GCovSummary.py
new file mode 100755
index 00000000..bfa6710a
--- /dev/null
+++ b/longbow/src/python/site-packages/longbow/GCovSummary.py
@@ -0,0 +1,42 @@
+#! /usr/bin/env python
+# Copyright (c) 2017 Cisco and/or its affiliates.
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at:
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+#
+import os
+import subprocess
+import re
+import sys
+import pprint
+import FileUtil
+import Language_C
+
+
+def removeTestSourceFiles(gcovSummary):
+ deleteThese = filter(lambda entry: Language_C.Module(entry).isTestSourceName(), gcovSummary)
+
+ for entry in deleteThese:
+ del gcovSummary[entry]
+
+ return gcovSummary
+
+
+def averageCoverage(summary):
+ score = 0.0
+
+ if len(summary) > 0:
+ sum = reduce(lambda x, y: x + y, map(lambda entry: summary[entry]["coverage"], summary))
+ score = sum / float(len(summary))
+
+ return score
diff --git a/longbow/src/python/site-packages/longbow/Language_C.py b/longbow/src/python/site-packages/longbow/Language_C.py
new file mode 100755
index 00000000..1d97a676
--- /dev/null
+++ b/longbow/src/python/site-packages/longbow/Language_C.py
@@ -0,0 +1,204 @@
+#! /usr/bin/env python
+# Copyright (c) 2017 Cisco and/or its affiliates.
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at:
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+#
+import os
+import glob
+
+import fnmatch
+import subprocess
+
+def findFiles(startDir, pattern):
+ matches = []
+ for root, dirnames, filenames in os.walk(startDir):
+ for filename in fnmatch.filter(filenames, pattern):
+ matches.append(os.path.join(root, filename))
+
+ return matches
+
+def getLibPathForObject(libraryPath, filename):
+ '''
+ Returns a path to an object file suitable for nm
+ '''
+ result = ''
+
+ command = ['/usr/bin/ar', '-t', libraryPath]
+ output = subprocess.check_output(command)
+ lines = output.splitlines()
+
+ for line in lines:
+ tokens = line.split('.')
+ if tokens[0] == filename:
+ result = libraryPath + '(' + line + ')'
+ break
+
+ return result
+
+class Module:
+ '''
+ Represent a C language module.
+ A module consists of the file names of the C source, C header file, object file, and an executable file
+ '''
+ def __init__(self, srcPath, objectDirs=[]):
+ self.path = self.initialzePath(srcPath)
+ if not objectDirs:
+ objectDirs = [self.path]
+ split = srcPath.split("/")
+ self.originalName = split[len(split) - 1]
+ self.originalBaseName = os.path.basename(srcPath)
+ tokens = self.originalBaseName.split('.')
+ self.fileName = tokens[0].replace("/","")
+
+ if self.fileName.startswith("test_"):
+ self.fileName = self.fileName[5:]
+
+ # Search for an appropriate object
+ self.objectPath = "";
+ for objectDir in objectDirs:
+ if objectDir.endswith(".a"):
+ self.objectPath = getLibPathForObject(objectDir, self.fileName)
+ if self.objectPath:
+ break
+ else:
+ objectSearchPath = os.path.join(objectDir, self.fileName) + "*.o*"
+ ofiles = glob.glob(objectSearchPath);
+ if ofiles:
+ # if we've found some matches, assume we want the first.
+ self.objectPath = ofiles[0];
+ break
+ return
+
+ def isTestExecutableName(self):
+ return self.getTestExecutableName() == self.originalBaseName
+
+ def isTestSourceName(self):
+ return self.getTestSourceName() == self.originalBaseName
+
+ def isCSourceName(self):
+ return self.getCSourceName() == self.originalBaseName
+
+ def isCHeaderName(self):
+ return self.getCHeaderName() == self.originalBaseName
+
+ def getCSourceName(self):
+ return self.fileName + ".c"
+
+ def getCHeaderName(self):
+ return self.fileName + ".h"
+
+ def getPathToObjectFile(self):
+ return self.objectPath;
+
+ def getExecutableName(self):
+ return self.fileName
+
+ def getTestExecutableName(self):
+ return "test_" + self.fileName
+
+ def getTestSourceName(self):
+ return self.getTestExecutableName() + ".c"
+
+ def getNamespace(self):
+ sourceFileName = self.getCSourceName()
+ if (sourceFileName.find("_") >= 0):
+ stripped = sourceFileName[0:sourceFileName.index("_")]
+ return stripped
+ else:
+ return None
+
+ def getModuleName(self):
+ sourceFileName = self.getCSourceName()
+ split = sourceFileName.split("/")
+ sourceFileName = split[len(split) - 1]
+ if (sourceFileName.find(".") >= 0):
+ stripped = sourceFileName[0:sourceFileName.index(".")]
+ return stripped
+ else:
+ return None
+
+ def getModulePrefix(self):
+ sourceFileName = self.getCSourceName()
+ squashed = sourceFileName.replace("_", "")
+ if (squashed.find(".") >= 0):
+ return squashed[0:squashed.index(".")]
+ else:
+ return None
+
+ def getTypeName(self):
+ sourceFileName = self.getCSourceName()
+ if (sourceFileName.find(".") >= 0 and sourceFileName.find("_") >= 0):
+ stripped = sourceFileName[(sourceFileName.index("_") + 1):sourceFileName.index(".")]
+ return stripped
+ else:
+ return None
+
+ def getModulePath(self):
+ return self.path
+
+ def initialzePath(self, sourceFileName):
+ parts = sourceFileName.split("/")
+ parts = parts[0:len(parts) - 1]
+ return '/'.join(map(str, parts))
+
+if __name__ == '__main__':
+ cFile = Module("file.c.gcov")
+ if cFile.getCSourceName() != "file.c":
+ print "getCSourceName failed", cFile.getCSourceName()
+
+ if cFile.getCHeaderName() != "file.h":
+ print "getCHeaderName failed", cFile.getCHeaderName()
+
+ if cFile.getTestSourceName() != "test_file.c":
+ print "getTestSourceName failed", cFile.getTestSourceName()
+
+ if cFile.getTestExecutableName() != "test_file":
+ print "getTestExecutableName failed", cFile.getTestExecutableName()
+
+ if cFile.getNamespace() != None:
+ print "getNamespace failed", cFile.getNamespace()
+
+ if cFile.getModuleName() != "file":
+ print "getModuleName failed", cFile.getModuleName()
+
+ if cFile.getModulePrefix() != "file":
+ print "getModulePrefix failed", cFile.getModulePrefix()
+
+ if cFile.getTypeName() != None:
+ print "getTypeName failed", cFile.getTypeName()
+
+ cFile = Module("parc_Object.c.gcov")
+ if cFile.getCSourceName() != "parc_Object.c":
+ print "getCSourceName failed", cFile.getCSourceName()
+
+ if cFile.getCHeaderName() != "parc_Object.h":
+ print "getCHeaderName failed", cFile.getCHeaderName()
+
+ if cFile.getTestSourceName() != "test_parc_Object.c":
+ print "getTestSourceName failed", cFile.getTestSourceName()
+
+ if cFile.getTestExecutableName() != "test_parc_Object":
+ print "getTestExecutableName failed", cFile.getTestExecutableName()
+
+ if cFile.getNamespace() != "parc":
+ print "getNamespace failed", cFile.getNamespace()
+
+ if cFile.getModuleName() != "parc_Object":
+ print "getModuleName failed", cFile.getModuleName()
+
+ if cFile.getModulePrefix() != "parcObject":
+ print "getModulePrefix failed", cFile.getModulePrefix()
+
+ if cFile.getTypeName() != "Object":
+ print "getTypeName failed", cFile.getTypeName()
diff --git a/longbow/src/python/site-packages/longbow/LongBow.py b/longbow/src/python/site-packages/longbow/LongBow.py
new file mode 100755
index 00000000..d1f0e77a
--- /dev/null
+++ b/longbow/src/python/site-packages/longbow/LongBow.py
@@ -0,0 +1,96 @@
+#! /usr/bin/env python
+# Copyright (c) 2017 Cisco and/or its affiliates.
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at:
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+#
+import sys
+
+ansiRed = "\x1b[31m";
+ansiGreen = "\x1b[32m";
+ansiYellow = "\x1b[33m";
+ansiMagenta = "\x1b[35m";
+ansiReset = "\x1b[0m";
+
+
+def buildRed(string):
+ if sys.stdout.isatty():
+ return ansiRed + string + ansiReset
+ else:
+ return string
+
+
+def buildGreen(string):
+ if sys.stdout.isatty():
+ return ansiGreen + string + ansiReset
+ else:
+ return string
+
+
+def buildYellow(string):
+ if sys.stdout.isatty():
+ return ansiYellow + string + ansiReset
+ else:
+ return string
+
+
+def score(distribution, score):
+ result = "red"
+
+ if (score > distribution[0]):
+ result = "green"
+ elif (score > distribution[1]):
+ result = "yellow"
+
+ return result
+
+
+def scoreBuilder(distribution, score, string):
+ '''
+ scores is a list of 2 decreasing values.
+ The first is the minimum score for green, the second is the minimum score for yellow.
+ The rest art red
+ '''
+ if (score > distribution[0]):
+ return buildGreen(string)
+ elif (score > distribution[1]):
+ return buildYellow(string)
+ else:
+ return buildRed(string)
+
+
+def scorePrinter(distribution, score, string):
+ print scoreBuilder(distribution, score, string)
+
+
+def countLines(fileName):
+ i = 0
+ with open(fileName) as f:
+ for i, l in enumerate(f):
+ pass
+ return i + 1
+
+
+def CFileNameToFunctionPrefix(fileName):
+ '''
+ Given the name of a C source file or header file,
+ return the canonical name prefix for functions within that file.
+ For example, the input name "parc_Buffer.c" results in "parcBuffer_"
+ '''
+ fileName = os.path.basename(fileName);
+ fileNameSpace = os.path.splitext(fileName)[0]
+ parts = fileNameSpace.partition("_")
+ result = None
+ if len(parts) == 3:
+ result = parts[0] + parts[2] + "_"
+ return result
diff --git a/longbow/src/python/site-packages/longbow/NameReport.py b/longbow/src/python/site-packages/longbow/NameReport.py
new file mode 100755
index 00000000..1d38b4ec
--- /dev/null
+++ b/longbow/src/python/site-packages/longbow/NameReport.py
@@ -0,0 +1,818 @@
+#! /usr/bin/env python
+# Copyright (c) 2017 Cisco and/or its affiliates.
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at:
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+#
+
+import sys
+import os
+import subprocess
+import argparse
+import csv
+import traceback
+
+import LongBow
+from Language_C import Module
+from FileUtil import *
+from pprint import pprint
+
+class NoObjectFileException(Exception):
+ def __init__(self, value):
+ self.value = value
+ def __str__(self):
+ return repr(self.value)
+
+
+# Global error string formatting map
+conformanceErrorFormatMap = {
+ "NAMESPACE_MISMATCH" : "Function signature %s does not have the correct namespace prefix (%s)",
+ "MODULE_MISMATCH" : "Function signature %s does not have the correct module prefix (%s)",
+ "INVALID_STATIC_NAME" : "Local function signature %s must begin with an underscore '_'.",
+ "INVALID_GLOBAL_ENUM_NAME" : "Global enumeration prefix %s must match the module namespace %s.",
+ "INVALID_STATIC_ENUM_NAME" : "Local enumeration prefix %s must begin with an underscore '_'.",
+ "INVALID_GLOBAL_ENUM_VALUE_NAME" : "Global enumeration value prefix %s must match the enum name %s.",
+ "INVALID_GLOBAL_TYPEDEF_NAME" : "Global typedef prefix %s must match the module namespace %s.",
+ "INVALID_STATIC_TYPEDEF_NAME" : "Local typedef prefix %s must begin with an underscore '_'.",
+}
+
+def tuplesListToCSV(points):
+ '''
+ Convert a list of tuples -- data points -- to a list of CSV-formatted strings.
+ '''
+ lines = []
+ for point in points:
+ line = ""
+ for i in range(len(point) - 1):
+ line = line + str(point[i]) + ","
+ line = line + str(point[-1])
+ lines.append(line)
+ return lines
+
+def tuplesListToPrettyText(points, distribution = [99, 90]):
+ '''
+ Convert a list of tuples -- data points -- to a list of colorized strings based
+ on the provided distribution.
+ '''
+ lines = []
+ for point in points:
+ line = ""
+ for i in range(len(point) - 1):
+ line = line + str(point[i]) + " "
+ line = line + str(point[-1])
+ lines.append(LongBow.scoreBuilder(distribution, point[-1], line))
+ return lines
+
+def writeListToStream(llist, fileHandle, appendNewLine = True):
+ '''
+ Write the list of lines to the given file stream handle, appending a new line after
+ every line if told to do so.
+ '''
+ for line in llist:
+ fileHandle.write(line)
+ if appendNewLine:
+ fileHandle.write("\n")
+
+def isValidModuleName(namespace, typeName, moduleName):
+ '''
+ Determine if a given module name is valid (conforming) given the namespace and typename.
+
+ For example, a module in the `parc` namespace with typename `JSON` must be named `parc_JSON`.
+ '''
+ constructedName = namespace + "_" + typeName
+ return (moduleName == constructedName)
+
+def isValidFunctionName(namespace, modulePrefix, functionName, scope):
+ '''
+ Determine if a given function name is valid (conforming) given the namespace and typename.
+ '''
+ if (scope != "Local"):
+ if(not functionName.startswith(namespace)):
+ return False, conformanceErrorFormatMap["NAMESPACE_MISMATCH"] % ('"' + functionName + '"', namespace)
+ elif (not functionName.startswith(modulePrefix)):
+ return False, conformanceErrorFormatMap["MODULE_MISMATCH"] % ('"' + functionName + '"', modulePrefix)
+ else:
+ return True, ""
+ elif (scope == "Local" and not functionName.startswith("_")):
+ return False, conformanceErrorFormatMap["INVALID_STATIC_NAME"] % ('"' + functionName + '"')
+ else:
+ return True, ""
+
+def isValidTypedefName(typePrefix, name, static):
+ '''
+ Determine if a given typedef name is valid (conforming) given the namespace and typename.
+ '''
+ originalName = name
+ originalTypePrefix = typePrefix
+
+ name = name.lower()
+ typePrefix = typePrefix.lower()
+
+ if ((not static) and name.startswith(typePrefix)) and originalName[0].isupper():
+ return True, ""
+ elif ((not static) and (not name.startswith(typePrefix))):
+ return False, conformanceErrorFormatMap["INVALID_GLOBAL_TYPEDEF_NAME"] % ('"' + originalName + '"', originalTypePrefix)
+ elif (static and name.startswith('_')):
+ return True, ""
+ else:
+ return False, conformanceErrorFormatMap["INVALID_STATIC_TYPEDEF_NAME"] % ('"' + originalName + '"')
+
+def isValidEnumName(typePrefix, name, values, static):
+ '''
+ Determine if a given enumeration name is valid (conforming) given the namespace and typename.
+ '''
+ originalName = name
+ originalTypePrefix = typePrefix
+
+ name = name.lower()
+ typePrefix = typePrefix.lower()
+
+ if ((not static) and name.startswith(typePrefix)) and originalName[0].isupper():
+ pass
+ elif ((not static) and (not name.startswith(typePrefix))):
+ return False, conformanceErrorFormatMap["INVALID_GLOBAL_ENUM_NAME"] % ('"' + originalName + '"', originalTypePrefix)
+ elif (static and name.startswith('_')):
+ pass
+ else:
+ return False, conformanceErrorFormatMap["INVALID_STATIC_ENUM_NAME"] % ('"' + originalName + '"')
+
+ for enumVal in values:
+ if ((not static) and not enumVal.startswith(originalName)):
+ return False, conformanceErrorFormatMap["INVALID_GLOBAL_ENUM_VALUE_NAME"] % ('"' + enumVal + '"', originalName)
+ return True, ""
+
+def getTypedefs(path, source):
+ '''
+ Retrieve the names of typedefs (excluding enums) in a given file by parsing the file for
+ typedef specifications. This walks over every line in the file searching for each one,
+ since we cannot extract enum names nicely using linux tools.
+ '''
+ typedefs = []
+ pathToFile = os.path.join(path, source)
+ if os.path.isfile(pathToFile):
+ fin = open(os.path.join(path, source), 'r')
+ lines = fin.readlines()
+ i = 0 # LC
+ while (i < len(lines)):
+ line = lines[i].strip()
+ if not line.startswith("//"):
+ values = []
+ if "typedef" in line and "enum" not in line: # go to the end of the typedef
+ if "{" in line:
+ while "}" not in line:
+ i = i + 1
+ line = lines[i].strip()
+ name = line.replace("}","").replace(";","").strip()
+ typedefs.append(name)
+ else:
+ splits = line.split(" ")
+ if "struct" in splits[1]:
+ name = splits[3].replace(";","")
+ typedefs.append(name.strip())
+ else:
+ pass
+ i = i + 1
+ return typedefs
+
+def getEnumerations(path, source):
+ '''
+ Retrieve the names of enumerations in a given file by parsing the file for enum specifications.
+ This walks over every line in the file searching for enums, since we cannot extract enum names
+ nicely using linux tools.
+ '''
+ enums = []
+ pathToFile = os.path.join(path, source)
+ if os.path.isfile(pathToFile):
+ fin = open(os.path.join(path, source), 'r')
+ lines = fin.readlines()
+ i = 0 # LC
+ while (i < len(lines)):
+ line = lines[i].strip()
+ if not line.startswith("//"):
+ values = []
+ if "typedef enum" in line: # go to the end of the enumeration
+ while (i + 1 < len(lines) and line.find("}") < 0):
+ i = i + 1
+ line = lines[i].strip()
+ values.append(line) # append each string value
+ if (line.find("}") >= 0):
+ name = line.replace("}","").replace(";","")
+ values.pop(len(values) - 1)
+ enums.append((name.strip(), values))
+ i = i + 1
+ return enums
+
+def getTypedefsFromFiles(fileInfoList):
+ '''
+ Get the typedefs from each file in the fileInfoList.
+
+ Each element in fileInfoList is a tuple of the form (path, filename, staticTag),
+ where staticTag is a flag used to indicate if the typedefs in said file should
+ be treated as static.
+ '''
+ allTypedefs = []
+ for (path, fileName, staticTag) in fileInfoList:
+ typedefs = getTypedefs(path, fileName)
+ for typedef in typedefs:
+ allTypedefs.append((typedef, staticTag))
+ return allTypedefs
+
+def getEnumerationsFromFiles(fileInfoList):
+ '''
+ Get the enums from each file in the fileInfoList.
+
+ Each element in fileInfoList is a tuple of the form (path, filename, staticTag),
+ where staticTag is a flag used to indicate if the enums in said file should
+ be treated as static.
+ '''
+ allEnums = []
+ for (path, fileName, staticTag) in fileInfoList:
+ enums = getEnumerations(path, fileName)
+ for (enumName, values) in enums:
+ allEnums.append((enumName, values, staticTag))
+ return allEnums
+
+class FunctionConformanceContainer():
+ def __init__(self, module):
+ self.path = module.getModulePath()
+ self.module = module
+ self.failedFunctions = []
+ self.passedFunctions = []
+
+ if (len(self.path) > 0):
+ self.fullPath = self.path + os.sep + module.getCSourceName()
+ else:
+ self.fullPath = module.getCSourceName()
+
+ self.computeConformance()
+
+ def computeConformance(self):
+ functionDictionary = {}
+ objectFileName = self.module.getPathToObjectFile()
+ sourceFileName = os.path.join(self.path, self.module.getCSourceName())
+ temp = objectFileName
+ if '.a' in temp: # If this is a path into an archive
+ temp = temp.split('(')[0]
+ if not os.path.isfile(temp):
+ raise NoObjectFileException("You must compile " + str(sourceFileName) + " to generate a corresponding object or provide a special object file path")
+
+ try:
+ functionDictionary = getDarwinTestableFunctions(objectFileName)
+ except:
+ raise Exception("You must compile " + str(sourceFileName) + " to generate a corresponding object or provide a special object file path")
+
+ namespace = self.module.getNamespace()
+ modulePrefix = self.module.getModulePrefix()
+
+ # Find all passing/failing functions
+ if (namespace != None and modulePrefix != None):
+ for scope in functionDictionary:
+ for functionName in functionDictionary[scope]:
+ isValid, reason = isValidFunctionName(namespace, modulePrefix, functionName, scope)
+ if isValid:
+ self.addPassedFunction(functionName)
+ else:
+ self.addFailedFunction(functionName, reason)
+
+ def containsMainFunction(self):
+ for (functionName, reason) in self.failedFunctions:
+ if (functionName.strip() == "main" or functionName.strip().startswith("main")):
+ return True
+ for functionName in self.passedFunctions:
+ if (functionName.strip() == "main" or functionName.strip().startswith("main")):
+ return True
+ return False
+
+ @staticmethod
+ def getType():
+ return "function-names"
+
+ def addFailedFunction(self, function, reason):
+ self.failedFunctions.append((function, reason))
+
+ def getFailedFunctions(self):
+ return self.failedFunctions
+
+ def addPassedFunction(self, function):
+ self.passedFunctions.append(function)
+
+ def getPassedFunctions(self):
+ return self.passedFunctions
+
+ def analyzeConformance(self):
+ '''
+ Convert the raw pass/fail function results into a set of individual
+ data points (finegrain results) and overall percentage.
+ '''
+ numPassed = len(self.passedFunctions)
+ numFailed = len(self.failedFunctions)
+
+ self.percentage = 100.0
+ self.points = []
+
+ if (numPassed + numFailed > 0): # skip invalid entries
+ self.percentage = float(float(numPassed) / float(numFailed + numPassed)) * 100.0
+
+ # Data point schema:
+ # namespace, moduleName, targetName, topic, line, col, score
+ for fname in self.passedFunctions:
+ data = ["function-name", fname, 100.0]
+ self.points.append(data)
+
+ for (fname, reason) in self.failedFunctions:
+ data = ["function-name", fname, reason, 0.0]
+ self.points.append(data)
+
+ def getNumberOfPassed(self):
+ return len(self.passedFunctions)
+
+ def getNumberOfFailed(self):
+ return len(self.failedFunctions)
+
+ def totalCSV(self):
+ return tuplesListToCSV(map(lambda point : [self.fullPath] + point, self.points))
+
+ def totalText(self, distribution):
+ formattedLines = tuplesListToPrettyText(self.points, distribution)
+ return map(lambda formattedLine : self.fullPath + " " + formattedLine, formattedLines)
+
+ def summaryCSV(self):
+ line = [(self.getType(), self.percentage)]
+ return tuplesListToCSV(line)
+
+ def summaryText(self, distribution):
+ line = [(self.getType(), self.percentage)]
+ return tuplesListToPrettyText(line, distribution)
+
+ def getScore(self):
+ return (self.getType(), self.percentage)
+
+
+class EnumConformanceContainer():
+ def __init__(self, module):
+ self.path = module.getModulePath()
+ self.module = module
+ self.failedEnums = []
+ self.passedEnums = []
+
+ if (len(self.path) > 0):
+ self.fullPath = self.path + os.sep + module.getCSourceName()
+ else:
+ self.fullPath = module.getCSourceName()
+
+ self.computeConformance()
+
+ def computeConformance(self):
+ sourceFileName = self.module.getCSourceName()
+ headerFileName = self.module.getCHeaderName()
+
+ enums = getEnumerationsFromFiles([(self.path, sourceFileName, True), (self.path, headerFileName, False)])
+ modulePrefix = self.module.getModulePrefix()
+ if (modulePrefix != None):
+ for (enumName, values, staticTag) in enums:
+ isValid, reason = isValidEnumName(modulePrefix, enumName, values, staticTag)
+ if isValid:
+ self.addPassedEnum(enumName)
+ else:
+ self.addFailedEnum(enumName, reason)
+
+ @staticmethod
+ def getType():
+ return "enum-names"
+
+ def addFailedEnum(self, enum, reason):
+ self.failedEnums.append((enum, reason))
+
+ def getFailedEnums(self):
+ return self.failedEnums
+
+ def addPassedEnum(self, enum):
+ self.passedEnums.append(enum)
+
+ def getPassedEnums(self):
+ return self.passedEnums
+
+ def analyzeConformance(self):
+ '''
+ Convert the raw pass/fail enum results into a set of individual
+ data points (finegrain results) and overall percentage.
+ '''
+ self.enumPercentage = 100.0
+ self.points = []
+ numPassed = len(self.passedEnums)
+ numFailed = len(self.failedEnums)
+
+ if (numPassed + numFailed > 0):
+ self.enumPercentage = float((float(numPassed) / float(numPassed + numFailed)) * 100)
+
+ for ename in self.passedEnums:
+ data = ["enum-name", ename, 100.0]
+ self.points.append(data)
+
+ for (ename, reason) in self.failedEnums:
+ data = ["enum-name", ename, reason, 0.0]
+ self.points.append(data)
+
+ def getNumberOfPassed(self):
+ return len(self.passedEnums)
+
+ def getNumberOfFailed(self):
+ return len(self.failedEnums)
+
+ def totalCSV(self):
+ return tuplesListToCSV(map(lambda point : [self.fullPath] + point, self.points))
+
+ def totalText(self, distribution):
+ formattedLines = tuplesListToPrettyText(self.points, distribution)
+ return map(lambda formattedLine : self.fullPath + " " + formattedLine, formattedLines)
+
+ def summaryCSV(self):
+ line = [(self.getType(), self.enumPercentage)]
+ return tuplesListToCSV(line)
+
+ def summaryText(self, distribution):
+ line = [(self.getType(), self.enumPercentage)]
+ return tuplesListToPrettyText(line, distribution)
+
+ def getScore(self):
+ return (self.getType(), self.enumPercentage)
+
+class TypedefConformanceContainer():
+ def __init__(self, module):
+ self.path = module.getModulePath()
+ self.module = module
+ self.failedTypedefs = []
+ self.passedTypedefs = []
+
+ if (len(self.path) > 0):
+ self.fullPath = self.path + os.sep + module.getCSourceName()
+ else:
+ self.fullPath = module.getCSourceName()
+
+ self.computeConformance()
+
+ def computeConformance(self):
+ sourceFileName = self.module.getCSourceName()
+ headerFileName = self.module.getCHeaderName()
+
+ typedefs = getTypedefsFromFiles([(self.path, sourceFileName, True), (self.path, headerFileName, False)])
+
+ modulePrefix = self.module.getModulePrefix()
+ if (modulePrefix != None):
+ for (typedefName, staticTag) in typedefs:
+ isValid, reason = isValidTypedefName(modulePrefix, typedefName, staticTag)
+ if isValid:
+ self.addPassedTypedef(typedefName)
+ else:
+ self.addFailedTypedef(typedefName, reason)
+
+ @staticmethod
+ def getType():
+ return "typedef-names"
+
+ def addFailedTypedef(self, typedef, reason):
+ self.failedTypedefs.append((typedef, reason))
+
+ def getFailedTypedefs(self):
+ return self.failedTypedefs
+
+ def addPassedTypedef(self, typedef):
+ self.passedTypedefs.append(typedef)
+
+ def getPassedTypedefs(self):
+ return self.passedTypedefs
+
+ def analyzeConformance(self):
+ '''
+ Convert the raw pass/fail typedef results into a set of individual
+ data points (finegrain results) and overall percentage.
+ '''
+ self.points = []
+ self.typedefPercentage = 100.0
+ numPassed = len(self.passedTypedefs)
+ numFailed = len(self.failedTypedefs)
+ if (numPassed + numFailed > 0):
+ self.typedefPercentage = float(float(numPassed) / float(numFailed + numPassed)) * 100
+
+ for tName in self.passedTypedefs:
+ data = ["typedef-name", tName, 100.0]
+ self.points.append(data)
+
+ for (tName, reason) in self.failedTypedefs:
+ data = ["typedef-name", tName, reason, 0.0]
+ self.points.append(data)
+
+ def getNumberOfPassed(self):
+ return len(self.passedTypedefs)
+
+ def getNumberOfFailed(self):
+ return len(self.failedTypedefs)
+
+ def totalCSV(self):
+ return tuplesListToCSV(map(lambda point : [self.fullPath] + point, self.points))
+
+ def totalText(self, distribution):
+ formattedLines = tuplesListToPrettyText(self.points, distribution)
+ return map(lambda formattedLine : self.fullPath + " " + formattedLine, formattedLines)
+
+ def summaryCSV(self):
+ line = [(self.getType(), self.typedefPercentage)]
+ return tuplesListToCSV(line)
+
+ def summaryText(self, distribution):
+ line = [(self.getType(), self.typedefPercentage)]
+ return tuplesListToPrettyText(line, distribution)
+
+ def getScore(self):
+ return (self.getType(), self.typedefPercentage)
+
+class ModuleConformanceContainer():
+ '''
+ This conformance container stores a collection of individual type naming
+ conformance results within a particular module, and uses the information
+ contained therein to provide total finegrain and summarized
+ results of the conformance results for each type.
+ '''
+
+ def __init__(self, module):
+ self.conformanceContainers = []
+ self.path = module.getModulePath()
+ self.module = module
+ self.validName = False
+ self.process = True
+
+ if (len(self.path) > 0):
+ self.fullPath = self.path + os.sep + module.getCSourceName()
+ else:
+ self.fullPath = module.getCSourceName()
+
+ def setProcess(self, value):
+ self.process = value
+
+ def processModule(self):
+ return self.process
+
+ def addConformanceContainer(self, complianceContainer):
+ self.conformanceContainers.append(complianceContainer)
+
+ def analyzeConformance(self):
+ for container in self.conformanceContainers:
+ container.analyzeConformance()
+
+ def getNumberOfPassed(self):
+ tuples = []
+ for container in self.conformanceContainers:
+ tuples.append((container.getType(), container.getNumberOfPassed()))
+ return tuples # list of (topic, # of passed)
+
+ def getNumberOfFailed(self):
+ tuples = []
+ for container in self.conformanceContainers:
+ tuples.append((container.getType(), container.getNumberOfFailed()))
+ return tuples # list of (topic, # of failed)
+
+ def totalCSV(self):
+ csvTuples = []
+ for container in self.conformanceContainers:
+ csvTuples = csvTuples + container.totalCSV()
+ return csvTuples
+
+ def totalText(self, distribution):
+ textTuples = []
+ for container in self.conformanceContainers:
+ textTuples = textTuples + container.totalText(distribution)
+ return textTuples
+
+ def summaryCSV(self):
+ singleTuple = [self.fullPath]
+ for container in self.conformanceContainers:
+ csvGroup = container.summaryCSV()
+ singleTuple = singleTuple + [csvGroup[-1]]
+ return tuplesListToCSV([tuple(singleTuple)])
+
+ def summaryText(self, distribution, divider=' '):
+ formattedLine = self.fullPath
+ for container in self.conformanceContainers:
+ lineGroup = container.summaryText(distribution)[0].split(" ")
+ formattedLine = formattedLine + divider + lineGroup[-2] + ' ' + lineGroup[-1]
+ return [formattedLine]
+
+ def getScores(self):
+ scores = {}
+ for container in self.conformanceContainers:
+ scoreKey, scoreVal = container.getScore()
+ scores[scoreKey] = scoreVal
+ return scores
+
+class ModuleSetConformanceContainer():
+ '''
+ This conformance container stores a collection of individual module naming
+ conformance results, and uses the information contained therein to provide
+ summaries of conformance results.
+ '''
+
+ def __init__(self):
+ self.conformanceList = []
+
+ def addConformanceContainer(self, container):
+ self.conformanceList.append(container)
+
+ def analyzeConformance(self):
+ passed = {} # passed type-number bucket
+ failed = {} # failed type-number bucket
+
+ for container in self.conformanceList:
+ passedSet = container.getNumberOfPassed()
+ for (conformanceType, number) in passedSet:
+ if (conformanceType in passed):
+ passed[conformanceType] = passed[conformanceType] + number
+ else:
+ passed[conformanceType] = number
+ failedSet = container.getNumberOfFailed()
+ for (conformanceType, number) in failedSet:
+ if (conformanceType in failed):
+ failed[conformanceType] = failed[conformanceType] + number
+ else:
+ failed[conformanceType] = number
+
+ self.typeConformancePercentages = {}
+ for conformanceType in passed:
+ total = passed[conformanceType] + failed[conformanceType]
+ percentage = 100.0
+ if (total > 0):
+ percentage = (float(passed[conformanceType]) / float(total)) * 100.0
+ self.typeConformancePercentages[conformanceType] = percentage
+
+ def summaryCSV(self):
+ collatedTuple = ["average-scores"]
+ for conformanceType in self.typeConformancePercentages:
+ collatedTuple.append(conformanceType) # append type
+ collatedTuple.append(self.typeConformancePercentages[conformanceType]) # append percentage
+ return tuplesListToCSV([tuple(collatedTuple)])
+
+ def summaryText(self, distribution):
+ formattedLine = "average-scores"
+ for conformanceType in self.typeConformancePercentages:
+ prettyTypeText = tuplesListToPrettyText([(conformanceType, self.typeConformancePercentages[conformanceType])])[0]
+ formattedLine = formattedLine + " " + prettyTypeText
+ return [formattedLine]
+
+def computeModuleNameConformance(module):
+ '''
+ Compute the module name conformance. There is no container for this result
+ since it's a simple boolean.
+ '''
+ namespace = module.getNamespace()
+ moduleName = module.getModuleName()
+ typeName = module.getTypeName()
+
+ if (namespace != None and moduleName != None and typeName != None):
+ return isValidModuleName(namespace, typeName, moduleName)
+ else:
+ return False
+
+def computeModuleConformance(module):
+ '''
+ Compute the naming conformance results for an entire module,
+ which includes conformance results for all types contained therein.
+ '''
+ moduleContainer = ModuleConformanceContainer(module)
+
+ # Get the compliance results for functions, memorizing whether or not a main function was seen
+ functionContainer = FunctionConformanceContainer(module)
+ moduleContainer.addConformanceContainer(functionContainer)
+ moduleContainer.setProcess(not functionContainer.containsMainFunction())
+
+ # Now handle enums, typedefs, etc.
+ moduleContainer.addConformanceContainer(EnumConformanceContainer(module))
+ moduleContainer.addConformanceContainer(TypedefConformanceContainer(module))
+ moduleContainer.setValidName = computeModuleNameConformance(module)
+
+ # Now that we have the data, run over it to generate the results
+ moduleContainer.analyzeConformance()
+
+ return moduleContainer
+
+def getConformanceHeaders():
+ headers = [FunctionConformanceContainer.getType(), EnumConformanceContainer.getType(), TypedefConformanceContainer.getType()]
+ return headers
+
+def gradeAndPrint(targets, objectDirs, problemsOnly=False, printPrefix=""):
+ if len(targets) < 1:
+ print "No Files To Grade"
+ return
+
+ distribution = [99, 90]
+ maxFileNameLength = max(max(map(lambda target: len(target), targets)), len("File Name"))
+
+ moduleConformanceSet = ModuleSetConformanceContainer()
+ headers = getConformanceHeaders()
+ pformat = '{prefix}{:<{maxFileNameLength}}'
+ nformat = pformat
+ for header in headers:
+ nformat = nformat + '{:>15}'
+ print nformat.format('File Name', *headers, prefix=printPrefix, maxFileNameLength=maxFileNameLength)
+
+
+ for target in targets:
+ module = Module(target, objectDirs)
+ if module.isTestSourceName():
+ continue
+ fileNamePrefix = module.getModuleName()
+ path = module.getModulePath()
+ try:
+ moduleConformance = computeModuleConformance(module)
+ if not moduleConformance.processModule():
+ pass
+ else:
+ moduleConformanceSet.addConformanceContainer(moduleConformance)
+ scores = moduleConformance.getScores()
+ minScore = 100.0
+ for key in scores:
+ score = scores[key]
+ if score < minScore:
+ minScore = score
+ scores[key] = '%3.1f'%score
+ if problemsOnly and minScore == 100.0:
+ continue
+ printVals=[]
+ for hval in headers:
+ score = 'N/A'
+ if hval in scores:
+ score = scores[hval]
+ printVals.append(score)
+ line = nformat.format(target, *printVals, prefix=printPrefix, maxFileNameLength=maxFileNameLength)
+ LongBow.scorePrinter(distribution, minScore, line)
+ except NoObjectFileException as e:
+ eformat = pformat + "Could Not Grade: No .o file found for file"
+ line = eformat.format(target, prefix=printPrefix, maxFileNameLength=maxFileNameLength, msg=e)
+ print LongBow.buildRed(line)
+ pass
+ except Exception as e:
+ eformat = pformat + "Could Not Grade: {msg}"
+ line = eformat.format(target, prefix=printPrefix, maxFileNameLength=maxFileNameLength, msg=e)
+ print LongBow.buildRed(line)
+ pass
+ moduleConformanceSet.analyzeConformance()
+
+
+def commandLineMain(args, targets, objectDir):
+ distribution = eval(args.distribution)
+ moduleConformanceSet = ModuleSetConformanceContainer()
+
+ summary = args.summary
+ average = args.average
+ finegrain = args.finegrain
+ if not (summary or average or finegrain):
+ summary = True
+
+ objectDirs = [objectDir]
+ for i in range(len(targets)):
+ module = Module(targets[i], objectDirs)
+ prefix = module.getModuleName()
+ path = module.getModulePath()
+
+ tb = None
+ try:
+ moduleConformance = computeModuleConformance(module)
+ if not moduleConformance.processModule():
+ print >> sys.stderr, "Skipping module " + str(prefix) + ": contains a `main` function"
+ else:
+ moduleConformanceSet.addConformanceContainer(moduleConformance)
+
+ if summary:
+ if args.output == "text":
+ writeListToStream(moduleConformance.summaryText(distribution), sys.stdout)
+ else:
+ writeListToStream(moduleConformance.summaryCSV(), sys.stdout)
+
+ if finegrain:
+ if args.output == "text":
+ writeListToStream(moduleConformance.totalText(distribution), sys.stdout)
+ else:
+ writeListToStream(moduleConformance.totalCSV(), sys.stdout)
+
+ except Exception as e:
+ tb = traceback.format_exc()
+ print >> sys.stderr, "Error: can't analyze conformance of " + os.path.join(path, prefix) + ": " + str(e)
+ finally:
+ if tb != None and args.trace:
+ print tb
+ pass
+
+ moduleConformanceSet.analyzeConformance()
+ if average:
+ if args.output == "text":
+ writeListToStream(moduleConformanceSet.summaryText(distribution), sys.stdout)
+ else:
+ writeListToStream(moduleConformanceSet.summaryCSV(), sys.stdout)
diff --git a/longbow/src/python/site-packages/longbow/StyleReport.py b/longbow/src/python/site-packages/longbow/StyleReport.py
new file mode 100755
index 00000000..7e8d72e0
--- /dev/null
+++ b/longbow/src/python/site-packages/longbow/StyleReport.py
@@ -0,0 +1,382 @@
+#! /usr/bin/env python
+# Copyright (c) 2017 Cisco and/or its affiliates.
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at:
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+#
+
+import sys
+import os
+import tempfile
+import subprocess
+import difflib
+import csv
+import argparse
+
+import LongBow
+import ANSITerm
+import FileUtil
+import pprint
+
+def getExemplar(fileName, command, config):
+ """Create the exemplar formatted file into memory as a string"""
+
+ with open(fileName) as inputFile:
+ result = subprocess.check_output([command, "-q", "-c", config], stdin=inputFile)
+ return result;
+
+
+def diff(exemplar, fileName):
+ d = difflib.Differ()
+ differ = d.compare(exemplar.splitlines(), fileName.splitlines())
+ return differ
+
+
+class Ratchet:
+ def __init__(self):
+ self.currentValue = 0
+ self.signal = 0
+
+ def value(self):
+ return self.currentValue
+
+ def toggle(self, signal):
+ if self.signal == "-":
+ if signal == "-":
+ self.currentValue = self.currentValue + 1
+ elif signal == "?":
+ self.currentValue = self.currentValue + 1
+ self.signal = 0
+ else:
+ self.currentValue = self.currentValue + 1
+ self.signal = 0
+ pass
+ elif self.signal == "+":
+ if signal == "-":
+ self.currentValue = self.currentValue + 1
+ elif signal == "?":
+ self.currentValue = self.currentValue + 1
+ self.signal = 0
+ else:
+ self.currentValue = self.currentValue + 1
+ self.signal = 0
+ pass
+ else:
+ self.signal = signal;
+
+ return self.currentValue
+
+
+def computeNonCompliantLines(differ):
+ lines = 0
+ changes = Ratchet()
+
+ for l in differ:
+ if l.startswith('-'):
+ changes.toggle(l[0])
+ lines = lines - 1
+ elif l.startswith('+'):
+ changes.toggle(l[0])
+ lines = lines + 1
+ elif l.startswith('?'):
+ pass
+ elif l.startswith(' '):
+ lines = lines +1
+ else:
+ print "What is this:", l
+
+ return changes.value()
+
+
+def reportWhy(differ):
+ print '\n'.join(diff)
+ return
+
+
+class SyntaxCompliance:
+ def __init__(self, fileName, exemplarCommand, exemplarConfig):
+ self.fileName = fileName
+ self.nonCompliantLines = 0
+ self.score = 0
+ self.exemplarCommand = exemplarCommand
+ self.exemplarConfig = exemplarConfig
+ try:
+ self.fileData = FileUtil.readFileString(self.fileName)
+ self.totalLines = len(self.fileData.splitlines())
+ except IOError, e:
+ print >> sys.stderr, e
+ sys.exit(1)
+ pass
+
+ def check(self):
+ self.exemplarData = getExemplar(self.fileName, self.exemplarCommand, self.exemplarConfig)
+ differ = diff(self.fileData, self.exemplarData)
+
+ self.nonCompliantLines = computeNonCompliantLines(differ)
+
+ return self
+
+ def report(self):
+ result = { "fileName" : self.fileName,
+ "label": "style",
+ "score": self.getScore(),
+ "totalLines" : self.getTotalLines(),
+ "nonCompliantLines" : self.getNonCompliantLines()
+ }
+ return result
+
+ def getFileName(self):
+ return self.fileName
+
+ def getExemplarCommand(self):
+ return self.exemplarCommand;
+
+ def getExemplarConfig(self):
+ return self.exemplarConfig;
+
+ def getScore(self):
+ result = 0
+ try:
+ result = int(100 * (1.0 - (float(self.getNonCompliantLines()) / float(self.getTotalLines()))))
+ except ZeroDivisionError:
+ pass
+ return result
+
+ def getTotalLines(self):
+ return self.totalLines
+
+ def getNonCompliantLines(self):
+ return self.nonCompliantLines
+
+ def explain(self):
+ self.exemplarData = getExemplar(self.fileName, self.exemplarCommand, self.exemplarConfig)
+ differ = diff(self.fileData, self.exemplarData)
+
+ ansiTerm = ANSITerm.ANSITerm()
+
+ for l in differ:
+ if l[0] == '-':
+ ansiTerm.printColorized("red", l)
+ elif l[0] == '+':
+ ansiTerm.printColorized("green", l)
+ elif l[0] == '?':
+ ansiTerm.printColorized("yellow", l[0:len(l)-1])
+ else:
+ print l
+ pass
+ return
+
+
+def csvScore(distribution, report):
+ string = "style,%s,%d,%d,%.2f" % (report["fileName"], report["totalLines"], report["nonCompliantLines"], report["score"])
+ LongBow.scorePrinter(distribution, report["score"], string)
+ return
+
+
+def csvAverage(distribution, complianceList):
+ scores = map(lambda target: target.getScore(), complianceList)
+ sum = reduce(lambda sum, score : sum + score, scores)
+ value = float(sum) / float(len(complianceList))
+ LongBow.scorePrinter(distribution, value, "%.2f" % (value))
+ return
+
+
+def csvTotal(distribution, complianceList):
+ totalLines = reduce(lambda sum, x: sum + x, map(lambda element : element.getTotalLines(), complianceList))
+ totalNonCompliantLines = reduce(lambda sum, x: sum + x, map(lambda element : element.getNonCompliantLines(), complianceList))
+ value = 100.0 - (100.0 * float(totalNonCompliantLines) / float(totalLines))
+ LongBow.scorePrinter(distribution, value, "%.2f" % (value))
+ return
+
+
+def csvSummary(distribution, complianceList):
+ map(lambda target: csvScore(distribution, target.report()), complianceList)
+ return
+
+
+def textScore(distribution, report, maxFileNameLength, prefix=""):
+ '''
+
+ '''
+ format = "%s%-*s %6d %6d %6.2f"
+ string = format % (prefix, maxFileNameLength, report["fileName"], report["totalLines"], report["nonCompliantLines"], report["score"])
+ LongBow.scorePrinter(distribution, report["score"], string)
+ return
+
+
+def textAverage(distribution, complianceList):
+ scores = map(lambda target: target.getScore(), complianceList)
+ sum = reduce(lambda sum, score : sum + score, scores)
+ value = float(sum) / float(len(complianceList))
+ LongBow.scorePrinter(distribution, value, "%.2f" % (value))
+ return
+
+
+def textTotal(distribution, complianceList):
+ totalLines = reduce(lambda sum, x: sum + x, map(lambda element : element.getTotalLines(), complianceList))
+ totalNonCompliantLines = reduce(lambda sum, x: sum + x, map(lambda element : element.getNonCompliantLines(), complianceList))
+ value = 100.0 - (100.0 * float(totalNonCompliantLines) / float(totalLines))
+ LongBow.scorePrinter(distribution, value, "%.2f" % (value))
+ return
+
+
+def textSummary(distribution, complianceList, prefix=""):
+ if len(complianceList) > 0:
+ maxFileNameLength = max(max(map(lambda target: len(target.getFileName()), complianceList)), len("File Name"))
+
+ print "%s%-*s %6s %6s %6s" % (prefix, maxFileNameLength, "File Name", "Lines", "Errors", "Score")
+ map(lambda target: textScore(distribution, target.report(), maxFileNameLength, prefix), complianceList)
+
+ return
+
+
+def textVisual(complianceList):
+ map(lambda target: target.explain(), complianceList)
+ return
+
+
+def openDiff(sourceFile, exemplarCommand, exemplarConfig):
+ exemplar = getExemplar(sourceFile, exemplarCommand, exemplarConfig);
+ temporaryExemplarFile = tempfile.NamedTemporaryFile(suffix=".c", delete=False)
+ try:
+ with open(temporaryExemplarFile.name, "w") as exemplarOutput:
+ exemplarOutput.write(exemplar)
+
+ subprocess.check_output(["opendiff", sourceFile, temporaryExemplarFile.name, "-merge", sourceFile])
+ finally:
+ pass
+
+ return
+
+
+def displaySummary(args, complianceList):
+ distribution = eval(args.distribution)
+
+ if args.output == "text":
+ textSummary(distribution, complianceList)
+ elif args.output == "gui":
+ textSummary(distribution, complianceList)
+ else:
+ csvSummary(distribution, complianceList)
+ return
+
+
+def displayAverage(args, complianceList):
+
+ distribution = eval(args.distribution)
+
+ if args.output == "text":
+ textAverage(distribution, complianceList)
+ elif args.output == "gui":
+ textAverage(distribution, complianceList)
+ else:
+ csvAverage(distribution, complianceList)
+ return
+
+
+def displayTotal(args, complianceList):
+ distribution = eval(args.distribution)
+
+ if args.output == "text":
+ textTotal(distribution, complianceList)
+ elif args.output == "gui":
+ textTotal(distribution, complianceList)
+ else:
+ csvTotal(distribution, complianceList)
+ return
+
+
+def guiVisual(args, complianceList):
+ map(lambda target: openDiff(target.getFileName(), target.getExemplarCommand(), target.getExemplarConfig()), complianceList)
+ return
+
+
+def displayVisual(args, complianceList):
+ if args.output == "text":
+ textVisual(complianceList)
+ elif args.output == "gui":
+ guiVisual(args, complianceList)
+ else:
+ print >> sys.stderr, "Unsupported output format '%s'. Expected 'text' or 'gui'." % (args.output)
+ sys.exit(1)
+ return
+
+
+def sortComplianceList(args, complianceList):
+
+ sorter = {
+ "name" : { "function" : lambda k: k.getFileName(), "reverse" : False },
+ "descending-name" : { "function" : lambda k: k.getFileName(), "reverse" : True },
+ "score" : { "function" : lambda k: k.getScore(), "reverse" : False },
+ "descending-score" : { "function" : lambda k: k.getScore(), "reverse" : True },
+ "size" : { "function" : lambda k: k.getTotalLines(), "reverse" : False },
+ "descending-size" : { "function" : lambda k: k.getTotalLines(), "reverse" : True },
+ }
+
+ if args.key == "help":
+ print >> sys.stderr, "Supported sort keys:"
+ map(lambda k: sys.stderr.write("'" + k + "' "), sorted(sorter))
+ print
+ sys.exit(1)
+
+ if args.key in sorter:
+ complianceList = sorted(complianceList, key=sorter[args.key]["function"], reverse=sorter[args.key]["reverse"])
+ else:
+ print >> sys.stderr, "Unsupported sort key '%s'. Type '--key help'" % (args.key)
+ sys.exit(1)
+
+ return complianceList
+
+
+def exclude(args, complianceList):
+ excluded = map(lambda token : token.strip(), args.exclude.split(","))
+ complianceList = filter(lambda entry: LongBow.score(eval(args.distribution), entry.getScore()) not in excluded, complianceList)
+
+ return complianceList
+
+
+def gradeAndPrint(targets, exemplarCommand, exemplarConfig, problemsOnly=False, prefix=""):
+ complianceList = []
+ problemList = []
+ for target in targets:
+ try:
+ complianceList.append(SyntaxCompliance(target, exemplarCommand, exemplarConfig).check())
+ except:
+ problemList.append(target)
+ pass
+ complianceList = sorted(complianceList, key=lambda k: k.getFileName())
+ if problemsOnly:
+ complianceList = filter(lambda entry: entry.getScore() < 100, complianceList)
+ distribution=[99,90]
+ textSummary(distribution, complianceList, prefix)
+
+ for target in problemList:
+ print LongBow.buildRed("%s%s could not be evaluated" % (prefix, target))
+
+
+def commandLineMain(args, targets, exemplarCommand, exemplarConfig):
+ complianceList = map(lambda target: SyntaxCompliance(target, exemplarCommand, exemplarConfig).check(), targets)
+
+ complianceList = sortComplianceList(args, complianceList)
+
+ complianceList = exclude(args, complianceList)
+
+ if args.summary:
+ displaySummary(args, complianceList)
+ elif args.average:
+ displayAverage(args, complianceList)
+ elif args.total:
+ displayTotal(args, complianceList)
+ elif args.visual:
+ displayVisual(args, complianceList)
+ return
diff --git a/longbow/src/python/site-packages/longbow/SymbolTable.py b/longbow/src/python/site-packages/longbow/SymbolTable.py
new file mode 100755
index 00000000..20a909d7
--- /dev/null
+++ b/longbow/src/python/site-packages/longbow/SymbolTable.py
@@ -0,0 +1,87 @@
+#! /usr/bin/env python
+# Copyright (c) 2017 Cisco and/or its affiliates.
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at:
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+#
+import os
+import subprocess
+import re
+import sys
+import pprint
+
+def parseLocation(location):
+ token = location.split("[")
+ objectFileName = location
+
+ if len(token) > 1:
+ libraryName = token[0]
+ objectFileName = token[1].split("]")[0]
+ else:
+ libraryName = None
+
+ objectFileName = objectFileName.split(":")[0]
+ return (libraryName, objectFileName)
+
+def parseDarwinOutput(lines, accumulator = { }):
+
+ for line in lines:
+ token = line.split(" ")
+ fullName = token[0]
+
+ libraryName, objectFileName = parseLocation(token[0])
+ name = token[1]
+ type = token[2]
+ if fullName in accumulator:
+ if type == "U":
+ accumulator[fullName]["undefined"].append({ "name" : name })
+ elif type == "T":
+ accumulator[fullName]["defined"].append({ "name" : name })
+ elif type == "D":
+ accumulator[fullName]["globalData"].append({ "name" : name })
+ else:
+ accumulator[fullName] = { "fullName" : fullName, "libraryName" : libraryName, "objectFileName" : objectFileName, "defined" : [], "undefined" : [], "globalData" : [] }
+
+ return accumulator
+
+def getDarwinSymbolTable(objectFileName, accumulator = { }):
+ command = [ "/usr/bin/nm", "-PAog", objectFileName ]
+
+ output = subprocess.check_output(command)
+ lines = output.splitlines()
+ return parseDarwinOutput(lines, accumulator)
+
+
+def getSymbolTable(objectFileName, accumulator = { }):
+ '''
+ {
+ fullName : { "defined" : list of dictionaries,
+ "undefined": list of dictionaries,
+ "globalData" : list of dictionaries,
+ "libraryName" : string
+ "objectFileName : string
+ },
+ }
+ '''
+ return getDarwinSymbolTable(objectFileName, accumulator)
+
+
+if __name__ == '__main__':
+
+ table = dict()
+ for f in sys.argv:
+ table = getSymbolTable(f, table)
+
+ pp = pprint.PrettyPrinter(indent=4, width=132)
+
+ pp.pprint(table)
diff --git a/longbow/src/python/site-packages/longbow/VocabularyReport.py b/longbow/src/python/site-packages/longbow/VocabularyReport.py
new file mode 100755
index 00000000..5609db11
--- /dev/null
+++ b/longbow/src/python/site-packages/longbow/VocabularyReport.py
@@ -0,0 +1,162 @@
+#! /usr/bin/env python
+# Copyright (c) 2017 Cisco and/or its affiliates.
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at:
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+#
+
+import sys
+import itertools
+
+import LongBow
+
+def computeVocabularyScore(tokenCount):
+ return 100.0
+
+
+def csvFunctionResult(file, function):
+ score = computeVocabularyScore(file.token_count)
+ string = "vocabulary,%s,%s,%d,%d,%.2f" % (file.filename, function.name, function.start_line, function.token_count, score)
+
+ LongBow.scorePrinter([90, 80], score, string)
+ return function.token_count
+
+
+def csvFileVocabulary(file):
+ score = computeVocabularyScore(file.token_count)
+ string = "vocabulary,%s,,,%.2f,%.2f" % (file.filename, file.average_token, score)
+ LongBow.scorePrinter([90, 80], score, string)
+ return
+
+
+def csvFunction(fileInformationList):
+ for fileInformation in fileInformationList:
+ complexities = map(lambda function: csvFunctionResult(fileInformation, function), fileInformation)
+ return
+
+
+def csvSummary(fileInformationList):
+ map(lambda file: csvFileVocabulary(file), fileInformationList)
+ return
+
+
+def textFunctionResult(file, function, maxFileNameLength, maxFunctionNameLength):
+ score = computeVocabularyScore(function.token_count)
+ format = "%-" + str(maxFileNameLength) + "s %-" + str(maxFunctionNameLength) + "s %3d %3d %6.2f"
+ string = format % (file.filename, function.name, function.start_line, function.token_count, score)
+
+ LongBow.scorePrinter([90, 80], score, string)
+ return function.cyclomatic_complexity
+
+
+def textFileVocabulary(file, maxFileNameLength, printFormat=""):
+ score = computeVocabularyScore(file.average_CCN)
+ if printFormat == "":
+ printFormat = "%-" + str(maxFileNameLength) + "s %6.2f %6.2f"
+ string = printFormat % (file.filename, file.average_token, score)
+ LongBow.scorePrinter([90, 80], score, string)
+ return
+
+
+def computeMaxFileNameLength(fileInformationList):
+ result = 0
+ for fileInformation in fileInformationList:
+ if len(fileInformation.filename) > result:
+ result = len(fileInformation.filename)
+ return result
+
+
+def computeMaxFunctionNameLength(fileInformationList):
+ result = 0
+ for fileInformation in fileInformationList:
+ if len(fileInformation.filename) > result:
+ result = len(fileInformation.filename)
+ return result
+
+
+def textFunction(fileInformationList):
+ maxFileNameLength = max(map(lambda fileInformation: len(fileInformation.filename), fileInformationList))
+ maxFunctionNameLength = max(map(lambda fileInformation: max(map(lambda function: len(function.name), fileInformation)), fileInformationList))
+
+ for fileInformation in fileInformationList:
+ complexities = map(lambda function: textFunctionResult(fileInformation, function, maxFileNameLength, maxFunctionNameLength), fileInformation)
+ return
+
+
+def textSummary(fileInformationList, prefix=""):
+ if len(fileInformationList) < 1:
+ print "%sNo Files To Grade" % prefix
+ return
+ maxFileNameLength = max(map(lambda fileInformation: len(fileInformation.filename), fileInformationList))
+ printFormat = prefix + "%-" + str(maxFileNameLength) + "s %10s %6s"
+ print printFormat % ("File Path", "Ave Token", "Score")
+ printFormat = prefix + "%-" + str(maxFileNameLength) + "s %10.2f %6.2f"
+ map(lambda file: textFileVocabulary(file, maxFileNameLength, printFormat), fileInformationList)
+ return
+
+
+def computeAverage(fileInformationList):
+ vocabulary = map(lambda fileInformation : fileInformation.average_token, fileInformationList)
+ sum = reduce(lambda sum, x: sum + x, vocabulary)
+ return float(sum) / float(len(vocabulary))
+
+
+def gradeAndPrint(fileList, hfcca, problemsOnly=False, prefix=""):
+ options, arguments = hfcca.createHfccaCommandLineParser().parse_args(args=["foo"])
+ result = hfcca.analyze(fileList, options)
+
+ # Convert from that iterator to a simple list...
+ fileInformationList = map(lambda x : x, result)
+ if problemsOnly:
+ fileInformationList = filter(lambda item: computeVocabularyScore(item.average_CCN) < 100, fileInformationList)
+
+ textSummary(fileInformationList, prefix)
+
+def commandLineMain(args, hfcca):
+ targets = []
+
+ if args.stdin:
+ for line in sys.stdin:
+ t = line.strip()
+ if (len(t) > 0):
+ targets.append(t)
+ else:
+ targets = args.files
+
+ if (len(targets) == 0):
+ print >> sys.stderr, "Error: target list cannot be empty"
+
+ # If nothing was specified, print the summary as a default
+ if args.summary == False and args.function == False and args.average == False:
+ args.summary = True
+
+ options, arguments = hfcca.createHfccaCommandLineParser().parse_args(args=["VocabularyReport"])
+ result = hfcca.analyze(targets, options)
+
+ # Convert from that iterator to a simple list...
+ fileInformationList = map(lambda x : x, result)
+
+ if args.function:
+ if args.output == "text":
+ textFunction(fileInformationList)
+ else:
+ csvFunction(fileInformationList)
+
+ if args.summary:
+ if args.output == "text":
+ textSummary(fileInformationList)
+ else:
+ csvSummary(fileInformationList)
+
+ if args.average:
+ print "%.2f" % computeAverage(fileInformationList)