Skip to content

Instantly share code, notes, and snippets.

@igniteflow
Last active July 30, 2018 07:36
Show Gist options
  • Save igniteflow/4654814 to your computer and use it in GitHub Desktop.
Save igniteflow/4654814 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