Skip to content

Instantly share code, notes, and snippets.

@bitprophet
Created August 19, 2011 01:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bitprophet/2add11c2971d7f51f7cb to your computer and use it in GitHub Desktop.
Save bitprophet/2add11c2971d7f51f7cb to your computer and use it in GitHub Desktop.
from fabric.api import *
import subprocess
def demo():
cmd = r'dir "sub directory with spaces"'
print('Command: {}'.format(cmd))
mylocal(cmd, capture=False)
def mylocal(command, capture):
real_command = command
# By default, capture both stdout and stderr
PIPE = subprocess.PIPE
out_stream = PIPE
err_stream = PIPE
# Tie in to global output controls as best we can; our capture argument
# takes precedence over the output settings.
if not capture:
if output.stdout:
out_stream = None
if output.stderr:
err_stream = None
p = subprocess.Popen(real_command, shell=True, stdout=out_stream,
stderr=err_stream)
(stdout, stderr) = p.communicate()
# Handle error condition (deal with stdout being None, too)
class _AttributeString(str): pass
out = _AttributeString(stdout.strip() if stdout else "")
err = _AttributeString(stderr.strip() if stderr else "")
out.failed = False
out.stderr = err
out.return_code = p.returncode
if p.returncode != 0:
out.failed = True
msg = "local() encountered an error (return code %s) while executing '%s'" % (p.returncode, command)
_handle_failure(message=msg)
out.succeeded = not out.failed
# If we were capturing, this will be a string; otherwise it will be None.
return out
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment