Skip to content

Instantly share code, notes, and snippets.

@hamiltont
Created February 25, 2015 00:02
Show Gist options
  • Save hamiltont/6cfd16fe30bf47389089 to your computer and use it in GitHub Desktop.
Save hamiltont/6cfd16fe30bf47389089 to your computer and use it in GitHub Desktop.
def __count_sloc(self):
frameworks = gather_frameworks(include=self.test,
exclude=self.exclude, benchmarker=self)
jsonResult = {}
for framework, testlist in frameworks.iteritems():
if not os.path.exists(os.path.join(testlist[0].directory, "source_code")):
logging.warn("Cannot count lines of code for %s - no 'source_code' file", framework)
continue
# Unfortunately the source_code files use lines like
# ./cpoll_cppsp/www/fortune_old instead of
# ./www/fortune_old
# so we have to back our working dir up one level
wd = os.path.dirname(testlist[0].directory)
try:
command = "cloc "
if os.path.exists(os.path.join(testlist[0].directory, "cloc_defs.txt")):
command += "--conf-file %s" % os.path.join(testlist[0].directory, "cloc_defs.txt")
logging.info("Using custom cloc definitions for %s", framework")
command += "--list-file=%s --yaml" % os.path.join(testlist[0].directory, "source_code")
# Find the last instance of the word 'code' in the yaml output. This should
# be the line count for the sum of all listed files or just the line count
# for the last file in the case where there's only one file listed.
command = command + "| grep code | tail -1 | cut -d: -f 2"
logging.debug("Running \"%s\" (cwd=%s)", command, wd)
lineCount = subprocess.check_output(command, cwd=wd, shell=True)
jsonResult[framework] = int(lineCount)
except subprocess.CalledProcessError:
continue
except ValueError as ve:
logging.warn("Unable to get linecount for %s due to error '%s'", framework, ve)
self.results['rawData']['slocCounts'] = jsonResult
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment