feat: make NAS self-build deployment canonical
This commit is contained in:
+38
-20
@@ -1,5 +1,6 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import threading
|
||||
import time
|
||||
from pathlib import Path
|
||||
|
||||
@@ -15,33 +16,48 @@ from .scanner import index_file
|
||||
|
||||
class Handler(FileSystemEventHandler):
|
||||
def __init__(self, root: Path, db_path: Path, l2: bool = False):
|
||||
self.root = root
|
||||
self.root = root.resolve()
|
||||
self.db_path = db_path
|
||||
self.l2 = l2
|
||||
self._lock = threading.Lock()
|
||||
|
||||
def _process(self, filename: str) -> None:
|
||||
path = Path(filename)
|
||||
if path.is_symlink() or path.suffix.lower() not in MUSIC_EXTENSIONS or ".dedupe" in path.parts:
|
||||
if path.is_symlink() or path.suffix.lower() not in MUSIC_EXTENSIONS:
|
||||
return
|
||||
try:
|
||||
previous = path.stat().st_size
|
||||
stable = 0
|
||||
while stable < STABILITY_CHECKS - 1:
|
||||
time.sleep(STABILITY_INTERVAL_SECONDS)
|
||||
size = path.stat().st_size
|
||||
if size == previous:
|
||||
stable += 1
|
||||
else:
|
||||
previous = size
|
||||
stable = 0
|
||||
with connect(self.db_path) as conn:
|
||||
index_file(conn, path)
|
||||
# Exact dedupe is byte-for-byte safe. Acoustic dedupe is opt-in.
|
||||
exact_dedupe(self.db_path, self.root)
|
||||
if self.l2:
|
||||
acoustic_dedupe(self.db_path, self.root)
|
||||
except (FileNotFoundError, PermissionError, OSError):
|
||||
path.relative_to(self.root)
|
||||
except ValueError:
|
||||
return
|
||||
if ".dedupe" in path.parts or path.name.startswith("."):
|
||||
return
|
||||
|
||||
# watchdog may emit several events while a downloader is still writing.
|
||||
# Serialize the worker so a file is never indexed while another event
|
||||
# is changing the same database or replacing a duplicate.
|
||||
with self._lock:
|
||||
try:
|
||||
previous = path.stat()
|
||||
stable = 0
|
||||
while stable < STABILITY_CHECKS - 1:
|
||||
time.sleep(STABILITY_INTERVAL_SECONDS)
|
||||
current = path.stat()
|
||||
if (current.st_size, current.st_mtime_ns) == (
|
||||
previous.st_size,
|
||||
previous.st_mtime_ns,
|
||||
):
|
||||
stable += 1
|
||||
else:
|
||||
previous = current
|
||||
stable = 0
|
||||
with connect(self.db_path) as conn:
|
||||
index_file(conn, path)
|
||||
# Exact byte-for-byte dedupe is always safe. L2 remains opt-in.
|
||||
exact_dedupe(self.db_path, self.root)
|
||||
if self.l2:
|
||||
acoustic_dedupe(self.db_path, self.root)
|
||||
except (FileNotFoundError, PermissionError, OSError):
|
||||
return
|
||||
|
||||
def on_created(self, event):
|
||||
if not event.is_directory:
|
||||
@@ -52,9 +68,11 @@ class Handler(FileSystemEventHandler):
|
||||
self._process(event.dest_path)
|
||||
|
||||
|
||||
|
||||
def watch(root: Path, db_path: Path, l2: bool = False) -> None:
|
||||
root = root.resolve()
|
||||
if not root.is_dir():
|
||||
raise ValueError(f"music root does not exist or is not a directory: {root}")
|
||||
db_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
observer = Observer()
|
||||
observer.schedule(Handler(root, db_path, l2=l2), str(root), recursive=True)
|
||||
observer.start()
|
||||
|
||||
Reference in New Issue
Block a user