Skip to content

Instantly share code, notes, and snippets.

@Wookie1
Forked from davidhoness/usb_share.py
Last active May 17, 2021 11:02
Show Gist options
  • Save Wookie1/0c8aa562a353339c0e413b6b9cff2eef to your computer and use it in GitHub Desktop.
Save Wookie1/0c8aa562a353339c0e413b6b9cff2eef to your computer and use it in GitHub Desktop.
#!/usr/bin/python3
import time
import os
from watchdog.observers import Observer
from watchdog.events import *
CMD_MOUNT = "modprobe g_mass_storage file=/piusb.bin stall=0 ro=0 removable=1"
CMD_UNMOUNT = "modprobe -r g_mass_storage"
CMD_SYNC = "sync"
WATCH_PATH = "/mnt/usb_share"
ACT_EVENTS = [DirDeletedEvent, DirMovedEvent, FileDeletedEvent, FileModifiedEvent, FileMovedEvent]
ACT_TIME_OUT = 30
class DirtyHandler(FileSystemEventHandler):
def __init__(self):
self.reset()
def on_any_event(self, event):
if type(event) in ACT_EVENTS:
self._dirty = True
self._dirty_time = time.time()
@property
def dirty(self):
return self._dirty
@property
def dirty_time(self):
return self._dirty_time
def reset(self):
self._dirty = False
self._dirty_time = 0
self._path = None
os.system(CMD_MOUNT)
evh = DirtyHandler()
observer = Observer()
observer.schedule(evh, path=WATCH_PATH, recursive=True)
observer.start()
try:
while True:
while evh.dirty:
time_out = time.time() - evh.dirty_time
if time_out >= ACT_TIME_OUT:
os.system(CMD_UNMOUNT)
time.sleep(1)
os.system(CMD_SYNC)
time.sleep(1)
os.system(CMD_MOUNT)
evh.reset()
time.sleep(1)
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment