I want to run my fabric script locally, which will in turn, log into my server, switch user to deploy, activate the projects .virtualenv, which will change dir to the project and issue a git pull.
# here i need to switch to the virtualenv
Right now, you can do what I do, which is kludgy but works perfectly well* (this usage assumes you're using virtualenvwrapper -- which you should be -- but you can easily substitute in the rather longer 'source' call you mentioned, if not):
def task(): workon = 'workon myvenv && ' run(workon + 'git pull') run(workon + 'do other stuff, etc')
Since version 1.0, Fabric has a
prefix context manager which uses this technique so you can for example:
def task(): with prefix('workon myvenv'): run('git pull') run('do other stuff, etc')
* There are bound to be cases where using the
command1 && command2 approach may blow up on you, such as when
command1 fails (
command2 will never run) or if
command1 isn't properly escaped and contains special shell characters, and so forth.