Skip to content

Instantly share code, notes, and snippets.



Created May 13, 2011
What would you like to do?
sudo -H for Fabric
import fabric.api
from contextlib import contextmanager
def shell(new_shell):
old_shell, =, new_shell
yield = old_shell
def sudo(*args, **kwargs):
if not kwargs.get('use_home', True):
return fabric.api.sudo(*args, **kwargs)
# simulate 'sudo -H'
user = kwargs.get('user', 'root')
with shell("HOME=~%s %s" % (user,
return fabric.api.sudo(*args, **kwargs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment