added functionality to skip verifying the file, which works around sftp servers that remove the file immediately after it's been closed.

This commit is contained in:
Larry Wright 2009-12-18 17:02:55 -06:00
parent cb913d5d32
commit b4ee844a3c
2 changed files with 35 additions and 4 deletions

View File

@ -533,7 +533,7 @@ class SFTPClient (BaseSFTP):
""" """
return self._cwd return self._cwd
def put(self, localpath, remotepath, callback=None): def put(self, localpath, remotepath, callback=None, confirm = True):
""" """
Copy a local file (C{localpath}) to the SFTP server as C{remotepath}. Copy a local file (C{localpath}) to the SFTP server as C{remotepath}.
Any exception raised by operations will be passed through. This Any exception raised by operations will be passed through. This
@ -574,9 +574,12 @@ class SFTPClient (BaseSFTP):
fr.close() fr.close()
finally: finally:
fl.close() fl.close()
s = self.stat(remotepath) if confirm:
if s.st_size != size: s = self.stat(remotepath)
raise IOError('size mismatch in put! %d != %d' % (s.st_size, size)) if s.st_size != size:
raise IOError('size mismatch in put! %d != %d' % (s.st_size, size))
else:
s = SFTPAttributes()
return s return s
def get(self, remotepath, localpath, callback=None): def get(self, remotepath, localpath, callback=None):

View File

@ -36,6 +36,7 @@ import unittest
import paramiko import paramiko
from stub_sftp import StubServer, StubSFTPServer from stub_sftp import StubServer, StubSFTPServer
from loop import LoopSocket from loop import LoopSocket
from paramiko.sftp_attr import SFTPAttributes
ARTICLE = ''' ARTICLE = '''
Insulin sensitivity and liver insulin receptor structure in ducks from two Insulin sensitivity and liver insulin receptor structure in ducks from two
@ -666,6 +667,33 @@ class SFTPTest (unittest.TestCase):
f.close() f.close()
finally: finally:
sftp.unlink(FOLDER + '/zero') sftp.unlink(FOLDER + '/zero')
def test_N_put_without_confirm(self):
"""
verify that get/put work.
"""
import os, warnings
warnings.filterwarnings('ignore', 'tempnam.*')
localname = os.tempnam()
text = 'All I wanted was a plastic bunny rabbit.\n'
f = open(localname, 'wb')
f.write(text)
f.close()
saved_progress = []
def progress_callback(x, y):
saved_progress.append((x, y))
res = sftp.put(localname, FOLDER + '/bunny.txt', progress_callback, false)
self.assertEquals(SFTPAttributes(), res)
f = sftp.open(FOLDER + '/bunny.txt', 'r')
self.assertEquals(text, f.read(128))
f.close()
self.assertEquals((41, 41), saved_progress[-1])
os.unlink(localname)
sftp.unlink(FOLDER + '/bunny.txt')
def XXX_test_M_seek_append(self): def XXX_test_M_seek_append(self):
""" """