Skip to content

Commit

Permalink
backend: work with bytes and lazy decode using local preferredencoding
Browse files Browse the repository at this point in the history
Still broken because remote system encoding may be different, and
preferredencoding isn't 100% reliable
  • Loading branch information
philpep committed Mar 20, 2015
1 parent fb08d35 commit 8807f38
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 16 deletions.
45 changes: 41 additions & 4 deletions testinfra/backend/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,50 @@

from __future__ import unicode_literals

import collections
import locale
import pipes


CommandResult = collections.namedtuple('CommandResult', [
'rc', 'stdout', 'stderr', 'command',
])
ENCODING = locale.getpreferredencoding()


class CommandResult(object):

def __init__(self, exit_status, stdout_bytes, stderr_bytes, command):
self.exit_status = exit_status
self.stdout_bytes = stdout_bytes
self.stderr_bytes = stderr_bytes
self._stdout = None
self._stderr = None
self.command = command
super(CommandResult, self).__init__()

@property
def rc(self):
return self.exit_status

@property
def stdout(self):
if self._stdout is None:
self._stdout = self.stdout_bytes.decode(ENCODING)
return self._stdout

@property
def stderr(self):
if self._stderr is None:
self._stderr = self.stderr_bytes.decode(ENCODING)
return self._stderr

def __repr__(self):
return (
"CommandResult(exit_status=%s, stdout=%s, "
"stderr=%s, command=%s)"
) % (
self.exit_status,
repr(self.stdout_bytes),
repr(self.stderr_bytes),
repr(self.command),
)


class BaseBackend(object):
Expand Down
2 changes: 1 addition & 1 deletion testinfra/backend/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ def run(self, command, *args, **kwargs):
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=kwargs.get("decode", True))
)
stdout, stderr = p.communicate()
return base.CommandResult(p.returncode, stdout, stderr, command)
5 changes: 3 additions & 2 deletions testinfra/backend/paramiko.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from __future__ import absolute_import

import logging

from testinfra.backend import base

try:
Expand Down Expand Up @@ -58,6 +59,6 @@ def run(self, command, *args, **kwargs):
logger.info("RUN %s", command)
chan.exec_command(command)
rc = chan.recv_exit_status()
stdout = ''.join(chan.makefile('rb'))
stderr = ''.join(chan.makefile_stderr('rb'))
stdout = b''.join(chan.makefile('rb'))
stderr = b''.join(chan.makefile_stderr('rb'))
return base.CommandResult(rc, stdout, stderr, command)
11 changes: 4 additions & 7 deletions testinfra/backend/ssh.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,7 @@ def run(self, command, *args, **kwargs):
start = out.stdout.find("TESTINFRA_START;") + len("TESTINFRA_START;")
end = out.stdout.find("TESTINFRA_END") - 1
rc, stdout, stderr = out.stdout[start:end].split(";")

return base.CommandResult(
rc=int(rc),
stdout=base64.b64decode(stdout),
stderr=base64.b64decode(stderr),
command=orig_command,
)
rc = int(rc)
stdout = base64.b64decode(stdout)
stderr = base64.b64decode(stderr)
return base.CommandResult(rc, stdout, stderr, orig_command)
7 changes: 5 additions & 2 deletions testinfra/modules/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,13 @@ def sha256sum(self):
"sha256sum %s | cut -d ' ' -f 1", self.path)

def _get_content(self, decode):
out = self.run_test("cat -- %s", self.path, decode=decode)
out = self.run_test("cat -- %s", self.path)
if out.rc != 0:
raise RuntimeError("Unexpected output %s" % (out,))
return out.stdout
if decode:
return out.stdout
else:
return out.stdout_bytes

@property
def content(self):
Expand Down

0 comments on commit 8807f38

Please sign in to comment.