Skip to content

Instantly share code, notes, and snippets.

@twneale
Last active March 25, 2018 12:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save twneale/6855493 to your computer and use it in GitHub Desktop.
Save twneale/6855493 to your computer and use it in GitHub Desktop.
fab context manager to activate virtualenv before running command
'''See http://stackoverflow.com/a/5359988/120991
'''
from fabric.api import *
from contextlib import contextmanager
env.update(
use_ssh_config=True,
directory='/home/ubuntu/projects/thingy',
activate='source /home/ubuntu/.virtualenvs/thingy/bin/activate',
)
@contextmanager
def virtualenv():
'''Context manager to activate virtualenv.
'''
with cd(env.directory):
with prefix(env.activate):
yield
def supervisor(command, process):
with virtualenv():
run('supervisor restart something')
@Med116
Copy link

Med116 commented Dec 6, 2013

thanks for this script

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment