From f16f21acb6e0db2156e05f54a31c6a75f35abdb1 Mon Sep 17 00:00:00 2001 From: ArlindKadra Date: Thu, 20 Sep 2018 11:09:41 +0100 Subject: [PATCH 1/4] First initial change --- ci_scripts/flake8_diff.sh | 145 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 145 insertions(+) create mode 100644 ci_scripts/flake8_diff.sh diff --git a/ci_scripts/flake8_diff.sh b/ci_scripts/flake8_diff.sh new file mode 100644 index 0000000000..9ba98837a2 --- /dev/null +++ b/ci_scripts/flake8_diff.sh @@ -0,0 +1,145 @@ +#!/bin/bash + +# This script is used in Travis to check that PRs do not add obvious +# flake8 violations. It relies on two things: +# - find common ancestor between branch and +# openml/openml-python remote +# - run flake8 --diff on the diff between the branch and the common +# ancestor +# +# Additional features: +# - the line numbers in Travis match the local branch on the PR +# author machine. +# - ./build_tools/travis/flake8_diff.sh can be run locally for quick +# turn-around + +set -e +# pipefail is necessary to propagate exit codes +set -o pipefail + +PROJECT=openml/openml-python +PROJECT_URL=https://github.com/$PROJECT.git + +# Find the remote with the project name (upstream in most cases) +REMOTE=$(git remote -v | grep $PROJECT | cut -f1 | head -1 || echo '') + +# Add a temporary remote if needed. For example this is necessary when +# Travis is configured to run in a fork. In this case 'origin' is the +# fork and not the reference repo we want to diff against. +if [[ -z "$REMOTE" ]]; then + TMP_REMOTE=tmp_reference_upstream + REMOTE=$TMP_REMOTE + git remote add $REMOTE $PROJECT_URL +fi + +echo "Remotes:" +echo '--------------------------------------------------------------------------------' +git remote --verbose + +# Travis does the git clone with a limited depth (50 at the time of +# writing). This may not be enough to find the common ancestor with +# $REMOTE/master so we unshallow the git checkout +if [[ -a .git/shallow ]]; then + echo -e '\nTrying to unshallow the repo:' + echo '--------------------------------------------------------------------------------' + git fetch --unshallow +fi + +if [[ "$TRAVIS" == "true" ]]; then + if [[ "$TRAVIS_PULL_REQUEST" == "false" ]] + then + # In main repo, using TRAVIS_COMMIT_RANGE to test the commits + # that were pushed into a branch + if [[ "$PROJECT" == "$TRAVIS_REPO_SLUG" ]]; then + if [[ -z "$TRAVIS_COMMIT_RANGE" ]]; then + echo "New branch, no commit range from Travis so passing this test by convention" + exit 0 + fi + COMMIT_RANGE=$TRAVIS_COMMIT_RANGE + fi + else + # We want to fetch the code as it is in the PR branch and not + # the result of the merge into master. This way line numbers + # reported by Travis will match with the local code. + LOCAL_BRANCH_REF=travis_pr_$TRAVIS_PULL_REQUEST + # In Travis the PR target is always origin + git fetch origin pull/$TRAVIS_PULL_REQUEST/head:refs/$LOCAL_BRANCH_REF + fi +fi + +# If not using the commit range from Travis we need to find the common +# ancestor between $LOCAL_BRANCH_REF and $REMOTE/develop +if [[ -z "$COMMIT_RANGE" ]]; then + if [[ -z "$LOCAL_BRANCH_REF" ]]; then + LOCAL_BRANCH_REF=$(git rev-parse --abbrev-ref HEAD) + fi + echo -e "\nLast 2 commits in $LOCAL_BRANCH_REF:" + echo '--------------------------------------------------------------------------------' + git --no-pager log -2 $LOCAL_BRANCH_REF + + REMOTE_MASTER_REF="$REMOTE/develop" + # Make sure that $REMOTE_MASTER_REF is a valid reference + echo -e "\nFetching $REMOTE_MASTER_REF" + echo '--------------------------------------------------------------------------------' + git fetch $REMOTE master:refs/remotes/$REMOTE_MASTER_REF + LOCAL_BRANCH_SHORT_HASH=$(git rev-parse --short $LOCAL_BRANCH_REF) + REMOTE_MASTER_SHORT_HASH=$(git rev-parse --short $REMOTE_MASTER_REF) + + COMMIT=$(git merge-base $LOCAL_BRANCH_REF $REMOTE_MASTER_REF) || \ + echo "No common ancestor found for $(git show $LOCAL_BRANCH_REF -q) and $(git show $REMOTE_MASTER_REF -q)" + + if [ -z "$COMMIT" ]; then + exit 1 + fi + + COMMIT_SHORT_HASH=$(git rev-parse --short $COMMIT) + + echo -e "\nCommon ancestor between $LOCAL_BRANCH_REF ($LOCAL_BRANCH_SHORT_HASH)"\ + "and $REMOTE_MASTER_REF ($REMOTE_MASTER_SHORT_HASH) is $COMMIT_SHORT_HASH:" + echo '--------------------------------------------------------------------------------' + git --no-pager show --no-patch $COMMIT_SHORT_HASH + + COMMIT_RANGE="$COMMIT_SHORT_HASH..$LOCAL_BRANCH_SHORT_HASH" + + if [[ -n "$TMP_REMOTE" ]]; then + git remote remove $TMP_REMOTE + fi + +else + echo "Got the commit range from Travis: $COMMIT_RANGE" +fi + +echo -e '\nRunning flake8 on the diff in the range' "$COMMIT_RANGE" \ + "($(git rev-list $COMMIT_RANGE | wc -l) commit(s)):" +echo '--------------------------------------------------------------------------------' + +# We ignore files from sklearn/externals. Unfortunately there is no +# way to do it with flake8 directly (the --exclude does not seem to +# work with --diff). We could use the exclude magic in the git pathspec +# ':!sklearn/externals' but it is only available on git 1.9 and Travis +# uses git 1.8. +# We need the following command to exit with 0 hence the echo in case +# there is no match +MODIFIED_FILES="$(git diff --name-only $COMMIT_RANGE | \ + grep -v 'doc/sphinxext/sphinx_gallery' || echo "no_match")" + +check_files() { + files="$1" + shift + options="$*" + if [ -n "$files" ]; then + # Conservative approach: diff without context (--unified=0) so that code + # that was not changed does not create failures + git diff --unified=0 $COMMIT_RANGE -- $files | flake8 --diff --show-source $options + fi +} + +if [[ "$MODIFIED_FILES" == "no_match" ]]; then + echo "No file outside sklearn/externals and doc/sphinxext/sphinx_gallery has been modified" +else + + check_files "$(echo "$MODIFIED_FILES" | grep -v ^examples)" + check_files "$(echo "$MODIFIED_FILES" | grep ^examples)" \ + --config ./examples/.flake8 +fi +echo -e "No problem detected by flake8\n" \ No newline at end of file From decee8290da35e779105f36be5a567c2889cfdb5 Mon Sep 17 00:00:00 2001 From: ArlindKadra Date: Thu, 20 Sep 2018 11:30:49 +0100 Subject: [PATCH 2/4] Changing notation, fixing fetch from master --- ci_scripts/flake8_diff.sh | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/ci_scripts/flake8_diff.sh b/ci_scripts/flake8_diff.sh index 9ba98837a2..94cb50a294 100644 --- a/ci_scripts/flake8_diff.sh +++ b/ci_scripts/flake8_diff.sh @@ -38,7 +38,7 @@ git remote --verbose # Travis does the git clone with a limited depth (50 at the time of # writing). This may not be enough to find the common ancestor with -# $REMOTE/master so we unshallow the git checkout +# $REMOTE/develop so we unshallow the git checkout if [[ -a .git/shallow ]]; then echo -e '\nTrying to unshallow the repo:' echo '--------------------------------------------------------------------------------' @@ -59,7 +59,7 @@ if [[ "$TRAVIS" == "true" ]]; then fi else # We want to fetch the code as it is in the PR branch and not - # the result of the merge into master. This way line numbers + # the result of the merge into develop. This way line numbers # reported by Travis will match with the local code. LOCAL_BRANCH_REF=travis_pr_$TRAVIS_PULL_REQUEST # In Travis the PR target is always origin @@ -77,16 +77,16 @@ if [[ -z "$COMMIT_RANGE" ]]; then echo '--------------------------------------------------------------------------------' git --no-pager log -2 $LOCAL_BRANCH_REF - REMOTE_MASTER_REF="$REMOTE/develop" - # Make sure that $REMOTE_MASTER_REF is a valid reference - echo -e "\nFetching $REMOTE_MASTER_REF" + REMOTE_DEV_REF="$REMOTE/develop" + # Make sure that $REMOTE_DEV_REF is a valid reference + echo -e "\nFetching $REMOTE_DEV_REF" echo '--------------------------------------------------------------------------------' - git fetch $REMOTE master:refs/remotes/$REMOTE_MASTER_REF + git fetch $REMOTE develop:refs/remotes/$REMOTE_DEV_REF LOCAL_BRANCH_SHORT_HASH=$(git rev-parse --short $LOCAL_BRANCH_REF) - REMOTE_MASTER_SHORT_HASH=$(git rev-parse --short $REMOTE_MASTER_REF) + REMOTE_DEV_SHORT_HASH=$(git rev-parse --short $REMOTE_DEV_REF) - COMMIT=$(git merge-base $LOCAL_BRANCH_REF $REMOTE_MASTER_REF) || \ - echo "No common ancestor found for $(git show $LOCAL_BRANCH_REF -q) and $(git show $REMOTE_MASTER_REF -q)" + COMMIT=$(git merge-base $LOCAL_BRANCH_REF $REMOTE_DEV_REF) || \ + echo "No common ancestor found for $(git show $LOCAL_BRANCH_REF -q) and $(git show $REMOTE_DEV_REF -q)" if [ -z "$COMMIT" ]; then exit 1 @@ -95,7 +95,7 @@ if [[ -z "$COMMIT_RANGE" ]]; then COMMIT_SHORT_HASH=$(git rev-parse --short $COMMIT) echo -e "\nCommon ancestor between $LOCAL_BRANCH_REF ($LOCAL_BRANCH_SHORT_HASH)"\ - "and $REMOTE_MASTER_REF ($REMOTE_MASTER_SHORT_HASH) is $COMMIT_SHORT_HASH:" + "and $REMOTE_DEV_REF ($REMOTE_DEV_SHORT_HASH) is $COMMIT_SHORT_HASH:" echo '--------------------------------------------------------------------------------' git --no-pager show --no-patch $COMMIT_SHORT_HASH From a55b6a13c19f7decb8997ceb4f548175c242ab1d Mon Sep 17 00:00:00 2001 From: Guillaume Lemaitre Date: Thu, 20 Sep 2018 05:42:54 -0700 Subject: [PATCH 3/4] [MRG] CI: add flake8 check in travis (#534) * CI: add flake8 check in travis * FIX: typo --- .travis.yml | 1 + ci_scripts/install.sh | 3 +++ ci_scripts/test.sh | 43 ++++++++++++++++++++++++++----------------- 3 files changed, 30 insertions(+), 17 deletions(-) diff --git a/.travis.yml b/.travis.yml index 5bbc2928e3..f08c8a3967 100644 --- a/.travis.yml +++ b/.travis.yml @@ -21,6 +21,7 @@ env: - DISTRIB="conda" PYTHON_VERSION="3.6" EXAMPLES="true" SKLEARN_VERSION="0.18.2" - DISTRIB="conda" PYTHON_VERSION="3.6" DOCTEST="true" SKLEARN_VERSION="0.18.2" # - DISTRIB="conda" PYTHON_VERSION="3.7" SKLEARN_VERSION="0.18.2" + - DISTRIB="conda" PYTHON_VERSION="3.6" SKLEARN_VERSION="0.18.2" RUN_FLAKE8="true" SKIP_TESTS="true" install: source ci_scripts/install.sh script: bash ci_scripts/test.sh diff --git a/ci_scripts/install.sh b/ci_scripts/install.sh index 098650115b..4e23056baf 100644 --- a/ci_scripts/install.sh +++ b/ci_scripts/install.sh @@ -39,6 +39,9 @@ fi if [[ "$COVERAGE" == "true" ]]; then pip install codecov pytest-cov fi +if [[ "$RUN_FLAKE8" == "true" ]]; then + pip install flake8 +fi python --version python -c "import numpy; print('numpy %s' % numpy.__version__)" diff --git a/ci_scripts/test.sh b/ci_scripts/test.sh index ba18d7b631..250b4c0619 100644 --- a/ci_scripts/test.sh +++ b/ci_scripts/test.sh @@ -1,25 +1,34 @@ set -e -# Get into a temp directory to run test from the installed scikit learn and -# check if we do not leave artifacts -mkdir -p $TEST_DIR +run_tests() { + # Get into a temp directory to run test from the installed scikit learn and + # check if we do not leave artifacts + mkdir -p $TEST_DIR -cwd=`pwd` -test_dir=$cwd/tests -doctest_dir=$cwd/doc + cwd=`pwd` + test_dir=$cwd/tests + doctest_dir=$cwd/doc -cd $TEST_DIR + cd $TEST_DIR + if [[ "$EXAMPLES" == "true" ]]; then + pytest -sv $test_dir/test_examples/ + elif [[ "$DOCTEST" == "true" ]]; then + python -m doctest $doctest_dir/usage.rst + fi -if [[ "$EXAMPLES" == "true" ]]; then - pytest -sv $test_dir/test_examples/ -elif [[ "$DOCTEST" == "true" ]]; then - python -m doctest $doctest_dir/usage.rst -fi + if [[ "$COVERAGE" == "true" ]]; then + PYTEST_ARGS='--cov=openml' + else + PYTEST_ARGS='' + fi + + pytest -n 4 --timeout=600 --timeout-method=thread -sv --ignore='test_OpenMLDemo.py' $PYTEST_ARGS $test_dir +} -if [[ "$COVERAGE" == "true" ]]; then - PYTEST_ARGS='--cov=openml' -else - PYTEST_ARGS='' +if [[ "$RUN_FLAKE8" == "true" ]]; then + source ci_scripts/flake8_diff.sh fi -pytest -n 4 --timeout=600 --timeout-method=thread -sv --ignore='test_OpenMLDemo.py' $PYTEST_ARGS $test_dir +if [[ "$SKIP_TESTS" != "true" ]]; then + run_tests +fi From 42d3c61aecf3d99492b10abcf5dd7dea42ac601f Mon Sep 17 00:00:00 2001 From: ArlindKadra Date: Thu, 20 Sep 2018 14:43:32 +0100 Subject: [PATCH 4/4] Updated accordingly --- ci_scripts/flake8_diff.sh | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/ci_scripts/flake8_diff.sh b/ci_scripts/flake8_diff.sh index 94cb50a294..90d7923ad4 100644 --- a/ci_scripts/flake8_diff.sh +++ b/ci_scripts/flake8_diff.sh @@ -1,5 +1,7 @@ #!/bin/bash +# Inspired from https://github.com/scikit-learn/scikit-learn/blob/master/build_tools/travis/flake8_diff.sh + # This script is used in Travis to check that PRs do not add obvious # flake8 violations. It relies on two things: # - find common ancestor between branch and @@ -10,7 +12,7 @@ # Additional features: # - the line numbers in Travis match the local branch on the PR # author machine. -# - ./build_tools/travis/flake8_diff.sh can be run locally for quick +# - ./ci_scripts/flake8_diff.sh can be run locally for quick # turn-around set -e @@ -112,16 +114,9 @@ fi echo -e '\nRunning flake8 on the diff in the range' "$COMMIT_RANGE" \ "($(git rev-list $COMMIT_RANGE | wc -l) commit(s)):" echo '--------------------------------------------------------------------------------' - -# We ignore files from sklearn/externals. Unfortunately there is no -# way to do it with flake8 directly (the --exclude does not seem to -# work with --diff). We could use the exclude magic in the git pathspec -# ':!sklearn/externals' but it is only available on git 1.9 and Travis -# uses git 1.8. # We need the following command to exit with 0 hence the echo in case # there is no match -MODIFIED_FILES="$(git diff --name-only $COMMIT_RANGE | \ - grep -v 'doc/sphinxext/sphinx_gallery' || echo "no_match")" +MODIFIED_FILES="$(git diff --name-only $COMMIT_RANGE || echo "no_match")" check_files() { files="$1" @@ -135,7 +130,7 @@ check_files() { } if [[ "$MODIFIED_FILES" == "no_match" ]]; then - echo "No file outside sklearn/externals and doc/sphinxext/sphinx_gallery has been modified" + echo "No file has been modified" else check_files "$(echo "$MODIFIED_FILES" | grep -v ^examples)"