Added filtering support.

Filtering can be specified using an arbitrary number of -x (or --exclude)
parameters. The filtering works in a similar fashion to an inverted grep,
meaning that matched file names will be excluded from the generated
statistics.
This commit is contained in:
Adam Waldenberg 2012-05-22 17:57:44 +02:00
parent 81e3f5d82c
commit 93cf15f500
6 changed files with 73 additions and 10 deletions

View File

@ -19,6 +19,7 @@
from changes import FileDiff
import comment
import filtering
import missing
import multiprocessing
import os
@ -78,7 +79,7 @@ class Blame:
lines = ls_tree_r.readlines()
for i, row in enumerate(lines):
if FileDiff.is_valid_extension(row):
if FileDiff.is_valid_extension(row) and not filtering.set_filtered(FileDiff.get_filename(row)):
if not missing.add(row.strip()):
blame_string = "git blame -w {0} \"".format("-C -C -M" if hard else "") + row.strip() + "\""
thread = BlameThread(blame_string, FileDiff.get_extension(row), self.blames)

View File

@ -18,6 +18,7 @@
# along with gitinspector. If not, see <http://www.gnu.org/licenses/>.
import extensions
import filtering
import os
import re
import terminal
@ -41,6 +42,11 @@ class FileDiff:
string = string.split("|")[0].strip().strip("{}")
return os.path.splitext(string)[1][1:]
@staticmethod
def get_filename(string):
string = string.split("|")[0].strip().strip("{}")
return string.strip()
@staticmethod
def is_valid_extension(string):
extension = FileDiff.get_extension(string)
@ -93,7 +99,7 @@ class Changes:
found_valid_extension = False
commit = Commit(i)
if FileDiff.is_filediff_line(i):
if FileDiff.is_filediff_line(i) and not filtering.set_filtered(FileDiff.get_filename(i)):
extensions.add_located(FileDiff.get_extension(i))
if FileDiff.is_valid_extension(i):

50
filtering.py Normal file
View File

@ -0,0 +1,50 @@
# 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/>.
import terminal
__filters__ = []
__filtered_files__ = set()
def get():
return __filters__
def add(string):
__filters__.append(string)
def get_filered():
return __filtered_files__
def set_filtered(file_name):
string = file_name.strip()
if len(string) > 0:
for i in __filters__:
if string.find(i) != -1:
__filtered_files__.add(string)
return True
return False
def output():
if __filtered_files__:
print "\nThe following files were excluded from the statistics due to the"
print "specified exclusion patterns:"
for i in __filtered_files__:
print i

View File

@ -21,6 +21,7 @@
import blame
import changes
import extensions
import filtering
import getopt
import help
import metrics
@ -57,6 +58,7 @@ class Runner:
metrics.output()
missing.output()
filtering.output()
if self.list_file_types:
extensions.output()
@ -67,8 +69,8 @@ if __name__ == "__main__":
__run__ = Runner()
try:
__opts__, __args__ = getopt.gnu_getopt(sys.argv[1:], "cf:hHlmTw", ["checkout-missing", "file-types=", "hard",
"help", "list-file-types", "metrics","tda367", "timeline",
__opts__, __args__ = getopt.gnu_getopt(sys.argv[1:], "cf:hHlmTwx:", ["checkout-missing", "exclude=", "file-types=",
"hard", "help", "list-file-types", "metrics","tda367", "timeline",
"version"])
except getopt.error, msg:
print sys.argv[0], "\b:", msg
@ -101,6 +103,8 @@ if __name__ == "__main__":
__run__.timeline = True
elif o in("-w"):
__run__.useweeks = True
elif o in("-x", "--exclude"):
filtering.add(a)
for arg in __args__:
__run__.repo = arg

View File

@ -36,12 +36,13 @@ Mandatory arguments to long options are mandatory for short options too.
analysis of commits
-T, --timeline show commit timeline, including author names
--tda367 show statistics and information in a way that
is formatted for the course TDA367/DIT211
this is currently the same as -lmTw, but
will later include some additional searches
for things such as documentation (RAD/SDD)
is formatted for the course TDA367/DIT211;
this is the same as calling the same as -lmTw
-w show all statistical information in weeks
instead of in months
-x an exclusion pattern describing file names that
should be excluded from the statistics
-h, --help display this help and exit
--version output version information and exit

View File

@ -19,6 +19,7 @@
from changes import FileDiff
import comment
import filtering
import missing
import os
@ -31,13 +32,13 @@ class Metrics:
ls_tree_r = os.popen("git ls-tree --name-only -r HEAD")
for i in ls_tree_r.readlines():
if FileDiff.is_valid_extension(i):
if FileDiff.is_valid_extension(i) and not filtering.set_filtered(FileDiff.get_filename(i)):
if not missing.add(i.strip()):
file_r = open(i.strip(), "r")
extension = FileDiff.get_extension(i)
lines = Metrics.get_eloc(file_r, extension)
if __metric_eloc__[extension] < lines:
if __metric_eloc__.get(extension, None) != None and __metric_eloc__[extension] < lines:
self.eloc[i.strip()] = lines
@staticmethod