Skip to content

Commit

Permalink
CLOCBear: Add CLOCBear
Browse files Browse the repository at this point in the history
This bear gives statistics about number of lines,
number of comment lines, and total number of lines.

Closes coala#1577
  • Loading branch information
shreyans800755 committed Apr 26, 2017
1 parent 28f582e commit bee639d
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .ci/deps.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ esac
# apt-get commands
export DEBIAN_FRONTEND=noninteractive

deps="libclang1-3.4 indent mono-mcs chktex r-base julia golang-go luarocks verilator cppcheck flawfinder devscripts"
deps="libclang1-3.4 indent mono-mcs chktex r-base julia golang-go luarocks verilator cppcheck flawfinder devscripts cloc"
deps_infer="m4 opam"

case $CIRCLE_BUILD_IMAGE in
Expand Down
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ addons:
- opam
- php-codesniffer
- verilator
- cloc

cache:
pip: true
Expand Down
58 changes: 58 additions & 0 deletions bears/general/CLOCBear.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import re

from coalib.bearlib.abstractions.Linter import linter
from coalib.results.Result import Result
from dependency_management.requirements.DistributionRequirement import (
DistributionRequirement)


@linter(executable='cloc',
use_stdout=True,
use_stderr=False)
class CLOCBear:
"""
Summarises file of or number of files in directory structure
with total lines, number comment lines, actual code lines using tool cloc
"""
LANGUAGES = {'All'}
REQUIREMENTS = {DistributionRequirement(apt_get='cloc')}
AUTHORS = {'The coala developers'}
AUTHORS_EMAILS = {'[email protected]'}
LICENSE = 'AGPL-3.0'
CAN_DETECT = {'Comment lines','Empty lines'}

@staticmethod
def create_arguments(filename, file, config_file):
return ('', filename)

def process_output(self, output, filename, file):
out = output
lines = out.split('\n')
files = lines[1][:lines[1].find('unique')-1]
if(files == 0):
msg = 'No valid files. File should belong to valid programming language.'
yield Result.from_values(origin = self,
message = msg,
file=filename)
else:
regex = r"(^\S+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)"
for row in lines[8:-2]:
match = re.search(regex, row)
lang = match.group(1)
nfiles = match.group(2)
blank = (int)(match.group(3))
comment = (int)(match.group(4))
code = (int)(match.group(5))
total = blank + comment + code
report = \
"""Language: {0}
Total files: {1}
Total lines: {2}
Code lines: {3}({4:.2f}%)
Comment lines: {5}({6:.2f}%)
Blank lines: {7}({8:.2f}%)
"""\
.format(lang, nfiles, total, code, code*100./total, comment, comment*100./total, blank, blank*100./total)
yield Result.from_values(origin = self,
message = report,
file = filename)
12 changes: 12 additions & 0 deletions tests/general/CLOCBearTest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from bears.general.CLOCBear import CLOCBear
from coalib.testing.LocalBearTestHelper import verify_local_bear

test_file = """
int main() {
return 0;
}
"""

CLOCBearTest = verify_local_bear(CLOCBear,
valid_files=(),
invalid_files=(test_file,))

0 comments on commit bee639d

Please sign in to comment.