Last active
December 15, 2016 20:58
-
-
Save martini97/541734d43b933772dfc3b155ea6527e1 to your computer and use it in GitHub Desktop.
awesome pre-commit python
This file contains 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 | |
import fileinput | |
import os | |
import re | |
import shutil | |
import sys | |
# Checks to see if version needs to be increased | |
sys.stdin = open('/dev/tty') | |
increase = raw_input("Increase version? (y/n): ") | |
if increase.lower() == 'y': | |
# Getting the path to the setup.py dir | |
hooks = os.path.dirname(os.path.abspath(__file__)) | |
git = os.path.dirname(os.path.abspath(hooks)) | |
root = os.path.dirname(os.path.abspath(git)) | |
setup = os.path.join(root, 'setup.py') | |
# Create a backup, just in case | |
shutil.copyfile(setup, setup + '.bak') | |
# Read the file and change the version number | |
new_setup = '' | |
with open(setup, 'r') as set_file: | |
for line in set_file: | |
if 'VERSION = "' in line: | |
version_number = line.replace('VERSION = ', '').replace('"', '').\ | |
replace(",", '') | |
version_number = version_number.split('.') | |
version_number[-1] = str(int(version_number[-1]) + 1) | |
version_number = '.'.join(version_number) | |
line = 'VERSION = "' + version_number + '",' + '\n' | |
new_setup += line | |
# Rewrite the file with the new version number | |
with open(setup, 'w') as set_file: | |
set_file.write(new_setup) | |
print("Version increased, commit again.") | |
sys.exit(1) | |
flake8 = "flake8 $(git status -s | grep -E '\.py$' | cut -c 4-)" | |
flake8_code = os.WEXITSTATUS(os.system(flake8)) | |
nosetests = "nosetests" | |
nose_code = os.WEXITSTATUS(os.system(nosetests)) | |
errors = '' | |
if flake8_code == 0 and nose_code == 0: | |
sys.exit(0) | |
elif flake8_code != 0: | |
errors += 'Flake8 failed\n' | |
elif nose_code != 0: | |
errors += 'Nosetests failed\n' | |
print(errors) | |
commit = raw_input("Commit anyway? (y/n): ") | |
if commit.lower() == 'y': | |
sys.exit(0) | |
sys.exit(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment