BAPSicle/dev/pre-commit

62 lines
1.8 KiB
Text
Raw Normal View History

2021-04-10 19:25:18 +00:00
#!/usr/bin/env python3
2020-10-24 14:23:06 +00:00
# from https://github.com/chibiegg/git-autopep8
from __future__ import with_statement, print_function
import os
import re
import subprocess
import sys
2021-09-11 16:48:57 +00:00
# See for codes https://pypi.org/project/autopep8/ #features
2020-10-24 14:23:06 +00:00
# don't fill in both of these
select_codes = []
2021-09-11 16:48:57 +00:00
# "E121", "E122", "E123", "E124", "E125", "E126", "E127", "E128", "E129", "E131", "E501"]
ignore_codes = ["E402", "E226", "E24", "W50", "W690"]
2020-10-24 14:23:06 +00:00
# Add things like "--max-line-length=120" below
2021-04-08 20:25:58 +00:00
overrides = ["--max-line-length=127"]
2020-10-24 14:23:06 +00:00
def system(*args, **kwargs):
kwargs.setdefault('stdout', subprocess.PIPE)
proc = subprocess.Popen(args, **kwargs)
2021-09-11 16:48:57 +00:00
out, _ = proc.communicate()
2020-10-24 14:23:06 +00:00
return out
def autopep8(filepath):
args = ['autopep8', '--in-place']
if select_codes and ignore_codes:
print(u'Error: select and ignore codes are mutually exclusive')
sys.exit(1)
elif select_codes:
args.extend(('--select', ','.join(select_codes)))
elif ignore_codes:
args.extend(('--ignore', ','.join(ignore_codes)))
args.extend(overrides)
args.append(filepath)
system(*args)
def main():
print("Running pre-commit hook.")
try:
import autopep8 as ap8
except ImportError:
print("'autopep8' is required. Please install with `pip install autopep8`.", file=sys.stderr)
exit(1)
2021-09-11 16:48:57 +00:00
modified = re.compile('^[AM]+\\s+(?P<name>.*\\.py)', re.MULTILINE)
2020-10-24 14:23:06 +00:00
basedir = system('git', 'rev-parse', '--show-toplevel').decode("utf-8").strip()
files = system('git', 'status', '--porcelain').decode("utf-8")
files = modified.findall(files)
for name in files:
filepath = os.path.join(basedir, name)
print("Processing file:", filepath)
autopep8(filepath)
system("git", "add", filepath)
if __name__ == '__main__':
main()