Guest User

Untitled

a guest
Apr 4th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. import errno
  2. import os
  3. import paramiko
  4.  
  5.  
  6. class Uploader(object):
  7. def __init__(self, host, basepath='', username=None, password=None, keyfile=None, *args, **kwargs):
  8. self.host = host
  9. self.basepath = basepath
  10. self.username = username
  11. self.password = password
  12. self.keyfile = keyfile
  13. self.currentpath = self.basepath
  14. super(Uploader, self).__init__(*args, **kwargs)
  15. self.setup()
  16.  
  17. def cwd(self):
  18. return self.currentpath
  19.  
  20. def cd(self, path=''):
  21. if path and path[0] == '/':
  22. newpath = os.path.abspath(os.path.join(self.basepath, path))
  23. else:
  24. newpath = os.path.abspath(os.path.join(self.currentpath, path))
  25. self.sftp.lstat(newpath)
  26. self.currentpath = os.path.join(newpath)
  27. return newpath
  28.  
  29. def setup(self):
  30. self.ssh = paramiko.SSHClient()
  31. self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  32. ssh_kwargs = {}
  33. if self.username:
  34. ssh_kwargs['username'] = self.username
  35. if self.password:
  36. ssh_kwargs['password'] = self.password
  37. if self.keyfile:
  38. ssh_kwargs['key_filename'] = self.keyfile
  39. self.ssh.connect(self.host, **ssh_kwargs)
  40. self.sftp = self.ssh.open_sftp()
  41.  
  42. def mkdir(self, path, recursive=True):
  43. try:
  44. self.sftp.mkdir(os.path.join(self.basepath, path))
  45. except IOError, inst:
  46. if getattr(inst,'errno') == errno.ENOENT and recursive:
  47. pathdirs = path.split(os.path.sep)
  48. for pp in range(1,len(pathdirs)):
  49. currentpath = os.path.join(self.basepath, *pathdirs[:pp+1])
  50. try:
  51. self.ftp.lstat(currentpath)
  52. except IOError, inst:
  53. if getattr(inst,'errno') == errno.ENOENT:
  54. self.sftp.mkdir(currentpath)
  55. else:
  56. raise
  57. else:
  58. raise
  59. return path
  60.  
  61. def upload(self, sourcefile, path=None, destname=None, overwrite=False):
  62. """
  63. sourcefile can be a filepath or a file object
  64. so long as the file object has a value for name
  65. """
  66. try:
  67. filename = sourcefile.name
  68. except AttributeError:
  69. filename = sourcefile
  70. if not destname:
  71. _, destname = os.path.split(filename)
  72. if path == None:
  73. path = self.currentpath
  74. else:
  75. path = os.path.join(self.basepath, path)
  76. destpath = os.path.join(path, destname)
  77. filename_prefix, filename_ext = os.path.splitext(destpath)
  78. counter = 0
  79. while True:
  80. try:
  81. exists = self.sftp.lstat(destpath)
  82. if overwrite:
  83. print "Filename found, but because overwrite is true I will overwrite it."
  84. break
  85. counter += 1
  86. destpath = "%s_%d%s" % (filename_prefix, counter, filename_ext)
  87. print "Filename found, will try %s" % destpath
  88. except:
  89. break
  90. print "Uploading %s to %s" % (filename, destpath)
  91. self.sftp.put(filename, destpath)
  92. return destpath
  93.  
  94. def finish(self):
  95. self.sftp.close()
  96. self.ssh.close()
Add Comment
Please, Sign In to add comment