Commit 0858702c authored by j4hangir's avatar j4hangir 😈
Browse files

add initial sync

parent 9f1060d7
......@@ -8,6 +8,7 @@ from glob import glob
from threading import Timer
import click
from filehash import FileHash
from rich import print
from watchdog.events import FileSystemEventHandler, EVENT_TYPE_DELETED, EVENT_TYPE_MOVED
from watchdog.observers import Observer
......@@ -28,7 +29,7 @@ def list_of(path: str):
class FSEvents(FileSystemEventHandler):
debounce = 3.0 # secs
debounce = 3.0 # secs
def __init__(self, src: str, dst: str, logger=None):
super().__init__()
......@@ -40,6 +41,22 @@ class FSEvents(FileSystemEventHandler):
self.timer = Timer(0, self.update_files)
self.logger = logger or logging.root
def sync(self) -> bool:
"Runs only once at first-run, returns whether or not can sync anyways"
if not self.files: raise Exception("No files to scan")
exists = 0
md5hasher = FileHash('md5')
for path in self.files:
src = f'{self.src}{path}'
if not os.path.exists(src): continue
exists += 1
dst = f'{self.dst}{path}'
if md5hasher.hash_file(src) != md5hasher.hash_file(dst):
self.to_update.add(path)
if self.to_update: self.update_files()
return exists / len(self.files) * 100 > 0.6
def update_files(self):
# print('going to update', self.to_update)
files = self.to_update.copy()
......@@ -52,7 +69,7 @@ class FSEvents(FileSystemEventHandler):
# @TODO 1/16/2021 by j4hangir: make handle create
if (path := event.src_path.replace('\\', '/')).endswith('~'): return # temp file, e.g. intellij
path = path.replace('\\', '/')[self.srclen:]
path = path[self.srclen:]
# print(path, self.files)
if not path in self.files: return
......@@ -119,7 +136,11 @@ def nmsync(dst):
print(f'Watching [bold magenta]{src}[/]')
observer = Observer()
observer.schedule(FSEvents(src, dst), src, recursive=True)
synchronizer = FSEvents(src, dst)
if not synchronizer.sync():
print("[red]Invalid folder, none of the files found![/]")
return
observer.schedule(synchronizer, src, recursive=True)
observer.start()
try:
while True:
......
......@@ -10,6 +10,7 @@ setup(name='nmsync',
install_requires=[
'watchdog',
'rich',
'filehash',
'click'
],
zip_safe=False)
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment