PageRenderTime 24ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/silversupport/transfermethods.py

https://bitbucket.org/ianb/silverlining/
Python | 86 lines | 70 code | 16 blank | 0 comment | 24 complexity | c966310e00154d37c179ef75a66bfe53 MD5 | raw file
Possible License(s): GPL-2.0
  1. import os
  2. from silversupport.shell import run
  3. import warnings
  4. warnings.filterwarnings('ignore', r'tempnam is a potential security risk')
  5. def local(dir, dest):
  6. if is_archive(dest):
  7. archive(dir, dest)
  8. else:
  9. run(['cp', '-ar', dir, dest])
  10. def ssh(dir, dest):
  11. if is_archive(dest):
  12. tmp = make_temp_name(dest)
  13. try:
  14. archive(dir, tmp)
  15. run(['scp', '-p', tmp, dest])
  16. finally:
  17. if os.path.exists(tmp):
  18. os.unlink(tmp)
  19. else:
  20. run(['scp', '-rp', dir, dest])
  21. def rsync(dir, dest):
  22. if is_archive(dest):
  23. tmp = make_temp_name(dest)
  24. try:
  25. archive(dir, tmp)
  26. run(['rsync', '-t', dir, dest])
  27. finally:
  28. if os.path.exists(tmp):
  29. os.unlink(tmp)
  30. else:
  31. run(['rsync', '-rt', dir, dest])
  32. def make_temp_name(dest):
  33. name = os.tempnam() + extension(dest)
  34. return name
  35. def is_archive(name):
  36. return extension(name) in ('.zip', '.tar.gz', '.tar.bz2', '.tgz', 'tbz2')
  37. def extension(name):
  38. path, ext = os.path.splitext(name)
  39. if ext in ('.gz', '.bz2'):
  40. ext = os.path.splitext(path)[1] + ext
  41. return ext
  42. def archive(dir, dest):
  43. ext = extension(dest)
  44. if ext == '.zip':
  45. run(['zip', '-r', '--symlinks', dest, dir])
  46. elif ext in ('.tar.gz', '.tgz', '.tar.bz2', '.tbz2'):
  47. if ext in ('.tar.gz', '.tgz'):
  48. op = 'z'
  49. else:
  50. op = 'j'
  51. run(['tar', op + 'fc', dest, '.'],
  52. cwd=dir)
  53. else:
  54. assert 0, 'unknown extension %r' % ext
  55. def unarchive(archive, dest):
  56. if not os.path.exists(dest):
  57. os.makedirs(dest)
  58. ext = extension(dest)
  59. archive = os.path.abspath(archive)
  60. if ext == '.zip':
  61. run(['unzip', archive], cwd=dest)
  62. elif ext in ('.tar.gz', '.tgz', '.tar.gz2', '.tbz2'):
  63. if ext in ('.tar.gz', '.tgz'):
  64. op = 'z'
  65. else:
  66. op = 'j'
  67. run(['tar', op+'fx', archive],
  68. cwd=dest)
  69. else:
  70. assert 0, 'unknown extension: %r' % ext