2012-05-14 15:24:45 +02:00
|
|
|
# coding: utf-8
|
|
|
|
#
|
2015-09-24 04:10:32 +02:00
|
|
|
# Copyright © 2012-2015 Ejwa Software. All rights reserved.
|
2012-05-14 15:24:45 +02:00
|
|
|
#
|
|
|
|
# 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/>.
|
|
|
|
|
2013-06-17 07:47:47 +02:00
|
|
|
from __future__ import unicode_literals
|
2014-02-20 20:57:57 +01:00
|
|
|
import re
|
2012-05-28 16:35:47 +02:00
|
|
|
import subprocess
|
2015-10-20 18:48:32 +02:00
|
|
|
from .changes import FileDiff
|
2015-10-21 05:26:20 +02:00
|
|
|
from . import comment, filtering, interval
|
2015-10-20 18:40:08 +02:00
|
|
|
|
2015-09-24 04:10:32 +02:00
|
|
|
__metric_eloc__ = {"java": 500, "c": 500, "cpp": 500, "cs": 500, "h": 300, "hpp": 300, "php": 500, "py": 500, "glsl": 1000,
|
2012-05-14 15:24:45 +02:00
|
|
|
"rb": 500, "js": 500, "sql": 1000, "xml": 1000}
|
|
|
|
|
2015-10-12 03:15:30 +02:00
|
|
|
__metric_cc_tokens__ = [[["java", "js", "c", "cc", "cpp"], ["else", r"for\s+\(.*\)", r"if\s+\(.*\)", r"case\s+\w+:",
|
|
|
|
"default:", r"while\s+\(.*\)"],
|
2014-02-20 20:57:57 +01:00
|
|
|
["assert", "break", "continue", "return"]],
|
2015-10-12 03:15:30 +02:00
|
|
|
[["cs"], ["else", r"for\s+\(.*\)", r"foreach\s+\(.*\)", r"goto\s+\w+:", r"if\s+\(.*\)", r"case\s+\w+:",
|
|
|
|
"default:", r"while\s+\(.*\)"],
|
2015-09-24 04:10:32 +02:00
|
|
|
["assert", "break", "continue", "return"]],
|
2015-10-12 03:15:30 +02:00
|
|
|
[["py"], [r"^\s+elif .*:$", r"^\s+else:$", r"^\s+for .*:", r"^\s+if .*:$", r"^\s+while .*:$"],
|
|
|
|
[r"^\s+assert", "break", "continue", "return"]]]
|
2014-02-20 20:57:57 +01:00
|
|
|
|
|
|
|
METRIC_CYCLOMATIC_COMPLEXITY_THRESHOLD = 50
|
2014-02-25 06:08:26 +01:00
|
|
|
METRIC_CYCLOMATIC_COMPLEXITY_DENSITY_THRESHOLD = 0.75
|
2014-02-20 20:57:57 +01:00
|
|
|
|
2015-10-12 03:15:30 +02:00
|
|
|
class MetricsLogic(object):
|
2012-05-20 22:02:08 +02:00
|
|
|
def __init__(self):
|
2012-05-14 15:24:45 +02:00
|
|
|
self.eloc = {}
|
2014-02-20 20:57:57 +01:00
|
|
|
self.cyclomatic_complexity = {}
|
|
|
|
self.cyclomatic_complexity_density = {}
|
|
|
|
|
2015-09-07 01:53:24 +02:00
|
|
|
ls_tree_r = subprocess.Popen(["git", "ls-tree", "--name-only", "-r", interval.get_ref()], bufsize=1,
|
2013-06-06 02:12:56 +02:00
|
|
|
stdout=subprocess.PIPE).stdout
|
2012-05-14 15:24:45 +02:00
|
|
|
|
|
|
|
for i in ls_tree_r.readlines():
|
2013-06-17 07:47:47 +02:00
|
|
|
i = i.strip().decode("unicode_escape", "ignore")
|
2013-05-16 03:06:28 +02:00
|
|
|
i = i.encode("latin-1", "replace")
|
|
|
|
i = i.decode("utf-8", "replace").strip("\"").strip("'").strip()
|
|
|
|
|
2015-10-20 18:48:32 +02:00
|
|
|
if FileDiff.is_valid_extension(i) and not filtering.set_filtered(FileDiff.get_filename(i)):
|
2015-09-07 01:53:24 +02:00
|
|
|
file_r = subprocess.Popen(["git", "show", interval.get_ref() + ":{0}".format(i.strip())],
|
|
|
|
bufsize=1, stdout=subprocess.PIPE).stdout.readlines()
|
2012-05-14 15:24:45 +02:00
|
|
|
|
2015-10-20 18:48:32 +02:00
|
|
|
extension = FileDiff.get_extension(i)
|
2013-08-03 10:30:43 +02:00
|
|
|
lines = MetricsLogic.get_eloc(file_r, extension)
|
2014-11-27 12:38:58 +01:00
|
|
|
cycc = MetricsLogic.get_cyclomatic_complexity(file_r, extension)
|
2013-08-03 10:30:43 +02:00
|
|
|
|
|
|
|
if __metric_eloc__.get(extension, None) != None and __metric_eloc__[extension] < lines:
|
|
|
|
self.eloc[i.strip()] = lines
|
2012-05-14 15:24:45 +02:00
|
|
|
|
2014-11-27 12:38:58 +01:00
|
|
|
if METRIC_CYCLOMATIC_COMPLEXITY_THRESHOLD < cycc:
|
|
|
|
self.cyclomatic_complexity[i.strip()] = cycc
|
2014-02-20 20:57:57 +01:00
|
|
|
|
2014-11-27 12:38:58 +01:00
|
|
|
if lines > 0 and METRIC_CYCLOMATIC_COMPLEXITY_DENSITY_THRESHOLD < cycc / float(lines):
|
|
|
|
self.cyclomatic_complexity_density[i.strip()] = cycc / float(lines)
|
2014-02-20 20:57:57 +01:00
|
|
|
|
2015-11-01 03:36:40 +01:00
|
|
|
def __add__(self, other):
|
|
|
|
if other == None:
|
|
|
|
return self
|
|
|
|
|
|
|
|
self.eloc.update(other.eloc)
|
|
|
|
self.cyclomatic_complexity.update(other.cyclomatic_complexity)
|
|
|
|
self.cyclomatic_complexity_density.update(other.cyclomatic_complexity_density)
|
|
|
|
return self
|
|
|
|
|
2014-02-20 20:57:57 +01:00
|
|
|
@staticmethod
|
|
|
|
def get_cyclomatic_complexity(file_r, extension):
|
|
|
|
is_inside_comment = False
|
|
|
|
cc_counter = 0
|
|
|
|
|
|
|
|
entry_tokens = None
|
|
|
|
exit_tokens = None
|
|
|
|
|
|
|
|
for i in __metric_cc_tokens__:
|
|
|
|
if extension in i[0]:
|
|
|
|
entry_tokens = i[1]
|
|
|
|
exit_tokens = i[2]
|
|
|
|
|
|
|
|
if entry_tokens or exit_tokens:
|
|
|
|
for i in file_r:
|
|
|
|
i = i.decode("utf-8", "replace")
|
|
|
|
(_, is_inside_comment) = comment.handle_comment_block(is_inside_comment, extension, i)
|
|
|
|
|
|
|
|
if not is_inside_comment and not comment.is_comment(extension, i):
|
2014-11-27 12:38:58 +01:00
|
|
|
for j in entry_tokens:
|
|
|
|
if re.search(j, i, re.DOTALL):
|
2014-02-20 20:57:57 +01:00
|
|
|
cc_counter += 2
|
2014-11-27 12:38:58 +01:00
|
|
|
for j in exit_tokens:
|
|
|
|
if re.search(j, i, re.DOTALL):
|
2014-02-20 20:57:57 +01:00
|
|
|
cc_counter += 1
|
2014-11-27 12:38:58 +01:00
|
|
|
return cc_counter
|
2014-02-20 20:57:57 +01:00
|
|
|
|
|
|
|
return -1
|
|
|
|
|
2012-05-14 15:24:45 +02:00
|
|
|
@staticmethod
|
|
|
|
def get_eloc(file_r, extension):
|
|
|
|
is_inside_comment = False
|
|
|
|
eloc_counter = 0
|
|
|
|
|
2014-02-20 20:57:57 +01:00
|
|
|
for i in file_r:
|
|
|
|
i = i.decode("utf-8", "replace")
|
|
|
|
(_, is_inside_comment) = comment.handle_comment_block(is_inside_comment, extension, i)
|
2012-05-14 15:24:45 +02:00
|
|
|
|
2014-02-20 20:57:57 +01:00
|
|
|
if not is_inside_comment and not comment.is_comment(extension, i):
|
2012-05-14 15:24:45 +02:00
|
|
|
eloc_counter += 1
|
|
|
|
|
|
|
|
return eloc_counter
|