Skip to content

Instantly share code, notes, and snippets.

@bobuss
Created September 13, 2013 13:19
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bobuss/6550630 to your computer and use it in GitHub Desktop.
Save bobuss/6550630 to your computer and use it in GitHub Desktop.
Makefile for python project continuous integration
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Copyright (C) 2012 Rodolphe Quiedeville <rodolphe@quiedeville.org>
This program 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.
This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
"""
from os import path, access, X_OK
from os import chdir
from sys import argv
from commands import getoutput
from optparse import OptionParser
VERSION = "1.2.0"
def trigger_start(string):
"""
Return true if we can begin to count
"""
if string.startswith('language,filename,blank,comment,code'):
return True
else:
return False
def trigger_stop(string):
"""
Return true if we have to stop counting
"""
if string.startswith('files,language,blank,comment,code'):
return True
else:
return False
def lang(string):
"""
Return then language name formatted
"""
langname = string.lower()
if langname == 'bourne shell':
langname = 'shell'
return langname
def namedir(string):
"""
Return the name dir "a la sloccount"
"""
if string.startswith('/'):
nmd = path.dirname(string).split('/')[1]
else:
nmd = path.dirname(string).split('/')[0]
if nmd == '.':
nmd = 'top_dir'
return nmd
def load_exclude(filename):
"""
Look if an exlude file is present
"""
optname = '--exclude-list-file'
if path.isfile(filename):
return '%s=%s' % (optname, path.abspath(filename))
else:
return ""
def readopts(cmdargs):
"""
Read options passed on command line
"""
opts = ""
parser = OptionParser()
parser.add_option("--exclude-list-file",
action="store",
type="string",
dest="exclude_filelist",
default=None)
parser.add_option("--binary",
action="store",
type="string",
dest="clocpath",
default="/usr/bin/cloc")
options = parser.parse_args(args=cmdargs)[0]
if options.exclude_filelist is not None:
opts = load_exclude(options.exclude_filelist)
if options.clocpath is not None:
if not path.isfile(options.clocpath):
exit('File does not exists : %s' % (options.clocpath))
if not access(options.clocpath, X_OK):
exit('File does not exists : %s' % (options.clocpath))
return options.clocpath, opts
def cloc_cmdline(fpath, cmdarg):
"""
Build the cloc command line
"""
(binary, cloc_opts) = readopts(cmdarg)
cmdline = "%s --csv %s --by-file-by-lang %s %s""" % (binary,
'--exclude-dir=.git',
cloc_opts,
fpath)
return cmdline
def parse_cloc(text):
"""
Parse the cloc output
"""
flag = False
output = ""
for line in text.split('\n'):
if trigger_stop(line):
flag = False
if flag:
datas = line.split(',')
output += '%s\t%s\t%s\t%s\n' % (datas[4],
lang(datas[0]),
namedir(datas[1]),
datas[1])
if trigger_start(line):
flag = True
return output
def main(arguments):
"""
Main function
"""
fpath = arguments[len(arguments) - 1]
if path.isdir(fpath):
chdir(fpath)
fpath = '.'
cloc = cloc_cmdline(fpath, arguments[1:])
text = getoutput(cloc)
return parse_cloc(text)
if __name__ == '__main__': # pragma: no cover
print main(argv)
ENV=env
SRC_CORE=src
CLOC=$(shell which cloc)
test-all: clean pep8 cloc init-coverage test-unit test-functionnal test-integration report-coverage flake8 lint clone
pep8:
$(ENV)/bin/pep8 --max-line-length=99 $(SRC_CORE) > pep8.log || :
cloc:
$(ROOT)/clokins.py --binary=$(CLOC) $(SRC_CORE) > sloccount.sc || :
init-coverage:
$(ENV)/bin/coverage erase
test-unit:
$(ENV)/bin/coverage run -a --source=$(SRC_CORE) $(ENV)/bin/nosetests --with-xunit --xunit-file=xunit_unit.xml tests/unit_tests/ || :
test-functionnal:
$(ENV)/bin/coverage run -a --source=$(SRC_CORE) $(ENV)/bin/nosetests --with-xunit --xunit-file=xunit_func.xml tests/func_tests/ || :
test-integration:
$(ENV)/bin/coverage run -a --source=$(SRC_CORE) $(ENV)/bin/nosetests --with-xunit --xunit-file=xunit_integration.xml tests/integration_tests/ || :
report-coverage:
$(ENV)/bin/coverage xml -o coverage.xml
flake8:
$(ENV)/bin/flake8 --max-line-length 99 --max-complexity 10 $(SRC_CORE) > pyflakes.log || :
lint:
find $(SRC_CORE) -name "*.py" | egrep -v 'tests' | xargs $(ENV)/bin/pylint --rcfile=.pylintrc --output-format=parseable --reports=y > pylint.log || :
clone:
$(ENV)/bin/clonedigger --cpd-output tcg --ignore-dir=tests -o output.xml || :
clean:
find $(SRC_CORE) -name "*.pyc" | xargs rm
rm -f pep8.log
rm -f pyflakes.log
rm -f pylint.log
rm -f sloccount.sc
rm -f output.xml
rm -f coverage.xml
rm -f xunit*.xml
rm -rf cover
flake8==2.0
flake8-immediate==0.2
pep8==1.4.5
pep8-naming==0.2.1
pyflakes==0.6.1
pylint==0.27.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment