-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetric_tddchecker.py
executable file
·67 lines (61 loc) · 2.98 KB
/
metric_tddchecker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import subprocess
import json
import os
import re
import subprocess
import sys
import gc
#Hope that setup is ok
def compute_tdd_metric(repo):
os.chdir(os.path.expanduser("~/repos"))
matchObj = re.match(r'.*\/(.*)', repo)
short_name = matchObj.group(1)
os.chdir("./{0}/{1}".format(short_name, short_name))
os.system("git fetch --all")
os.system("git reset --hard origin/master")
relevant_commits = subprocess.check_output(["git", "log", '--pretty=format:"%H"', "--since='2017-08-01T00:00:00-07:00'"]).decode("utf-8")
relevant_commits_list = relevant_commits.split("\n")
#print(relevant_commits_list)
retVal = {}
for j in relevant_commits_list[::-1]:
rollback = j.replace('"', '').replace("'", "")
os.system("git reset --hard {0}".format(rollback))
#parent_results = subprocess.check_output(["git", "log", "--pretty=%P", "-n", "1"]).decode("utf-8")
parent_results = subprocess.check_output("git cat-file -p " + rollback + " | awk 'NR > 1 {if(/^parent/){print $2; next}{exit}}'", shell=True).decode("utf-8")
parent_commits_list = parent_results.split("\n")
commit_timestamp = subprocess.check_output(["git", "log", "--pretty=%at", "-1"]).decode("utf-8")
#print(commit_timestamp)
retVal[rollback] = [int(commit_timestamp)]
for i in parent_commits_list:
inside_dict = {}
comp_branch = i.replace("\n", "")
if comp_branch == "":
continue
if not os.path.exists("../commits/{0}.xml".format(comp_branch)):
inside_dict.update({"commit_before": comp_branch, "total": -1, "missing": -1, "error": "commit file does not exist"})
else:
gc.collect()
diff_results = subprocess.check_output(["diff-cover", "../commits/{0}.xml".format(rollback), "--compare-branch={0}".format(comp_branch)]).decode("utf-8")
total_match = re.search(r'Total:(?:\s*)(\d+)', diff_results)
if total_match:
total = int(str(total_match.group(1)))
else:
total = 0
missing_match = re.search(r'Missing:(?:\s*)(\d+)', diff_results)
if missing_match:
missing = int(str(missing_match.group(1)))
else:
missing = 0
inside_dict.update({"commit_before": comp_branch, "total": total, "missing": missing})
retVal[rollback].append(inside_dict)
os.chdir("..")
if (not(os.path.isdir("tddresults"))):
os.system("mkdir tddresults")
os.system("rm tddresults/result.json")
with open("tddresults/result.json", "w") as file:
file.write(json.dumps(retVal))
# compute_tdd_metric("adnanhemani/slc-app")
if sys.argv[1]:
compute_tdd_metric(sys.argv[1])
else:
print("You forgot to include which repository to run this script on. You should call this function like this: python metric_tddchecker.py <insert repo long-name here>")