Last active
August 8, 2017 22:20
-
-
Save javiromero/e778a4d220d23d6bd890 to your computer and use it in GitHub Desktop.
Python scripts to check for new/missing Django migrations and Cleaning *.pyc and *. pyo for deleted *.py files from a repo & to remove any empty dir after every checkout
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
#!/bin/env python | |
""" | |
A hook to git that removes orphan files "*.pyc" and "*.pyo" for "*.py" | |
beeing deleted or renamed by git checkout. It also removes their empty parent | |
directories. | |
Place it to "my_local_repository/.git/hooks/post-checkout" and make it executable. | |
Nothing is cleaned for .py files deleted manually or by "git rm" etc. | |
Related to http://stackoverflow.com/q/1504724/448474 | |
""" | |
import glob | |
import os | |
import sys | |
from subprocess import Popen, PIPE | |
def main(): | |
# print("HOOK POST-CHECKOUT") | |
oldrev, newrev, _ = sys.argv[1:] | |
cmd = 'git diff --name-only --diff-filter=DR {0}..{1}'.format(oldrev, newrev) | |
dirs = set() | |
p = Popen(cmd.split(), stdout=PIPE) | |
for line in p.stdout: | |
filepath = line.strip() | |
root, srcext = os.path.splitext(filepath) | |
if srcext == '.py' and not os.path.isfile(filepath): | |
parent_dir = os.path.dirname(root) | |
cache_dir = os.path.join(parent_dir, '__pycache__') | |
for ext in ('.pyc', '.pyo'): | |
if os.path.isdir(cache_dir): | |
for pycfile in glob.iglob(os.path.join(cache_dir, '*' + ext)): | |
os.remove(pycfile) | |
if not os.listdir(cache_dir): | |
os.rmdir(cache_dir) | |
dirs.add(parent_dir) | |
if os.path.isfile(root + ext): | |
os.remove(root + ext) | |
dirs.add(parent_dir) | |
p.stdout.close() | |
p.wait() | |
for pdir in sorted(dirs, key=lambda x: len(x.split(os.path.sep)), reverse=True): | |
if os.path.isdir(pdir) and not os.listdir(pdir): | |
os.rmdir(pdir) | |
if __name__ == '__main__': | |
main() |
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 | |
import sys | |
import subprocess | |
from collections import defaultdict | |
def main(): | |
# print("HOOK POST-CHECKOUT") | |
fn, previous_head, current_head, is_branch = sys.argv | |
if not is_branch == '1': | |
sys.exit(0) | |
diff = defaultdict(list) | |
output = subprocess.check_output( | |
'git --no-pager diff --name-status %s %s ' % ( | |
previous_head, current_head), shell=True) | |
for i in output.split('\n'): | |
if not i: | |
continue | |
op, fname = i.split('\t') | |
diff[op].append(fname) | |
to_revert = [] | |
for i in diff.get('D', []): | |
if 'migrations' in i: | |
to_revert.append(i) | |
to_run = [] | |
for i in diff.get('A', []): | |
if 'migrations' in i: | |
to_run.append(i) | |
if to_revert: | |
print('\nATTENTION!!!\n' | |
'You have deleted migrations in this branch.\n' | |
'You may need to return to the previous one and revert those:') | |
for i in to_revert: | |
print('\t * %s' % i) | |
print('\nPlease ignore this warning if you\'ve already done it') | |
if to_run: | |
print('You have new migrations in this branch:') | |
for i in to_run: | |
print(i) | |
print('You can see the migrations by running:') | |
print('python manage.py migrate --list') | |
if __name__ == '__main__': | |
main() |
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 | |
import subprocess | |
def main(): | |
command = 'git diff HEAD@{1} --stat | grep requirements | wc -l' | |
output = subprocess.check_output(command, shell=True) | |
if int(output) > 0: | |
print('\nATTENTION!!!\n' | |
'There has been changes to the project requirements.' | |
'Remember to update your virtual env') | |
if __name__ == '__main__': | |
main() |
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 | |
import subprocess | |
def main(): | |
command = 'git diff HEAD@{1} --stat -- mozio/settings | wc -l' | |
output = subprocess.check_output(command, shell=True) | |
if int(output) > 0: | |
print('\nATTENTION!!!\n' | |
'There has been changes to the settings folder.' | |
'Remember to redeploy the app') | |
if __name__ == '__main__': | |
main() |
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 python2 | |
import git_post_checkout_migrations | |
import git_post_checkout_clean_pyc_and_empty_dirs | |
import git_post_checkout_settings | |
git_post_checkout_migrations.main() | |
git_post_checkout_clean_pyc_and_empty_dirs.main() | |
git_post_checkout_settings.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment