Skip to content

Commit

Permalink
setup.py: new subcommand: lint
Browse files Browse the repository at this point in the history
  • Loading branch information
kernc committed Apr 1, 2016
1 parent d891a49 commit 147ba98
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 8 deletions.
22 changes: 15 additions & 7 deletions .travis/check_pylint_diff
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
# Licensed under http://www.apache.org/licenses/LICENSE-2.0
# Created By: [email protected]

set -o pipefail
set -o nounset
set -o errexit

ARG1=${1:-}
GIT_REPO="$(pwd)"
TMP_REPO="$(mktemp -d)"
TMP_REPO="$(mktemp -d pylint_diff.XXXXXXX)"
SCRIPT=$(basename "$0")
PYLINT="$(command -v pylint 2>/dev/null || true)"

trap 'status=$?; cd '$GIT_REPO'; rm -rf '$TMP_REPO'; exit $status' EXIT

Expand Down Expand Up @@ -41,14 +43,18 @@ Given the commit tree:

case $ARG1 in -h|--help) print_help ; esac

if [ ! "$PYLINT" ]; then
echo 'Error: pylint is required'
exit 3
fi

# Make a local clone: prevents copying of objects
# Handle shallow git clones
[ -f "$GIT_REPO/.git/shallow" ] &&
is_shallow=true
is_shallow=$([ -f "$GIT_REPO/.git/shallow" ] && echo true || echo)
if [ "$is_shallow" ]; then
mv "$GIT_REPO/.git/shallow" "$GIT_REPO/.git/shallow-bak"
fi
git clone -q --local "$GIT_REPO" "$TMP_REPO" 2>/dev/null
git clone -q --local --depth=50 "$GIT_REPO" "$TMP_REPO" 2>/dev/null
if [ "$is_shallow" ]; then
mv "$GIT_REPO/.git/shallow-bak" "$GIT_REPO/.git/shallow"
cp "$GIT_REPO/.git/shallow" "$TMP_REPO/.git/shallow"
Expand Down Expand Up @@ -95,8 +101,9 @@ checkout ()
n_lint_errors ()
{
echo "$CHANGED_FILES" |
xargs pylint |
awk -F'[\\. ]' '/^Your code has been rated at /{ print $7 }'
xargs "$PYLINT" |
awk -F'[\\. ]' '/^Your code has been rated at /{ print $7 }' ||
true
}

echo "Running pylint on current commit ($CURRENT_COMMIT)"
Expand All @@ -107,7 +114,8 @@ checkout $PREVIOUS_COMMIT
RESULT_PARENT=$( n_lint_errors )

checkout $CURRENT_COMMIT
echo "$CHANGED_FILES" | xargs pylint || true
echo
echo "$CHANGED_FILES" | xargs "$PYLINT" || true

echo "Pylint results"
echo "=============="
Expand Down
5 changes: 5 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ In addition, we add the following guidelines:
reason) should have a `__name__ == '__main__'`-fenced code block that
shows/tests the gist of that module in a user-friendly way.

Please ensure your commits pass code quality assurance by executing:

pip install -r requirements-dev.txt
python setup.py lint

[PEP-8]: https://www.python.org/dev/peps/pep-0008/
[Google Python Style Guide]: https://google.github.io/styleguide/pyguide.html
[Napoleon]: http://www.sphinx-doc.org/en/stable/ext/napoleon.html
Expand Down
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pylint
radon
23 changes: 22 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import os
import sys
import subprocess
from setuptools import find_packages
from setuptools import find_packages, Command

if sys.version_info < (3, 4):
sys.exit('Orange requires Python >= 3.4')
Expand Down Expand Up @@ -182,6 +182,24 @@ def configuration(parent_package='', top_path=None):
}


class LintCommand(Command):
"""A setup.py lint subcommand developers can run locally."""
description = "run code linter(s)"
user_options = []
initialize_options = finalize_options = lambda self: None

def run(self):
"""Lint current branch compared to a reasonable master branch"""
sys.exit(subprocess.call('''
set -eu
upstream=$(git rev-parse -q --verify upstream/master)
origin=$(git rev-parse -q --verify origin/master)
master=$(git rev-parse -q --verify master)
best_ancestor=$(git merge-base HEAD ${upstream:-${origin:-$master}})
.travis/check_pylint_diff $best_ancestor
''', shell=True, cwd=os.path.dirname(os.path.abspath(__file__))))


def setup_package():
write_version_py()
setup(
Expand All @@ -201,6 +219,9 @@ def setup_package():
entry_points=ENTRY_POINTS,
zip_safe=False,
test_suite='Orange.tests.test_suite',
cmdclass={
'lint': LintCommand,
},
)

if __name__ == '__main__':
Expand Down

0 comments on commit 147ba98

Please sign in to comment.