Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peshmerge/d17d80795cde89ec01e1a1de41dace22 to your computer and use it in GitHub Desktop.
Save peshmerge/d17d80795cde89ec01e1a1de41dace22 to your computer and use it in GitHub Desktop.
A common use case in a Fabric script is to: 1. Activate the virtualenv 2. cd to the project dir Here is a simple context manager to achieve this with a one line with statement:
"""
A common use case in a Fabric script is to:
1. Activate the virtualenv
2. cd to the project dir
Here is a simple context manager to achieve this with a one line with statement:
"""
# fabconfig.py
def dev():
env.build = 'dev'
env.virtualenv = '/path/to/ve/'
env.activate = 'source %(virtualenv)s/bin/activate' % env
env.code_dir = '/var/www/sap/gsa/builds/%(build)s' % env
# fabfile.py
from fabric.api import prefix
from contextlib import contextmanager as _contextmanager
@_contextmanager
def virtualenv():
with cd(env.virtualenv), prefix(env.activate), cd(env.code_dir):
yield
def test_management_command():
with virtualenv():
run('./manage.py solr update_all')
# local shell
fab dev solr_update_all
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment