def test_shell_wrap_escapes_command_if_shell_is_true(): """ _shell_wrap() escapes given command if shell=True """ cmd = "cd \"Application Support\"" eq_(_shell_wrap(cmd, shell=True), '%s "%s"' % (env.shell, _shell_escape(cmd)))
def test_shell_wrap_does_not_escape_command_if_shell_is_true_and_shell_escape_is_false( ): """ _shell_wrap() does no escaping if shell=True and shell_escape=False """ cmd = "cd \"Application Support\"" eq_(_shell_wrap(cmd, shell_escape=False, shell=True), '%s "%s"' % (env.shell, cmd))
def test_shell_wrap_does_not_escape_command_if_shell_is_true_and_shell_escape_is_false(): """ _shell_wrap() does no escaping if shell=True and shell_escape=False """ cmd = "cd \"Application Support\"" eq_( _shell_wrap(cmd, shell_escape=False, shell=True), '%s "%s"' % (env.shell, cmd) )
def test_shell_wrap_escapes_command_if_shell_is_true(): """ _shell_wrap() escapes given command if shell=True """ cmd = "cd \"Application Support\"" eq_( _shell_wrap(cmd, shell_escape=True, shell=True), '%s "%s"' % (env.shell, _shell_escape(cmd)) )
def _run_host_command(command, shell=True, pty=True, combine_stderr=True): """ Run host wrapper command as root (Modified from fabric.operations._run_command to ignore prefixes, path(), cd(), and always use sudo.) """ # Set up new var so original argument can be displayed verbatim later. given_command = command # Handle context manager modifications, and shell wrapping wrapped_command = _shell_wrap( command, shell, _sudo_prefix(None) ) # Execute info line if output.debug: print("[%s] %s: %s" % (env.host_string, 'sudo', wrapped_command)) elif output.running: print("[%s] %s: %s" % (env.host_string, 'sudo', given_command)) # Actual execution, stdin/stdout/stderr handling, and termination stdout, stderr, status = _execute(default_channel(), wrapped_command, pty, combine_stderr) # Assemble output string out = _AttributeString(stdout) err = _AttributeString(stderr) # Error handling out.failed = False if status != 0: out.failed = True msg = "%s() received nonzero return code %s while executing" % ( 'sudo', status ) if env.warn_only: msg += " '%s'!" % given_command else: msg += "!\n\nRequested: %s\nExecuted: %s" % ( given_command, wrapped_command ) error(message=msg, stdout=out, stderr=err) # Attach return code to output string so users who have set things to # warn only, can inspect the error code. out.return_code = status # Convenience mirror of .failed out.succeeded = not out.failed # Attach stderr for anyone interested in that. out.stderr = err return out
def test_shell_wrap(): prefix = "prefix" command = "command" for description, shell, sudo_prefix, result in ( ("shell=True, sudo_prefix=None", True, None, '%s "%s"' % (env.shell, command)), ("shell=True, sudo_prefix=string", True, prefix, prefix + ' %s "%s"' % (env.shell, command)), ("shell=False, sudo_prefix=None", False, None, command), ("shell=False, sudo_prefix=string", False, prefix, prefix + " " + command), ): eq_.description = "_shell_wrap: %s" % description yield eq_, _shell_wrap(command, shell_escape=True, shell=shell, sudo_prefix=sudo_prefix), result del eq_.description
def test_shell_wrap(): prefix = "prefix" command = "command" for description, shell, sudo_prefix, result in ( ("shell=True, sudo_prefix=None", True, None, "%s \"%s\"" % (env.shell, command)), ("shell=True, sudo_prefix=string", True, prefix, prefix + " %s \"%s\"" % (env.shell, command)), ("shell=False, sudo_prefix=None", False, None, command), ("shell=False, sudo_prefix=string", False, prefix, prefix + " " + command), ): eq_.description = "_shell_wrap: %s" % description yield eq_, _shell_wrap(command, shell, sudo_prefix), result del eq_.description
def test_shell_wrap_does_not_escape_command_if_shell_is_false(): """ _shell_wrap() does no escaping if shell=False """ cmd = "cd \"Application Support\"" eq_(_shell_wrap(cmd, shell_escape=True, shell=False), cmd)
def test_shell_wrap_does_not_escape_command_if_shell_is_false(): """ _shell_wrap() does no escaping if shell=False """ cmd = "cd \"Application Support\"" eq_(_shell_wrap(cmd, shell=False), cmd)
def _run_host_command(command, shell=True, pty=True, combine_stderr=True, quiet=False, warn_only=False, stdout=None, stderr=None, timeout=None): """ Run host wrapper command as root (Modified from fabric.operations._run_command to ignore prefixes, path(), cd(), and always use sudo.) """ manager = _noop if warn_only: manager = warn_only_manager # Quiet's behavior is a superset of warn_only's, so it wins. if quiet: manager = quiet_manager with manager(): # Set up new var so original argument can be displayed verbatim later. given_command = command # Handle context manager modifications, and shell wrapping wrapped_command = _shell_wrap( command, # !! removed _prefix_commands() & _prefix_env_vars() shell, _sudo_prefix(None) # !! always use sudo ) # Execute info line which = 'sudo' # !! always use sudo if output.debug: print(("[%s] %s: %s" % (env.host_string, which, wrapped_command))) elif output.running: print(("[%s] %s: %s" % (env.host_string, which, given_command))) # Actual execution, stdin/stdout/stderr handling, and termination result_stdout, result_stderr, status = _execute( channel=default_channel(), command=wrapped_command, pty=pty, combine_stderr=combine_stderr, invoke_shell=False, stdout=stdout, stderr=stderr, timeout=timeout) # Assemble output string out = _AttributeString(result_stdout) err = _AttributeString(result_stderr) # Error handling out.failed = False out.command = given_command out.real_command = wrapped_command if status not in env.ok_ret_codes: out.failed = True msg = "%s() received nonzero return code %s while executing" % ( which, status) if env.warn_only: msg += " '%s'!" % given_command else: msg += "!\n\nRequested: %s\nExecuted: %s" % (given_command, wrapped_command) error(message=msg, stdout=out, stderr=err) # Attach return code to output string so users who have set things to # warn only, can inspect the error code. out.return_code = status # Convenience mirror of .failed out.succeeded = not out.failed # Attach stderr for anyone interested in that. out.stderr = err return out
def _run_command_local(command, shell=True, combine_stderr=True, sudo=False, user=None): ''' Local implementation of fabric.operations._run_command that uses subprocess to execute. ''' # Conditionally import error handling function, since different fabric # versions handle this differently try: from fabric.utils import error except ImportError: from fabric.operations import _handle_failure error = lambda msg=None, **kwargs: _handle_failure(msg) # Set up new var so original argument can be displayed verbatim later. given_command = command # Pick up cuisine sudo mode and password as appropriate if sudo and cuisine.sudo_password(): sudo_prefix = ('echo "%s" | %s -S -p ""' % (cuisine.sudo_password, env.sudo_prefix)) else: sudo_prefix = env.sudo_prefix # Handle context manager modifications, and shell wrapping with settings(sudo_prefix=sudo_prefix): wrapped_command = _shell_wrap( _prefix_commands(_prefix_env_vars(command), 'remote'), shell, _sudo_prefix(user) if sudo else None ) # Execute info line which = 'sudo' if sudo else 'run' if output.debug: print("[%s] %s: %s" % ('local', which, wrapped_command)) elif output.running: print("[%s] %s: %s" % ('local', which, given_command)) # Actual execution, stdin/stdout/stderr handling, and termination stdout, stderr, status = _execute_local(wrapped_command, shell=shell, combine_stderr=combine_stderr) # Assemble output string out = _AttributeString(stdout) err = _AttributeString(stderr) # Error handling out.failed = False if status != 0: out.failed = True msg = "%s() received nonzero return code %s while executing" % ( which, status ) if env.warn_only: msg += " '%s'!" % given_command else: msg += "!\n\nRequested: %s\nExecuted: %s" % ( given_command, wrapped_command ) error(message=msg, stdout=out, stderr=err) # Attach return code to output string so users who have set things to # warn only, can inspect the error code. out.return_code = status # Convenience mirror of .failed out.succeeded = not out.failed # Attach stderr for anyone interested in that. out.stderr = err return out
def _run_host_command(command, shell=True, pty=True, combine_stderr=True, quiet=False, warn_only=False, stdout=None, stderr=None, timeout=None): """ Run host wrapper command as root (Modified from fabric.operations._run_command to ignore prefixes, path(), cd(), and always use sudo.) """ manager = _noop if warn_only: manager = warn_only_manager # Quiet's behavior is a superset of warn_only's, so it wins. if quiet: manager = quiet_manager with manager(): # Set up new var so original argument can be displayed verbatim later. given_command = command # Handle context manager modifications, and shell wrapping wrapped_command = _shell_wrap( command, # !! removed _prefix_commands() & _prefix_env_vars() shell, _sudo_prefix(None) # !! always use sudo ) # Execute info line which = 'sudo' # !! always use sudo if output.debug: print("[%s] %s: %s" % (env.host_string, which, wrapped_command)) elif output.running: print("[%s] %s: %s" % (env.host_string, which, given_command)) # Actual execution, stdin/stdout/stderr handling, and termination result_stdout, result_stderr, status = _execute( channel=default_channel(), command=wrapped_command, pty=pty, combine_stderr=combine_stderr, invoke_shell=False, stdout=stdout, stderr=stderr, timeout=timeout) # Assemble output string out = _AttributeString(result_stdout) err = _AttributeString(result_stderr) # Error handling out.failed = False out.command = given_command out.real_command = wrapped_command if status not in env.ok_ret_codes: out.failed = True msg = "%s() received nonzero return code %s while executing" % ( which, status ) if env.warn_only: msg += " '%s'!" % given_command else: msg += "!\n\nRequested: %s\nExecuted: %s" % ( given_command, wrapped_command ) error(message=msg, stdout=out, stderr=err) # Attach return code to output string so users who have set things to # warn only, can inspect the error code. out.return_code = status # Convenience mirror of .failed out.succeeded = not out.failed # Attach stderr for anyone interested in that. out.stderr = err return out
def _run_command_local(command, shell=True, combine_stderr=True, sudo=False, user=None): ''' Local implementation of fabric.operations._run_command that uses subprocess to execute. ''' # Conditionally import error handling function, since different fabric # versions handle this differently try: from fabric.utils import error except ImportError: from fabric.operations import _handle_failure error = lambda msg=None, **kwargs: _handle_failure(msg) # Set up new var so original argument can be displayed verbatim later. given_command = command # Pick up cuisine sudo mode and password as appropriate if sudo and cuisine.sudo_password(): sudo_prefix = ('echo "%s" | %s -S -p ""' % (cuisine.sudo_password, env.sudo_prefix)) else: sudo_prefix = env.sudo_prefix # Handle context manager modifications, and shell wrapping with settings(sudo_prefix=sudo_prefix): wrapped_command = _shell_wrap( _prefix_commands(_prefix_env_vars(command), 'remote'), shell, _sudo_prefix(user) if sudo else None) # Execute info line which = 'sudo' if sudo else 'run' if output.debug: print("[%s] %s: %s" % ('local', which, wrapped_command)) elif output.running: print("[%s] %s: %s" % ('local', which, given_command)) # Actual execution, stdin/stdout/stderr handling, and termination stdout, stderr, status = _execute_local(wrapped_command, shell=shell, combine_stderr=combine_stderr) # Assemble output string out = _AttributeString(stdout) err = _AttributeString(stderr) # Error handling out.failed = False if status != 0: out.failed = True msg = "%s() received nonzero return code %s while executing" % (which, status) if env.warn_only: msg += " '%s'!" % given_command else: msg += "!\n\nRequested: %s\nExecuted: %s" % (given_command, wrapped_command) error(message=msg, stdout=out, stderr=err) # Attach return code to output string so users who have set things to # warn only, can inspect the error code. out.return_code = status # Convenience mirror of .failed out.succeeded = not out.failed # Attach stderr for anyone interested in that. out.stderr = err return out