Created
August 19, 2011 01:59
-
-
Save bitprophet/f49a9de060810d8db8a3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): | |
from fabric.state import win32 | |
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 if win32 else [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