345
def put(self, relpath, f):
362
def put(self, relpath, f, mode=None):
347
364
Copy the file-like or string object into the location.
349
366
:param relpath: Location to put the contents, relative to base.
350
367
:param f: File-like or string object.
368
:param mode: The final mode for the file
352
final_path = self._abspath(relpath)
353
tmp_relpath = '%s.tmp.%.9f.%d.%d' % (relpath, time.time(),
370
final_path = self._remote_path(relpath)
371
self._put(final_path, f, mode=mode)
373
def _put(self, abspath, f, mode=None):
374
"""Helper function so both put() and copy_abspaths can reuse the code"""
375
tmp_abspath = '%s.tmp.%.9f.%d.%d' % (abspath, time.time(),
354
376
os.getpid(), random.randint(0,0x7FFFFFFF))
355
tmp_abspath = self._abspath(tmp_relpath)
356
fout = self._sftp_open_exclusive(tmp_relpath)
377
fout = self._sftp_open_exclusive(tmp_abspath, mode=mode)
381
fout.set_pipelined(True)
360
382
self._pump(f, fout)
361
except (paramiko.SSHException, IOError), e:
362
self._translate_io_exception(e, relpath, ': unable to write')
383
except (IOError, paramiko.SSHException), e:
384
self._translate_io_exception(e, tmp_abspath)
386
self._sftp.chmod(tmp_abspath, mode)
389
self._rename(tmp_abspath, abspath)
363
390
except Exception, e:
364
391
# If we fail, try to clean up the temporary file
365
392
# before we throw the exception
366
393
# but don't let another exception mess things up
394
# Write out the traceback, because otherwise
395
# the catch and throw destroys it
397
mutter(traceback.format_exc())
369
401
self._sftp.remove(tmp_abspath)
374
# sftp rename doesn't allow overwriting, so play tricks:
375
tmp_safety = 'bzr.tmp.%.9f.%d.%d' % (time.time(), os.getpid(), random.randint(0, 0x7FFFFFFF))
376
tmp_safety = self._abspath(tmp_safety)
378
self._sftp.rename(final_path, tmp_safety)
385
self._sftp.rename(tmp_abspath, final_path)
386
except (paramiko.SSHException, IOError), e:
387
self._translate_io_exception(e, relpath, ': unable to rename')
393
self._sftp.unlink(tmp_safety)
395
self._sftp.rename(tmp_safety, final_path)
403
# raise the saved except
405
# raise the original with its traceback if we can.
397
408
def iter_files_recursive(self):
398
409
"""Walk the relative paths of all files in this transport."""
409
def mkdir(self, relpath):
420
def mkdir(self, relpath, mode=None):
410
421
"""Create a directory at the given path."""
412
path = self._abspath(relpath)
423
path = self._remote_path(relpath)
424
# In the paramiko documentation, it says that passing a mode flag
425
# will filtered against the server umask.
426
# StubSFTPServer does not do this, which would be nice, because it is
427
# what we really want :)
428
# However, real servers do use umask, so we really should do it that way
413
429
self._sftp.mkdir(path)
431
self._sftp.chmod(path, mode=mode)
414
432
except (paramiko.SSHException, IOError), e:
415
self._translate_io_exception(e, relpath, ': unable to mkdir',
433
self._translate_io_exception(e, path, ': unable to mkdir',
416
434
failure_exc=FileExists)
418
436
def _translate_io_exception(self, e, path, more_info='', failure_exc=NoSuchFile):
497
513
total = self._get_total(relpaths)
499
515
for path in relpaths:
500
path_from = self._abspath(relpath)
501
path_to = other._abspath(relpath)
516
path_from = self._remote_path(relpath)
517
path_to = other._remote_path(relpath)
502
518
self._update_pb(pb, 'copy-to', count, total)
503
self._copy_abspaths(path_from, path_to)
519
self._copy_abspaths(path_from, path_to, mode=mode)
507
return super(SFTPTransport, self).copy_to(relpaths, other, pb=pb)
509
# The dummy implementation just does a simple get + put
510
def copy_entry(path):
511
other.put(path, self.get(path))
513
return self._iterate_over(relpaths, copy_entry, pb, 'copy_to', expand=False)
523
return super(SFTPTransport, self).copy_to(relpaths, other, mode=mode, pb=pb)
525
def _rename(self, abs_from, abs_to):
526
"""Do a fancy rename on the remote server.
528
Using the implementation provided by osutils.
531
fancy_rename(abs_from, abs_to,
532
rename_func=self._sftp.rename,
533
unlink_func=self._sftp.remove)
534
except (IOError, paramiko.SSHException), e:
535
self._translate_io_exception(e, abs_from, ': unable to rename to %r' % (abs_to))
515
537
def move(self, rel_from, rel_to):
516
538
"""Move the item at rel_from to the location at rel_to"""
517
path_from = self._abspath(rel_from)
518
path_to = self._abspath(rel_to)
520
self._sftp.rename(path_from, path_to)
521
except (IOError, paramiko.SSHException), e:
522
self._translate_io_exception(e, path_from, ': unable to move to: %r' % path_to)
539
path_from = self._remote_path(rel_from)
540
path_to = self._remote_path(rel_to)
541
self._rename(path_from, path_to)
524
543
def delete(self, relpath):
525
544
"""Delete the item at relpath"""
526
path = self._abspath(relpath)
545
path = self._remote_path(relpath)
528
547
self._sftp.remove(path)
529
548
except (IOError, paramiko.SSHException), e:
787
806
WARNING: This breaks the SFTPClient abstraction, so it
788
807
could easily break against an updated version of paramiko.
790
:param relpath: The relative path, where the file should be opened
809
:param abspath: The remote absolute path where the file should be opened
810
:param mode: The mode permissions bits for the new file
792
path = self._sftp._adjust_cwd(self._abspath(relpath))
812
path = self._sftp._adjust_cwd(abspath)
793
813
attr = SFTPAttributes()
794
mode = (SFTP_FLAG_WRITE | SFTP_FLAG_CREATE
816
omode = (SFTP_FLAG_WRITE | SFTP_FLAG_CREATE
795
817
| SFTP_FLAG_TRUNC | SFTP_FLAG_EXCL)
797
t, msg = self._sftp._request(CMD_OPEN, path, mode, attr)
819
t, msg = self._sftp._request(CMD_OPEN, path, omode, attr)
798
820
if t != CMD_HANDLE:
799
821
raise TransportError('Expected an SFTP handle')
800
822
handle = msg.get_string()
801
return SFTPFile(self._sftp, handle, 'w', -1)
823
return SFTPFile(self._sftp, handle, 'wb', -1)
802
824
except (paramiko.SSHException, IOError), e:
803
self._translate_io_exception(e, relpath, ': unable to open',
825
self._translate_io_exception(e, abspath, ': unable to open',
804
826
failure_exc=FileExists)
829
# ------------- server test implementation --------------
833
from bzrlib.tests.stub_sftp import StubServer, StubSFTPServer
835
STUB_SERVER_KEY = """
836
-----BEGIN RSA PRIVATE KEY-----
837
MIICWgIBAAKBgQDTj1bqB4WmayWNPB+8jVSYpZYk80Ujvj680pOTh2bORBjbIAyz
838
oWGW+GUjzKxTiiPvVmxFgx5wdsFvF03v34lEVVhMpouqPAYQ15N37K/ir5XY+9m/
839
d8ufMCkjeXsQkKqFbAlQcnWMCRnOoPHS3I4vi6hmnDDeeYTSRvfLbW0fhwIBIwKB
840
gBIiOqZYaoqbeD9OS9z2K9KR2atlTxGxOJPXiP4ESqP3NVScWNwyZ3NXHpyrJLa0
841
EbVtzsQhLn6rF+TzXnOlcipFvjsem3iYzCpuChfGQ6SovTcOjHV9z+hnpXvQ/fon
842
soVRZY65wKnF7IAoUwTmJS9opqgrN6kRgCd3DASAMd1bAkEA96SBVWFt/fJBNJ9H
843
tYnBKZGw0VeHOYmVYbvMSstssn8un+pQpUm9vlG/bp7Oxd/m+b9KWEh2xPfv6zqU
844
avNwHwJBANqzGZa/EpzF4J8pGti7oIAPUIDGMtfIcmqNXVMckrmzQ2vTfqtkEZsA
845
4rE1IERRyiJQx6EJsz21wJmGV9WJQ5kCQQDwkS0uXqVdFzgHO6S++tjmjYcxwr3g
846
H0CoFYSgbddOT6miqRskOQF3DZVkJT3kyuBgU2zKygz52ukQZMqxCb1fAkASvuTv
847
qfpH87Qq5kQhNKdbbwbmd2NxlNabazPijWuphGTdW0VfJdWfklyS2Kr+iqrs/5wV
848
HhathJt636Eg7oIjAkA8ht3MQ+XSl9yIJIS8gVpbPxSw5OMfw0PjVE7tBdQruiSc
849
nvuQES5C9BMHjF39LZiGH1iLQy7FgdHyoP+eodI7
850
-----END RSA PRIVATE KEY-----
854
class SingleListener(threading.Thread):
856
def __init__(self, callback):
857
threading.Thread.__init__(self)
858
self._callback = callback
859
self._socket = socket.socket()
860
self._socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
861
self._socket.bind(('localhost', 0))
862
self._socket.listen(1)
863
self.port = self._socket.getsockname()[1]
864
self.stop_event = threading.Event()
867
s, _ = self._socket.accept()
868
# now close the listen socket
870
self._callback(s, self.stop_event)
873
self.stop_event.set()
874
# We should consider waiting for the other thread
875
# to stop, because otherwise we get spurious
876
# bzr: ERROR: Socket exception: Connection reset by peer (54)
877
# because the test suite finishes before the thread has a chance
878
# to close. (Especially when only running a few tests)
881
class SFTPServer(Server):
882
"""Common code for SFTP server facilities."""
884
def _get_sftp_url(self, path):
885
"""Calculate a sftp url to this server for path."""
886
return 'sftp://foo:bar@localhost:%d/%s' % (self._listener.port, path)
889
self._original_vendor = None
891
self._server_homedir = None
892
self._listener = None
897
def log(self, message):
898
"""What to do here? do we need this? Its for the StubServer.."""
899
self.logs.append(message)
901
def _run_server(self, s, stop_event):
902
ssh_server = paramiko.Transport(s)
903
key_file = os.path.join(self._homedir, 'test_rsa.key')
904
file(key_file, 'w').write(STUB_SERVER_KEY)
905
host_key = paramiko.RSAKey.from_private_key_file(key_file)
906
ssh_server.add_server_key(host_key)
907
server = StubServer(self)
908
ssh_server.set_subsystem_handler('sftp', paramiko.SFTPServer,
909
StubSFTPServer, root=self._root,
910
home=self._server_homedir)
911
event = threading.Event()
912
ssh_server.start_server(event, server)
914
stop_event.wait(30.0)
917
"""See bzrlib.transport.Server.setUp."""
918
# XXX: 20051124 jamesh
919
# The tests currently pop up a password prompt when an external ssh
920
# is used. This forces the use of the paramiko implementation.
922
self._original_vendor = _ssh_vendor
924
self._homedir = os.getcwdu()
925
if self._server_homedir is None:
926
self._server_homedir = self._homedir
928
# FIXME WINDOWS: _root should be _server_homedir[0]:/
929
self._listener = SingleListener(self._run_server)
930
self._listener.setDaemon(True)
931
self._listener.start()
934
"""See bzrlib.transport.Server.tearDown."""
936
self._listener.stop()
937
_ssh_vendor = self._original_vendor
940
class SFTPAbsoluteServer(SFTPServer):
941
"""A test server for sftp transports, using absolute urls."""
944
"""See bzrlib.transport.Server.get_url."""
945
return self._get_sftp_url("%%2f%s" %
946
urlescape(self._homedir[1:]))
949
class SFTPHomeDirServer(SFTPServer):
950
"""A test server for sftp transports, using homedir relative urls."""
953
"""See bzrlib.transport.Server.get_url."""
954
return self._get_sftp_url("")
957
class SFTPSiblingAbsoluteServer(SFTPAbsoluteServer):
958
"""A test servere for sftp transports, using absolute urls to non-home."""
961
self._server_homedir = '/dev/noone/runs/tests/here'
962
super(SFTPSiblingAbsoluteServer, self).setUp()
965
def get_test_permutations():
966
"""Return the permutations to be used in testing."""
967
return [(SFTPTransport, SFTPAbsoluteServer),
968
(SFTPTransport, SFTPHomeDirServer),
969
(SFTPTransport, SFTPSiblingAbsoluteServer),