Skip to content

Instantly share code, notes, and snippets.

@MichaelCurrie
Last active April 26, 2021 17:18
Show Gist options
  • Select an option

  • Save MichaelCurrie/802ce28c993ff2dd632c to your computer and use it in GitHub Desktop.

Select an option

Save MichaelCurrie/802ce28c993ff2dd632c to your computer and use it in GitHub Desktop.
Automatically fix PEP-8 issues using Travis-CI

Automatically fix PEP-8 issues using Travis-CI

PEP-8 is a set of Python style recommendations. pep8 is a module that checks your .py file for violations. To make your Travis-CI build fail if you have any violations, you could add these lines to your .travis.yml:

before_install:
    - pip install pep8
    
script:
    # Run pep8 on all .py files in all subfolders
    # (I ignore "E402: module level import not at top of file"
    # because of use case sys.path.append('..'); import <module>)
    - find . -name \*.py -exec pep8 --ignore=E402 {} +

However, since this makes my build fail for very trivial violations, like extra whitespace after a line of code, I find it isn't practical to include this check unless I also automatically fix any issues. autopep8(https://pypi.python.org/pypi/autopep8) can perform this fix; now let's get Travis-CI to run it after every build, and if there are corrections, commit and push these changes.

language: python
python:
    - 2.7
    - 3.3
    - 3.4
    - 3.5
notifications:
    email: false

before_install:
    - sudo apt-get update
    - sudo apt-get -y install python-pip
    - sudo pip install --upgrade pip
    - pip install --upgrade pip
    - pip install pep8
    - pip install autopep8

script:
    # Run pep8 on all .py files in all subfolders
    # We must ignore E402 module level import not at top of file
    # because of use case sys.path.append('..'); import <module>
    - num_errors_before=`find . -name \*.py -exec pep8 --ignore=E402 {} + | wc -l`
    - echo $num_errors_before

    - cd "$TRAVIS_BUILD_DIR"
    - git config --global user.email "[email protected]"
    # From https://help.github.com/articles/setting-your-username-in-git/:
    # "Tip: You don't have to use your real name--any name works. Git 
    # actually associates commits by email address; the username is only 
    # used for identification. If you use your email address associated 
    # with a GitHub account, we'll use your GitHub username, instead of 
    # this name.
    - git config --global user.name "Travis"
    - git checkout $TRAVIS_BRANCH

    - find . -name \*.py -exec autopep8 --recursive --aggressive --aggressive --in-place {} +
    - num_errors_after=`find . -name \*.py -exec pep8 --ignore=E402 {} + | wc -l`
    - echo $num_errors_after

    - |
        if (( $num_errors_after < $num_errors_before )); then
            git commit -a -m "PEP-8 Fix"
            git config --global push.default simple # Push only to the current branch.  
            # Make sure to make the output quiet, or else the API token will 
            # leak!  This works because the API key can replace your password.
            git push --quiet
        fi
    - cd "$TRAVIS_BUILD_DIR"

    # List the remaining errors - these will have to be fixed manually
    - find . -name \*.py -exec pep8 --ignore=E402 {} +
@leoyyoung
Copy link

Hi,

Glad to see your travis config with pep8.
I tried your config but I found if there are errors, the travis does not show "Failed", see console log here:
Do you meet this issue? Thx

./src/base/BatmanSBApi.py:12:1: E302 expected 2 blank lines, found 1
./src/base/BatmanSBApi.py:21:1: E302 expected 2 blank lines, found 1
./src/base/BatmanSBApi.py:23:1: W293 blank line contains whitespace
2 E302 expected 2 blank lines, found 1
1 W293 blank line contains whitespace
The command "find . -name *.py -exec pep8 --statistics {} ;" exited with 0.
Done. Your build exited with 0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment