2012-05-14 15:24:45 +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/>.
|
|
|
|
|
2012-05-25 16:42:29 +02:00
|
|
|
from __future__ import print_function
|
2013-03-11 00:23:50 +01:00
|
|
|
from outputable import Outputable
|
2012-05-14 15:24:45 +02:00
|
|
|
from changes import FileDiff
|
|
|
|
import comment
|
2012-05-22 17:57:44 +02:00
|
|
|
import filtering
|
2012-05-14 15:24:45 +02:00
|
|
|
import missing
|
2012-05-28 16:35:47 +02:00
|
|
|
import subprocess
|
2012-05-14 15:24:45 +02:00
|
|
|
|
|
|
|
__metric_eloc__ = {"java": 500, "c": 500, "cpp": 500, "h": 300, "hpp": 300, "py": 500, "glsl": 1000,
|
|
|
|
"rb": 500, "js": 500, "sql": 1000, "xml": 1000}
|
|
|
|
|
2013-03-11 00:23:50 +01:00
|
|
|
class MetricsLogic:
|
2012-05-20 22:02:08 +02:00
|
|
|
def __init__(self):
|
2012-05-14 15:24:45 +02:00
|
|
|
self.eloc = {}
|
2012-05-28 16:35:47 +02:00
|
|
|
ls_tree_r = subprocess.Popen("git ls-tree --name-only -r HEAD", shell=True, bufsize=1, stdout=subprocess.PIPE).stdout
|
2012-05-14 15:24:45 +02:00
|
|
|
|
|
|
|
for i in ls_tree_r.readlines():
|
2012-05-29 03:13:49 +02:00
|
|
|
i = i.decode("utf-8", "replace")
|
2012-05-22 17:57:44 +02:00
|
|
|
if FileDiff.is_valid_extension(i) and not filtering.set_filtered(FileDiff.get_filename(i)):
|
2012-05-20 22:02:08 +02:00
|
|
|
if not missing.add(i.strip()):
|
2012-05-28 16:35:47 +02:00
|
|
|
file_r = open(i.strip(), "rb")
|
2012-05-14 15:24:45 +02:00
|
|
|
extension = FileDiff.get_extension(i)
|
2013-03-11 00:23:50 +01:00
|
|
|
lines = MetricsLogic.get_eloc(file_r, extension)
|
2012-05-14 15:24:45 +02:00
|
|
|
|
2012-05-22 17:57:44 +02:00
|
|
|
if __metric_eloc__.get(extension, None) != None and __metric_eloc__[extension] < lines:
|
2012-05-14 15:24:45 +02:00
|
|
|
self.eloc[i.strip()] = lines
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_eloc(file_r, extension):
|
|
|
|
is_inside_comment = False
|
|
|
|
eloc_counter = 0
|
|
|
|
|
|
|
|
for j in file_r.readlines():
|
2012-05-29 03:13:49 +02:00
|
|
|
j = j.decode("utf-8", "replace")
|
2012-05-16 04:14:48 +02:00
|
|
|
if is_inside_comment and comment.has_comment_end(extension, j):
|
|
|
|
is_inside_comment = False
|
|
|
|
elif comment.has_comment_begining(extension, j):
|
2012-05-14 15:24:45 +02:00
|
|
|
is_inside_comment = True
|
|
|
|
|
2012-05-16 04:14:48 +02:00
|
|
|
if not is_inside_comment and not comment.is_comment(extension, j):
|
2012-05-14 15:24:45 +02:00
|
|
|
eloc_counter += 1
|
|
|
|
|
|
|
|
return eloc_counter
|
|
|
|
|
2012-10-23 11:25:52 +02:00
|
|
|
__eloc_info_text__ = "The following files are suspiciously big (in order of severity)"
|
|
|
|
__metrics_missing_info_text__ = "No metrics violations were found in the repository"
|
|
|
|
|
2013-03-11 00:23:50 +01:00
|
|
|
class Metrics(Outputable):
|
|
|
|
def output_text(self):
|
|
|
|
metrics_logic = MetricsLogic()
|
|
|
|
|
|
|
|
if not metrics_logic.eloc:
|
|
|
|
print("\n" + __metrics_missing_info_text__ + ".")
|
|
|
|
else:
|
|
|
|
print("\n" + __eloc_info_text__ + ":")
|
|
|
|
for i in sorted(set([(j, i) for (i, j) in metrics_logic.eloc.items()]), reverse = True):
|
|
|
|
print(i[1] + " (" + str(i[0]) + " eloc)")
|
|
|
|
|
|
|
|
def output_xml(self):
|
|
|
|
metrics_logic = MetricsLogic()
|
|
|
|
|
|
|
|
if not metrics_logic.eloc:
|
|
|
|
print("\t<metrics>\n\t\t<message>" + __metrics_missing_info_text__ + "</message>\n\t</metrics>")
|
|
|
|
else:
|
|
|
|
eloc_xml = ""
|
|
|
|
for i in sorted(set([(j, i) for (i, j) in metrics_logic.eloc.items()]), reverse = True):
|
|
|
|
eloc_xml += "\t\t\t\t\t<violation>\n"
|
|
|
|
eloc_xml += "\t\t\t\t\t\t<file-name>" + i[1] + "</file-name>\n"
|
|
|
|
eloc_xml += "\t\t\t\t\t\t<lines-of-code>" + str(i[0]) + "</lines-of-code>\n"
|
|
|
|
eloc_xml += "\t\t\t\t\t</violation>\n"
|
|
|
|
|
|
|
|
print("\t\t<metrics>\n\t\t\t<eloc>\n\t\t\t\t<message>" + __eloc_info_text__ +
|
|
|
|
"</message>\n\t\t\t\t<violations>\n" + eloc_xml + "\t\t\t\t</violations>\n\t\t\t</eloc>\n\t\t</metrics>")
|