Revision: 92
Author: janne.t.harkonen
Date: Wed Nov 3 04:03:36 2010
Log: common base class for the SSH clients
http://code.google.com/p/robotframework-sshlibrary/source/detail?r=92
Modified:
/trunk/atest/put_file.txt
/trunk/src/SSHLibrary/__init__.py
/trunk/src/SSHLibrary/javaclient.py
/trunk/src/SSHLibrary/pythonclient.py
=======================================
--- /trunk/atest/put_file.txt Tue Nov 2 07:12:24 2010
+++ /trunk/atest/put_file.txt Wed Nov 3 04:03:36 2010
@@ -20,6 +20,10 @@
Put File With Different Name
Put Files And Verify ${TEST FILE}
/home/${USERNAME}/another_name.txt another_name.txt
[Teardown] Execute Command rm -f another_name.txt
+
+Put File With Newlines
+ Put Files And Verify ${FILE WITH NEWLINES} /home/${USERNAME}/
${FILE WITH NEWLINES NAME}
+ [Teardown] Execute Command rm -f ${FILE WITH NEWLINES NAME}
Put File With Pattern
Put Files And Verify ${TEST FILE PATTERN} /home/${USERNAME}/ ${TEST
FILE NAME} ${TEST FILE 2 NAME}
=======================================
--- /trunk/src/SSHLibrary/__init__.py Wed Nov 3 03:50:12 2010
+++ /trunk/src/SSHLibrary/__init__.py Wed Nov 3 04:03:36 2010
@@ -495,7 +495,7 @@
raise AssertionError("No match found for '%s' in %s"
% (expected, utils.secs_to_timestr(timeout)))
- def put_file(self, source, destination='.', mode='0744',
newlines='default'):
+ def put_file(self, source, destination='.', mode='0744'):
"""Copies file(s) from local host to remote host using existing
SSH connection.
1. If the destination is an existing file, the src file is copied
@@ -528,18 +528,18 @@
mode = int(mode,8)
self._client.create_sftp_client()
localfiles = self._get_put_file_sources(source)
- self._debug('Source pattern matched local files: %s' %
utils.seq2str(localfiles))
remotefiles, remotepath =
self._get_put_file_destinations(localfiles, destination)
self._client.create_missing_remote_path(remotepath)
for src, dst in zip(localfiles, remotefiles):
self._info("Putting '%s' to '%s'" % (src, dst))
- self._client.put_file(src, dst, mode, newlines)
+ self._client.put_file(src, dst, mode)
self._client.close_sftp_client()
def _get_put_file_sources(self, source):
sources = [f for f in glob.glob(source.replace('/', os.sep)) if
os.path.isfile(f)]
if not sources:
raise AssertionError("There were no source files
matching '%s'" % source)
+ self._debug('Source pattern matched local files: %s' %
utils.seq2str(sources))
return sources
def _get_put_file_destinations(self, sources, dest):
=======================================
--- /trunk/src/SSHLibrary/javaclient.py Fri Feb 27 02:42:05 2009
+++ /trunk/src/SSHLibrary/javaclient.py Wed Nov 3 04:03:36 2010
@@ -14,20 +14,18 @@
import jarray
-
-from java.io import File, BufferedReader, InputStreamReader, IOException, \
- FileOutputStream, BufferedWriter, OutputStreamWriter
-
-from robot.errors import DataError
+from java.io import File, BufferedReader, InputStreamReader, IOException,
FileOutputStream
try:
- from com.trilead.ssh2 import StreamGobbler, SCPClient, Connection,
SFTPv3Client, \
- SFTPv3FileAttributes, SFTPException, DebugLogger
+ from com.trilead.ssh2 import StreamGobbler, Connection, SFTPv3Client,
SFTPException
except ImportError:
raise ImportError('Importing Trilead SSH classes failed. '
'Make sure you have the Trilead jar file in
CLASSPATH.')
-
-class SSHClient(object):
+from robot.errors import DataError
+from client import SSHLibraryClient
+
+
+class SSHClient(SSHLibraryClient):
def __init__(self, host, port=22):
self.client = Connection(host, port)
@@ -37,7 +35,7 @@
def login(self, username, password):
if not self.client.authenticateWithPassword(username, password):
raise AssertionError("Authentication failed for user: %s" %
username)
-
+
def login_with_public_key(self, username, key, password):
try:
if not self.client.authenticateWithPublicKey(username,
File(key), password):
@@ -93,7 +91,7 @@
def write(self, text):
self._writer.write(text)
self._writer.flush()
-
+
def read(self):
data = ''
if self._stdout.available():
@@ -101,21 +99,21 @@
self._stdout.read(buf)
data += ''.join([chr(b) for b in buf])
return data
-
+
def read_char(self):
if self._stdout.available():
buf = jarray.zeros(1, 'b')
self._stdout.read(buf)
return chr(buf[0])
return ''
-
- def create_sftp_client(self):
+
+ def create_sftp_client(self):
self.sftp_client = SFTPv3Client(self.client)
self.homedir = self.sftp_client.canonicalPath('.') + '/'
-
+
def close_sftp_client(self):
self.sftp_client.close()
-
+
def create_missing_remote_path(self, path):
if path.startswith('/'):
curdir = '/'
@@ -129,9 +127,8 @@
except IOException:
print "*INFO* Creating missing remote directory '%s'" %
curdir
self.sftp_client.mkdir(curdir, 0744)
-
+
def put_file(self, source, dest, mode):
- size = 0
localfile = open(source, 'rb')
remotefile = self.sftp_client.createFile(dest)
try:
@@ -140,6 +137,7 @@
self.sftp_client.fsetstat(remotefile, tempstats)
except SFTPException:
pass
+ size = 0
while True:
data = localfile.read(4096)
datalen = len(data)
@@ -149,11 +147,11 @@
size += datalen
self.sftp_client.closeFile(remotefile)
localfile.close()
-
+
def listfiles(self, path):
return [ fileinfo.filename for fileinfo in
self.sftp_client.ls(path) if
fileinfo.attributes.getOctalPermissions().startswith('0100') ]
-
+
def get_file(self, source, dest):
localfile = FileOutputStream(dest)
tempstats = self.sftp_client.stat(source)
@@ -174,4 +172,3 @@
self.sftp_client.closeFile(remotefile)
localfile.flush()
localfile.close()
-
=======================================
--- /trunk/src/SSHLibrary/pythonclient.py Wed Nov 3 00:02:08 2010
+++ /trunk/src/SSHLibrary/pythonclient.py Wed Nov 3 04:03:36 2010
@@ -14,14 +14,15 @@
import stat
import posixpath
-
-from robot.errors import DataError
try:
import paramiko
except ImportError:
raise ImportError('Importing paramiko SSH module or its dependencies
failed. '
'Make sure you have the required modules installed.')
+from robot.errors import DataError
+from client import SSHLibraryClient
+
# There doesn't seem to be a simpler way to increase banner timeout
def _monkey_patched_start_client(self, event=None):
self.banner_timeout = 45
@@ -31,7 +32,7 @@
paramiko.transport.Transport.start_client = _monkey_patched_start_client
-class SSHClient(object):
+class SSHClient(SSHLibraryClient):
enable_ssh_logging = staticmethod(lambda path:
paramiko.util.log_to_file(path))