Skip to content

Commit

Permalink
Support non-default remote working directories
Browse files Browse the repository at this point in the history
  • Loading branch information
xolox committed May 27, 2015
1 parent c3c295c commit 6b68657
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
16 changes: 11 additions & 5 deletions executor/__init__.py
Expand Up @@ -3,7 +3,7 @@
# Programmer friendly subprocess wrapper.
#
# Author: Peter Odding <peter@peterodding.com>
# Last Change: May 26, 2015
# Last Change: May 27, 2015
# URL: https://executor.readthedocs.org

"""
Expand Down Expand Up @@ -60,7 +60,7 @@
)

# Semi-standard module versioning.
__version__ = '3.5'
__version__ = '3.6'

# Initialize a logger.
logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -94,6 +94,12 @@
``fish`` or ``zsh`` syntax :-).
"""

DEFAULT_WORKING_DIRECTORY = os.curdir
"""
The default working directory for external commands (a string). Defaults to the
working directory of the current process using :data:`os.curdir`.
"""


def execute(*command, **options):
"""
Expand Down Expand Up @@ -374,10 +380,10 @@ def decoded_stderr(self):
@mutable_property
def directory(self):
"""
The working directory for the external command (a string). Defaults to
the working directory of the current process using :data:`os.curdir`.
The working directory for the external command (a string, defaults to
:data:`DEFAULT_WORKING_DIRECTORY`).
"""
return os.curdir
return DEFAULT_WORKING_DIRECTORY

@property
def encoded_input(self):
Expand Down
9 changes: 6 additions & 3 deletions executor/ssh/client.py
@@ -1,7 +1,7 @@
# Programmer friendly subprocess wrapper.
#
# Author: Peter Odding <peter@peterodding.com>
# Last Change: May 26, 2015
# Last Change: May 27, 2015
# URL: https://executor.readthedocs.org

"""
Expand All @@ -22,7 +22,7 @@
import os

# Modules included in our package.
from executor import ExternalCommand, ExternalCommandFailed, quote
from executor import DEFAULT_WORKING_DIRECTORY, ExternalCommand, ExternalCommandFailed, quote
from executor.concurrent import CommandPool
from executor.property_manager import mutable_property, required_property

Expand Down Expand Up @@ -181,7 +181,10 @@ def command_line(self):
ssh_command.extend(('-o', 'StrictHostKeyChecking=no'))
ssh_command.extend(('-o', 'UserKnownHostsFile=%s' % self.known_hosts_file))
ssh_command.append(self.ssh_alias)
ssh_command.append(quote(super(RemoteCommand, self).command_line))
remote_command = quote(super(RemoteCommand, self).command_line)
if self.directory != DEFAULT_WORKING_DIRECTORY:
remote_command = 'cd %s && %s' % (quote(self.directory), remote_command)
ssh_command.append(remote_command)
return ssh_command

@mutable_property
Expand Down
9 changes: 8 additions & 1 deletion executor/tests.py
@@ -1,7 +1,7 @@
# Automated tests for the `executor' module.
#
# Author: Peter Odding <peter@peterodding.com>
# Last Change: May 26, 2015
# Last Change: May 27, 2015
# URL: https://executor.readthedocs.org

# Standard library modules.
Expand Down Expand Up @@ -276,6 +276,13 @@ def test_ssh_unreachable(self):
lambda: RemoteCommand('this.domain.surely.wont.exist.right', 'date', silent=True).start(),
)

def test_remote_working_directory(self):
with SSHServer(async=True) as server:
some_random_directory = tempfile.mkdtemp()
cmd = RemoteCommand('127.0.0.1', 'pwd', capture=True, directory=some_random_directory, **server.client_options)
cmd.start()
assert cmd.output == some_random_directory

def test_remote_error_handling(self):
with SSHServer(async=True) as server:
cmd = RemoteCommand('127.0.0.1', 'exit 42', **server.client_options)
Expand Down

0 comments on commit 6b68657

Please sign in to comment.