I am using the Python library, Fabric, to do some remote server maintenance. Fabric automatically outputs all of the responses to remote and local commands unless you wrap the command in a couple with statements. Like so, on a local machine,
with settings(warn_only='true'):
with hide('running', 'stdout', 'stderr', 'warnings'):
output = local("uname -a", True)
or like this on a remote machine:
with settings(warn_only='true'):
with hide('running', 'stdout', 'stderr', 'warnings'):
output = run("uname -a")
I am writing a long and complex task and find myself repeating those two with statements over and over again. I want to write a function called _mute() to prevent that repetition. It would let me do something like this:
def _mute(fabric_cmd, args):
with settings(warn_only='true'):
with hide('running', 'stdout', 'stderr', 'warnings'):
output = fabric_cmd(args)
return output
def some_remote_task():
# Run a remote task silently
_mute(remote, 'uname -a')
def some_local_task():
# Run a local task silently
_mute(local, 'uname -a', True)
I've looked into some solutions and know that "eval" could do this for me. But every page I read about eval suggests that it's almost always a bad idea because of security issues. I looked into partials, but I couldn't figure out how to make an argument in my _mute function callable. I'm guessing there's a higher level Python concept I'm missing here. What's the pythonic way to go about doing this? Thanks for any direction you might be able to provide.