Skip to content

Instantly share code, notes, and snippets.

@mx-moth
Created November 7, 2012 01:21
Show Gist options
  • Save mx-moth/4028948 to your computer and use it in GitHub Desktop.
Save mx-moth/4028948 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import sys
import subprocess
colour_codes = {
'none': '\033[0m',
'red': '\033[91m',
'green': '\033[92m',
'yellow': '\033[93m',
}
def colour(c, text):
return colour_codes[c] + text + colour_codes['none']
def git(args):
git = subprocess.Popen(['git'] + args, stdout = subprocess.PIPE)
return git.stdout.read().strip()
def git_diff(old, new):
return git(['diff', '--name-only', '{0}..{1}'.format(old, new)])
def run_cmd(cmd, msg=None):
if msg is not None:
print msg
return subprocess.Popen(cmd, stdout = subprocess.PIPE)
if __name__ == '__main__':
for line in sys.stdin.xreadlines():
old, new, ref = line.strip().split(' ')
print colour('red', "old: {0} new: {1} ref: {2}".format(old, new, ref))
lines = git_diff(old, new).split('\n')
# check for changes, and perform any actions required
if any("requirements.txt" in line for line in lines)
run_cmd('date', "requirements.txt has changed")
if any("/static/" in line for line in lines):
run_cmd(['manage.py', 'collectstatic'], "Static files have changed")
if any("/migrations/" in line for line in lines):
run_cmd(['manage.py', 'migrate'], "New migrations have been added")
# cleanup dead pyc files
run_cmd(['find', '.' '-name', '\*.pyc', '-delete'], 'removing stale bytecode')
# restart gunicorn
run_cmd(['sudo', 'svc', '-d', '/etc/service/PROJECT_NAME'], "Stopping server")
run_cmd(['sudo', 'svc', '-o', '/etc/service/PROJECT_NAME'], "Starting server")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment