Revision: 139
Author:   janne.t.harkonen
Date:     Thu Aug 16 04:16:56 2012
Log:      Read Command Output: return values like Execute Command
http://code.google.com/p/robotframework-sshlibrary/source/detail?r=139

Modified:
 /trunk/src/SSHLibrary/__init__.py
 /trunk/src/SSHLibrary/client.py
 /trunk/src/SSHLibrary/javaclient.py
 /trunk/src/SSHLibrary/pythonclient.py

=======================================
--- /trunk/src/SSHLibrary/__init__.py   Thu Aug 16 04:16:45 2012
+++ /trunk/src/SSHLibrary/__init__.py   Thu Aug 16 04:16:56 2012
@@ -341,20 +341,8 @@
         returned.
         """
         self._info("Executing command '%s'" % command)
-        return self._client.execute_command(command,
- *self._options_for_exec_command(return_stdout, return_stderr,
-                                                return_rc))
-
-    def _options_for_exec_command(self, stdout, stderr, rc):
-        if not isinstance(stdout, basestring):
-            return stdout, stderr, rc
-        # Handle legacy options for configuring returned outputs
-        stdout = stdout.lower()
-        if stdout == 'stderr':
-            return False, True, rc
-        if stdout == 'both':
-            return True, True, rc
-        return stdout, stderr, rc
+ opts = self._output_options(return_stdout, return_stderr, return_rc)
+        return self._client.execute_command(command, *opts)

     def start_command(self, command):
         """Starts command execution on remote host.
@@ -372,7 +360,8 @@
         self._command = command
         self._client.start_command(command)

-    def read_command_output(self, ret_mode='stdout'):
+    def read_command_output(self, return_stdout=True, return_stderr=False,
+                            return_rc=False):
"""Reads and returns/logs output (stdout and/or stderr) of a command.

         Command must have been started using `Start Command` before this
@@ -385,14 +374,19 @@
| ${out} | ${err}= | Read Command Output | both | #stdout and stderr are returned |
         """
         self._info("Reading output of command '%s'" % self._command)
- return self._process_output(self._client.read_command_output(ret_mode)) + opts = self._output_options(return_stdout, return_stderr, return_rc)
+        return self._client.read_command_output(*opts)

-    def _process_output(self, output):
-        def _strip_possible_newline(text):
-            return text[:-1] if text.endswith('\n') else text
-        if isinstance(output, tuple):
-            return [_strip_possible_newline(out) for out in output]
-        return _strip_possible_newline(output)
+    def _output_options(self, stdout, stderr, rc):
+        if not isinstance(stdout, basestring):
+            return stdout, stderr, rc
+        # Handle legacy options for configuring returned outputs
+        stdout = stdout.lower()
+        if stdout == 'stderr':
+            return False, True, rc
+        if stdout == 'both':
+            return True, True, rc
+        return stdout, stderr, rc

     def write(self, text, loglevel=None):
         """Writes given text over the connection and appends newline.
=======================================
--- /trunk/src/SSHLibrary/client.py     Tue Aug 14 05:55:47 2012
+++ /trunk/src/SSHLibrary/client.py     Thu Aug 16 04:16:56 2012
@@ -26,19 +26,31 @@
         self.shell = None
         self.client = self._create_client()

-    def execute_command(self, command, want_stdout, want_stderr, want_rc):
+    def execute_command(self, command, return_stdout, return_stderr,
+                        return_rc):
         stdout, stderr, rc = self._execute_command(command)
+        return self._return_outputs(stdout, stderr, rc, return_stdout,
+                                    return_stderr, return_rc)
+
+ def _return_outputs(self, stdout, stderr, rc, return_stdout, return_stderr,
+                        return_rc):
         ret = []
-        if want_stdout:
+        print return_stdout, return_stderr, return_rc
+        if return_stdout:
             ret.append(self._process_output(stdout))
-        if want_stderr:
+        if return_stderr:
             ret.append(self._process_output(stderr))
-        if want_rc:
+        if return_rc:
             ret.append(rc)
         if len(ret) == 1:
             return ret[0]
         return ret

+    def read_command_output(self, return_stdout, return_stderr, return_rc):
+        stdout, stderr, rc = self._read_command_output()
+        return self._return_outputs(stdout, stderr, rc, return_stdout,
+                                    return_stderr, return_rc)
+
     def _process_output(self, text):
         if text.endswith('\n'):
             return text[:-1]
=======================================
--- /trunk/src/SSHLibrary/javaclient.py Tue Aug 14 05:55:47 2012
+++ /trunk/src/SSHLibrary/javaclient.py Thu Aug 16 04:16:56 2012
@@ -51,22 +51,21 @@
         self.client.close()

     def _execute_command(self, command):
-        session = self.client.openSession()
-        session.execCommand(command)
+        session = self._start_command(command)
+        return self._read_command_output(session)
+
+    def _read_command_output(self, session=None):
+        session = session or self.sess
         stdout = self._read_from_stream(session.getStdout())
         stderr = self._read_from_stream(session.getStderr())
         rc = session.getExitStatus()
         session.close()
         return stdout, stderr, rc

-    def _read_outputs(self, sess, ret_mode):
-        stdout = self._read_from_stream(sess.getStdout())
-        stderr = self._read_from_stream(sess.getStderr())
-        if ret_mode.lower() == 'both':
-            return stdout, stderr
-        if ret_mode.lower() == 'stderr':
-            return stderr
-        return stdout
+    def _start_command(self, command):
+        session = self.client.openSession()
+        session.execCommand(command)
+        return session

     def _read_from_stream(self, stream):
         reader = BufferedReader(InputStreamReader(StreamGobbler(stream)))
@@ -78,13 +77,7 @@
         return result

     def start_command(self, command):
-        self.sess = self.client.openSession()
-        self.sess.execCommand(command)
-
-    def read_command_output(self, ret_mode):
-        outputs = self._read_outputs(self.sess, ret_mode)
-        self.sess.close()
-        return outputs
+        self.sess = self._start_command(command)

     def open_shell(self, term_type, width, height):
         self.shell = self.client.openSession()
=======================================
--- /trunk/src/SSHLibrary/pythonclient.py       Tue Aug 14 05:55:47 2012
+++ /trunk/src/SSHLibrary/pythonclient.py       Thu Aug 16 04:16:56 2012
@@ -60,24 +60,22 @@
         self.client.close()

     def _execute_command(self, command):
+        channel, stdout, stderr = self._start_command(command)
+        return stdout.read(), stderr.read(), channel.recv_exit_status()
+
+    def _start_command(self, command):
         channel = self.client.get_transport().open_session()
         channel.exec_command(command)
         stdout = channel.makefile('rb', -1)
         stderr = channel.makefile_stderr('rb', -1)
-        return stdout.read(), stderr.read(), channel.recv_exit_status()
+        return channel, stdout, stderr

     def start_command(self, command):
-        _, self.stdout, self.stderr = self.client.exec_command(command)
+ self.channel, self.stdout, self.stderr = self._start_command(command)

-    def read_command_output(self, ret_mode):
- return self._read_command_output(self.stdout, self.stderr, ret_mode)
-
-    def _read_command_output(self, stdout, stderr, ret_mode):
-        if ret_mode.lower() == 'both':
-            return stdout.read(), stderr.read()
-        if ret_mode.lower() == 'stderr':
-            return stderr.read()
-        return stdout.read()
+    def _read_command_output(self):
+        return self.stdout.read(), self.stderr.read(), \
+            self.channel.recv_exit_status()

     def open_shell(self, term_type, width, height):
         self.shell = self.client.invoke_shell(term_type, width, height)

Reply via email to