Skip to content

Instantly share code, notes, and snippets.

@skliarpawlo
Created February 3, 2015 11:01
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 skliarpawlo/d506a6f91a7d58b0b92f to your computer and use it in GitHub Desktop.
Save skliarpawlo/d506a6f91a7d58b0b92f to your computer and use it in GitHub Desktop.
pep8ify script
"""
Script auto-formats all files you changed.
pep8ify.py [commit_from, [commit_to]]
- If no parameters passed it formats all files added for commit
- If one parameter passed (it should be link to commit), and it will format all files changed there, like for: commit_from~1..commit_from
- If two parameter passed it should be link to commit, and it will format all files changed there, like for: commit_from..commit_to
If you want to use it for automatically, create file .git/hooks/pre-commit with content:
#!/usr/bin/env bash
python pep8ify.py
And run git commands under python virtual env with autopep8 installed
"""
import sys
from fabric.operations import local
import os
print("UNISPORT HOOK: I'll prettify your ugly code, dude")
if len(sys.argv) == 1:
output = local("git diff --cached --name-only", capture=True)
elif len(sys.argv) == 2:
output = local(
"git diff --name-only {commit}~1..{commit}".format(commit=sys.argv[1]), capture=True)
elif len(sys.argv) == 3:
output = local(
"git diff --name-only {commit_from}..{commit_to}".format(
commit_from=sys.argv[1],
commit_to=sys.argv[2]),
capture=True)
output = output.strip()
if not output:
print("Nothing to autoformat!")
exit(0)
for file_path in output.split("\n"):
if os.path.exists(file_path) and file_path[-3:] == '.py':
print("File aggressively autoformatted: {file_path}".format(
file_path=file_path
))
local(
"autopep8 --in-place --aggressive {file_path}".format(file_path=file_path))
local("git add {file_path}".format(
file_path=file_path
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment