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
808
WARNING: This breaks the SFTPClient abstraction, so it
788
809
could easily break against an updated version of paramiko.
790
:param relpath: The relative path, where the file should be opened
811
:param abspath: The remote absolute path where the file should be opened
812
:param mode: The mode permissions bits for the new file
792
path = self._sftp._adjust_cwd(self._abspath(relpath))
814
path = self._sftp._adjust_cwd(abspath)
793
815
attr = SFTPAttributes()
794
mode = (SFTP_FLAG_WRITE | SFTP_FLAG_CREATE
818
omode = (SFTP_FLAG_WRITE | SFTP_FLAG_CREATE
795
819
| SFTP_FLAG_TRUNC | SFTP_FLAG_EXCL)
797
t, msg = self._sftp._request(CMD_OPEN, path, mode, attr)
821
t, msg = self._sftp._request(CMD_OPEN, path, omode, attr)
798
822
if t != CMD_HANDLE:
799
823
raise TransportError('Expected an SFTP handle')
800
824
handle = msg.get_string()
801
return SFTPFile(self._sftp, handle, 'w', -1)
825
return SFTPFile(self._sftp, handle, 'wb', -1)
802
826
except (paramiko.SSHException, IOError), e:
803
self._translate_io_exception(e, relpath, ': unable to open',
827
self._translate_io_exception(e, abspath, ': unable to open',
804
828
failure_exc=FileExists)
831
# ------------- server test implementation --------------
835
from bzrlib.tests.stub_sftp import StubServer, StubSFTPServer
837
STUB_SERVER_KEY = """
838
-----BEGIN RSA PRIVATE KEY-----
839
MIICWgIBAAKBgQDTj1bqB4WmayWNPB+8jVSYpZYk80Ujvj680pOTh2bORBjbIAyz
840
oWGW+GUjzKxTiiPvVmxFgx5wdsFvF03v34lEVVhMpouqPAYQ15N37K/ir5XY+9m/
841
d8ufMCkjeXsQkKqFbAlQcnWMCRnOoPHS3I4vi6hmnDDeeYTSRvfLbW0fhwIBIwKB
842
gBIiOqZYaoqbeD9OS9z2K9KR2atlTxGxOJPXiP4ESqP3NVScWNwyZ3NXHpyrJLa0
843
EbVtzsQhLn6rF+TzXnOlcipFvjsem3iYzCpuChfGQ6SovTcOjHV9z+hnpXvQ/fon
844
soVRZY65wKnF7IAoUwTmJS9opqgrN6kRgCd3DASAMd1bAkEA96SBVWFt/fJBNJ9H
845
tYnBKZGw0VeHOYmVYbvMSstssn8un+pQpUm9vlG/bp7Oxd/m+b9KWEh2xPfv6zqU
846
avNwHwJBANqzGZa/EpzF4J8pGti7oIAPUIDGMtfIcmqNXVMckrmzQ2vTfqtkEZsA
847
4rE1IERRyiJQx6EJsz21wJmGV9WJQ5kCQQDwkS0uXqVdFzgHO6S++tjmjYcxwr3g
848
H0CoFYSgbddOT6miqRskOQF3DZVkJT3kyuBgU2zKygz52ukQZMqxCb1fAkASvuTv
849
qfpH87Qq5kQhNKdbbwbmd2NxlNabazPijWuphGTdW0VfJdWfklyS2Kr+iqrs/5wV
850
HhathJt636Eg7oIjAkA8ht3MQ+XSl9yIJIS8gVpbPxSw5OMfw0PjVE7tBdQruiSc
851
nvuQES5C9BMHjF39LZiGH1iLQy7FgdHyoP+eodI7
852
-----END RSA PRIVATE KEY-----
856
class SingleListener(threading.Thread):
858
def __init__(self, callback):
859
threading.Thread.__init__(self)
860
self._callback = callback
861
self._socket = socket.socket()
862
self._socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
863
self._socket.bind(('localhost', 0))
864
self._socket.listen(1)
865
self.port = self._socket.getsockname()[1]
866
self.stop_event = threading.Event()
869
s, _ = self._socket.accept()
870
# now close the listen socket
872
self._callback(s, self.stop_event)
875
self.stop_event.set()
876
# We should consider waiting for the other thread
877
# to stop, because otherwise we get spurious
878
# bzr: ERROR: Socket exception: Connection reset by peer (54)
879
# because the test suite finishes before the thread has a chance
880
# to close. (Especially when only running a few tests)
883
class SFTPServer(Server):
884
"""Common code for SFTP server facilities."""
886
def _get_sftp_url(self, path):
887
"""Calculate a sftp url to this server for path."""
888
return 'sftp://foo:bar@localhost:%d/%s' % (self._listener.port, path)
891
self._original_vendor = None
893
self._server_homedir = None
894
self._listener = None
899
def log(self, message):
900
"""What to do here? do we need this? Its for the StubServer.."""
901
self.logs.append(message)
903
def _run_server(self, s, stop_event):
904
ssh_server = paramiko.Transport(s)
905
key_file = os.path.join(self._homedir, 'test_rsa.key')
906
file(key_file, 'w').write(STUB_SERVER_KEY)
907
host_key = paramiko.RSAKey.from_private_key_file(key_file)
908
ssh_server.add_server_key(host_key)
909
server = StubServer(self)
910
ssh_server.set_subsystem_handler('sftp', paramiko.SFTPServer,
911
StubSFTPServer, root=self._root,
912
home=self._server_homedir)
913
event = threading.Event()
914
ssh_server.start_server(event, server)
916
stop_event.wait(30.0)
919
"""See bzrlib.transport.Server.setUp."""
920
# XXX: 20051124 jamesh
921
# The tests currently pop up a password prompt when an external ssh
922
# is used. This forces the use of the paramiko implementation.
924
self._original_vendor = _ssh_vendor
926
self._homedir = os.getcwdu()
927
if self._server_homedir is None:
928
self._server_homedir = self._homedir
930
# FIXME WINDOWS: _root should be _server_homedir[0]:/
931
self._listener = SingleListener(self._run_server)
932
self._listener.setDaemon(True)
933
self._listener.start()
936
"""See bzrlib.transport.Server.tearDown."""
938
self._listener.stop()
939
_ssh_vendor = self._original_vendor
942
class SFTPAbsoluteServer(SFTPServer):
943
"""A test server for sftp transports, using absolute urls."""
946
"""See bzrlib.transport.Server.get_url."""
947
return self._get_sftp_url(urlescape(self._homedir[1:]))
950
class SFTPHomeDirServer(SFTPServer):
951
"""A test server for sftp transports, using homedir relative urls."""
954
"""See bzrlib.transport.Server.get_url."""
955
return self._get_sftp_url("~/")
958
class SFTPSiblingAbsoluteServer(SFTPAbsoluteServer):
959
"""A test servere for sftp transports, using absolute urls to non-home."""
962
self._server_homedir = '/dev/noone/runs/tests/here'
963
super(SFTPSiblingAbsoluteServer, self).setUp()
966
def get_test_permutations():
967
"""Return the permutations to be used in testing."""
968
return [(SFTPTransport, SFTPAbsoluteServer),
969
(SFTPTransport, SFTPHomeDirServer),
970
(SFTPTransport, SFTPSiblingAbsoluteServer),