Use 'with' for opening most file and SFTPFIle objects
This commit is contained in:
parent
7471515fff
commit
2da5f1fb45
|
@ -96,7 +96,8 @@ try:
|
|||
sftp.mkdir("demo_sftp_folder")
|
||||
except IOError:
|
||||
print('(assuming demo_sftp_folder/ already exists)')
|
||||
sftp.open('demo_sftp_folder/README', 'w').write('This was created by demo_sftp.py.\n')
|
||||
with sftp.open('demo_sftp_folder/README', 'w') as f:
|
||||
f.write('This was created by demo_sftp.py.\n')
|
||||
with open('demo_sftp.py', 'r') as f:
|
||||
data = f.read()
|
||||
sftp.open('demo_sftp_folder/demo_sftp.py', 'w').write(data)
|
||||
|
|
|
@ -192,11 +192,10 @@ class SSHClient (object):
|
|||
if self.known_hosts is not None:
|
||||
self.load_host_keys(self.known_hosts)
|
||||
|
||||
f = open(filename, 'w')
|
||||
with open(filename, 'w') as f:
|
||||
for hostname, keys in self._host_keys.items():
|
||||
for keytype, key in keys.items():
|
||||
f.write('%s %s %s\n' % (hostname, keytype, key.get_base64()))
|
||||
f.close()
|
||||
|
||||
def get_host_keys(self):
|
||||
"""
|
||||
|
|
|
@ -172,8 +172,7 @@ class HostKeys (MutableMapping):
|
|||
|
||||
@raise IOError: if there was an error reading the file
|
||||
"""
|
||||
f = open(filename, 'r')
|
||||
try:
|
||||
with open(filename, 'r') as f:
|
||||
for lineno, line in enumerate(f):
|
||||
line = line.strip()
|
||||
if (len(line) == 0) or (line[0] == '#'):
|
||||
|
@ -186,8 +185,6 @@ class HostKeys (MutableMapping):
|
|||
e.hostnames.remove(h)
|
||||
if len(e.hostnames):
|
||||
self._entries.append(e)
|
||||
finally:
|
||||
f.close()
|
||||
|
||||
def save(self, filename):
|
||||
"""
|
||||
|
@ -203,14 +200,11 @@ class HostKeys (MutableMapping):
|
|||
|
||||
@since: 1.6.1
|
||||
"""
|
||||
f = open(filename, 'w')
|
||||
try:
|
||||
with open(filename, 'w') as f:
|
||||
for e in self._entries:
|
||||
line = e.to_line()
|
||||
if line:
|
||||
f.write(line)
|
||||
finally:
|
||||
f.close()
|
||||
|
||||
def lookup(self, hostname):
|
||||
"""
|
||||
|
|
|
@ -282,11 +282,8 @@ class PKey (object):
|
|||
encrypted, and C{password} is C{None}.
|
||||
@raise SSHException: if the key file is invalid.
|
||||
"""
|
||||
f = open(filename, 'r')
|
||||
try:
|
||||
with open(filename, 'r') as f:
|
||||
data = self._read_private_key(tag, f, password)
|
||||
finally:
|
||||
f.close()
|
||||
return data
|
||||
|
||||
def _read_private_key(self, tag, f, password=None):
|
||||
|
@ -354,13 +351,10 @@ class PKey (object):
|
|||
|
||||
@raise IOError: if there was an error writing the file.
|
||||
"""
|
||||
f = open(filename, 'w', o600)
|
||||
try:
|
||||
with open(filename, 'w', o600) as f:
|
||||
# grrr... the mode doesn't always take hold
|
||||
os.chmod(filename, o600)
|
||||
self._write_private_key(tag, f, data, password)
|
||||
finally:
|
||||
f.close()
|
||||
|
||||
def _write_private_key(self, tag, f, data, password=None):
|
||||
f.write('-----BEGIN %s PRIVATE KEY-----\n' % tag)
|
||||
|
|
|
@ -113,8 +113,7 @@ class ModulusPack (object):
|
|||
@raise IOError: passed from any file operations that fail.
|
||||
"""
|
||||
self.pack = {}
|
||||
f = open(filename, 'r')
|
||||
try:
|
||||
with open(filename, 'r') as f:
|
||||
for line in f:
|
||||
line = line.strip()
|
||||
if (len(line) == 0) or (line[0] == '#'):
|
||||
|
@ -123,8 +122,6 @@ class ModulusPack (object):
|
|||
self._parse_modulus(line)
|
||||
except:
|
||||
continue
|
||||
finally:
|
||||
f.close()
|
||||
|
||||
def get_modulus(self, min, prefer, max):
|
||||
bitsizes = sorted(self.pack.keys())
|
||||
|
|
|
@ -561,10 +561,9 @@ class SFTPClient (BaseSFTP):
|
|||
|
||||
@since: 1.4
|
||||
"""
|
||||
fr = self.file(remotepath, 'wb')
|
||||
with self.file(remotepath, 'wb') as fr:
|
||||
fr.set_pipelined(True)
|
||||
size = 0
|
||||
try:
|
||||
while True:
|
||||
data = fl.read(32768)
|
||||
fr.write(data)
|
||||
|
@ -573,8 +572,6 @@ class SFTPClient (BaseSFTP):
|
|||
callback(size, file_size)
|
||||
if len(data) == 0:
|
||||
break
|
||||
finally:
|
||||
fr.close()
|
||||
if confirm:
|
||||
s = self.stat(remotepath)
|
||||
if s.st_size != size:
|
||||
|
@ -610,11 +607,8 @@ class SFTPClient (BaseSFTP):
|
|||
@since: 1.4
|
||||
"""
|
||||
file_size = os.stat(localpath).st_size
|
||||
fl = open(localpath, 'rb')
|
||||
try:
|
||||
with open(localpath, 'rb') as fl:
|
||||
return self.putfo(fl, remotepath, os.stat(localpath).st_size, callback, confirm)
|
||||
finally:
|
||||
fl.close()
|
||||
|
||||
def getfo(self, remotepath, fl, callback=None):
|
||||
"""
|
||||
|
@ -636,10 +630,9 @@ class SFTPClient (BaseSFTP):
|
|||
|
||||
@since: 1.4
|
||||
"""
|
||||
fr = self.open(remotepath, 'rb')
|
||||
with self.open(remotepath, 'rb') as fr:
|
||||
file_size = self.stat(remotepath).st_size
|
||||
fr.prefetch()
|
||||
try:
|
||||
size = 0
|
||||
while True:
|
||||
data = fr.read(32768)
|
||||
|
@ -649,8 +642,6 @@ class SFTPClient (BaseSFTP):
|
|||
callback(size, file_size)
|
||||
if len(data) == 0:
|
||||
break
|
||||
finally:
|
||||
fr.close()
|
||||
return size
|
||||
|
||||
def get(self, remotepath, localpath, callback=None):
|
||||
|
@ -671,11 +662,8 @@ class SFTPClient (BaseSFTP):
|
|||
@since: 1.4
|
||||
"""
|
||||
file_size = self.stat(remotepath).st_size
|
||||
fl = open(localpath, 'wb')
|
||||
try:
|
||||
with open(localpath, 'wb') as fl:
|
||||
size = self.getfo(remotepath, fl, callback)
|
||||
finally:
|
||||
fl.close()
|
||||
s = os.stat(localpath)
|
||||
if s.st_size != size:
|
||||
raise IOError('size mismatch in get! %d != %d' % (s.st_size, size))
|
||||
|
|
|
@ -166,11 +166,8 @@ class SFTPServer (BaseSFTP, SubsystemHandler):
|
|||
if attr._flags & attr.FLAG_AMTIME:
|
||||
os.utime(filename, (attr.st_atime, attr.st_mtime))
|
||||
if attr._flags & attr.FLAG_SIZE:
|
||||
f = open(filename, 'w+')
|
||||
try:
|
||||
with open(filename, 'w+') as f:
|
||||
f.truncate(attr.st_size)
|
||||
finally:
|
||||
f.close()
|
||||
set_file_attr = staticmethod(set_file_attr)
|
||||
|
||||
|
||||
|
|
|
@ -56,9 +56,8 @@ Ngw3qIch/WgRmMHy4kBq1SsXMjQCte1So6HBMvBPIW5SiMTmjCfZZiw4AYHK+B/JaOwaG9yRg2Ejg\
|
|||
class HostKeysTest (unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
f = open('hostfile.temp', 'w')
|
||||
with open('hostfile.temp', 'w') as f:
|
||||
f.write(test_hosts_file)
|
||||
f.close()
|
||||
|
||||
def tearDown(self):
|
||||
os.unlink('hostfile.temp')
|
||||
|
|
|
@ -186,10 +186,9 @@ class SFTPTest (unittest.TestCase):
|
|||
"""
|
||||
verify that a file can be created and written, and the size is correct.
|
||||
"""
|
||||
f = sftp.open(FOLDER + '/duck.txt', 'w')
|
||||
try:
|
||||
with sftp.open(FOLDER + '/duck.txt', 'w') as f:
|
||||
f.write(ARTICLE)
|
||||
f.close()
|
||||
self.assertEqual(sftp.stat(FOLDER + '/duck.txt').st_size, 1483)
|
||||
finally:
|
||||
sftp.remove(FOLDER + '/duck.txt')
|
||||
|
@ -209,40 +208,36 @@ class SFTPTest (unittest.TestCase):
|
|||
"""
|
||||
verify that a file can be opened for append, and tell() still works.
|
||||
"""
|
||||
f = sftp.open(FOLDER + '/append.txt', 'w')
|
||||
try:
|
||||
with sftp.open(FOLDER + '/append.txt', 'w') as f:
|
||||
f.write('first line\nsecond line\n')
|
||||
self.assertEqual(f.tell(), 23)
|
||||
f.close()
|
||||
|
||||
f = sftp.open(FOLDER + '/append.txt', 'a+')
|
||||
with sftp.open(FOLDER + '/append.txt', 'a+') as f:
|
||||
f.write('third line!!!\n')
|
||||
self.assertEqual(f.tell(), 37)
|
||||
self.assertEqual(f.stat().st_size, 37)
|
||||
f.seek(-26, f.SEEK_CUR)
|
||||
self.assertEqual(f.readline(), 'second line\n')
|
||||
finally:
|
||||
f.close()
|
||||
sftp.remove(FOLDER + '/append.txt')
|
||||
|
||||
def test_5_rename(self):
|
||||
"""
|
||||
verify that renaming a file works.
|
||||
"""
|
||||
f = sftp.open(FOLDER + '/first.txt', 'w')
|
||||
try:
|
||||
with sftp.open(FOLDER + '/first.txt', 'w') as f:
|
||||
f.write('content!\n')
|
||||
f.close()
|
||||
sftp.rename(FOLDER + '/first.txt', FOLDER + '/second.txt')
|
||||
try:
|
||||
f = sftp.open(FOLDER + '/first.txt', 'r')
|
||||
self.assertTrue(False, 'no exception on reading nonexistent file')
|
||||
except IOError:
|
||||
pass
|
||||
f = sftp.open(FOLDER + '/second.txt', 'r')
|
||||
with sftp.open(FOLDER + '/second.txt', 'r') as f:
|
||||
f.seek(-6, f.SEEK_END)
|
||||
self.assertEqual(u(f.read(4)), 'tent')
|
||||
f.close()
|
||||
finally:
|
||||
try:
|
||||
sftp.remove(FOLDER + '/first.txt')
|
||||
|
@ -300,10 +295,9 @@ class SFTPTest (unittest.TestCase):
|
|||
"""
|
||||
verify that the setstat functions (chown, chmod, utime, truncate) work.
|
||||
"""
|
||||
f = sftp.open(FOLDER + '/special', 'w')
|
||||
try:
|
||||
with sftp.open(FOLDER + '/special', 'w') as f:
|
||||
f.write('x' * 1024)
|
||||
f.close()
|
||||
|
||||
stat = sftp.stat(FOLDER + '/special')
|
||||
sftp.chmod(FOLDER + '/special', (stat.st_mode & ~o777) | o600)
|
||||
|
@ -339,12 +333,11 @@ class SFTPTest (unittest.TestCase):
|
|||
verify that the fsetstat functions (chown, chmod, utime, truncate)
|
||||
work on open files.
|
||||
"""
|
||||
f = sftp.open(FOLDER + '/special', 'w')
|
||||
try:
|
||||
with sftp.open(FOLDER + '/special', 'w') as f:
|
||||
f.write('x' * 1024)
|
||||
f.close()
|
||||
|
||||
f = sftp.open(FOLDER + '/special', 'r+')
|
||||
with sftp.open(FOLDER + '/special', 'r+') as f:
|
||||
stat = f.stat()
|
||||
f.chmod((stat.st_mode & ~o777) | o600)
|
||||
stat = f.stat()
|
||||
|
@ -372,7 +365,6 @@ class SFTPTest (unittest.TestCase):
|
|||
f.truncate(512)
|
||||
stat = f.stat()
|
||||
self.assertEqual(stat.st_size, 512)
|
||||
f.close()
|
||||
finally:
|
||||
sftp.remove(FOLDER + '/special')
|
||||
|
||||
|
@ -384,11 +376,10 @@ class SFTPTest (unittest.TestCase):
|
|||
buffering is reset on 'seek'.
|
||||
"""
|
||||
try:
|
||||
f = sftp.open(FOLDER + '/duck.txt', 'w')
|
||||
with sftp.open(FOLDER + '/duck.txt', 'w') as f:
|
||||
f.write(ARTICLE)
|
||||
f.close()
|
||||
|
||||
f = sftp.open(FOLDER + '/duck.txt', 'r+')
|
||||
with sftp.open(FOLDER + '/duck.txt', 'r+') as f:
|
||||
line_number = 0
|
||||
loc = 0
|
||||
pos_list = []
|
||||
|
@ -402,7 +393,6 @@ class SFTPTest (unittest.TestCase):
|
|||
self.assertEqual(f.readline()[:4], 'duck')
|
||||
f.seek(pos_list[10], f.SEEK_SET)
|
||||
self.assertEqual(f.readline(), 'duck types were equally resistant to exogenous insulin compared with chicken.\n')
|
||||
f.close()
|
||||
finally:
|
||||
sftp.remove(FOLDER + '/duck.txt')
|
||||
|
||||
|
@ -411,17 +401,15 @@ class SFTPTest (unittest.TestCase):
|
|||
create a text file, seek back and change part of it, and verify that the
|
||||
changes worked.
|
||||
"""
|
||||
f = sftp.open(FOLDER + '/testing.txt', 'w')
|
||||
try:
|
||||
with sftp.open(FOLDER + '/testing.txt', 'w') as f:
|
||||
f.write('hello kitty.\n')
|
||||
f.seek(-5, f.SEEK_CUR)
|
||||
f.write('dd')
|
||||
f.close()
|
||||
|
||||
self.assertEqual(sftp.stat(FOLDER + '/testing.txt').st_size, 13)
|
||||
f = sftp.open(FOLDER + '/testing.txt', 'r')
|
||||
with sftp.open(FOLDER + '/testing.txt', 'r') as f:
|
||||
data = f.read(20)
|
||||
f.close()
|
||||
self.assertEqual(data, 'hello kiddy.\n')
|
||||
finally:
|
||||
sftp.remove(FOLDER + '/testing.txt')
|
||||
|
@ -434,16 +422,14 @@ class SFTPTest (unittest.TestCase):
|
|||
# skip symlink tests on windows
|
||||
return
|
||||
|
||||
f = sftp.open(FOLDER + '/original.txt', 'w')
|
||||
try:
|
||||
with sftp.open(FOLDER + '/original.txt', 'w') as f:
|
||||
f.write('original\n')
|
||||
f.close()
|
||||
sftp.symlink('original.txt', FOLDER + '/link.txt')
|
||||
self.assertEqual(sftp.readlink(FOLDER + '/link.txt'), 'original.txt')
|
||||
|
||||
f = sftp.open(FOLDER + '/link.txt', 'r')
|
||||
with sftp.open(FOLDER + '/link.txt', 'r') as f:
|
||||
self.assertEqual(f.readlines(), ['original\n'])
|
||||
f.close()
|
||||
|
||||
cwd = sftp.normalize('.')
|
||||
if cwd[-1] == '/':
|
||||
|
@ -477,18 +463,16 @@ class SFTPTest (unittest.TestCase):
|
|||
"""
|
||||
verify that buffered writes are automatically flushed on seek.
|
||||
"""
|
||||
f = sftp.open(FOLDER + '/happy.txt', 'w', 1)
|
||||
try:
|
||||
with sftp.open(FOLDER + '/happy.txt', 'w', 1) as f:
|
||||
f.write('full line.\n')
|
||||
f.write('partial')
|
||||
f.seek(9, f.SEEK_SET)
|
||||
f.write('?\n')
|
||||
f.close()
|
||||
|
||||
f = sftp.open(FOLDER + '/happy.txt', 'r')
|
||||
with sftp.open(FOLDER + '/happy.txt', 'r') as f:
|
||||
self.assertEqual(f.readline(), 'full line?\n')
|
||||
self.assertEqual(f.read(7), 'partial')
|
||||
f.close()
|
||||
finally:
|
||||
try:
|
||||
sftp.remove(FOLDER + '/happy.txt')
|
||||
|
@ -544,9 +528,8 @@ class SFTPTest (unittest.TestCase):
|
|||
self.assertEqual(['beta'], sftp.listdir('.'))
|
||||
|
||||
sftp.chdir('beta')
|
||||
f = sftp.open('fish', 'w')
|
||||
with sftp.open('fish', 'w') as f:
|
||||
f.write('hello\n')
|
||||
f.close()
|
||||
sftp.chdir('..')
|
||||
self.assertEqual(['fish'], sftp.listdir('beta'))
|
||||
sftp.chdir('..')
|
||||
|
@ -575,17 +558,15 @@ class SFTPTest (unittest.TestCase):
|
|||
fd, localname = mkstemp()
|
||||
os.close(fd)
|
||||
text = b('All I wanted was a plastic bunny rabbit.\n')
|
||||
f = open(localname, 'wb')
|
||||
with open(localname, 'wb') as f:
|
||||
f.write(text)
|
||||
f.close()
|
||||
saved_progress = []
|
||||
def progress_callback(x, y):
|
||||
saved_progress.append((x, y))
|
||||
sftp.put(localname, FOLDER + '/bunny.txt', progress_callback)
|
||||
|
||||
f = sftp.open(FOLDER + '/bunny.txt', 'rb')
|
||||
with sftp.open(FOLDER + '/bunny.txt', 'rb') as f:
|
||||
self.assertEqual(text, f.read(128))
|
||||
f.close()
|
||||
self.assertEqual((41, 41), saved_progress[-1])
|
||||
|
||||
os.unlink(localname)
|
||||
|
@ -594,9 +575,8 @@ class SFTPTest (unittest.TestCase):
|
|||
saved_progress = []
|
||||
sftp.get(FOLDER + '/bunny.txt', localname, progress_callback)
|
||||
|
||||
f = open(localname, 'rb')
|
||||
with open(localname, 'rb') as f:
|
||||
self.assertEqual(text, f.read(128))
|
||||
f.close()
|
||||
self.assertEqual((41, 41), saved_progress[-1])
|
||||
|
||||
os.unlink(localname)
|
||||
|
@ -608,12 +588,11 @@ class SFTPTest (unittest.TestCase):
|
|||
(it's an sftp extension that we support, and may be the only ones who
|
||||
support it.)
|
||||
"""
|
||||
f = sftp.open(FOLDER + '/kitty.txt', 'w')
|
||||
with sftp.open(FOLDER + '/kitty.txt', 'w') as f:
|
||||
f.write('here kitty kitty' * 64)
|
||||
f.close()
|
||||
|
||||
try:
|
||||
f = sftp.open(FOLDER + '/kitty.txt', 'r')
|
||||
with sftp.open(FOLDER + '/kitty.txt', 'r') as f:
|
||||
sum = f.check('sha1')
|
||||
self.assertEqual('91059CFC6615941378D413CB5ADAF4C5EB293402', u(hexlify(sum)).upper())
|
||||
sum = f.check('md5', 0, 512)
|
||||
|
@ -621,7 +600,6 @@ class SFTPTest (unittest.TestCase):
|
|||
sum = f.check('md5', 0, 0, 510)
|
||||
self.assertEqual('EB3B45B8CD55A0707D99B177544A319F373183D241432BB2157AB9E46358C4AC90370B5CADE5D90336FC1716F90B36D6',
|
||||
u(hexlify(sum)).upper())
|
||||
f.close()
|
||||
finally:
|
||||
sftp.unlink(FOLDER + '/kitty.txt')
|
||||
|
||||
|
@ -645,9 +623,8 @@ class SFTPTest (unittest.TestCase):
|
|||
"""
|
||||
verify that unicode strings are encoded into utf8 correctly.
|
||||
"""
|
||||
f = sftp.open(FOLDER + '/something', 'w')
|
||||
with sftp.open(FOLDER + '/something', 'w') as f:
|
||||
f.write('okay')
|
||||
f.close()
|
||||
|
||||
try:
|
||||
sftp.rename(FOLDER + '/something', FOLDER + '/' + unicode_folder)
|
||||
|
@ -660,9 +637,8 @@ class SFTPTest (unittest.TestCase):
|
|||
sftp.mkdir(FOLDER + '/' + unicode_folder)
|
||||
try:
|
||||
sftp.chdir(FOLDER + '/' + unicode_folder)
|
||||
f = sftp.open('something', 'w')
|
||||
with sftp.open('something', 'w') as f:
|
||||
f.write('okay')
|
||||
f.close()
|
||||
sftp.unlink('something')
|
||||
finally:
|
||||
sftp.chdir()
|
||||
|
@ -675,14 +651,12 @@ class SFTPTest (unittest.TestCase):
|
|||
f = sftp.open(FOLDER + '/zero', 'w')
|
||||
f.close()
|
||||
try:
|
||||
f = sftp.open(FOLDER + '/zero', 'r')
|
||||
with sftp.open(FOLDER + '/zero', 'r') as f:
|
||||
f.readv([(0, 12)])
|
||||
f.close()
|
||||
|
||||
f = sftp.open(FOLDER + '/zero', 'r')
|
||||
with sftp.open(FOLDER + '/zero', 'r') as f:
|
||||
f.prefetch()
|
||||
f.read(100)
|
||||
f.close()
|
||||
finally:
|
||||
sftp.unlink(FOLDER + '/zero')
|
||||
|
||||
|
@ -695,9 +669,8 @@ class SFTPTest (unittest.TestCase):
|
|||
fd, localname = mkstemp()
|
||||
os.close(fd)
|
||||
text = 'All I wanted was a plastic bunny rabbit.\n'
|
||||
f = open(localname, 'w')
|
||||
with open(localname, 'w') as f:
|
||||
f.write(text)
|
||||
f.close()
|
||||
saved_progress = []
|
||||
def progress_callback(x, y):
|
||||
saved_progress.append((x, y))
|
||||
|
@ -705,9 +678,8 @@ class SFTPTest (unittest.TestCase):
|
|||
|
||||
self.assertEqual(SFTPAttributes().attr, res.attr)
|
||||
|
||||
f = sftp.open(FOLDER + '/bunny.txt', 'r')
|
||||
with sftp.open(FOLDER + '/bunny.txt', 'r') as f:
|
||||
self.assertEqual(text, f.read(128))
|
||||
f.close()
|
||||
self.assertEqual((41, 41), saved_progress[-1])
|
||||
|
||||
os.unlink(localname)
|
||||
|
@ -719,19 +691,17 @@ class SFTPTest (unittest.TestCase):
|
|||
|
||||
does not work except through paramiko. :( openssh fails.
|
||||
"""
|
||||
f = sftp.open(FOLDER + '/append.txt', 'a')
|
||||
try:
|
||||
with sftp.open(FOLDER + '/append.txt', 'a') as f:
|
||||
f.write('first line\nsecond line\n')
|
||||
f.seek(11, f.SEEK_SET)
|
||||
f.write('third line\n')
|
||||
f.close()
|
||||
|
||||
f = sftp.open(FOLDER + '/append.txt', 'r')
|
||||
with sftp.open(FOLDER + '/append.txt', 'r') as f:
|
||||
self.assertEqual(f.stat().st_size, 34)
|
||||
self.assertEqual(f.readline(), 'first line\n')
|
||||
self.assertEqual(f.readline(), 'second line\n')
|
||||
self.assertEqual(f.readline(), 'third line\n')
|
||||
f.close()
|
||||
finally:
|
||||
sftp.remove(FOLDER + '/append.txt')
|
||||
|
||||
|
|
|
@ -66,9 +66,8 @@ class BigSFTPTest (unittest.TestCase):
|
|||
numfiles = 100
|
||||
try:
|
||||
for i in range(numfiles):
|
||||
f = sftp.open('%s/file%d.txt' % (FOLDER, i), 'w', 1)
|
||||
with sftp.open('%s/file%d.txt' % (FOLDER, i), 'w', 1) as f:
|
||||
f.write('this is file #%d.\n' % i)
|
||||
f.close()
|
||||
sftp.chmod('%s/file%d.txt' % (FOLDER, i), o660)
|
||||
|
||||
# now make sure every file is there, by creating a list of filenmes
|
||||
|
@ -76,9 +75,8 @@ class BigSFTPTest (unittest.TestCase):
|
|||
numlist = list(range(numfiles))
|
||||
while len(numlist) > 0:
|
||||
r = numlist[random.randint(0, len(numlist) - 1)]
|
||||
f = sftp.open('%s/file%d.txt' % (FOLDER, r))
|
||||
with sftp.open('%s/file%d.txt' % (FOLDER, r)) as f:
|
||||
self.assertEqual(f.readline(), 'this is file #%d.\n' % r)
|
||||
f.close()
|
||||
numlist.remove(r)
|
||||
finally:
|
||||
for i in range(numfiles):
|
||||
|
@ -95,12 +93,11 @@ class BigSFTPTest (unittest.TestCase):
|
|||
kblob = (1024 * 'x')
|
||||
start = time.time()
|
||||
try:
|
||||
f = sftp.open('%s/hongry.txt' % FOLDER, 'w')
|
||||
with sftp.open('%s/hongry.txt' % FOLDER, 'w') as f:
|
||||
for n in range(1024):
|
||||
f.write(kblob)
|
||||
if n % 128 == 0:
|
||||
sys.stderr.write('.')
|
||||
f.close()
|
||||
sys.stderr.write(' ')
|
||||
|
||||
self.assertEqual(sftp.stat('%s/hongry.txt' % FOLDER).st_size, 1024 * 1024)
|
||||
|
@ -108,11 +105,10 @@ class BigSFTPTest (unittest.TestCase):
|
|||
sys.stderr.write('%ds ' % round(end - start))
|
||||
|
||||
start = time.time()
|
||||
f = sftp.open('%s/hongry.txt' % FOLDER, 'r')
|
||||
with sftp.open('%s/hongry.txt' % FOLDER, 'r') as f:
|
||||
for n in range(1024):
|
||||
data = f.read(1024)
|
||||
self.assertEqual(data, kblob)
|
||||
f.close()
|
||||
|
||||
end = time.time()
|
||||
sys.stderr.write('%ds ' % round(end - start))
|
||||
|
@ -127,13 +123,12 @@ class BigSFTPTest (unittest.TestCase):
|
|||
kblob = bytes().join([struct.pack('>H', n) for n in range(512)])
|
||||
start = time.time()
|
||||
try:
|
||||
f = sftp.open('%s/hongry.txt' % FOLDER, 'wb')
|
||||
with sftp.open('%s/hongry.txt' % FOLDER, 'wb') as f:
|
||||
f.set_pipelined(True)
|
||||
for n in range(1024):
|
||||
f.write(kblob)
|
||||
if n % 128 == 0:
|
||||
sys.stderr.write('.')
|
||||
f.close()
|
||||
sys.stderr.write(' ')
|
||||
|
||||
self.assertEqual(sftp.stat('%s/hongry.txt' % FOLDER).st_size, 1024 * 1024)
|
||||
|
@ -141,7 +136,7 @@ class BigSFTPTest (unittest.TestCase):
|
|||
sys.stderr.write('%ds ' % round(end - start))
|
||||
|
||||
start = time.time()
|
||||
f = sftp.open('%s/hongry.txt' % FOLDER, 'rb')
|
||||
with sftp.open('%s/hongry.txt' % FOLDER, 'rb') as f:
|
||||
f.prefetch()
|
||||
|
||||
# read on odd boundaries to make sure the bytes aren't getting scrambled
|
||||
|
@ -156,7 +151,6 @@ class BigSFTPTest (unittest.TestCase):
|
|||
offset = n % 1024
|
||||
self.assertEqual(data, k2blob[offset:offset + chunk])
|
||||
n += chunk
|
||||
f.close()
|
||||
|
||||
end = time.time()
|
||||
sys.stderr.write('%ds ' % round(end - start))
|
||||
|
@ -167,13 +161,12 @@ class BigSFTPTest (unittest.TestCase):
|
|||
sftp = get_sftp()
|
||||
kblob = bytes().join([struct.pack('>H', n) for n in range(512)])
|
||||
try:
|
||||
f = sftp.open('%s/hongry.txt' % FOLDER, 'wb')
|
||||
with sftp.open('%s/hongry.txt' % FOLDER, 'wb') as f:
|
||||
f.set_pipelined(True)
|
||||
for n in range(1024):
|
||||
f.write(kblob)
|
||||
if n % 128 == 0:
|
||||
sys.stderr.write('.')
|
||||
f.close()
|
||||
sys.stderr.write(' ')
|
||||
|
||||
self.assertEqual(sftp.stat('%s/hongry.txt' % FOLDER).st_size, 1024 * 1024)
|
||||
|
@ -182,7 +175,7 @@ class BigSFTPTest (unittest.TestCase):
|
|||
k2blob = kblob + kblob
|
||||
chunk = 793
|
||||
for i in range(10):
|
||||
f = sftp.open('%s/hongry.txt' % FOLDER, 'rb')
|
||||
with sftp.open('%s/hongry.txt' % FOLDER, 'rb') as f:
|
||||
f.prefetch()
|
||||
base_offset = (512 * 1024) + 17 * random.randint(1000, 2000)
|
||||
offsets = [base_offset + j * chunk for j in range(100)]
|
||||
|
@ -195,7 +188,6 @@ class BigSFTPTest (unittest.TestCase):
|
|||
n_offset = offset % 1024
|
||||
self.assertEqual(data, k2blob[n_offset:n_offset + chunk])
|
||||
offset += chunk
|
||||
f.close()
|
||||
end = time.time()
|
||||
sys.stderr.write('%ds ' % round(end - start))
|
||||
finally:
|
||||
|
@ -205,13 +197,12 @@ class BigSFTPTest (unittest.TestCase):
|
|||
sftp = get_sftp()
|
||||
kblob = bytes().join([struct.pack('>H', n) for n in range(512)])
|
||||
try:
|
||||
f = sftp.open('%s/hongry.txt' % FOLDER, 'wb')
|
||||
with sftp.open('%s/hongry.txt' % FOLDER, 'wb') as f:
|
||||
f.set_pipelined(True)
|
||||
for n in range(1024):
|
||||
f.write(kblob)
|
||||
if n % 128 == 0:
|
||||
sys.stderr.write('.')
|
||||
f.close()
|
||||
sys.stderr.write(' ')
|
||||
|
||||
self.assertEqual(sftp.stat('%s/hongry.txt' % FOLDER).st_size, 1024 * 1024)
|
||||
|
@ -220,7 +211,7 @@ class BigSFTPTest (unittest.TestCase):
|
|||
k2blob = kblob + kblob
|
||||
chunk = 793
|
||||
for i in range(10):
|
||||
f = sftp.open('%s/hongry.txt' % FOLDER, 'rb')
|
||||
with sftp.open('%s/hongry.txt' % FOLDER, 'rb') as f:
|
||||
base_offset = (512 * 1024) + 17 * random.randint(1000, 2000)
|
||||
# make a bunch of offsets and put them in random order
|
||||
offsets = [base_offset + j * chunk for j in range(100)]
|
||||
|
@ -234,7 +225,6 @@ class BigSFTPTest (unittest.TestCase):
|
|||
offset = readv_list[i][0]
|
||||
n_offset = offset % 1024
|
||||
self.assertEqual(next(ret), k2blob[n_offset:n_offset + chunk])
|
||||
f.close()
|
||||
end = time.time()
|
||||
sys.stderr.write('%ds ' % round(end - start))
|
||||
finally:
|
||||
|
@ -248,29 +238,26 @@ class BigSFTPTest (unittest.TestCase):
|
|||
sftp = get_sftp()
|
||||
kblob = (1024 * 'x')
|
||||
try:
|
||||
f = sftp.open('%s/hongry.txt' % FOLDER, 'w')
|
||||
with sftp.open('%s/hongry.txt' % FOLDER, 'w') as f:
|
||||
f.set_pipelined(True)
|
||||
for n in range(1024):
|
||||
f.write(kblob)
|
||||
if n % 128 == 0:
|
||||
sys.stderr.write('.')
|
||||
f.close()
|
||||
sys.stderr.write(' ')
|
||||
|
||||
self.assertEqual(sftp.stat('%s/hongry.txt' % FOLDER).st_size, 1024 * 1024)
|
||||
|
||||
for i in range(10):
|
||||
f = sftp.open('%s/hongry.txt' % FOLDER, 'r')
|
||||
with sftp.open('%s/hongry.txt' % FOLDER, 'r') as f:
|
||||
f.prefetch()
|
||||
f.close()
|
||||
f = sftp.open('%s/hongry.txt' % FOLDER, 'r')
|
||||
with sftp.open('%s/hongry.txt' % FOLDER, 'r') as f:
|
||||
f.prefetch()
|
||||
for n in range(1024):
|
||||
data = f.read(1024)
|
||||
self.assertEqual(data, kblob)
|
||||
if n % 128 == 0:
|
||||
sys.stderr.write('.')
|
||||
f.close()
|
||||
sys.stderr.write(' ')
|
||||
finally:
|
||||
sftp.remove('%s/hongry.txt' % FOLDER)
|
||||
|
@ -282,18 +269,17 @@ class BigSFTPTest (unittest.TestCase):
|
|||
sftp = get_sftp()
|
||||
kblob = bytes().join([struct.pack('>H', n) for n in range(512)])
|
||||
try:
|
||||
f = sftp.open('%s/hongry.txt' % FOLDER, 'wb')
|
||||
with sftp.open('%s/hongry.txt' % FOLDER, 'wb') as f:
|
||||
f.set_pipelined(True)
|
||||
for n in range(1024):
|
||||
f.write(kblob)
|
||||
if n % 128 == 0:
|
||||
sys.stderr.write('.')
|
||||
f.close()
|
||||
sys.stderr.write(' ')
|
||||
|
||||
self.assertEqual(sftp.stat('%s/hongry.txt' % FOLDER).st_size, 1024 * 1024)
|
||||
|
||||
f = sftp.open('%s/hongry.txt' % FOLDER, 'rb')
|
||||
with sftp.open('%s/hongry.txt' % FOLDER, 'rb') as f:
|
||||
f.prefetch()
|
||||
data = f.read(1024)
|
||||
self.assertEqual(data, kblob)
|
||||
|
@ -308,7 +294,6 @@ class BigSFTPTest (unittest.TestCase):
|
|||
self.assertEqual(k2blob[offset:offset + chunk_size], data)
|
||||
base_offset += chunk_size
|
||||
|
||||
f.close()
|
||||
sys.stderr.write(' ')
|
||||
finally:
|
||||
sftp.remove('%s/hongry.txt' % FOLDER)
|
||||
|
@ -321,24 +306,22 @@ class BigSFTPTest (unittest.TestCase):
|
|||
sftp = get_sftp()
|
||||
kblob = bytes().join([struct.pack('>H', n) for n in range(512)])
|
||||
try:
|
||||
f = sftp.open('%s/hongry.txt' % FOLDER, 'wb')
|
||||
with sftp.open('%s/hongry.txt' % FOLDER, 'wb') as f:
|
||||
f.set_pipelined(True)
|
||||
for n in range(1024):
|
||||
f.write(kblob)
|
||||
if n % 128 == 0:
|
||||
sys.stderr.write('.')
|
||||
f.close()
|
||||
sys.stderr.write(' ')
|
||||
|
||||
self.assertEqual(sftp.stat('%s/hongry.txt' % FOLDER).st_size, 1024 * 1024)
|
||||
|
||||
f = sftp.open('%s/hongry.txt' % FOLDER, 'rb')
|
||||
with sftp.open('%s/hongry.txt' % FOLDER, 'rb') as f:
|
||||
data = list(f.readv([(23 * 1024, 128 * 1024)]))
|
||||
self.assertEqual(1, len(data))
|
||||
data = data[0]
|
||||
self.assertEqual(128 * 1024, len(data))
|
||||
|
||||
f.close()
|
||||
sys.stderr.write(' ')
|
||||
finally:
|
||||
sftp.remove('%s/hongry.txt' % FOLDER)
|
||||
|
@ -350,9 +333,8 @@ class BigSFTPTest (unittest.TestCase):
|
|||
sftp = get_sftp()
|
||||
mblob = (1024 * 1024 * 'x')
|
||||
try:
|
||||
f = sftp.open('%s/hongry.txt' % FOLDER, 'w', 128 * 1024)
|
||||
with sftp.open('%s/hongry.txt' % FOLDER, 'w', 128 * 1024) as f:
|
||||
f.write(mblob)
|
||||
f.close()
|
||||
|
||||
self.assertEqual(sftp.stat('%s/hongry.txt' % FOLDER).st_size, 1024 * 1024)
|
||||
finally:
|
||||
|
@ -367,21 +349,19 @@ class BigSFTPTest (unittest.TestCase):
|
|||
t.packetizer.REKEY_BYTES = 512 * 1024
|
||||
k32blob = (32 * 1024 * 'x')
|
||||
try:
|
||||
f = sftp.open('%s/hongry.txt' % FOLDER, 'w', 128 * 1024)
|
||||
with sftp.open('%s/hongry.txt' % FOLDER, 'w', 128 * 1024) as f:
|
||||
for i in range(32):
|
||||
f.write(k32blob)
|
||||
f.close()
|
||||
|
||||
self.assertEqual(sftp.stat('%s/hongry.txt' % FOLDER).st_size, 1024 * 1024)
|
||||
self.assertNotEqual(t.H, t.session_id)
|
||||
|
||||
# try to read it too.
|
||||
f = sftp.open('%s/hongry.txt' % FOLDER, 'r', 128 * 1024)
|
||||
with sftp.open('%s/hongry.txt' % FOLDER, 'r', 128 * 1024) as f:
|
||||
f.prefetch()
|
||||
total = 0
|
||||
while total < 1024 * 1024:
|
||||
total += len(f.read(32 * 1024))
|
||||
f.close()
|
||||
finally:
|
||||
sftp.remove('%s/hongry.txt' % FOLDER)
|
||||
t.packetizer.REKEY_BYTES = pow(2, 30)
|
||||
|
|
|
@ -142,9 +142,8 @@ class UtilTest(ParamikoTest):
|
|||
self.assertEqual(hex, '9110e2f6793b69363e58173e9436b13a5a4b339005741d5c680e505f57d871347b4239f14fb5c46e857d5e100424873ba849ac699cea98d729e57b3e84378e8b')
|
||||
|
||||
def test_5_host_keys(self):
|
||||
f = open('hostfile.temp', 'w')
|
||||
with open('hostfile.temp', 'w') as f:
|
||||
f.write(test_hosts_file)
|
||||
f.close()
|
||||
try:
|
||||
hostdict = paramiko.util.load_host_keys('hostfile.temp')
|
||||
self.assertEqual(2, len(hostdict))
|
||||
|
|
Loading…
Reference in New Issue