Skip to content

Commit

Permalink
Merge pull request diffblue#342 from diffblue/add_java_app_stats_script
Browse files Browse the repository at this point in the history
Added script for computation of statistics of a Java application.
  • Loading branch information
marek-trtik authored Apr 9, 2018
2 parents a4fcc99 + 3830307 commit 6985dcb
Showing 1 changed file with 77 additions and 0 deletions.
77 changes: 77 additions & 0 deletions scripts/jstats.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import argparse
import os
import json


def _parse_cmd_line():
parser = argparse.ArgumentParser(
description="The script computes a statistics of java files under the passed directory. "
"There is also added count of 'web.xml' files to the statistics, in case "
"the directory hold Java web application(s).")
parser.add_argument("-V", "--version", action="store_true",
help="Prints a version string.")
parser.add_argument("root_dir", type=str,
help="A directory under which Java files (and web.xml files) will be considered for the statistics.")
parser.add_argument("-o", "--output", type=str, default="jstats_result.json",
help="The file into which the statistics will be written to. The file does not have to exist. "
"The data are saved in JSON format. If the option is not specified, then the results will "
"be written into file 'jstats_result.json' under the current working directory.")
return parser.parse_args()


def count_lines_in_file(file_pathname):
with open(file_pathname, "r") as ifile:
content = [line.strip() for line in ifile.readlines() if len(line.strip()) > 0]
return len(content)


def compute_statistics_of_java_files_under_directory(search_root_dir):
java_files = [os.path.join(root, filename)
for root, dir_name, file_names in os.walk(search_root_dir)
for filename in file_names
if os.path.splitext(filename)[1].lower() == ".java"]
file_lines = map(count_lines_in_file, java_files)
if len(java_files) == 0:
num_lines_total = 0
num_lines_per_file_min = 0
num_lines_per_file_max = 0
average_num_lines = 0
else:
num_lines_total = sum(file_lines)
num_lines_per_file_min = min(file_lines)
num_lines_per_file_max = max(file_lines)
average_num_lines = num_lines_total / len(java_files)
statistics = dict()
statistics["count_of_java_files"] = len(java_files)
statistics["minimal_number_of_lines_in_java_files"] = num_lines_per_file_min
statistics["maximal_number_of_lines_in_java_files"] = num_lines_per_file_max
statistics["average_number_of_lines_in_java_files"] = average_num_lines
statistics["total_number_of_lines_in_java_files"] = num_lines_total
return statistics


def _main():
cmdline = _parse_cmd_line()

if cmdline.version:
print("v.1.0")
return 0

cmdline.root_dir = os.path.abspath(cmdline.root_dir)
if not os.path.isdir(cmdline.root_dir):
print("ERROR: cannot find/access the passed directory '" + cmdline.root_dir + "'.")
return 1
if os.path.isdir(cmdline.output):
print("ERROR: cannot find/access the output file '" + cmdline.output + "'.")
return 1

stats = compute_statistics_of_java_files_under_directory(cmdline.root_dir)
stats["num_web.xml_files"] = len([0 for _, _, file_names in os.walk(cmdline.root_dir) for filename in file_names if filename == "web.xml"])
with open(cmdline.output, "w") as ofile:
ofile.write(json.dumps(stats, sort_keys=True, indent=4))
return 0


if __name__ == "__main__":
exit(_main())

0 comments on commit 6985dcb

Please sign in to comment.