Another copy of my dotfiles. Because I don't completely trust GitHub.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.5 KiB

4 years ago
  1. import os
  2. import tarfile
  3. from gnupg import GPG
  4. from ranger.api.commands import Command
  5. from subprocess import run
  6. class encrypt(Command):
  7. """:encrypt
  8. Encrypts a file or dir (as a tar.gz) with the default gpg key
  9. """
  10. def tardir(self, path):
  11. """:tardir
  12. tars a directory into a dir of the same name appended with .tar.gz
  13. returns the name of the tarfile
  14. """
  15. output_path = path + '.tar.gz'
  16. with tarfile.open(output_path, "w:gz") as tar_handle:
  17. for root, dirs, files in os.walk(path):
  18. for file in files:
  19. tar_handle.add(os.path.join(root, file))
  20. return output_path
  21. def execute(self):
  22. gpg_home = os.path.join(os.path.expanduser('~'), '.gnupg/')
  23. default_recpipient = os.environ['DEFAULT_RECIPIENT']
  24. if not default_recpipient:
  25. self.fm.notify('DEFAULT_RECIPIENT environment variable must be set')
  26. return
  27. gpg = GPG(gpgbinary='/usr/bin/gpg', gnupghome=gpg_home)
  28. paths = [os.path.basename(f.path) for f in self.fm.thistab.get_selection()]
  29. for p in paths:
  30. if os.path.isdir(p):
  31. new_p = self.tardir(p)
  32. run(['rm', '-rf', p])
  33. p = new_p
  34. with open(p, 'rb') as f:
  35. enc = gpg.encrypt_file(f, default_recpipient)
  36. with open(p + '.gpg', 'wb+') as out:
  37. out.write(enc.data)
  38. if os.path.isfile(p):
  39. os.remove(p)