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
|
# -*- coding: utf-8 -*-
import calendar
import difflib
import os
import shutil
import time
from code import CodeMetrics
from git import Repo
class RepositoryChecker:
"""Browse a Git repository."""
def __init__(self, url, year):
"""Initialize the analysis of a Git repository."""
self._repo = Repo(url)
self._dump_path = url + '_dump'
self._start = calendar.timegm(time.strptime('1/1/%u' % year, '%d/%m/%Y'))
self._end = calendar.timegm(time.strptime('1/1/%u' % (year + 1), '%d/%m/%Y'))
self._timeline = {}
self._last_metrics = None
self._insertions = 0
self._deletions = 0
self._added = 0
self._removed = 0
self._old = 0
self._new = 0
self._killed = 0
def __str__(self):
"""Provide a pretty print of the checker."""
desc = ''
desc += '=== Code ===\n'
desc += 'Insertions: %u\n' % self._insertions
desc += 'Deletions: %u\n' % self._deletions
desc += '\n'
desc += '=== Files ===\n'
desc += 'Added: %u\n' % self._added
desc += 'Removed: %u\n' % self._removed
desc += '\n'
desc += '=== TODO/FIXME ===\n'
desc += 'Old: %u\n' % self._old
desc += 'New: %u\n' % self._new
desc += 'Killed: %u\n' % self._killed
return desc
def process(self):
"""Collect Git info."""
selection = list(self._repo.iter_commits('master'))
count = 0
previous = None
for commit in list(selection):
valid = self._start <= commit.committed_date and commit.committed_date < self._end
if not(previous or valid):
continue
elif previous and not(valid):
break
previous = commit
count += 1
i = 0
previous = None
for commit in list(selection):
valid = self._start <= commit.committed_date and commit.committed_date < self._end
if not(previous or valid):
continue
elif previous and not(valid):
break
previous = commit
print('\r[+] Collecting Git info... %d%%' % ((i * 100) / count), end='')
# Insertions / deletions
total = commit.stats.total
self._insertions += total.get('insertions', 0)
self._deletions += total.get('deletions', 0)
# The run command is :
#
# git diff-tree SHA SHA~1 -r --abbrev=40 --full-index -M --raw --no-color
#
# Beware: all is reversed!
for diff in commit.diff(commit.hexsha + '~1', create_patch=True, ignore_blank_lines=True,
ignore_space_at_eol=True, diff_filter='cr'):
# Added / removed
if diff.new_file:
self._removed += 1
if diff.deleted_file:
self._added += 1
# TODO / FIXME / REMME
blob_a = None
blob_b = None
try:
if diff.a_blob:
blob_a = diff.a_blob.data_stream.read().decode('utf-8').splitlines(1)
if diff.b_blob:
blob_b = diff.b_blob.data_stream.read().decode('utf-8').splitlines(1)
except UnicodeDecodeError:
pass
if blob_a is None and blob_b is None:
# Binary file
pass
elif blob_a is None:
for line in blob_b:
if 'TODO' in line or 'FIXME' in line or 'REMME' in line:
self._killed += 1
elif blob_b is None:
for line in blob_a:
if 'TODO' in line or 'FIXME' in line or 'REMME' in line:
self._new += 1
else:
for line in difflib.unified_diff(blob_a, blob_b):
if line.startswith('+++') or line.startswith('---'):
continue
if 'TODO' in line or 'FIXME' in line or 'REMME' in line:
if line.startswith('-'):
self._new += 1
elif line.startswith('+'):
self._killed += 1
# Single lines of code
progress = '\r[+] Collecting Git info... %d%%' % ((i * 100) / count)
self._delete_dump()
msg = self._build_dump(commit.tree, progress)
print('\r' + ' ' * len(msg[1:]), end='')
print(progress, end='')
cm = CodeMetrics(self._dump_path)
msg = cm.process(progress)
print('\r' + ' ' * len(msg[1:]), end='')
print(progress, end='')
self._timeline[commit.committed_date] = cm.count_all_lines()
if self._last_metrics is None:
self._last_metrics = cm
# All remaining TODO / FIXME
msg = self._grep_for_toto_fixme(commit.tree, progress)
print('\r' + ' ' * len(msg[1:]), end='')
print(progress, end='')
i += 1
print('\r[+] Collecting Git info... %d%%' % ((i * 100) / count))
def _delete_dump(self):
"""Delete all dumped items."""
if os.path.exists(self._dump_path):
shutil.rmtree(self._dump_path)
def _build_dump(self, tree, msg):
"""Dump all items from a commit tree."""
if not os.path.exists(self._dump_path):
os.makedirs(self._dump_path)
count = len(list(tree.traverse()))
i = 0
for item in tree.traverse():
print(msg + ' -> Dumping items... %d%%' % ((i * 100) / count), end='')
path = os.path.join(self._dump_path, item.path)
if item.type == 'tree':
if not os.path.exists(path):
os.makedirs(path)
elif item.type == 'blob':
with open(path, 'wb') as out:
out.write(item.data_stream.read())
i += 1
msg = msg + ' -> Dumping items... %d%%' % ((i * 100) / count)
print(msg, end='')
return msg
def _grep_for_toto_fixme(self, tree, msg):
"""Find all waiting TODO / FIXME markers."""
count = len(list(tree.traverse()))
i = 0
for item in tree.traverse():
print(msg + ' -> Searching for markers... %d%%' % ((i * 100) / count), end='')
if item.type == 'blob':
blob = None
try:
blob = item.data_stream.read().decode('utf-8').splitlines(1)
except UnicodeDecodeError:
pass
if blob:
for line in blob:
if 'TODO' in line or 'FIXME' in line or 'REMME' in line:
self._old += 1
i += 1
msg = msg + ' -> Searching for markers... %d%%' % ((i * 100) / count)
print(msg, end='')
return msg
def get(self, name):
"""Provide a memorized property."""
return getattr(self, '_' + name)
|