Automatically check Python scripts with GitHub + Travis-CI + pycodestyle

Automatic check with travis-ci + pycodestyle

This article is the third day of the DSL Advent Calendar.

The content is like trying to automatically check if the Python code conforms to PEP8 every time you push to the repository using the travis-ci function. I had the impression that there was no Japanese article trying to do something like this before, so I decided to write it this time. ** Please note that this is an automatic check of pycodestyle and does not automatically reject push or merge. ** **

What is travis-ci

Sakura Knowledge is easy to understand. Roughly excerpted, the following can be easily executed with the automatic test execution service linked with GitHub.

--Automatically check out the source code from the set GitHub repository and execute the pre-specified build and test process --Notify developers of test results on the Travis CI site, various APIs, emails, etc. --If the test completes successfully and there is no problem, deploy the software to the hosting service specified in advance.

It's convenient.

What is pycodestyle

There is a Python code convention called PEP8, and it is a library that checks whether the code conforms to that convention. I think that other articles will be helpful for detailed talk about the output results of PEP8 and pycodestyle.

Try using

Installation procedure

The procedure is also on the travis-ci tutorial page.

  1. Go to Travis-ci.com and link with GitHub Account Registration
  2. Press the Activate button to register the repository for which you want to use the function.
  3. Place .travis.yml as root in the repository.
  4. Push to remote repository
  5. Check the result with travis-ci

Here is a brief summary.

.travis.yml Describe what kind of processing is performed in the contents of .travis.yml. This time I want to check the code with pycodestyle of python package, so it looks like the following

.tavis.yml


language: python3
cache: pip
sudo: false
install:
  - pip install pycodestyle
script:
  - pycodestyle .

sample code Check with the simple code below to see how it works.

helloworld.py


print("hello",end="")  # hello
print("world") # world

By the way, in this code, Line 1: There is no space after the ",". 2nd line: There is only one space before the "#" in the comment text (correctly two). Does not comply with PEP8, so please detect it automatically.

Operation check

Before correction

After completing various settings, try pushing to github. Then, the following log appeared in the log of travis-ci. (Excerpt)

スクリーンショット 2019-12-02 18.12.20.png

log



$ pycodestyle .
./helloworld.py:1:14: E231 missing whitespace after ','
./helloworld.py:2:15: E261 at least two spaces before inline comment
The command "pycodestyle ." exited with 1.
store build cache

Done. Your build exited with 1.

When you push, the pycodestyle check starts. If it gets caught, it will be returned with status code 1, so it seems that it is build faild. After all it seems that two points do not conform to PEP8. to correct.

Revised

Correct the stuck part and push again

helloworld.py


print("hello", end="")  # hello
print("world")  # world

スクリーンショット 2019-12-02 18.14.13.png

log


$ pycodestyle .
The command "pycodestyle ." exited with 0.

Yes! It looks like it's a beautiful code.

Summary

Automatic check is now added every time the Python code is updated using travis-ci and pycodestyle. If you want to fix it automatically, you may find it easier to maintain the code by using a module such as autopep8. When developing by yourself, you may not care much about the cleanliness of the code, but in reality you can not say so, so I think you should be careful (boomerang).

reference

Sakura Knowledge Tutorial page for travis-ci

Recommended Posts

Automatically check Python scripts with GitHub + Travis-CI + pycodestyle
Domain check with Python
Check version with python
Check python coverage with pytest-cov
Utilize Python custom scripts with StackStorm
Automatically build Python documentation with Sphinx
Check installed modules from Python scripts
[Python] Easy argument type check with dataclass
Check the existence of the file with python
Check stock prices with slackbot using python
Automatically aggregate JCG deck distribution with Python
[Python] Automatically operate the browser with Selenium
I'm using tox and Python 3.3 with Travis-CI
Make Python scripts into Windows-executable .exes with Pyinstaller
Automatically search and download YouTube videos with Python
Read data with python / netCDF> nc.variables [] / Check data size
Check the date of the flag duty with Python
Automatically format Python code into PEP8-compliant code with Emacs
Try to automatically generate Python documents with Sphinx
FizzBuzz with Python3
Scraping with Python
Python nan check
Statistics with python
python grammar check
gevent with travis-ci
Scraping with Python
Python with Go
Twilio with Python
Integrate with Python
Play with 2016-Python
AES256 with python
Tested with Python
python starts with ()
with syntax (Python)
Bingo with python
Zundokokiyoshi with python
Upload as open data using CKAN API in Python & automatically link with Github Actions
Excel with Python
Microcomputer with Python
Cast with python
Automatically paste images into PowerPoint materials with python + α
Use Cursur that closes automatically with sqlite3 in Python
I tried to automatically generate a password with Python3
Easily enter Azure LUIS learning texts with Python scripts
Automatically translate DeepL into English with Python and Selenium