Skip to content

Instantly share code, notes, and snippets.

@afsal-parseltongue
Created April 12, 2022 10:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save afsal-parseltongue/a0dee4c25bd2b4d9616adbec56c0ebad to your computer and use it in GitHub Desktop.
Save afsal-parseltongue/a0dee4c25bd2b4d9616adbec56c0ebad to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
"""
Forked from https://gist.github.com/810399
"""
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 main():
modified = re.compile('^\s*[AM]+\s+(?P<name>.*\.py$)', re.MULTILINE)
files = system('git', 'status', '--porcelain').decode("utf-8")
files = modified.findall(files)
tempdir = tempfile.mkdtemp()
for name in files:
filename = os.path.join(tempdir, name)
filepath = os.path.dirname(filename)
if not os.path.exists(filepath):
os.makedirs(filepath)
with open(filename, 'w') as f:
system('git', 'show', ':' + name, stdout=f)
args = ['pycodestyle', '.', '--max-line-length=120', '--exclude', 'migrations']
output = system(*args, cwd=tempdir)
shutil.rmtree(tempdir)
if output:
print(u'PEP8 style violations have been detected. Please fix them\n')
print(output.decode("utf-8"),)
sys.exit(1)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment