Skip to content

Instantly share code, notes, and snippets.

@timjstewart
Last active December 27, 2015 19:39
Show Gist options
  • Save timjstewart/7377954 to your computer and use it in GitHub Desktop.
Save timjstewart/7377954 to your computer and use it in GitHub Desktop.
Bash script that does high level analysis of Python, Java, and Scala source files
#! /bin/bash
#
# overview -- searches through all Java, Python, and Scala files in the current
# directory and all subdirectories and lists some basic stats about
# each file
if [ "$1" == "--help" ]
then
cat <<-HERE >&2
usage: overview [--help]
\\n - number of lines
Dep - number of dependencies (e.g. imports)
Cls - number of classes
Lp - number of loops
If - number of ifs
Els - number of elses
Try - number of try blocks
Exc - number of exceptions thrown
New - number of new statements
// - number of comments
[ ] - todo
HERE
exit
fi
echo " \\n Dep Cls Lp If Els Try Exc New // [ ]" >&2
for fn in $(find . \( -iname '*.java' -o -iname '*.scala' -o -name '*.py' \))
do
if [ -r $fn ]
then
gawk -v file_name=$fn '
# Lines with something on them
! /^[ ]+$/ {
lines++
}
# comments that suggest more work to be done
tolower($_) ~ /\ytodo\y|\ytodo\y|\ykludge\y/ {
todos++
}
# Java/Scala single line comment
/^[ \t]*\/\// {
comments++
next
}
# Java/Scala start multi-line comment
/^[ \t]*\/\*/ {
comments++
next
}
# Java/Scala end multi-line comment
/^[ \t]*\*\// {
comments++
next
}
# Java/Scala multi-line comment line _maybe_
/^[ \t]*\*/ {
comments++
next
}
# Python single line comment
/^[ \t]*#/ {
comments++
next
}
# Java/Scala new (does not handle Scala case classes)
/\ynew\y/ {
news++
}
# Java/Scala/Python import
/\yimport\y/ {
imports++
}
# Java/Scala/Python class definition
/\yclass\y/ {
classes++
}
# Branching statements
/\yif\y/ {
ifs++
}
# When an if is not enough
/\yelse\y|\yelif\y/ {
elses++
}
# Loops
/\ywhile\y|\yfor\y|\yforeach\y|\ymap\y/ {
loops++
}
# Try blocks entered
/\ytry\y/ {
trys++
}
# Exceptions thrown
/\ythrow\y|\yraise\y/ {
throws++
}
END {
# Print a report
printf "%4d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %s\n",
lines, imports, classes, loops, ifs,
elses, trys, throws, news, comments,
todos, file_name
}
' < ${fn}
fi
done | sort -nrk 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment