~bmerry/duplicity/pydrive-regular

« back to all changes in this revision

Viewing changes to duplicity/backends/_ssh_paramiko.py

  • Committer: Michael Terry
  • Date: 2014-04-17 20:50:57 UTC
  • mto: This revision was merged to the branch mainline in revision 975.
  • Revision ID: michael.terry@canonical.com-20140417205057-4cxo1yebh0oer02a
Solve except 2to3 fix

Show diffs side-by-side

added added

removed removed

Lines of Context:
134
134
        try:
135
135
            if os.path.isfile("/etc/ssh/ssh_known_hosts"):
136
136
                self.client.load_system_host_keys("/etc/ssh/ssh_known_hosts")
137
 
        except Exception, e:
 
137
        except Exception as e:
138
138
            raise BackendException("could not load /etc/ssh/ssh_known_hosts, maybe corrupt?")
139
139
        try:
140
140
            # use load_host_keys() to signal it's writable to paramiko
144
144
                self.client.load_host_keys(file)
145
145
            else:
146
146
                self.client._host_keys_filename = file
147
 
        except Exception, e:
 
147
        except Exception as e:
148
148
            raise BackendException("could not load ~/.ssh/known_hosts, maybe corrupt?")
149
149
 
150
150
        """ the next block reorganizes all host parameters into a
211
211
                                allow_agent=True, 
212
212
                                look_for_keys=True,
213
213
                                key_filename=self.config['identityfile'])
214
 
        except Exception, e:
 
214
        except Exception as e:
215
215
            raise BackendException("ssh connection to %s@%s:%d failed: %s" % (
216
216
                                    self.config['user'],
217
217
                                    self.config['hostname'],
229
229
        else:
230
230
            try:
231
231
                self.sftp=self.client.open_sftp()
232
 
            except Exception, e:
 
232
            except Exception as e:
233
233
                raise BackendException("sftp negotiation failed: %s" % e)
234
234
 
235
235
 
244
244
                        continue
245
245
                    try:
246
246
                        attrs=self.sftp.stat(d)
247
 
                    except IOError, e:
 
247
                    except IOError as e:
248
248
                        if e.errno == errno.ENOENT:
249
249
                            try:
250
250
                                self.sftp.mkdir(d)
251
 
                            except Exception, e:
 
251
                            except Exception as e:
252
252
                                raise BackendException("sftp mkdir %s failed: %s" % (self.sftp.normalize(".")+"/"+d,e))
253
253
                        else:
254
254
                            raise BackendException("sftp stat %s failed: %s" % (self.sftp.normalize(".")+"/"+d,e))
255
255
                    try:
256
256
                        self.sftp.chdir(d)
257
 
                    except Exception, e:
 
257
                    except Exception as e:
258
258
                        raise BackendException("sftp chdir to %s failed: %s" % (self.sftp.normalize(".")+"/"+d,e))
259
259
 
260
260
    def put(self, source_path, remote_filename = None):
275
275
                        chan=self.client.get_transport().open_session()
276
276
                        chan.settimeout(globals.timeout)
277
277
                        chan.exec_command("scp -t '%s'" % self.remote_dir) # scp in sink mode uses the arg as base directory
278
 
                    except Exception, e:
 
278
                    except Exception as e:
279
279
                        raise BackendException("scp execution failed: %s" % e)
280
280
                    # scp protocol: one 0x0 after startup, one after the Create meta, one after saving
281
281
                    # if there's a problem: 0x1 or 0x02 and some error text
298
298
                    try:
299
299
                        self.sftp.put(source_path.name,remote_filename)
300
300
                        return
301
 
                    except Exception, e:
 
301
                    except Exception as e:
302
302
                        raise BackendException("sftp put of %s (as %s) failed: %s" % (source_path.name,remote_filename,e))
303
 
            except Exception, e:
 
303
            except Exception as e:
304
304
                log.Warn("%s (Try %d of %d) Will retry in %d seconds." % (e,n,globals.num_retries,self.retry_delay))
305
305
        raise BackendException("Giving up trying to upload '%s' after %d attempts" % (remote_filename,n))
306
306
 
320
320
                        chan=self.client.get_transport().open_session()
321
321
                        chan.settimeout(globals.timeout)
322
322
                        chan.exec_command("scp -f '%s/%s'" % (self.remote_dir,remote_filename))
323
 
                    except Exception, e:
 
323
                    except Exception as e:
324
324
                        raise BackendException("scp execution failed: %s" % e)
325
325
 
326
326
                    chan.send('\0')     # overall ready indicator
343
343
                            buff=chan.recv(blocksize)
344
344
                            f.write(buff)
345
345
                            togo-=len(buff)
346
 
                    except Exception, e:
 
346
                    except Exception as e:
347
347
                        raise BackendException("scp get %s failed: %s" % (remote_filename,e))
348
348
 
349
349
                    msg=chan.recv(1)    # check the final status
357
357
                    try:
358
358
                        self.sftp.get(remote_filename,local_path.name)
359
359
                        return
360
 
                    except Exception, e:
 
360
                    except Exception as e:
361
361
                        raise BackendException("sftp get of %s (to %s) failed: %s" % (remote_filename,local_path.name,e))
362
362
                local_path.setdata()
363
 
            except Exception, e:
 
363
            except Exception as e:
364
364
                log.Warn("%s (Try %d of %d) Will retry in %d seconds." % (e,n,globals.num_retries,self.retry_delay))
365
365
        raise BackendException("Giving up trying to download '%s' after %d attempts" % (remote_filename,n))
366
366
 
379
379
                else:
380
380
                    try:
381
381
                        return self.sftp.listdir()
382
 
                    except Exception, e:
 
382
                    except Exception as e:
383
383
                        raise BackendException("sftp listing of %s failed: %s" % (self.sftp.getcwd(),e))
384
 
            except Exception, e:
 
384
            except Exception as e:
385
385
                log.Warn("%s (Try %d of %d) Will retry in %d seconds." % (e,n,globals.num_retries,self.retry_delay))
386
386
        raise BackendException("Giving up trying to list '%s' after %d attempts" % (self.remote_dir,n))
387
387
 
397
397
                    else:
398
398
                        try:
399
399
                            self.sftp.remove(fn)
400
 
                        except Exception, e:
 
400
                        except Exception as e:
401
401
                            raise BackendException("sftp rm %s failed: %s" % (fn,e))
402
402
 
403
403
                    # If we get here, we deleted this file successfully. Move on to the next one.
404
404
                    break
405
 
                except Exception, e:
 
405
                except Exception as e:
406
406
                    if n == globals.num_retries:
407
407
                        log.FatalError(str(e), log.ErrorCode.backend_error)
408
408
                    else:
416
416
            chan=self.client.get_transport().open_session()
417
417
            chan.settimeout(globals.timeout)
418
418
            chan.exec_command(cmd)
419
 
        except Exception, e:
 
419
        except Exception as e:
420
420
            raise BackendException("%sexecution failed: %s" % (errorprefix,e))
421
421
        output=chan.recv(-1)
422
422
        res=chan.recv_exit_status()
434
434
        sshconfig = paramiko.SSHConfig()
435
435
        try:
436
436
            sshconfig.parse(open(file))
437
 
        except Exception, e:
 
437
        except Exception as e:
438
438
            raise BackendException("could not load '%s', maybe corrupt?" % (file))
439
439
        
440
440
        return sshconfig.lookup(host)