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. ** **
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.
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.
The procedure is also on the travis-ci tutorial page.
.travis.yml
as root in the repository.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.
After completing various settings, try pushing to github. Then, the following log appeared in the log of travis-ci. (Excerpt)
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.
Correct the stuck part and push again
helloworld.py
print("hello", end="") # hello
print("world") # world
log
$ pycodestyle .
The command "pycodestyle ." exited with 0.
Yes! It looks like it's a beautiful code.
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).
Sakura Knowledge Tutorial page for travis-ci
Recommended Posts