Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
setupvirtualenv - move user into project directory upon virtualenv activation
#!/bin/bash
# This hook is sourced after this virtualenv is activated.
cd "{project_dir}"
# Helpful django aliases
alias djr='./manage.py runserver {djr_port}'
alias djt='./manage.py test'
#!/usr/bin/env python
"""Helper for setting up virtualenv postactivate script to move user into
project directory upon virtualenv activation (and other helpers).
Example:
$ setupvirtualenv fakespotipy /Users/sam/Source/fakespotipy
Creating `/Users/sam/.virtualenvs/fakespotipy/bin/postactivate` and pointing it to `/Users/sam/Source/fakespotipy`
$ cat ~/.virtualenvs/fakespotipy/bin/postactivate
#!/bin/bash
# This hook is sourced after this virtualenv is activated.
cd "/Users/sam/Source/fakespotipy"
# Helpful django aliases
alias djr='./manage.py runserver '
alias djt='./manage.py test'
"""
import os
import stat
import sys
PATH_HOME = os.path.expanduser("~")
PATH_VIRTUALENV_BASE = os.path.join(PATH_HOME, '.virtualenvs')
PATH_HERE = os.path.dirname(os.path.realpath(__file__))
FILENAME_TEMPLATE = os.path.join(PATH_HERE, 'postactivate_template')
def usage():
print "setupvirtualenv <virtualenv name> <directory> [<django runserver port>]\n"
def main():
if len(sys.argv) < 3:
usage()
return
vname, vdir = sys.argv[1:3]
if not os.path.exists(vdir):
print "Path '%s' doesn't exist!" % vdir
return
env_path = os.path.join(PATH_VIRTUALENV_BASE, vname, 'bin')
if not os.path.exists(env_path):
print "Path `%s` doesn't exist!" % env_path
return
filename = os.path.join(env_path, 'postactivate')
result_str = "Creating `%s` and pointing it to `%s`" % (filename, vdir)
vport = ''
if len(sys.argv) >= 4:
vport = sys.argv[3]
result_str += " and djr port `%s`" % vport
with open(filename, 'w') as fp:
template = open(FILENAME_TEMPLATE, 'r').read()
template_str = template.format(project_dir=vdir, djr_port=vport)
fp.write(template_str)
os.chmod(filename, stat.S_IRWXU | stat.S_IRWXG | stat.S_IROTH | stat.S_IXOTH)
print result_str
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.