Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # Spawns self using i3-listener, exits on
- # creation of /tmp/.stop-usb-copying
- import os, sys, subprocess, time, shutil
- STOP_F = '/tmp/.stop-usb-copying'
- USB_P_DIR = '/run/media/jeffrey/'
- if __name__ == '__main__':
- if len(sys.argv) < 2:
- print("Usage: usb-copier ./file-to-copy [./another-file]")
- print("Usage: usb-copier nospawn ./file-to-copy [./another-file]")
- print("Usage: usb-copier stop")
- sys.exit(5)
- if sys.argv[1] == "stop":
- subprocess.call(['touch', STOP_F])
- elif sys.argv[1] == "nospawn":
- files_watched = sys.argv[2:]
- copied = False # Toggled on USB detection
- while not os.path.exists(STOP_F):
- usb_in = False
- if os.path.exists(USB_P_DIR):
- usb_in = len(os.listdir(USB_P_DIR)) > 0
- if usb_in and not copied:
- usb_dirs = [ USB_P_DIR+'/'+x for x in os.listdir(USB_P_DIR) ]
- # Copy over and set flag
- for src_file in files_watched:
- for usb_dir in usb_dirs:
- shutil.copy(src_file, usb_dir)
- subprocess.call([
- 'notify-send', '-t', '2500',
- "Copied {} to {}".format(src_file, usb_dir)
- ])
- # Sync
- subprocess.call(['sync'])
- # Unmount based on heuristic; TODO use actual usb_dirs
- subprocess.call(['udisksctl', 'unmount', '-b', '/dev/sdb'])
- #for usb_dir in usb_dirs:
- copied = True
- elif not usb_in and copied:
- # Impossible to have copied w/o USB
- copied = False
- time.sleep(0.25)
- subprocess.call([
- 'notify-send', '-t', '2500',
- "usb-copier exited!"
- ])
- else:
- if os.path.exists(STOP_F):
- os.remove(STOP_F)
- # Spawn
- subprocess.call([
- "i3-msg", "exec",
- os.path.abspath(__file__),
- "nospawn"] +
- [ os.path.abspath(x) for x in sys.argv[1:]]
- )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement