|
1 | 1 | #!/bin/bash |
2 | 2 |
|
3 | | -# Inspired from https://github.com/scikit-learn/scikit-learn/blob/master/build_tools/travis/flake8_diff.sh |
4 | | - |
5 | | -# This script is used in Travis to check that PRs do not add obvious |
6 | | -# flake8 violations. It relies on two things: |
7 | | -# - find common ancestor between branch and |
8 | | -# openml/openml-python remote |
9 | | -# - run flake8 --diff on the diff between the branch and the common |
10 | | -# ancestor |
11 | | -# |
12 | | -# Additional features: |
13 | | -# - the line numbers in Travis match the local branch on the PR |
14 | | -# author machine. |
15 | | -# - ./ci_scripts/flake8_diff.sh can be run locally for quick |
16 | | -# turn-around |
17 | | - |
18 | | -set -e |
19 | | -# pipefail is necessary to propagate exit codes |
20 | | -set -o pipefail |
21 | | - |
22 | | -PROJECT=openml/openml-python |
23 | | -PROJECT_URL=https://github.com/$PROJECT.git |
24 | | - |
25 | | -# Find the remote with the project name (upstream in most cases) |
26 | | -REMOTE=$(git remote -v | grep $PROJECT | cut -f1 | head -1 || echo '') |
27 | | - |
28 | | -# Add a temporary remote if needed. For example this is necessary when |
29 | | -# Travis is configured to run in a fork. In this case 'origin' is the |
30 | | -# fork and not the reference repo we want to diff against. |
31 | | -if [[ -z "$REMOTE" ]]; then |
32 | | - TMP_REMOTE=tmp_reference_upstream |
33 | | - REMOTE=$TMP_REMOTE |
34 | | - git remote add $REMOTE $PROJECT_URL |
35 | | -fi |
36 | | - |
37 | | -echo "Remotes:" |
38 | | -echo '--------------------------------------------------------------------------------' |
39 | | -git remote --verbose |
40 | | - |
41 | | -echo "Travis variables:" |
42 | | -echo '--------------------------------------------------------------------------------' |
43 | | -echo "On travis: $TRAVIS" |
44 | | -echo "Current branch: $TRAVIS_BRANCH" |
45 | | -echo "Is a pull request test: $TRAVIS_PULL_REQUEST" |
46 | | -echo "Repository: $TRAVIS_REPO_SLUG" |
47 | | - |
48 | | -# Travis does the git clone with a limited depth (50 at the time of |
49 | | -# writing). This may not be enough to find the common ancestor with |
50 | | -# $REMOTE/develop so we unshallow the git checkout |
51 | | -if [[ -a .git/shallow ]]; then |
52 | | - echo -e '\nTrying to unshallow the repo:' |
53 | | - echo '--------------------------------------------------------------------------------' |
54 | | - git fetch --unshallow |
55 | | -fi |
56 | | - |
57 | | -if [[ "$TRAVIS" == "true" ]]; then |
58 | | - if [[ "$TRAVIS_BRANCH" == "master" ]] |
59 | | - then |
60 | | - # We do not test PEP8 on the master branch (or for the PR test into |
61 | | - # master) as this results in failures which are only shown for the |
62 | | - # pull request to finish a release (development to master) and are |
63 | | - # therefore a pain to fix |
64 | | - exit 0 |
65 | | - fi |
66 | | - if [[ "$TRAVIS_PULL_REQUEST" == "false" ]] |
67 | | - then |
68 | | - # In main repo, using TRAVIS_COMMIT_RANGE to test the commits |
69 | | - # that were pushed into a branch |
70 | | - if [[ "$PROJECT" == "$TRAVIS_REPO_SLUG" ]]; then |
71 | | - if [[ -z "$TRAVIS_COMMIT_RANGE" ]]; then |
72 | | - echo "New branch, no commit range from Travis so passing this test by convention" |
73 | | - exit 0 |
74 | | - fi |
75 | | - COMMIT_RANGE=$TRAVIS_COMMIT_RANGE |
76 | | - fi |
77 | | - else |
78 | | - # We want to fetch the code as it is in the PR branch and not |
79 | | - # the result of the merge into develop. This way line numbers |
80 | | - # reported by Travis will match with the local code. |
81 | | - LOCAL_BRANCH_REF=travis_pr_$TRAVIS_PULL_REQUEST |
82 | | - # In Travis the PR target is always origin |
83 | | - git fetch origin pull/$TRAVIS_PULL_REQUEST/head:refs/$LOCAL_BRANCH_REF |
84 | | - fi |
85 | | -fi |
86 | | - |
87 | | -# If not using the commit range from Travis we need to find the common |
88 | | -# ancestor between $LOCAL_BRANCH_REF and $REMOTE/develop |
89 | | -if [[ -z "$COMMIT_RANGE" ]]; then |
90 | | - if [[ -z "$LOCAL_BRANCH_REF" ]]; then |
91 | | - LOCAL_BRANCH_REF=$(git rev-parse --abbrev-ref HEAD) |
92 | | - fi |
93 | | - echo -e "\nLast 2 commits in $LOCAL_BRANCH_REF:" |
94 | | - echo '--------------------------------------------------------------------------------' |
95 | | - git --no-pager log -2 $LOCAL_BRANCH_REF |
96 | | - |
97 | | - REMOTE_DEV_REF="$REMOTE/develop" |
98 | | - # Make sure that $REMOTE_DEV_REF is a valid reference |
99 | | - echo -e "\nFetching $REMOTE_DEV_REF" |
100 | | - echo '--------------------------------------------------------------------------------' |
101 | | - git fetch $REMOTE develop:refs/remotes/$REMOTE_DEV_REF |
102 | | - LOCAL_BRANCH_SHORT_HASH=$(git rev-parse --short $LOCAL_BRANCH_REF) |
103 | | - REMOTE_DEV_SHORT_HASH=$(git rev-parse --short $REMOTE_DEV_REF) |
104 | | - |
105 | | - COMMIT=$(git merge-base $LOCAL_BRANCH_REF $REMOTE_DEV_REF) || \ |
106 | | - echo "No common ancestor found for $(git show $LOCAL_BRANCH_REF -q) and $(git show $REMOTE_DEV_REF -q)" |
107 | | - |
108 | | - if [ -z "$COMMIT" ]; then |
109 | | - exit 1 |
110 | | - fi |
111 | | - |
112 | | - COMMIT_SHORT_HASH=$(git rev-parse --short $COMMIT) |
113 | | - |
114 | | - echo -e "\nCommon ancestor between $LOCAL_BRANCH_REF ($LOCAL_BRANCH_SHORT_HASH)"\ |
115 | | - "and $REMOTE_DEV_REF ($REMOTE_DEV_SHORT_HASH) is $COMMIT_SHORT_HASH:" |
116 | | - echo '--------------------------------------------------------------------------------' |
117 | | - git --no-pager show --no-patch $COMMIT_SHORT_HASH |
118 | | - |
119 | | - COMMIT_RANGE="$COMMIT_SHORT_HASH..$LOCAL_BRANCH_SHORT_HASH" |
120 | | - |
121 | | - if [[ -n "$TMP_REMOTE" ]]; then |
122 | | - git remote remove $TMP_REMOTE |
123 | | - fi |
124 | | - |
125 | | -else |
126 | | - echo "Got the commit range from Travis: $COMMIT_RANGE" |
127 | | -fi |
128 | | - |
129 | | -echo -e '\nRunning flake8 on the diff in the range' "$COMMIT_RANGE" \ |
130 | | - "($(git rev-list $COMMIT_RANGE | wc -l) commit(s)):" |
131 | | -echo '--------------------------------------------------------------------------------' |
132 | | -# We need the following command to exit with 0 hence the echo in case |
133 | | -# there is no match |
134 | | -MODIFIED_FILES="$(git diff --no-ext-diff --name-only $COMMIT_RANGE || echo "no_match")" |
135 | | - |
136 | | -check_files() { |
137 | | - files="$1" |
138 | | - shift |
139 | | - options="$*" |
140 | | - if [ -n "$files" ]; then |
141 | | - # Conservative approach: diff without context (--unified=0) so that code |
142 | | - # that was not changed does not create failures |
143 | | - # git diff --no-ext-diff --unified=0 $COMMIT_RANGE -- $files | flake8 --ignore E402 --diff --show-source $options |
144 | | - flake8 --ignore E402,W503 --show-source --max-line-length 100 $options |
145 | | - fi |
146 | | -} |
147 | | - |
148 | | -if [[ "$MODIFIED_FILES" == "no_match" ]]; then |
149 | | - echo "No file has been modified" |
150 | | -else |
151 | | - |
152 | | - check_files "$(echo "$MODIFIED_FILES" | grep -v ^examples)" |
153 | | - check_files "$(echo "$MODIFIED_FILES" | grep ^examples)" \ |
154 | | - --config ./examples/.flake8 |
155 | | -fi |
156 | | -echo -e "No problem detected by flake8\n" |
| 3 | +flake8 --ignore E402,W503 --show-source --max-line-length 100 $options |
0 commit comments