Nested watchers was not a good idea
This commit is contained in:
parent
bd262e8f42
commit
0098e172cc
2 changed files with 17 additions and 40 deletions
30
src/main.pyw
30
src/main.pyw
|
|
@ -65,36 +65,24 @@ def main(
|
||||||
logger.info(f"Watching directory {source}")
|
logger.info(f"Watching directory {source}")
|
||||||
logger.info(f"Backing up to {destination}")
|
logger.info(f"Backing up to {destination}")
|
||||||
|
|
||||||
# Spin up a different thread for each subdirectory to keep up with performance
|
watcher = Watcher(
|
||||||
subdirs = sorted(
|
logger,
|
||||||
list(
|
source.resolve().absolute(),
|
||||||
filter(
|
destination.resolve().absolute(),
|
||||||
lambda x: source.joinpath(x).resolve().is_dir(),
|
ignore_pattern,
|
||||||
os.listdir(source),
|
|
||||||
)
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
watchers = [
|
watcher.start()
|
||||||
Watcher(
|
|
||||||
logger,
|
|
||||||
source.joinpath(subdir).resolve().absolute(),
|
|
||||||
destination.joinpath(subdir).resolve().absolute(),
|
|
||||||
ignore_pattern,
|
|
||||||
)
|
|
||||||
for subdir in subdirs
|
|
||||||
]
|
|
||||||
|
|
||||||
[x.start() for x in watchers]
|
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
time.sleep(0.01)
|
time.sleep(0.001)
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
print("Quitting...")
|
print("Quitting...")
|
||||||
[x.stop() for x in watchers]
|
watcher.stop()
|
||||||
break
|
break
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,6 @@ from watchdog.events import (
|
||||||
FileSystemMovedEvent,
|
FileSystemMovedEvent,
|
||||||
)
|
)
|
||||||
from watchdog.observers import Observer
|
from watchdog.observers import Observer
|
||||||
from threading import current_thread
|
|
||||||
import re
|
import re
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -24,6 +23,7 @@ class Watcher:
|
||||||
self.source = source.absolute()
|
self.source = source.absolute()
|
||||||
self.dest = destination.absolute()
|
self.dest = destination.absolute()
|
||||||
self.observer = Observer()
|
self.observer = Observer()
|
||||||
|
self.observer.daemon = True
|
||||||
self.handler = Handler(self.logger, self.source, self.dest, ignore_pattern)
|
self.handler = Handler(self.logger, self.source, self.dest, ignore_pattern)
|
||||||
|
|
||||||
def start(self):
|
def start(self):
|
||||||
|
|
@ -38,9 +38,8 @@ class Watcher:
|
||||||
FileSystemMovedEvent,
|
FileSystemMovedEvent,
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
self.observer.setDaemon(True)
|
|
||||||
self.observer.start()
|
|
||||||
|
|
||||||
|
self.observer.start()
|
||||||
self.logger.info(f"The Observer for {self.source} has been started!")
|
self.logger.info(f"The Observer for {self.source} has been started!")
|
||||||
|
|
||||||
def stop(self):
|
def stop(self):
|
||||||
|
|
@ -65,9 +64,7 @@ class Handler(FileSystemEventHandler):
|
||||||
event (watchdog.events.FileSystemEvent): the event fired
|
event (watchdog.events.FileSystemEvent): the event fired
|
||||||
"""
|
"""
|
||||||
dest_str = f", dest: {event.dest_path}" if event.dest_path else ""
|
dest_str = f", dest: {event.dest_path}" if event.dest_path else ""
|
||||||
self.logger.debug(
|
self.logger.debug(f"type: {event.event_type}, src: {event.src_path}{dest_str}")
|
||||||
f"type: {event.event_type}, src: {event.src_path}{dest_str}"
|
|
||||||
)
|
|
||||||
|
|
||||||
def __in_destination(self, path: Path) -> Path:
|
def __in_destination(self, path: Path) -> Path:
|
||||||
"""
|
"""
|
||||||
|
|
@ -124,9 +121,7 @@ class Handler(FileSystemEventHandler):
|
||||||
os.makedirs(in_dest.parent.absolute(), exist_ok=True)
|
os.makedirs(in_dest.parent.absolute(), exist_ok=True)
|
||||||
shutil.copy2(src, self.__in_destination(src))
|
shutil.copy2(src, self.__in_destination(src))
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
self.logger.warn(
|
self.logger.warn(f"Exception thrown in Handler#on_created:\n\t{exc}")
|
||||||
f"Exception thrown in Handler#on_created:\n\t{exc}"
|
|
||||||
)
|
|
||||||
|
|
||||||
def on_modified(self, event: FileModifiedEvent) -> None:
|
def on_modified(self, event: FileModifiedEvent) -> None:
|
||||||
"""
|
"""
|
||||||
|
|
@ -154,9 +149,7 @@ class Handler(FileSystemEventHandler):
|
||||||
os.makedirs(in_dest.parent.absolute(), exist_ok=True)
|
os.makedirs(in_dest.parent.absolute(), exist_ok=True)
|
||||||
shutil.copy2(src, in_dest)
|
shutil.copy2(src, in_dest)
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
self.logger.warn(
|
self.logger.warn(f"Exception thrown in Handler#on_modified:\n\t{exc}")
|
||||||
f"Exception thrown in Handler#on_modified:\n\t{exc}"
|
|
||||||
)
|
|
||||||
|
|
||||||
def on_moved(self, event: FileSystemMovedEvent):
|
def on_moved(self, event: FileSystemMovedEvent):
|
||||||
"""
|
"""
|
||||||
|
|
@ -198,9 +191,7 @@ class Handler(FileSystemEventHandler):
|
||||||
|
|
||||||
self.__recursively_clean_dirs_upwards(in_dest_before.parent.absolute())
|
self.__recursively_clean_dirs_upwards(in_dest_before.parent.absolute())
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
self.logger.warn(
|
self.logger.warn(f"Exception thrown in Handler#on_moved:\n\t{exc}")
|
||||||
f"Exception thrown in Handler#on_moved:\n\t{exc}"
|
|
||||||
)
|
|
||||||
|
|
||||||
def on_deleted(self, event: FileDeletedEvent) -> None:
|
def on_deleted(self, event: FileDeletedEvent) -> None:
|
||||||
"""
|
"""
|
||||||
|
|
@ -224,6 +215,4 @@ class Handler(FileSystemEventHandler):
|
||||||
|
|
||||||
self.__recursively_clean_dirs_upwards(in_dest.parent.absolute())
|
self.__recursively_clean_dirs_upwards(in_dest.parent.absolute())
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
self.logger.warn(
|
self.logger.warn(f"Exception thrown in Handler#on_deleted:\n\t{exc}")
|
||||||
f"Exception thrown in Handler#on_deleted:\n\t{exc}"
|
|
||||||
)
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue