-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.py
352 lines (287 loc) · 13.1 KB
/
parse.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
"""Contains a class to parse metrics for each commit in a git repository."""
from enum import Enum
import json
import os
from time import perf_counter
import re
import queue
import git
import pandas as pd
from pydriller import Repository, Commit
import radon
import radon.complexity
from radon.cli import Config
from radon.cli.harvest import CCHarvester, RawHarvester, HCHarvester, MIHarvester
from logger import get_logger
DATA_DIR = "data"
logger = get_logger()
class HarvesterOutcome(Enum):
"""Errors that can occur during metrics harvesting."""
SUCCESS = 0
PYTHON_VERSION_2 = 1
INVALID_CODE = 2
class MetricParse:
"""Parse metrics from a git repository."""
def __init__(self, repo_url: str):
"""
Initialize metric parser from repository url.
:param repo_url: repository url
"""
if not isinstance(repo_url, str) or not repo_url:
raise ValueError("Received repository URL was empty or None.")
self.repo_url = repo_url
self.repo_name = self.repo_url.strip('/').split("/")[-1]
self.repo_dir = os.path.join(DATA_DIR, "repos", self.repo_name)
self.repo: git.Repo
if os.path.isdir(self.repo_dir) and os.listdir(self.repo_dir):
self.repo = git.Repo(self.repo_dir)
else:
try:
self.repo = git.Repo.clone_from(self.repo_url, self.repo_dir)
except git.GitCommandError:
self.repo = None
return
self.repo.git.checkout(self.main_branch)
@staticmethod
def shorten_commit_message(commit_message: str, max_len: int = 100) -> str:
"""Shorten commit message for logging."""
msg = commit_message.replace("\n", " ")
if len(msg) > max_len:
msg = msg[:max_len]
msg += "..."
return msg
def _get_unprocessed_commit_hash_range(self) -> tuple[str | None, str | None, int]:
"""
Get start and end commit hashes not included in the repository's results table
and number of computed commits.
"""
if not os.path.exists(self._default_save_path):
return None, None, 0
results_df = pd.read_csv(self._default_save_path)
if results_df.empty:
return None, None, 0
# Reverse order: from the very first commit to the first in the results table.
first_hash = None
last_hash = results_df["hash"].iloc[0]
return first_hash, last_hash, len(results_df)
def save_metrics_for_each_commit(self, save_path: str = None) -> None:
"""Save info and metrics for each commit in main branch in a csv file."""
if self.repo is None:
return
branch_main = self.main_branch
if branch_main is None:
logger.info(f"Could not find main branch for {self.repo_name}.")
return
commit_metrics_list = []
# Unprocessed commit range. For when autosave is used.
start_hash, end_hash, num_computed = self._get_unprocessed_commit_hash_range()
commit_count = int(self.repo.git.rev_list('--count', 'HEAD')) - num_computed
traverser = Repository(
self.repo_dir,
only_in_branch=branch_main,
only_modifications_with_file_types=[".py"],
# order="reverse",
# from_commit=start_hash,
# to_commit=end_hash,
).traverse_commits()
recent_outcomes = queue.Queue(maxsize=100)
commit_start_time = perf_counter()
for i, commit in enumerate(traverser):
time_taken = perf_counter() - commit_start_time
commit_start_time = perf_counter()
print(
'repo', self.repo_name,
'| commit', i + 1, 'of', commit_count,
'| author:', commit.author.name,
'| date:', commit.committer_date,
'| lines_changed: ', f'{commit.lines} (+{commit.insertions} -{commit.deletions})',
'| time taken:', f'{time_taken:.2f}s',
'\n\tcommit message:', self.shorten_commit_message(commit.msg)
)
if time_taken > 60 or (time_taken > 20 and commit_count - i > 1000):
logger.info(f"Estimated time too high. Skipped repo {self.repo_name}.")
if os.path.exists(self._default_save_path):
os.remove(self._default_save_path)
break
commit_metric_dict = {
"hash": commit.hash,
"author": commit.author.name,
"date": commit.committer_date,
"commit_message": commit.msg,
"is_merge": commit.merge,
"lines_changed": commit.lines,
"insertions": commit.insertions,
"deletions": commit.deletions,
"dmm_unit_size": commit.dmm_unit_size,
"dmm_unit_complexity": commit.dmm_unit_complexity,
"dmm_unit_interfacing": commit.dmm_unit_interfacing,
}
sw_metrics, outcome = self._get_metrics(commit)
# If enough recent commits failed, stop processing.
if sum(recent_outcomes.queue) > 5:
logger.info(f"Too many recent errors. Stopped processing for {self.repo_name}.")
break
if recent_outcomes.full():
recent_outcomes.get()
if sw_metrics is None:
# Shortened message for logging.
commit_msg_short = self.shorten_commit_message(commit.msg)
if os.path.exists(self._default_save_path):
os.remove(self._default_save_path)
if outcome == HarvesterOutcome.PYTHON_VERSION_2:
logger.info(
f"Error computing metrics for {self.repo_name}. "
+ f"Invalid python version, stopped for repository at: \"{commit_msg_short}\" ({commit.hash}).")
break
elif outcome == HarvesterOutcome.INVALID_CODE:
logger.info(
f"Error computing metrics for {self.repo_name}. Source code could not be analyzed."
+ f"Skipped commit \"{commit_msg_short}\" ({commit.hash}).")
else:
logger.info(
f"Error computing metrics for {self.repo_name}. "
+ f"Unknown error at commit \"{commit_msg_short}\" ({commit.hash}).")
recent_outcomes.put(1) # Error occurred.
continue
recent_outcomes.put(0) # No error occurred.
# Add software metrics to commit metrics.
commit_metric_dict |= sw_metrics
commit_metrics_list.append(commit_metric_dict)
else: # No break occurred, all commits processed.
if not commit_metrics_list:
logger.warning(f"Found zero computable commits for {self.repo_name}.")
return
logger.info(f"Successfully processed {self.repo_name}. Saving results.")
self._save_to_csv(commit_metrics_list, save_path)
def _save_to_csv(self, metrics_list: list, save_path: str) -> None:
"""Save DataFrame of metrics to a csv file."""
if not metrics_list:
return
metrics_df = pd.DataFrame(metrics_list)
if save_path:
result_path = save_path
else:
result_path = self._default_save_path
# Merge with existing results if they exist. This allows autosave to work if implemented.
if os.path.exists(result_path):
old_df = pd.read_csv(result_path, index_col="ID", encoding="utf-8")
metrics_df = pd.concat([old_df, metrics_df], ignore_index=True)
metrics_df["date"] = pd.to_datetime(metrics_df["date"], utc=True)
metrics_df.sort_values("date", inplace=True)
metrics_df.reset_index(drop=True, inplace=True)
metrics_df.index.name = "ID"
metrics_df.to_csv(result_path, encoding="utf-8", mode="w")
def _get_metrics(self, commit: Commit) -> tuple[dict[str, float] | None, HarvesterOutcome]:
"""
Checkout parser's repo at given commit and compute software metrics for that commit.
Computes total raw metrics (LOC, LLOC, SLOC, comments), and average of other metrics.
"""
metric_dict = dict()
self.repo.git.checkout(commit.hash, force=True)
config = Config(
exclude=[],
ignore=[],
no_assert=True,
show_closures=False,
order=radon.complexity.SCORE,
show_complexity=True,
min='A',
max='F',
total_average=True,
include_ipynb=False,
multi=True, # Count multiline strings as comment lines as well.
by_function=False,
)
# Dict to store lists of unit complexity metrics.
unit_complexity_lists = dict()
# List to store errors from harvester. Contains lists of [str, dict].
harvester_errors: list[list] = []
# Raw metrics. Summed across the commit.
raw_harvester = RawHarvester([self.repo_dir], config)
raw_results = json.loads(raw_harvester.as_json())
keys = ["LOC", # Lines of code (total).
"LLOC", # Logical lines of code (containing exactly one statement).
"SLOC", # Source lines of code.
"comments"] # Comment lines.
for key in keys:
metric_dict["radon_" + key] = 0
for file_path, file in raw_results.items():
if "error" in file:
harvester_errors.append([file_path, file])
break
for key in keys:
metric_dict["radon_" + key] += file[key.lower()]
# Cyclomatic complexity. Per function.
cc_harvester = CCHarvester([self.repo_dir], config)
cc_results = json.loads(cc_harvester.as_json())
unit_complexity_lists["cc"] = [] # Cyclomatic complexity.
for file_path, file in cc_results.items():
if "error" in file:
harvester_errors.append([file_path, file])
break
for unit in file:
unit_complexity_lists["cc"].append(unit["complexity"])
# Maintainability index. Per file.
mi_harvester = MIHarvester([self.repo_dir], config)
mi_results = json.loads(mi_harvester.as_json())
unit_complexity_lists["MI"] = []
for file_path, file in mi_results.items():
if "error" in file:
harvester_errors.append([file_path, file])
break
unit_complexity_lists["MI"].append(file["mi"])
# Halstead's complexity. Per file.
config.by_function = False
hc_harvester = HCHarvester([self.repo_dir], config)
hc_results = json.loads(hc_harvester.as_json())
keys = ["vocabulary", "length", "volume", "difficulty", "effort", "time", "bugs"]
for key in keys:
unit_complexity_lists[key] = []
for file_path, file in hc_results.items():
if "error" in file:
harvester_errors.append([file_path, file])
break
for key in keys:
unit_complexity_lists[key].append(file["total"][key])
if harvester_errors:
for file_path, file in harvester_errors:
if file["error"].startswith("Missing parentheses in call to 'print'. Did you mean print(...)?"):
return None, HarvesterOutcome.PYTHON_VERSION_2
logger.info(f"Error in harvester at {file_path}: {file['error']}")
return None, HarvesterOutcome.INVALID_CODE
# Compute average of each metric, across the commit.
for metric in unit_complexity_lists:
metric_dict["radon_avg_" + metric] = self._metric_avg(unit_complexity_lists[metric])
return metric_dict, HarvesterOutcome.SUCCESS
@staticmethod
def _metric_avg(metrics: list) -> float | None:
"""Compute average of metrics."""
if not metrics:
return None
return sum(metrics) / len(metrics)
@property
def _default_save_path(self) -> str:
"""Default save path for results."""
return os.path.join(DATA_DIR, "results", self.repo_name + ".csv")
@property
def main_branch(self) -> str:
"""Main or master branch of the parser's repository."""
# Candidates named main or master.
candidates = ["main", "master", "origin/main", "origin/master"]
refs = self.repo.references
for candidate in candidates:
if candidate in refs:
return candidate
# Default branch.
show_result = self.repo.git.remote("show", "origin")
matches = re.search(r"\s*HEAD branch:\s*(.*)", show_result)
if matches:
default_branch = matches.group(1)
if default_branch:
return default_branch
raise ValueError(f"Could not find main branch for {self.repo_name}.")
def trial():
metric_parse = MetricParse("https://github.com/coreyleveen/irc_bot")
metric_parse.save_metrics_for_each_commit()
if __name__ == "__main__":
trial()