Revision: 93
Author: janne.t.harkonen
Date: Wed Nov 3 06:30:21 2010
Log: Allow specifying newlines to be used on remote system when putting
files
http://code.google.com/p/robotframework-sshlibrary/source/detail?r=93
Added:
/trunk/src/SSHLibrary/client.py
Modified:
/trunk/atest/put_file.txt
/trunk/src/SSHLibrary/__init__.py
/trunk/src/SSHLibrary/javaclient.py
/trunk/src/SSHLibrary/pythonclient.py
=======================================
--- /dev/null
+++ /trunk/src/SSHLibrary/client.py Wed Nov 3 06:30:21 2010
@@ -0,0 +1,30 @@
+# Copyright 2008-2009 Nokia Siemens Networks Oyj
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+class SSHLibraryClient(object):
+
+ def put_file(self, source, dest, mode, newline_char):
+ remotefile = self._create_remote_file(dest, mode)
+ localfile = open(source, 'rb')
+ position = 0
+ while True:
+ data = localfile.read(4096)
+ if not data:
+ break
+ if newline_char and '\n' in data:
+ data = data.replace('\n', newline_char)
+ self._write_to_remote_file(remotefile, data, position)
+ position += len(data)
+ self._close_remote_file(remotefile)
+ localfile.close()
=======================================
--- /trunk/atest/put_file.txt Wed Nov 3 04:03:36 2010
+++ /trunk/atest/put_file.txt Wed Nov 3 06:30:21 2010
@@ -1,8 +1,12 @@
*** Settings ***
Documentation This suite contains test for 'Put File 'keywords These
tests use some dummy test files from under the scripts directory.
Suite Setup Login As Valid User
-Suite Teardown Close Connection
+Suite Teardown Run Keywords Close Connection Remove Local Tempdir
Resource resources/ssh_library_resources.txt
+Library String
+
+*** Variables ***
+${TMPDIR} robot-tmpdir
*** Test Cases ***
Put File With Absolute Destination
@@ -24,6 +28,16 @@
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 And Specify Remote Newlines
+ [Setup] Create Directory ${TMPDIR}
+ Put File And Specify Newlines ${FILE WITH NEWLINES}
/home/${USERNAME}/ CRLF ${FILE WITH NEWLINES NAME}
+ SSHLibrary.Get File /home/${USERNAME}/${FILE WITH NEWLINES NAME}
${TMPDIR}${/}
+ ${content}= OS.Get Binary File /home/${USERNAME}/${FILE WITH
NEWLINES NAME}
+ ${expected}= OS.Get File ${FILE WITH NEWLINES}
+ ${expected}= Replace String ${expected} \n \r\n
+ Should Be Equal ${content} ${expected}
+ [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}
@@ -34,12 +48,12 @@
Put File To Absolute Destination With Missing Subdirectories
Put File ${TEST FILE} /home/${USERNAME}/robotdir/
- Verify Listing robotdir ${TEST FILE NAME}
+ Verify Remote Files Exist robotdir ${TEST FILE NAME}
[Teardown] Execute Command rm -rf robotdir
Put File To Relative Destination With Missing Subdirectories
Put File ${TEST FILE} robotdir/anotherdir/foo.txt
- Verify Listing robotdir/anotherdir foo.txt
+ Verify Remote Files Exist robotdir/anotherdir foo.txt
[Teardown] Execute Command rm -rf robotdir
Put File With Pattern Matching Directory
@@ -52,15 +66,28 @@
*** Keywords ***
Put Files And Verify
[Arguments] ${source} ${destination} @{expected}
+ Vefify Remote Files do Not Exist @{expected}
+ Put File ${source} ${destination}
+ Verify Remote Files Exist ${EMPTY} @{expected}
+
+Put File And Specify Newlines
+ [Arguments] ${source} ${destination} ${newlines} @{expected}
+ Vefify Remote Files do Not Exist @{expected}
+ Put File ${source} ${destination} newlines=${newlines}
+ Verify Remote Files Exist ${EMPTY} @{expected}
+
+
+Vefify Remote Files do Not Exist
+ [Arguments] @{expected}
${listing} = Execute Command ls
: FOR ${filename} IN @{expected}
\ Should Not Contain ${listing} ${filename}
- Put File ${source} ${destination}
- Verify Listing ${EMPTY} @{expected}
-
-Verify Listing
+
+Verify Remote Files Exist
[Arguments] ${dirname} @{expected}
${listing} = Execute Command ls ${dirname}
: FOR ${filename} IN @{expected}
\ Should Contain ${listing} ${filename}
+Remove Local Tempdir
+ Remove Directory ${TMPDIR} yes
=======================================
--- /trunk/src/SSHLibrary/__init__.py Wed Nov 3 04:03:36 2010
+++ /trunk/src/SSHLibrary/__init__.py Wed Nov 3 06:30:21 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'):
+ def put_file(self, source, destination='.', mode='0744',
newlines='default'):
"""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
@@ -532,7 +532,8 @@
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)
+ self._client.put_file(src, dst, mode,
+
{'CRLF': '\r\n', 'LF': '\n'}.get(newlines, None))
self._client.close_sftp_client()
def _get_put_file_sources(self, source):
=======================================
--- /trunk/src/SSHLibrary/javaclient.py Wed Nov 3 04:03:36 2010
+++ /trunk/src/SSHLibrary/javaclient.py Wed Nov 3 06:30:21 2010
@@ -128,8 +128,7 @@
print "*INFO* Creating missing remote directory '%s'" %
curdir
self.sftp_client.mkdir(curdir, 0744)
- def put_file(self, source, dest, mode):
- localfile = open(source, 'rb')
+ def _create_remote_file(self, dest, mode):
remotefile = self.sftp_client.createFile(dest)
try:
tempstats = self.sftp_client.fstat(remotefile)
@@ -137,16 +136,13 @@
self.sftp_client.fsetstat(remotefile, tempstats)
except SFTPException:
pass
- size = 0
- while True:
- data = localfile.read(4096)
- datalen = len(data)
- if datalen == 0:
- break
- self.sftp_client.write(remotefile, size, data, 0, datalen)
- size += datalen
+ return remotefile
+
+ def _write_to_remote_file(self, remotefile, data, position):
+ self.sftp_client.write(remotefile, position, data, 0, len(data))
+
+ def _close_remote_file(self, remotefile):
self.sftp_client.closeFile(remotefile)
- localfile.close()
def listfiles(self, path):
return [ fileinfo.filename for fileinfo in
self.sftp_client.ls(path) if
=======================================
--- /trunk/src/SSHLibrary/pythonclient.py Wed Nov 3 04:03:36 2010
+++ /trunk/src/SSHLibrary/pythonclient.py Wed Nov 3 06:30:21 2010
@@ -110,9 +110,17 @@
self.sftp_client.mkdir(dirname)
self.sftp_client.chdir(dirname)
- def put_file(self, source, dest, mode):
- self.sftp_client.put(source, dest)
+ def _create_remote_file(self, dest, mode):
+ remotfile = self.sftp_client.file(dest, 'wb')
+ remotfile.set_pipelined(True)
self.sftp_client.chmod(dest, mode)
+ return remotfile
+
+ def _write_to_remote_file(self, remotefile, data, position):
+ remotefile.write(data)
+
+ def _close_remote_file(self, remotefile):
+ remotefile.close()
def listfiles(self, path):
return[ getattr(fileinfo, 'filename', '?') for fileinfo