-
-
Save piojanu/50aefcef8d1b1d3e0761c6beea3a6336 to your computer and use it in GitHub Desktop.
Applies autopep8 on all staged files and verify them with pycodestyle
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from __future__ import with_statement, print_function | |
import os | |
import re | |
import shutil | |
import subprocess | |
import sys | |
import tempfile | |
def system(*args, **kwargs): | |
kwargs.setdefault('stdout', subprocess.PIPE) | |
proc = subprocess.Popen(args, **kwargs) | |
out, err = proc.communicate() | |
return out | |
def apply_autopep8(filepath): | |
args = ['autopep8', '--in-place'] | |
args.append(filepath) | |
return system(*args) | |
def verify_pycodestyle(filepath): | |
args = ['pycodestyle'] | |
args.append(filepath) | |
return system(*args) | |
def main(): | |
try: | |
import autopep8 | |
except ImportError: | |
print("'autopep8' is required. Please install with `pip install autopep8`.", file=sys.stderr) | |
exit(1) | |
try: | |
import pycodestyle | |
except ImportError: | |
print("'pycodestyle' is required. Please install with `pip install pycodestyle`.", file=sys.stderr) | |
exit(1) | |
modified = re.compile('^[AM]+\s+(?P<name>.*\.py)', re.MULTILINE) | |
basedir = system('git', 'rev-parse', '--show-toplevel').decode("utf-8").strip() | |
files = system('git', 'status', '--porcelain').decode("utf-8") | |
files = modified.findall(files) | |
clear = True | |
for name in files: | |
filepath = os.path.join(basedir, name) | |
output = apply_autopep8(filepath) | |
if len(output) != 0: | |
print("Autopep8: \n", output, file=sys.stderr) | |
clear = False | |
else: | |
system("git", "add", filepath) | |
output = verify_pycodestyle(filepath) | |
if len(output) != 0: | |
print("Pycodestyle: \n", output, file=sys.stderr) | |
clear = False | |
if not clear: | |
exit(1) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment