in scripts/commit_classifier.py [0:0]
def classify_methods(self, commit):
# Get commit hash from 4 months before the analysis time.
# The method-level analyzer needs 4 months of history.
stop_hash = None
four_months_ago = datetime.utcnow() - relativedelta(months=4)
for commit in repository.get_commits():
if dateutil.parser.parse(commit["pushdate"]) >= four_months_ago:
stop_hash = tuple(
vcs_map.mercurial_to_git(self.git_repo_dir, [commit["node"]])
)[0]
break
assert stop_hash is not None
p = subprocess.run(
[
"git",
"rev-list",
"-n",
"1",
"HEAD",
],
check=True,
capture_output=True,
cwd=self.git_repo_dir,
)
start_hash = p.stdout.decode().strip()
# Run the method-level analyzer.
subprocess.run(
[
"python3",
"tester.py",
"--repo",
self.git_repo_dir,
"--start",
start_hash,
"--stop",
stop_hash,
"--output",
os.path.abspath("method_level.csv"),
],
cwd=self.method_defect_predictor_dir,
)
method_level_results = []
try:
with open("method_level.csv", "r") as f:
reader = csv.DictReader(f)
for item in reader:
item["past_bugs"] = []
method_level_results.append(item)
except FileNotFoundError:
# No methods were classified.
pass
for method_level_result in method_level_results:
method_level_result_path = method_level_result["file_name"]
if method_level_result_path not in self.past_bugs_by_function:
continue
for path, functions in commit["functions"].items():
if method_level_result_path != path:
continue
for function in functions:
function_name = function["name"]
if function_name not in self.past_bugs_by_function[path]:
continue
if method_level_result["method_name"].endswith(function_name):
method_level_result["past_bugs"] = [
"Bug {} - {}".format(bug["id"], bug["summary"])
for bug in self.past_bugs_by_function[path][function_name][
-3:
]
]
with open("method_level.json", "w") as f:
json.dump(method_level_results, f)