Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate to Python 3, fix passing filename bug. #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 17 additions & 17 deletions cmetrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
# Authors : Israel Herraiz <[email protected]>


from commands import getoutput
from subprocess import getoutput
import os
import argparse

Expand Down Expand Up @@ -92,10 +92,10 @@ def getMedianCyclo (self):
cyclos = [x.cyclo for x in self.functions]

if len(cyclos) % 2 == 1:
return cyclos[(len(cyclos)+1)/2-1]
return cyclos[(len(cyclos)+1)//2-1]
else:
lower = cyclos[len(cyclos)/2-1]
upper = cyclos[len(cyclos)/2]
lower = cyclos[len(cyclos)//2-1]
upper = cyclos[len(cyclos)//2]

return (lower + upper) / 2

Expand Down Expand Up @@ -143,7 +143,7 @@ def measure_file (self, sourcecode_fn):

f = SourceCodeFile (sourcecode_fn)

mccabe_o = getoutput("%s -n %s" % (self._mccabe_cmd, f.path))
mccabe_o = getoutput("%s -n %r" % (self._mccabe_cmd, f.path))

for l in mccabe_o.split('\n'):

Expand All @@ -157,7 +157,7 @@ def measure_file (self, sourcecode_fn):
f.addFunction (Function (func, cyclo, sloc, returns))

if not self.only_measure_funcs:
kdsi_o = getoutput("%s %s" % (self._kdsi_cmd, f.path))
kdsi_o = getoutput("%s %r" % (self._kdsi_cmd, f.path))

loc, blanks, comment_l, comments, _name = kdsi_o.split()
f.loc = int(loc)
Expand All @@ -166,7 +166,7 @@ def measure_file (self, sourcecode_fn):
f.comments = int(comments)
f.sloc = f.loc - f.blanks - f.comment_l

halstead_o = getoutput("%s %s" % (self._halstead_cmd, f.path))
halstead_o = getoutput("%s %r" % (self._halstead_cmd, f.path))

_name, hlength, hvolume, hlevel, hmd = halstead_o.split()
f.hlength = int(hlength)
Expand All @@ -186,7 +186,7 @@ def print_files_without_functions (self, show_path=False, show_header=True):

if show_header:
template = "{0:<12} {1:>5} {2:>5} {3:>5} {4:>5} {5:>5} {6:>5} {7:>5} {8:>5} {9:>9} {10:>9} {11:>5} {12:>5} {13:>5} {14:>5} {15:>5}"
print template.format("FILE", "SLOC", "LOC", "FUNCS", "BLANK", "COM.L", "COM.N", "H LEN", "H VOL", "H LEVEL", "H MEN. D.", "MAXCY", "MINCY", "AVGCY", "MEDCY", "TOTCY",)
print(template.format("FILE", "SLOC", "LOC", "FUNCS", "BLANK", "COM.L", "COM.N", "H LEN", "H VOL", "H LEVEL", "H MEN. D.", "MAXCY", "MINCY", "AVGCY", "MEDCY", "TOTCY",))

if show_path:
template = "{0:<12} {1:>5} {2:>5} {3:>5} {4:>5} {5:>5} {6:>5} {7:>5} {8:>5} {9:>9.3e} {10:>9.3e} {11:>5} {12:>5} {13:>5} {14:>5} {15:>5} {16:<}"
Expand All @@ -209,7 +209,7 @@ def print_files_without_functions (self, show_path=False, show_header=True):


if show_path:
print template.format(f.name,
print(template.format(f.name,
f.sloc,
f.loc,
f.nfuncs,
Expand All @@ -225,10 +225,10 @@ def print_files_without_functions (self, show_path=False, show_header=True):
f.getAvgCyclo(),
f.getMedianCyclo(),
f.getTotalCyclo(),
f.path)
f.path))

else:
print template.format(f.name,
print(template.format(f.name,
f.sloc,
f.loc,
f.nfuncs,
Expand All @@ -243,15 +243,15 @@ def print_files_without_functions (self, show_path=False, show_header=True):
min_cyclo,
f.getAvgCyclo(),
f.getMedianCyclo(),
f.getTotalCyclo())
f.getTotalCyclo()))



def print_files_with_functions (self, show_path=False, show_header=True):

if show_header:
template = "{0:<12} {1:<15} {2:>5} {3:>5} {4:>5}"
print template.format("FILE", "FUNC", "SLOC", "CYCLO", "RETURN",)
print(template.format("FILE", "FUNC", "SLOC", "CYCLO", "RETURN",))

if show_path:
template = "{0:<12} {1:<15} {2:>5} {3:>5} {4:>5} {5:<}"
Expand All @@ -261,18 +261,18 @@ def print_files_with_functions (self, show_path=False, show_header=True):
for f in self._files:
for fun in f.functions:
if show_path:
print template.format(f.name[0:10],
print(template.format(f.name[0:10],
fun.name.split()[-1][0:15],
fun.sloc,
fun.cyclo,
fun.returns,
f.path)
f.path))
else:
print template.format(f.name[0:10],
print(template.format(f.name[0:10],
fun.name.split()[-1][0:15],
fun.sloc,
fun.cyclo,
fun.returns)
fun.returns))


if __name__ == '__main__':
Expand Down
8 changes: 4 additions & 4 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ AM_PROG_CC_STDC
AC_HEADER_STDC
AC_PROG_AWK
AM_PROG_LEX
AM_PATH_PYTHON([2.7])
AM_PATH_PYTHON([3])

case "$PYTHON_VERSION" in
3.0*|3.1*|3.2*)
2.*)
AC_MSG_ERROR([
Python 3 not compatible with cmetrics.
Set PYTHON to your Python 2.x binary.
Python 2 not compatible with cmetrics.
Set PYTHON to your Python 3.x binary.
]);;
esac

Expand Down
4 changes: 2 additions & 2 deletions halstead/halstead
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ then
exit 1
fi

for file in $*
for file in "$@"
do
stripcom ${file} | stripstr |\
stripcom "${file}" | stripstr |\
c_halsfilt 2> ${N1} |\
awk ' { for ( i = 1; i <= NF; i++) print $i; } ' |\
sort > ${N2}
Expand Down
4 changes: 2 additions & 2 deletions mccabe/mccabe
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ then
echo "-------------- --------------- ---------- -------------- --------------"
fi

for file in $*
for file in "$@"
do
stripcom ${file} | \
stripcom "${file}" | \
stripstr | \
sed -e "s|'?'|' '|" | \
awk 'BEGIN {
Expand Down