/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/transport/sftp.py

  • Committer: Jelmer Vernooij
  • Date: 2017-05-22 00:56:52 UTC
  • mfrom: (6621.2.26 py3_pokes)
  • Revision ID: jelmer@jelmer.uk-20170522005652-yjahcr9hwmjkno7n
Merge Python3 porting work ('py3 pokes')

Show diffs side-by-side

added added

removed removed

Lines of Context:
35
35
import time
36
36
import warnings
37
37
 
38
 
from breezy import (
 
38
from .. import (
39
39
    config,
40
40
    debug,
41
41
    errors,
42
42
    urlutils,
43
43
    )
44
 
from breezy.errors import (FileExists,
 
44
from ..errors import (FileExists,
45
45
                           NoSuchFile,
46
46
                           TransportError,
47
47
                           LockError,
48
48
                           PathError,
49
49
                           ParamikoNotPresent,
50
50
                           )
51
 
from breezy.osutils import fancy_rename
52
 
from breezy.trace import mutter, warning
53
 
from breezy.transport import (
 
51
from ..osutils import fancy_rename
 
52
from ..trace import mutter, warning
 
53
from ..transport import (
54
54
    FileFileStream,
55
55
    _file_streams,
56
56
    ssh,
72
72
 
73
73
try:
74
74
    import paramiko
75
 
except ImportError, e:
 
75
except ImportError as e:
76
76
    raise ParamikoNotPresent(e)
77
77
else:
78
78
    from paramiko.sftp import (SFTP_FLAG_WRITE, SFTP_FLAG_CREATE,
414
414
            if self._do_prefetch and (getattr(f, 'prefetch', None) is not None):
415
415
                f.prefetch(size)
416
416
            return f
417
 
        except (IOError, paramiko.SSHException), e:
 
417
        except (IOError, paramiko.SSHException) as e:
418
418
            self._translate_io_exception(e, path, ': error retrieving',
419
419
                failure_exc=errors.ReadError)
420
420
 
445
445
            if 'sftp' in debug.debug_flags:
446
446
                mutter('seek and read %s offsets', len(offsets))
447
447
            return self._seek_and_read(fp, offsets, relpath)
448
 
        except (IOError, paramiko.SSHException), e:
 
448
        except (IOError, paramiko.SSHException) as e:
449
449
            self._translate_io_exception(e, path, ': error retrieving')
450
450
 
451
451
    def recommended_page_size(self):
487
487
            try:
488
488
                fout.set_pipelined(True)
489
489
                length = self._pump(f, fout)
490
 
            except (IOError, paramiko.SSHException), e:
 
490
            except (IOError, paramiko.SSHException) as e:
491
491
                self._translate_io_exception(e, tmp_abspath)
492
492
            # XXX: This doesn't truly help like we would like it to.
493
493
            #      The problem is that openssh strips sticky bits. So while we
508
508
            closed = True
509
509
            self._rename_and_overwrite(tmp_abspath, abspath)
510
510
            return length
511
 
        except Exception, e:
 
511
        except Exception as e:
512
512
            # If we fail, try to clean up the temporary file
513
513
            # before we throw the exception
514
514
            # but don't let another exception mess things up
543
543
                    fout = self._get_sftp().file(abspath, mode='wb')
544
544
                    fout.set_pipelined(True)
545
545
                    writer(fout)
546
 
                except (paramiko.SSHException, IOError), e:
 
546
                except (paramiko.SSHException, IOError) as e:
547
547
                    self._translate_io_exception(e, abspath,
548
548
                                                 ': unable to open')
549
549
 
622
622
 
623
623
    def _mkdir(self, abspath, mode=None):
624
624
        if mode is None:
625
 
            local_mode = 0777
 
625
            local_mode = 0o777
626
626
        else:
627
627
            local_mode = mode
628
628
        try:
636
636
                # the sgid bit is set, report a warning to the user
637
637
                # with the umask fix.
638
638
                stat = self._get_sftp().lstat(abspath)
639
 
                mode = mode & 0777 # can't set special bits anyway
640
 
                if mode != stat.st_mode & 0777:
641
 
                    if stat.st_mode & 06000:
 
639
                mode = mode & 0o777 # can't set special bits anyway
 
640
                if mode != stat.st_mode & 0o777:
 
641
                    if stat.st_mode & 0o6000:
642
642
                        warning('About to chmod %s over sftp, which will result'
643
643
                                ' in its suid or sgid bits being cleared.  If'
644
644
                                ' you want to preserve those bits, change your '
645
645
                                ' environment on the server to use umask 0%03o.'
646
 
                                % (abspath, 0777 - mode))
 
646
                                % (abspath, 0o777 - mode))
647
647
                    self._get_sftp().chmod(abspath, mode=mode)
648
 
        except (paramiko.SSHException, IOError), e:
 
648
        except (paramiko.SSHException, IOError) as e:
649
649
            self._translate_io_exception(e, abspath, ': unable to mkdir',
650
650
                failure_exc=FileExists)
651
651
 
669
669
        try:
670
670
            handle = self._get_sftp().file(abspath, mode='wb')
671
671
            handle.set_pipelined(True)
672
 
        except (paramiko.SSHException, IOError), e:
 
672
        except (paramiko.SSHException, IOError) as e:
673
673
            self._translate_io_exception(e, abspath,
674
674
                                         ': unable to open')
675
675
        _file_streams[self.abspath(relpath)] = handle
727
727
            result = fout.tell()
728
728
            self._pump(f, fout)
729
729
            return result
730
 
        except (IOError, paramiko.SSHException), e:
 
730
        except (IOError, paramiko.SSHException) as e:
731
731
            self._translate_io_exception(e, relpath, ': unable to append')
732
732
 
733
733
    def rename(self, rel_from, rel_to):
735
735
        try:
736
736
            self._get_sftp().rename(self._remote_path(rel_from),
737
737
                              self._remote_path(rel_to))
738
 
        except (IOError, paramiko.SSHException), e:
 
738
        except (IOError, paramiko.SSHException) as e:
739
739
            self._translate_io_exception(e, rel_from,
740
740
                    ': unable to rename to %r' % (rel_to))
741
741
 
749
749
            fancy_rename(abs_from, abs_to,
750
750
                         rename_func=sftp.rename,
751
751
                         unlink_func=sftp.remove)
752
 
        except (IOError, paramiko.SSHException), e:
 
752
        except (IOError, paramiko.SSHException) as e:
753
753
            self._translate_io_exception(e, abs_from,
754
754
                                         ': unable to rename to %r' % (abs_to))
755
755
 
764
764
        path = self._remote_path(relpath)
765
765
        try:
766
766
            self._get_sftp().remove(path)
767
 
        except (IOError, paramiko.SSHException), e:
 
767
        except (IOError, paramiko.SSHException) as e:
768
768
            self._translate_io_exception(e, path, ': unable to delete')
769
769
 
770
770
    def external_url(self):
788
788
        try:
789
789
            entries = self._get_sftp().listdir(path)
790
790
            self._report_activity(sum(map(len, entries)), 'read')
791
 
        except (IOError, paramiko.SSHException), e:
 
791
        except (IOError, paramiko.SSHException) as e:
792
792
            self._translate_io_exception(e, path, ': failed to list_dir')
793
793
        return [urlutils.escape(entry) for entry in entries]
794
794
 
797
797
        path = self._remote_path(relpath)
798
798
        try:
799
799
            return self._get_sftp().rmdir(path)
800
 
        except (IOError, paramiko.SSHException), e:
 
800
        except (IOError, paramiko.SSHException) as e:
801
801
            self._translate_io_exception(e, path, ': failed to rmdir')
802
802
 
803
803
    def stat(self, relpath):
805
805
        path = self._remote_path(relpath)
806
806
        try:
807
807
            return self._get_sftp().lstat(path)
808
 
        except (IOError, paramiko.SSHException), e:
 
808
        except (IOError, paramiko.SSHException) as e:
809
809
            self._translate_io_exception(e, path, ': unable to stat')
810
810
 
811
811
    def readlink(self, relpath):
813
813
        path = self._remote_path(relpath)
814
814
        try:
815
815
            return self._get_sftp().readlink(path)
816
 
        except (IOError, paramiko.SSHException), e:
 
816
        except (IOError, paramiko.SSHException) as e:
817
817
            self._translate_io_exception(e, path, ': unable to readlink')
818
818
 
819
819
    def symlink(self, source, link_name):
826
826
                    '%r: unable to create symlink to %r' % (link_name, source),
827
827
                    sftp_retval
828
828
                )
829
 
        except (IOError, paramiko.SSHException), e:
 
829
        except (IOError, paramiko.SSHException) as e:
830
830
            self._translate_io_exception(e, link_name,
831
831
                                         ': unable to create symlink to %r' % (source))
832
832
 
887
887
                raise TransportError('Expected an SFTP handle')
888
888
            handle = msg.get_string()
889
889
            return SFTPFile(self._get_sftp(), handle, 'wb', -1)
890
 
        except (paramiko.SSHException, IOError), e:
 
890
        except (paramiko.SSHException, IOError) as e:
891
891
            self._translate_io_exception(e, abspath, ': unable to open',
892
892
                failure_exc=FileExists)
893
893
 
901
901
 
902
902
def get_test_permutations():
903
903
    """Return the permutations to be used in testing."""
904
 
    from breezy.tests import stub_sftp
 
904
    from ..tests import stub_sftp
905
905
    return [(SFTPTransport, stub_sftp.SFTPAbsoluteServer),
906
906
            (SFTPTransport, stub_sftp.SFTPHomeDirServer),
907
907
            (SFTPTransport, stub_sftp.SFTPSiblingAbsoluteServer),