2012-05-04 11:40:30 +02:00
|
|
|
# coding: utf-8
|
|
|
|
#
|
|
|
|
# Copyright © 2012 Ejwa Software. All rights reserved.
|
|
|
|
#
|
|
|
|
# This file is part of gitinspector.
|
|
|
|
#
|
|
|
|
# gitinspector is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# gitinspector is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with gitinspector. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
from changes import FileDiff
|
2012-05-09 12:14:25 +02:00
|
|
|
import comment
|
2012-05-07 09:38:11 +02:00
|
|
|
import missing
|
2012-05-20 16:46:09 +02:00
|
|
|
import multiprocessing
|
2012-05-20 22:02:08 +02:00
|
|
|
import os
|
2012-05-04 11:40:30 +02:00
|
|
|
import re
|
2012-05-18 04:33:10 +02:00
|
|
|
import sys
|
2012-05-04 11:40:30 +02:00
|
|
|
import terminal
|
2012-05-20 16:46:09 +02:00
|
|
|
import threading
|
2012-05-04 11:40:30 +02:00
|
|
|
|
2012-05-09 12:14:25 +02:00
|
|
|
class BlameEntry:
|
|
|
|
rows = 0
|
|
|
|
comments = 0
|
|
|
|
|
2012-05-20 22:02:08 +02:00
|
|
|
__thread_lock__ = threading.BoundedSemaphore(multiprocessing.cpu_count())
|
|
|
|
__blame_lock__ = threading.Lock()
|
2012-05-20 16:46:09 +02:00
|
|
|
|
|
|
|
class BlameThread(threading.Thread):
|
2012-05-20 22:02:08 +02:00
|
|
|
def __init__(self, blame_string, extension, blames):
|
|
|
|
__thread_lock__.acquire() # Lock controlling the number of threads running
|
2012-05-20 16:46:09 +02:00
|
|
|
threading.Thread.__init__(self)
|
|
|
|
|
|
|
|
self.blame_string = blame_string
|
|
|
|
self.extension = extension
|
|
|
|
self.blames = blames
|
|
|
|
|
|
|
|
def run(self):
|
2012-05-20 22:02:08 +02:00
|
|
|
git_blame_r = os.popen(self.blame_string)
|
2012-05-20 16:46:09 +02:00
|
|
|
is_inside_comment = False
|
|
|
|
|
|
|
|
for j in git_blame_r.readlines():
|
|
|
|
if Blame.is_blame_line(j):
|
|
|
|
author = Blame.get_author(j)
|
|
|
|
content = Blame.get_content(j)
|
2012-05-20 22:02:08 +02:00
|
|
|
__blame_lock__.acquire() # Global lock used to protect calls from here...
|
2012-05-20 16:46:09 +02:00
|
|
|
|
|
|
|
if self.blames.get(author, None) == None:
|
|
|
|
self.blames[author] = BlameEntry()
|
|
|
|
|
|
|
|
if comment.is_comment(self.extension, content):
|
|
|
|
self.blames[author].comments += 1
|
|
|
|
if is_inside_comment:
|
|
|
|
if comment.has_comment_end(self.extension, content):
|
|
|
|
is_inside_comment = False
|
|
|
|
else:
|
|
|
|
self.blames[author].comments += 1
|
|
|
|
elif comment.has_comment_begining(self.extension, content):
|
|
|
|
is_inside_comment = True
|
|
|
|
|
|
|
|
self.blames[author].rows += 1
|
2012-05-20 22:02:08 +02:00
|
|
|
__blame_lock__.release() # ...to here.
|
2012-05-20 16:46:09 +02:00
|
|
|
|
2012-05-20 22:02:08 +02:00
|
|
|
__thread_lock__.release() # Lock controlling the number of threads running
|
2012-05-20 16:46:09 +02:00
|
|
|
|
2012-05-04 11:40:30 +02:00
|
|
|
class Blame:
|
2012-05-20 22:02:08 +02:00
|
|
|
def __init__(self, hard):
|
2012-05-04 11:40:30 +02:00
|
|
|
self.blames = {}
|
2012-05-20 22:02:08 +02:00
|
|
|
ls_tree_r = os.popen("git ls-tree --name-only -r HEAD")
|
2012-05-18 04:33:10 +02:00
|
|
|
lines = ls_tree_r.readlines()
|
2012-05-04 11:40:30 +02:00
|
|
|
|
2012-05-20 22:02:08 +02:00
|
|
|
for i, row in enumerate(lines):
|
2012-05-18 04:33:10 +02:00
|
|
|
if FileDiff.is_valid_extension(row):
|
2012-05-20 22:02:08 +02:00
|
|
|
if not missing.add(row.strip()):
|
2012-05-20 16:46:09 +02:00
|
|
|
blame_string = "git blame -w {0} \"".format("-C -C -M" if hard else "") + row.strip() + "\""
|
2012-05-20 22:02:08 +02:00
|
|
|
thread = BlameThread(blame_string, FileDiff.get_extension(row), self.blames)
|
2012-05-21 15:43:51 +02:00
|
|
|
thread.daemon = True
|
2012-05-20 22:02:08 +02:00
|
|
|
thread.start()
|
2012-05-04 11:40:30 +02:00
|
|
|
|
2012-05-18 04:33:10 +02:00
|
|
|
if hard:
|
|
|
|
Blame.output_progress(i, len(lines))
|
|
|
|
|
2012-05-20 22:02:08 +02:00
|
|
|
# Make sure all threads have completed.
|
|
|
|
for i in range(0, multiprocessing.cpu_count()):
|
|
|
|
__thread_lock__.acquire()
|
|
|
|
|
2012-05-18 04:33:10 +02:00
|
|
|
@staticmethod
|
|
|
|
def output_progress(pos, length):
|
|
|
|
if sys.stdout.isatty():
|
|
|
|
terminal.clear_row()
|
|
|
|
print "\bChecking how many rows belong to each author (Progress): " + str(100 * pos / length) + "%",
|
|
|
|
sys.stdout.flush()
|
|
|
|
|
2012-05-04 11:40:30 +02:00
|
|
|
@staticmethod
|
|
|
|
def is_blame_line(string):
|
|
|
|
return string.find(" (") != -1
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_author(string):
|
2012-05-04 15:15:41 +02:00
|
|
|
author = re.search(" \((.*?)\d\d\d\d-\d\d-\d\d", string)
|
|
|
|
return re.sub("[^\w ]", "", author.group(1)).strip()
|
2012-05-04 11:40:30 +02:00
|
|
|
|
2012-05-09 12:14:25 +02:00
|
|
|
@staticmethod
|
|
|
|
def get_content(string):
|
|
|
|
content = re.search(" \d+\)(.*)", string)
|
|
|
|
return content.group(1).lstrip()
|
|
|
|
|
2012-05-20 22:02:08 +02:00
|
|
|
def output(hard):
|
2012-05-18 04:33:10 +02:00
|
|
|
print ""
|
2012-05-20 22:02:08 +02:00
|
|
|
blame = Blame(hard)
|
2012-05-04 11:40:30 +02:00
|
|
|
|
2012-05-18 04:33:10 +02:00
|
|
|
if hard:
|
|
|
|
terminal.clear_row()
|
|
|
|
|
|
|
|
print "\bBelow is the number of rows from each author that have survived and"
|
2012-05-04 11:40:30 +02:00
|
|
|
print "are still intact in the current revision:\n"
|
2012-05-09 12:14:25 +02:00
|
|
|
terminal.printb("Author".ljust(21) + "Rows".rjust(10) + "% in comments".rjust(16))
|
2012-05-04 15:15:41 +02:00
|
|
|
for i in sorted(blame.blames.items()):
|
2012-05-04 11:40:30 +02:00
|
|
|
print i[0].ljust(20)[0:20],
|
2012-05-09 12:14:25 +02:00
|
|
|
print str(i[1].rows).rjust(10),
|
|
|
|
print "{0:.2f}".format(100.0 * i[1].comments / i[1].rows).rjust(15)
|