Skip to content

Commit 6985dcb

Browse files
authored
Merge pull request diffblue#342 from diffblue/add_java_app_stats_script
Added script for computation of statistics of a Java application.
2 parents a4fcc99 + 3830307 commit 6985dcb

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

scripts/jstats.py

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import argparse
2+
import os
3+
import json
4+
5+
6+
def _parse_cmd_line():
7+
parser = argparse.ArgumentParser(
8+
description="The script computes a statistics of java files under the passed directory. "
9+
"There is also added count of 'web.xml' files to the statistics, in case "
10+
"the directory hold Java web application(s).")
11+
parser.add_argument("-V", "--version", action="store_true",
12+
help="Prints a version string.")
13+
parser.add_argument("root_dir", type=str,
14+
help="A directory under which Java files (and web.xml files) will be considered for the statistics.")
15+
parser.add_argument("-o", "--output", type=str, default="jstats_result.json",
16+
help="The file into which the statistics will be written to. The file does not have to exist. "
17+
"The data are saved in JSON format. If the option is not specified, then the results will "
18+
"be written into file 'jstats_result.json' under the current working directory.")
19+
return parser.parse_args()
20+
21+
22+
def count_lines_in_file(file_pathname):
23+
with open(file_pathname, "r") as ifile:
24+
content = [line.strip() for line in ifile.readlines() if len(line.strip()) > 0]
25+
return len(content)
26+
27+
28+
def compute_statistics_of_java_files_under_directory(search_root_dir):
29+
java_files = [os.path.join(root, filename)
30+
for root, dir_name, file_names in os.walk(search_root_dir)
31+
for filename in file_names
32+
if os.path.splitext(filename)[1].lower() == ".java"]
33+
file_lines = map(count_lines_in_file, java_files)
34+
if len(java_files) == 0:
35+
num_lines_total = 0
36+
num_lines_per_file_min = 0
37+
num_lines_per_file_max = 0
38+
average_num_lines = 0
39+
else:
40+
num_lines_total = sum(file_lines)
41+
num_lines_per_file_min = min(file_lines)
42+
num_lines_per_file_max = max(file_lines)
43+
average_num_lines = num_lines_total / len(java_files)
44+
statistics = dict()
45+
statistics["count_of_java_files"] = len(java_files)
46+
statistics["minimal_number_of_lines_in_java_files"] = num_lines_per_file_min
47+
statistics["maximal_number_of_lines_in_java_files"] = num_lines_per_file_max
48+
statistics["average_number_of_lines_in_java_files"] = average_num_lines
49+
statistics["total_number_of_lines_in_java_files"] = num_lines_total
50+
return statistics
51+
52+
53+
def _main():
54+
cmdline = _parse_cmd_line()
55+
56+
if cmdline.version:
57+
print("v.1.0")
58+
return 0
59+
60+
cmdline.root_dir = os.path.abspath(cmdline.root_dir)
61+
if not os.path.isdir(cmdline.root_dir):
62+
print("ERROR: cannot find/access the passed directory '" + cmdline.root_dir + "'.")
63+
return 1
64+
if os.path.isdir(cmdline.output):
65+
print("ERROR: cannot find/access the output file '" + cmdline.output + "'.")
66+
return 1
67+
68+
stats = compute_statistics_of_java_files_under_directory(cmdline.root_dir)
69+
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"])
70+
with open(cmdline.output, "w") as ofile:
71+
ofile.write(json.dumps(stats, sort_keys=True, indent=4))
72+
return 0
73+
74+
75+
if __name__ == "__main__":
76+
exit(_main())
77+

0 commit comments

Comments
 (0)