Skip to content

Instantly share code, notes, and snippets.

@philipsd6
Created February 17, 2012 21:11
Show Gist options
  • Save philipsd6/1855468 to your computer and use it in GitHub Desktop.
Save philipsd6/1855468 to your computer and use it in GitHub Desktop.
Rewrite of Fabric contrib.files.exists to allow sudo'ing as specific user, not just root.
def exists(path, as_user=None, use_sudo=False, verbose=False):
"""
Return True if given path exists on the current remote host.
If ``as_user`` is set, will use `sudo` as that user instead of `run`. If
''as_user'' or ''use_sudo'' is True, will use 'sudo' as root.
`exists` will, by default, hide all output (including the run line, stdout,
stderr and any warning resulting from the file not existing) in order to
avoid cluttering output. You may specify ``verbose=True`` to change this
behavior.
"""
if as_user is True or use_sudo:
func = sudo
elif as_user:
func = lambda x: sudo(x, user=as_user)
else:
func = run
cmd = 'test -e "$(echo %s)"' % path
# If verbose, run normally
if verbose:
with settings(warn_only=True):
return not func(cmd).failed
# Otherwise, be quiet
with settings(hide('everything'), warn_only=True):
return not func(cmd).failed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment