48 lines
1.3 KiB
Python
48 lines
1.3 KiB
Python
import os
|
|
import sys
|
|
import time
|
|
from watchdog.observers import Observer
|
|
from watchdog.events import FileSystemEventHandler
|
|
|
|
# Class for handling file system events
|
|
class WatchHandler(FileSystemEventHandler):
|
|
def on_modified(self, event):
|
|
if not event.is_directory:
|
|
print(f"File modified: {event.src_path}")
|
|
|
|
def on_created(self, event):
|
|
if not event.is_directory:
|
|
print(f"New file created: {event.src_path}")
|
|
|
|
def on_deleted(self, event):
|
|
if not event.is_directory:
|
|
print(f"File deleted: {event.src_path}")
|
|
|
|
# Get the watch directory from command line arguments or environment variable
|
|
WATCH_DIR = sys.argv[1] if len(sys.argv) > 1 else os.getenv('HUGO_WATCH_DIR')
|
|
|
|
if not WATCH_DIR:
|
|
print("Error: Please provide a directory as a command line argument or set the HUGO_WATCH_DIR environment variable.")
|
|
sys.exit(1)
|
|
|
|
if not os.path.isdir(WATCH_DIR):
|
|
print(f"Error: The provided directory '{WATCH_DIR}' does not exist or is not a directory.")
|
|
sys.exit(1)
|
|
|
|
# Setting up the observer
|
|
event_handler = WatchHandler()
|
|
observer = Observer()
|
|
observer.schedule(event_handler, path=WATCH_DIR, recursive=False)
|
|
|
|
observer.start()
|
|
print(f"Watching directory: {WATCH_DIR}")
|
|
|
|
try:
|
|
while True:
|
|
time.sleep(1)
|
|
except KeyboardInterrupt:
|
|
observer.stop()
|
|
|
|
observer.join()
|
|
|