#!/bin/sh -xefu
#
# Copyright (c) 2013, 2014, 2015 Intel, Inc.
#
# 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; version 2 of the License
#
# 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.

src="$1"
reports_dir="$2"

# Run pylint
mkdir -p ~/.pylint.d/
rm -f $reports_dir/pylint.log
export PYTHONPATH=$src
for path in `find $src -name \*.py`; do
    # Do not try checking empty "__init__.py" files
    if [ "${path##*/}" = "__init__.py" ] && ! [ -s "$path" ]; then
        continue
    fi

    pylint --output-format=parseable --reports=y --disable=I0011 $path >> $reports_dir/pylint.log
done || :

cd $src
if [ -d tests ] ; then
    if [ -n "`find tests -type f -name '*.py'`" ]; then
        # Run nosetests with coverage support
        nosetests -v --with-coverage --with-xunit

        # Run coverage
        coverage=$(which coverage || which python-coverage)
        $coverage xml
    fi
elif [ -f Makefile ] ; then
    if make -s -n test ; then
        make test
    fi
fi

# Move reports
for fn in nosetests.xml coverage.xml ; do
    [ -f "$fn" ] && mv $fn $reports_dir
done

echo 'Done'
