Skip to content

Commit e6a857c

Browse files
authored
[WIP] ENH flake8 (#206)
* first try from sklearn * Add script for flake8 * Update the makefile for local check * solve the install issue * see where is the flake8 file issue * source flake8 script before changing directory * Update the Makefile and change a doc * Test the doc as well
1 parent db45249 commit e6a857c

File tree

5 files changed

+181
-20
lines changed

5 files changed

+181
-20
lines changed

.travis.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ env:
3232
NUMPY_VERSION="1.9.3" SCIPY_VERSION="0.16.0"
3333
- DISTRIB="conda" PYTHON_VERSION="3.5" COVERAGE="true"
3434
NUMPY_VERSION="1.10.4" SCIPY_VERSION="0.17.0"
35+
# flake8 linting on diff wrt common ancestor with upstream/master
36+
- RUN_FLAKE8="true" SKIP_TESTS="true"
37+
DISTRIB="conda" PYTHON_VERSION="3.5"
38+
NUMPY_VERSION="1.10.4" SCIPY_VERSION="0.17.0"
3539

3640
install: source build_tools/travis/install.sh
3741
script: bash build_tools/travis/test_script.sh

Makefile

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,30 @@ clean:
1616
rm -rf doc/modules
1717
rm -rf examples/.ipynb_checkpoints
1818

19-
test:
19+
test-code:
2020
$(NOSETESTS) -s -v imblearn
2121

22-
coverage:
22+
test-doc:
23+
ifeq ($(BITS),64)
24+
$(NOSETESTS) -s -v doc/*.rst
25+
endif
26+
27+
test-coverage:
28+
rm -rf coverage .coverage
2329
$(NOSETESTS) imblearn -s -v --with-coverage --cover-package=imblearn
2430

31+
test: test-coverage test-doc
32+
2533
html:
2634
conda install -y sphinx sphinx_rtd_theme numpydoc
2735
export SPHINXOPTS=-W; make -C doc html
2836

2937
conda:
3038
conda-build conda-recipe
39+
40+
code-analysis:
41+
flake8 imblearn | grep -v __init__
42+
pylint -E imblearn/ -d E1103,E0611,E1101
43+
44+
flake8-diff:
45+
./build_tools/travis/flake8_diff.sh

build_tools/travis/flake8_diff.sh

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
#!/bin/bash
2+
3+
# This script is used in Travis to check that PRs do not add obvious
4+
# flake8 violations. It relies on two things:
5+
# - find common ancestor between branch and
6+
# scikit-learn/scikit-learn remote
7+
# - run flake8 --diff on the diff between the branch and the common
8+
# ancestor
9+
#
10+
# Additional features:
11+
# - the line numbers in Travis match the local branch on the PR
12+
# author machine.
13+
# - ./build_tools/travis/flake8_diff.sh can be run locally for quick
14+
# turn-around
15+
16+
set -e
17+
# pipefail is necessary to propagate exit codes
18+
set -o pipefail
19+
20+
PROJECT=scikit-learn-contrib/imbalanced-learn
21+
PROJECT_URL=https://github.com/$PROJECT.git
22+
23+
# Find the remote with the project name (upstream in most cases)
24+
REMOTE=$(git remote -v | grep $PROJECT | cut -f1 | head -1 || echo '')
25+
26+
# Add a temporary remote if needed. For example this is necessary when
27+
# Travis is configured to run in a fork. In this case 'origin' is the
28+
# fork and not the reference repo we want to diff against.
29+
if [[ -z "$REMOTE" ]]; then
30+
TMP_REMOTE=tmp_reference_upstream
31+
REMOTE=$TMP_REMOTE
32+
git remote add $REMOTE $PROJECT_URL
33+
fi
34+
35+
echo "Remotes:"
36+
echo '--------------------------------------------------------------------------------'
37+
git remote --verbose
38+
39+
# Travis does the git clone with a limited depth (50 at the time of
40+
# writing). This may not be enough to find the common ancestor with
41+
# $REMOTE/master so we unshallow the git checkout
42+
if [[ -a .git/shallow ]]; then
43+
echo -e '\nTrying to unshallow the repo:'
44+
echo '--------------------------------------------------------------------------------'
45+
git fetch --unshallow
46+
fi
47+
48+
if [[ "$TRAVIS" == "true" ]]; then
49+
if [[ "$TRAVIS_PULL_REQUEST" == "false" ]]
50+
then
51+
# In main repo, using TRAVIS_COMMIT_RANGE to test the commits
52+
# that were pushed into a branch
53+
if [[ "$PROJECT" == "$TRAVIS_REPO_SLUG" ]]; then
54+
if [[ -z "$TRAVIS_COMMIT_RANGE" ]]; then
55+
echo "New branch, no commit range from Travis so passing this test by convention"
56+
exit 0
57+
fi
58+
COMMIT_RANGE=$TRAVIS_COMMIT_RANGE
59+
fi
60+
else
61+
# We want to fetch the code as it is in the PR branch and not
62+
# the result of the merge into master. This way line numbers
63+
# reported by Travis will match with the local code.
64+
LOCAL_BRANCH_REF=travis_pr_$TRAVIS_PULL_REQUEST
65+
# In Travis the PR target is always origin
66+
git fetch origin pull/$TRAVIS_PULL_REQUEST/head:refs/$LOCAL_BRANCH_REF
67+
fi
68+
fi
69+
70+
# If not using the commit range from Travis we need to find the common
71+
# ancestor between $LOCAL_BRANCH_REF and $REMOTE/master
72+
if [[ -z "$COMMIT_RANGE" ]]; then
73+
if [[ -z "$LOCAL_BRANCH_REF" ]]; then
74+
LOCAL_BRANCH_REF=$(git rev-parse --abbrev-ref HEAD)
75+
fi
76+
echo -e "\nLast 2 commits in $LOCAL_BRANCH_REF:"
77+
echo '--------------------------------------------------------------------------------'
78+
git log -2 $LOCAL_BRANCH_REF
79+
80+
REMOTE_MASTER_REF="$REMOTE/master"
81+
# Make sure that $REMOTE_MASTER_REF is a valid reference
82+
echo -e "\nFetching $REMOTE_MASTER_REF"
83+
echo '--------------------------------------------------------------------------------'
84+
git fetch $REMOTE master:refs/remotes/$REMOTE_MASTER_REF
85+
LOCAL_BRANCH_SHORT_HASH=$(git rev-parse --short $LOCAL_BRANCH_REF)
86+
REMOTE_MASTER_SHORT_HASH=$(git rev-parse --short $REMOTE_MASTER_REF)
87+
88+
COMMIT=$(git merge-base $LOCAL_BRANCH_REF $REMOTE_MASTER_REF) || \
89+
echo "No common ancestor found for $(git show $LOCAL_BRANCH_REF -q) and $(git show $REMOTE_MASTER_REF -q)"
90+
91+
if [ -z "$COMMIT" ]; then
92+
exit 1
93+
fi
94+
95+
COMMIT_SHORT_HASH=$(git rev-parse --short $COMMIT)
96+
97+
echo -e "\nCommon ancestor between $LOCAL_BRANCH_REF ($LOCAL_BRANCH_SHORT_HASH)"\
98+
"and $REMOTE_MASTER_REF ($REMOTE_MASTER_SHORT_HASH) is $COMMIT_SHORT_HASH:"
99+
echo '--------------------------------------------------------------------------------'
100+
git show --no-patch $COMMIT_SHORT_HASH
101+
102+
COMMIT_RANGE="$COMMIT_SHORT_HASH..$LOCAL_BRANCH_SHORT_HASH"
103+
104+
if [[ -n "$TMP_REMOTE" ]]; then
105+
git remote remove $TMP_REMOTE
106+
fi
107+
108+
else
109+
echo "Got the commit range from Travis: $COMMIT_RANGE"
110+
fi
111+
112+
echo -e '\nRunning flake8 on the diff in the range' "$COMMIT_RANGE" \
113+
"($(git rev-list $COMMIT_RANGE | wc -l) commit(s)):"
114+
echo '--------------------------------------------------------------------------------'
115+
116+
check_files() {
117+
files="$1"
118+
options="$2"
119+
# Conservative approach: diff without context (--unified=0) so that code
120+
# that was not changed does not create failures
121+
git diff --unified=0 $COMMIT_RANGE -- $files | flake8 --diff --show-source $options
122+
}
123+
124+
echo -e "No problem detected by flake8\n"

build_tools/travis/install.sh

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ popd
2626
# provided versions
2727
conda create -n testenv --yes python=$PYTHON_VERSION pip nose \
2828
numpy=$NUMPY_VERSION scipy=$SCIPY_VERSION \
29-
libgfortran nomkl
29+
libgfortran nomkl flake8
3030
#source activate testenv
3131

3232
# Install nose-timer via pip
@@ -36,7 +36,7 @@ pip install nose-timer
3636
conda install --yes libgfortran \
3737
numpy=1.10.4 scipy=0.17.1 \
3838
scikit-learn=0.17.1 \
39-
six=1.10.0
39+
six=1.10.0
4040

4141
if [[ "$COVERAGE" == "true" ]]; then
4242
pip install coverage coveralls
@@ -50,3 +50,7 @@ python -c "import scipy; print('scipy %s' % scipy.__version__)"
5050

5151
cd $TRAVIS_BUILD_DIR
5252
python setup.py develop
53+
54+
if [[ "$RUN_FLAKE8" == "true" ]]; then
55+
conda install --yes flake8
56+
fi

build_tools/travis/test_script.sh

100644100755
Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,36 @@
88

99
set -e
1010

11-
# Get into a temp directory to run test from the installed scikit learn and
12-
# check if we do not leave artifacts
13-
mkdir -p $TEST_DIR
14-
# We need the setup.cfg for the nose settings
15-
cp setup.cfg $TEST_DIR
16-
cd $TEST_DIR
17-
18-
python --version
19-
python -c "import numpy; print('numpy %s' % numpy.__version__)"
20-
python -c "import scipy; print('scipy %s' % scipy.__version__)"
21-
python -c "import multiprocessing as mp; print('%d CPUs' % mp.cpu_count())"
22-
23-
if [[ "$COVERAGE" == "true" ]]; then
24-
nosetests -v -s --with-coverage --cover-package=$MODULE $MODULE
25-
else
26-
nosetests -v -s $MODULE
11+
run_tests(){
12+
# Get into a temp directory to run test from the installed scikit learn and
13+
# check if we do not leave artifacts
14+
mkdir -p $TEST_DIR
15+
# We need the setup.cfg for the nose settings
16+
cp setup.cfg $TEST_DIR
17+
cd $TEST_DIR
18+
19+
python --version
20+
python -c "import numpy; print('numpy %s' % numpy.__version__)"
21+
python -c "import scipy; print('scipy %s' % scipy.__version__)"
22+
python -c "import multiprocessing as mp; print('%d CPUs' % mp.cpu_count())"
23+
24+
if [[ "$COVERAGE" == "true" ]]; then
25+
nosetests -v -s --with-coverage --cover-package=$MODULE $MODULE
26+
else
27+
nosetests -v -s $MODULE
28+
fi
29+
30+
# Test doc
31+
cd $OLDPWD
32+
make test-doc
33+
}
34+
35+
if [[ "$RUN_FLAKE8" == "true" ]]; then
36+
source build_tools/travis/flake8_diff.sh
37+
fi
38+
39+
if [[ "$SKIP_TESTS" != "true" ]]; then
40+
run_tests
2741
fi
2842

2943
# Is directory still empty ?

0 commit comments

Comments
 (0)