added exclusion, fixed time display
This commit is contained in:
+79
-23
@@ -5,15 +5,40 @@ from watchdog.events import FileSystemEventHandler
|
||||
from watchdog.observers import Observer
|
||||
|
||||
from db_py import reset_by_file_id_and_device
|
||||
from scanner import full_scan, parse_deleted_result_dir_name, process_result_dir
|
||||
from scanner import (
|
||||
full_scan,
|
||||
parse_deleted_result_dir_name,
|
||||
process_result_dir,
|
||||
_is_unc_path,
|
||||
_normalize_input_path,
|
||||
)
|
||||
from sse_py import broadcast
|
||||
|
||||
_target_observer = None
|
||||
_results_observer = None
|
||||
_results_observers = []
|
||||
_scan_timer = None
|
||||
_scan_lock = threading.Lock()
|
||||
|
||||
|
||||
def _path_exists(path):
|
||||
"""Check if path exists, handling UNC paths."""
|
||||
if _is_unc_path(path):
|
||||
try:
|
||||
import smbclient
|
||||
return smbclient.path.isdir(path)
|
||||
except Exception:
|
||||
return False
|
||||
return os.path.isdir(path)
|
||||
|
||||
|
||||
def _normalize_watcher_path(path):
|
||||
"""Normalize path for watcher comparison, handling UNC paths."""
|
||||
normalized = _normalize_input_path(path)
|
||||
if not _is_unc_path(normalized):
|
||||
normalized = os.path.normcase(os.path.abspath(normalized))
|
||||
return normalized
|
||||
|
||||
|
||||
def _schedule_full_scan(target_dir, results_dir, delay_seconds=1.0):
|
||||
global _scan_timer
|
||||
with _scan_lock:
|
||||
@@ -66,16 +91,29 @@ class _TargetHandler(FileSystemEventHandler):
|
||||
|
||||
class _ResultsHandler(FileSystemEventHandler):
|
||||
def __init__(self, results_dir):
|
||||
self.results_dir = os.path.normcase(os.path.abspath(results_dir))
|
||||
self.results_dir = _normalize_watcher_path(results_dir)
|
||||
|
||||
def _is_direct_child_dir(self, path):
|
||||
parent = os.path.normcase(os.path.abspath(os.path.dirname(path)))
|
||||
return parent == self.results_dir
|
||||
try:
|
||||
if _is_unc_path(path):
|
||||
parent = _normalize_watcher_path(os.path.dirname(path))
|
||||
else:
|
||||
parent = os.path.normcase(os.path.abspath(os.path.dirname(path)))
|
||||
return parent == self.results_dir
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
def _is_file_under_result_child(self, path):
|
||||
parent_dir = os.path.normcase(os.path.abspath(os.path.dirname(path)))
|
||||
grandparent = os.path.normcase(os.path.abspath(os.path.dirname(parent_dir)))
|
||||
return grandparent == self.results_dir
|
||||
try:
|
||||
if _is_unc_path(path):
|
||||
parent_dir = _normalize_watcher_path(os.path.dirname(path))
|
||||
grandparent = _normalize_watcher_path(os.path.dirname(parent_dir))
|
||||
else:
|
||||
parent_dir = os.path.normcase(os.path.abspath(os.path.dirname(path)))
|
||||
grandparent = os.path.normcase(os.path.abspath(os.path.dirname(parent_dir)))
|
||||
return grandparent == self.results_dir
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
def _result_child_name_for_file(self, path):
|
||||
return os.path.basename(os.path.dirname(path))
|
||||
@@ -164,7 +202,7 @@ class _ResultsHandler(FileSystemEventHandler):
|
||||
|
||||
|
||||
def stop_watching():
|
||||
global _target_observer, _results_observer, _scan_timer
|
||||
global _target_observer, _results_observers, _scan_timer
|
||||
|
||||
if _scan_timer:
|
||||
_scan_timer.cancel()
|
||||
@@ -175,27 +213,30 @@ def stop_watching():
|
||||
_target_observer.join(timeout=2)
|
||||
_target_observer = None
|
||||
|
||||
if _results_observer:
|
||||
_results_observer.stop()
|
||||
_results_observer.join(timeout=2)
|
||||
_results_observer = None
|
||||
for obs in _results_observers:
|
||||
try:
|
||||
obs.stop()
|
||||
obs.join(timeout=2)
|
||||
except Exception as e:
|
||||
print(f"[watcher] error stopping results observer: {e}")
|
||||
_results_observers.clear()
|
||||
|
||||
|
||||
def start_watching(target_dir, results_dir):
|
||||
global _target_observer, _results_observer
|
||||
def start_watching(target_dir, results_dir, results_dir_ref=None):
|
||||
global _target_observer, _results_observers
|
||||
|
||||
stop_watching()
|
||||
if not target_dir or not results_dir:
|
||||
print("[watcher] target_dir/results_dir not configured; watcher disabled.")
|
||||
return
|
||||
|
||||
if not os.path.isdir(target_dir):
|
||||
print(f"[watcher] target_dir does not exist inside runtime: {target_dir}")
|
||||
if not _path_exists(target_dir):
|
||||
print(f"[watcher] target_dir does not exist or is not accessible: {target_dir}")
|
||||
print("[watcher] watcher disabled until valid paths are configured.")
|
||||
return
|
||||
|
||||
if not os.path.isdir(results_dir):
|
||||
print(f"[watcher] results_dir does not exist inside runtime: {results_dir}")
|
||||
if not _path_exists(results_dir):
|
||||
print(f"[watcher] results_dir does not exist or is not accessible: {results_dir}")
|
||||
print("[watcher] watcher disabled until valid paths are configured.")
|
||||
return
|
||||
|
||||
@@ -204,11 +245,26 @@ def start_watching(target_dir, results_dir):
|
||||
_target_observer.schedule(_TargetHandler(target_dir, results_dir), target_dir, recursive=True)
|
||||
_target_observer.daemon = True
|
||||
_target_observer.start()
|
||||
print(f"[watcher] target observer started for: {target_dir}")
|
||||
|
||||
_results_observer = Observer()
|
||||
_results_observer.schedule(_ResultsHandler(results_dir), results_dir, recursive=True)
|
||||
_results_observer.daemon = True
|
||||
_results_observer.start()
|
||||
# Watch primary results directory
|
||||
results_obs = Observer()
|
||||
results_obs.schedule(_ResultsHandler(results_dir), results_dir, recursive=True)
|
||||
results_obs.daemon = True
|
||||
results_obs.start()
|
||||
_results_observers.append(results_obs)
|
||||
print(f"[watcher] results observer started for: {results_dir}")
|
||||
|
||||
# Watch reference results directory if provided and different
|
||||
if results_dir_ref and results_dir_ref != results_dir and _path_exists(results_dir_ref):
|
||||
ref_obs = Observer()
|
||||
ref_obs.schedule(_ResultsHandler(results_dir_ref), results_dir_ref, recursive=True)
|
||||
ref_obs.daemon = True
|
||||
ref_obs.start()
|
||||
_results_observers.append(ref_obs)
|
||||
print(f"[watcher] results observer started for reference: {results_dir_ref}")
|
||||
elif results_dir_ref and results_dir_ref != results_dir:
|
||||
print(f"[watcher] reference results_dir does not exist or is not accessible: {results_dir_ref}")
|
||||
except Exception as exc:
|
||||
print(f"[watcher] failed to start watchers: {exc}")
|
||||
stop_watching()
|
||||
|
||||
Reference in New Issue
Block a user