50
49
from bzrlib.osutils import pathjoin, fancy_rename, getcwd
51
50
from bzrlib.symbol_versioning import (
52
51
deprecated_function,
55
54
from bzrlib.trace import mutter, warning
56
55
from bzrlib.transport import (
57
register_urlparse_netloc_protocol,
62
60
ConnectedTransport,
65
# Disable one particular warning that comes from paramiko in Python2.5; if
66
# this is emitted at the wrong time it tends to cause spurious test failures
67
# or at least noise in the test case::
69
# [1770/7639 in 86s, 1 known failures, 50 skipped, 2 missing features]
70
# test_permissions.TestSftpPermissions.test_new_files
71
# /var/lib/python-support/python2.5/paramiko/message.py:226: DeprecationWarning: integer argument expected, got float
72
# self.packet.write(struct.pack('>I', n))
73
warnings.filterwarnings('ignore',
74
'integer argument expected, got float',
75
category=DeprecationWarning,
76
module='paramiko.message')
80
65
except ImportError, e:
87
72
from paramiko.sftp_file import SFTPFile
75
register_urlparse_netloc_protocol('sftp')
90
78
_paramiko_version = getattr(paramiko, '__version_info__', (0, 0, 0))
91
79
# don't use prefetch unless paramiko version >= 1.5.5 (there were bugs earlier)
92
80
_default_do_prefetch = (_paramiko_version >= (1, 5, 5))
95
@deprecated_function(zero_ninety)
83
@deprecated_function(zero_nineteen)
96
84
def clear_connection_cache():
97
85
"""Remove all hosts from the SFTP connection cache.
243
238
self._translate_io_exception(e, path, ': error retrieving',
244
239
failure_exc=errors.ReadError)
246
def _readv(self, relpath, offsets):
241
def readv(self, relpath, offsets):
247
242
"""See Transport.readv()"""
248
243
# We overload the default readv() because we want to use a file
249
244
# that does not have prefetch enabled.
262
257
except (IOError, paramiko.SSHException), e:
263
258
self._translate_io_exception(e, path, ': error retrieving')
265
def recommended_page_size(self):
266
"""See Transport.recommended_page_size().
268
For SFTP we suggest a large page size to reduce the overhead
269
introduced by latency.
273
260
def _sftp_readv(self, fp, offsets, relpath='<unknown>'):
274
261
"""Use the readv() member of fp to do async readv.
382
369
:param mode: The final mode for the file
384
371
final_path = self._remote_path(relpath)
385
return self._put(final_path, f, mode=mode)
372
self._put(final_path, f, mode=mode)
387
374
def _put(self, abspath, f, mode=None):
388
375
"""Helper function so both put() and copy_abspaths can reuse the code"""
395
382
fout.set_pipelined(True)
396
length = self._pump(f, fout)
397
384
except (IOError, paramiko.SSHException), e:
398
385
self._translate_io_exception(e, tmp_abspath)
399
386
# XXX: This doesn't truly help like we would like it to.
539
525
"""Create a directory at the given path."""
540
526
self._mkdir(self._remote_path(relpath), mode=mode)
542
def open_write_stream(self, relpath, mode=None):
543
"""See Transport.open_write_stream."""
544
# initialise the file to zero-length
545
# this is three round trips, but we don't use this
546
# api more than once per write_group at the moment so
547
# it is a tolerable overhead. Better would be to truncate
548
# the file after opening. RBC 20070805
549
self.put_bytes_non_atomic(relpath, "", mode)
550
abspath = self._remote_path(relpath)
551
# TODO: jam 20060816 paramiko doesn't publicly expose a way to
552
# set the file mode at create time. If it does, use it.
553
# But for now, we just chmod later anyway.
556
handle = self._get_sftp().file(abspath, mode='wb')
557
handle.set_pipelined(True)
558
except (paramiko.SSHException, IOError), e:
559
self._translate_io_exception(e, abspath,
561
_file_streams[self.abspath(relpath)] = handle
562
return FileFileStream(self, relpath, handle)
564
528
def _translate_io_exception(self, e, path, more_info='',
565
529
failure_exc=PathError):
566
530
"""Translate a paramiko or IOError into a friendlier exception.