72 lines
2.4 KiB
Python
72 lines
2.4 KiB
Python
import time
|
|
import os
|
|
import re
|
|
from watchdog.observers import Observer
|
|
from watchdog.events import FileSystemEventHandler
|
|
|
|
# --- CONFIG ---
|
|
WATCH_DIR = "/das/work/units/mx/p22601/aareproc/" # <-- Set this to your directory
|
|
TARGET_FILENAME = "CORRECT.LP"
|
|
EXCLUDE_RE = re.compile(r"/(autoproc|man_proc)(/|$)")
|
|
NANO_RE = re.compile(r"SigAno\s+ Nano")
|
|
LINES_AFTER_NANO = 12
|
|
|
|
|
|
def is_excluded(path):
|
|
"""Return True if path matches any excluded directory pattern"""
|
|
return bool(EXCLUDE_RE.search(path))
|
|
|
|
|
|
class LPFileHandler(FileSystemEventHandler):
|
|
def on_created(self, event):
|
|
if not event.is_directory and os.path.basename(event.src_path) == TARGET_FILENAME:
|
|
if not is_excluded(event.src_path):
|
|
print(f"\n[CREATE] {event.src_path}")
|
|
self.process_file(event.src_path)
|
|
|
|
def on_modified(self, event):
|
|
if not event.is_directory and os.path.basename(event.src_path) == TARGET_FILENAME:
|
|
if not is_excluded(event.src_path):
|
|
print(f"\n[MODIFY] {event.src_path}")
|
|
self.process_file(event.src_path)
|
|
|
|
def process_file(self, filepath):
|
|
try:
|
|
with open(filepath, 'r') as f:
|
|
lines = f.readlines()
|
|
|
|
# 1. Look for ERROR lines
|
|
error_lines = [line for line in lines if "ERROR" in line]
|
|
if error_lines:
|
|
print("\033[91mERROR lines found:\033[0m")
|
|
for line in error_lines:
|
|
print(f"\033[91m{line.strip()}\033[0m")
|
|
|
|
# 2. Look for "Nano" and following lines
|
|
for idx, line in enumerate(lines):
|
|
if NANO_RE.search(line):
|
|
print("\033[92mMatch: 'Nano' found. Showing following 12 lines:\033[0m")
|
|
block = lines[idx:idx + 1 + LINES_AFTER_NANO]
|
|
print("".join(block))
|
|
break
|
|
|
|
except Exception as e:
|
|
print(f"Error reading {filepath}: {e}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
event_handler = LPFileHandler()
|
|
observer = Observer()
|
|
observer.schedule(event_handler, path=WATCH_DIR, recursive=True)
|
|
observer.start()
|
|
|
|
print(f"Watching '{WATCH_DIR}' for '{TARGET_FILENAME}'...\n(Excluding dirs: {EXCLUDE_RE.pattern})")
|
|
|
|
try:
|
|
while True:
|
|
time.sleep(1)
|
|
except KeyboardInterrupt:
|
|
print("Stopping watcher...")
|
|
observer.stop()
|
|
observer.join()
|