28
28
from gevent import sleep , socket , get_hub
29
29
from gevent .hub import Hub
30
30
from ssh2 .error_codes import LIBSSH2_ERROR_EAGAIN
31
- from ssh2 .exceptions import SFTPHandleError , \
32
- SFTPIOError as SFTPIOError_ssh2
31
+ from ssh2 .exceptions import SFTPHandleError , SFTPProtocolError
33
32
from ssh2 .session import Session
34
33
from ssh2 .sftp import LIBSSH2_FXF_READ , LIBSSH2_FXF_CREAT , LIBSSH2_FXF_WRITE , \
35
34
LIBSSH2_FXF_TRUNC , LIBSSH2_SFTP_S_IRUSR , LIBSSH2_SFTP_S_IRGRP , \
@@ -416,18 +415,12 @@ def _make_sftp(self):
416
415
sftp = self .session .sftp_init ()
417
416
except Exception as ex :
418
417
raise SFTPError (ex )
419
- errno = self .session .last_errno ()
420
- while (sftp is None and errno == LIBSSH2_ERROR_EAGAIN ) \
421
- or sftp == LIBSSH2_ERROR_EAGAIN :
418
+ while sftp == LIBSSH2_ERROR_EAGAIN :
422
419
wait_select (self .session )
423
420
try :
424
421
sftp = self .session .sftp_init ()
425
422
except Exception as ex :
426
423
raise SFTPError (ex )
427
- errno = self .session .last_errno ()
428
- if sftp is None and errno != LIBSSH2_ERROR_EAGAIN :
429
- raise SFTPError ("Error initialising SFTP - error code %s" ,
430
- errno )
431
424
return sftp
432
425
433
426
def _mkdir (self , sftp , directory ):
@@ -449,7 +442,7 @@ def _mkdir(self, sftp, directory):
449
442
LIBSSH2_SFTP_S_IXOTH
450
443
try :
451
444
self ._eagain (sftp .mkdir , directory , mode )
452
- except SFTPIOError_ssh2 as error :
445
+ except SFTPProtocolError as error :
453
446
msg = "Error occured creating directory %s on host %s - %s"
454
447
logger .error (msg , directory , self .host , error )
455
448
raise SFTPIOError (msg , directory , self .host , error )
@@ -485,7 +478,7 @@ def copy_file(self, local_file, remote_file, recurse=False,
485
478
if destination is not None :
486
479
try :
487
480
self ._eagain (sftp .stat , destination )
488
- except SFTPHandleError :
481
+ except ( SFTPHandleError , SFTPProtocolError ) :
489
482
self .mkdir (sftp , destination )
490
483
self .sftp_put (sftp , local_file , remote_file )
491
484
logger .info ("Copied local file %s to remote destination %s:%s" ,
@@ -508,7 +501,7 @@ def sftp_put(self, sftp, local_file, remote_file):
508
501
self ._sftp_put (remote_fh , local_file )
509
502
# THREAD_POOL.apply(
510
503
# sftp_put, args=(self.session, remote_fh, local_file))
511
- except SFTPIOError_ssh2 as ex :
504
+ except SFTPProtocolError as ex :
512
505
msg = "Error writing to remote file %s - %s"
513
506
logger .error (msg , remote_file , ex )
514
507
raise SFTPIOError (msg , remote_file , ex )
@@ -539,7 +532,7 @@ def mkdir(self, sftp, directory, _parent_path=None):
539
532
_dir = '/' .join ((_parent_path , _dir ))
540
533
try :
541
534
self ._eagain (sftp .stat , _dir )
542
- except SFTPHandleError as ex :
535
+ except ( SFTPHandleError , SFTPProtocolError ) as ex :
543
536
logger .debug ("Stat for %s failed with %s" , _dir , ex )
544
537
self ._mkdir (sftp , _dir )
545
538
if sub_dirs is not None :
@@ -582,7 +575,7 @@ def copy_remote_file(self, remote_file, local_file, recurse=False,
582
575
sftp = self ._make_sftp () if sftp is None else sftp
583
576
try :
584
577
self ._eagain (sftp .stat , remote_file )
585
- except SFTPHandleError :
578
+ except ( SFTPHandleError , SFTPProtocolError ) :
586
579
msg = "Remote file or directory %s does not exist"
587
580
logger .error (msg , remote_file )
588
581
raise SFTPIOError (msg , remote_file )
@@ -635,7 +628,7 @@ def scp_recv(self, remote_file, local_file, recurse=False, sftp=None,
635
628
if recurse :
636
629
try :
637
630
self ._eagain (sftp .stat , remote_file )
638
- except SFTPHandleError :
631
+ except ( SFTPHandleError , SFTPProtocolError ) :
639
632
msg = "Remote file or directory %s does not exist"
640
633
logger .error (msg , remote_file )
641
634
raise SCPError (msg , remote_file )
@@ -744,7 +737,7 @@ def scp_send(self, local_file, remote_file, recurse=False, sftp=None):
744
737
sftp = self ._make_sftp () if sftp is None else sftp
745
738
try :
746
739
self ._eagain (sftp .stat , destination )
747
- except SFTPHandleError :
740
+ except ( SFTPHandleError , SFTPProtocolError ) :
748
741
self .mkdir (sftp , destination )
749
742
self ._scp_send (local_file , remote_file )
750
743
logger .info ("SCP local file %s to remote destination %s:%s" ,
@@ -807,7 +800,7 @@ def sftp_get(self, sftp, remote_file, local_file):
807
800
# cannot be used simultaneously in multiple threads.
808
801
# THREAD_POOL.apply(
809
802
# sftp_get, args=(self.session, remote_fh, local_file))
810
- except SFTPIOError_ssh2 as ex :
803
+ except SFTPProtocolError as ex :
811
804
msg = "Error reading from remote file %s - %s"
812
805
logger .error (msg , remote_file , ex )
813
806
raise SFTPIOError (msg , remote_file , ex )
0 commit comments