Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.89 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. # Spawns self using i3-listener, exits on
  4. # creation of /tmp/.stop-usb-copying
  5.  
  6. import os, sys, subprocess, time, shutil
  7.  
  8. STOP_F = '/tmp/.stop-usb-copying'
  9. USB_P_DIR = '/run/media/jeffrey/'
  10.  
  11. if __name__ == '__main__':
  12. if len(sys.argv) < 2:
  13. print("Usage: usb-copier ./file-to-copy [./another-file]")
  14. print("Usage: usb-copier nospawn ./file-to-copy [./another-file]")
  15. print("Usage: usb-copier stop")
  16. sys.exit(5)
  17.  
  18. if sys.argv[1] == "stop":
  19. subprocess.call(['touch', STOP_F])
  20.  
  21. elif sys.argv[1] == "nospawn":
  22.  
  23. files_watched = sys.argv[2:]
  24. copied = False # Toggled on USB detection
  25. while not os.path.exists(STOP_F):
  26. usb_in = False
  27. if os.path.exists(USB_P_DIR):
  28. usb_in = len(os.listdir(USB_P_DIR)) > 0
  29.  
  30. if usb_in and not copied:
  31. usb_dirs = [ USB_P_DIR+'/'+x for x in os.listdir(USB_P_DIR) ]
  32. # Copy over and set flag
  33. for src_file in files_watched:
  34. for usb_dir in usb_dirs:
  35. shutil.copy(src_file, usb_dir)
  36. subprocess.call([
  37. 'notify-send', '-t', '2500',
  38. "Copied {} to {}".format(src_file, usb_dir)
  39. ])
  40.  
  41. # Sync
  42. subprocess.call(['sync'])
  43.  
  44. # Unmount based on heuristic; TODO use actual usb_dirs
  45. subprocess.call(['udisksctl', 'unmount', '-b', '/dev/sdb'])
  46. #for usb_dir in usb_dirs:
  47.  
  48. copied = True
  49.  
  50. elif not usb_in and copied:
  51. # Impossible to have copied w/o USB
  52. copied = False
  53.  
  54. time.sleep(0.25)
  55.  
  56. subprocess.call([
  57. 'notify-send', '-t', '2500',
  58. "usb-copier exited!"
  59. ])
  60.  
  61. else:
  62. if os.path.exists(STOP_F):
  63. os.remove(STOP_F)
  64. # Spawn
  65. subprocess.call([
  66. "i3-msg", "exec",
  67. os.path.abspath(__file__),
  68. "nospawn"] +
  69. [ os.path.abspath(x) for x in sys.argv[1:]]
  70. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement