transfer.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. from pybrary.ascii import clean_name
  2. from setux.logger import silent
  3. from setux.core.action import Action
  4. class Downloader(Action):
  5. @property
  6. def label(self):
  7. return f'<- {self.name}'
  8. def check(self):
  9. dest = self.target.file.fetch(self.dest, sudo=self.sudo)
  10. return dest.size and dest.size > 0
  11. def deploy(self):
  12. if self.sudo:
  13. bak = self.dest
  14. self.target.dir('/tmp/setux/dload', mode=777, sudo=self.sudo, verbose=False)
  15. self.dest = f'/tmp/setux/dload/{clean_name(self.name)}'
  16. ok = self.target.download(
  17. url = self.url,
  18. dst = self.dest,
  19. )
  20. if self.sudo:
  21. ret, out, err = self.target.run(f'cp {self.dest} {bak}', sudo=self.sudo, report='quiet')
  22. ok = ok and ret==0
  23. self.dest = bak
  24. self.target.file(self.dest, verbose=False,
  25. user = self.user,
  26. group = self.group,
  27. mode = self.mode,
  28. sudo = self.sudo,
  29. )
  30. return ok
  31. class Sender(Action):
  32. @property
  33. def labeler(self):
  34. return silent
  35. @property
  36. def label(self):
  37. dst = f' -> {self.dst}' if self.dst != self.src else ''
  38. return f'send {self.src}{dst}'
  39. def check(self):
  40. lhash = self.local.file(self.src, verbose=False).hash
  41. rhash = self.target.file(self.dst, verbose=False).hash
  42. return rhash == lhash
  43. def deploy(self):
  44. if hasattr(self, 'sudo') and self.sudo:
  45. return self.target.do_send_as(self.sudo, self.src, self.dst)
  46. else:
  47. return self.target.do_send(self.src, self.dst)
  48. class Syncer(Action):
  49. @property
  50. def label(self):
  51. dst = self.dst if hasattr(self, 'dst') else self.src
  52. dst = f' -> {dst}' if dst != self.src else ''
  53. return f'sync {self.src}{dst}'
  54. def check(self):
  55. dst = self.dst if hasattr(self, 'dst') else self.src
  56. lhash = self.local.dir(self.src, verbose=False).hash
  57. rhash = self.target.dir(dst, verbose=False).hash
  58. return rhash == lhash
  59. def deploy(self):
  60. dst = self.dst if hasattr(self, 'dst') else self.src
  61. return self.target.do_sync(self.src, dst)