diff --git a/management/sanguo_auto_sync/file-watcher.sh b/management/sanguo_auto_sync/file-watcher.sh index dd93ccd4a..d1c5ef29d 100755 --- a/management/sanguo_auto_sync/file-watcher.sh +++ b/management/sanguo_auto_sync/file-watcher.sh @@ -5,7 +5,7 @@ PROJECT_DIR="/Users/chufeng/.openclaw/sanguo_projects/sanguo_quant_live" LOG_FILE="$PROJECT_DIR/file-watcher.log" -SYNC_SCRIPT="$PROJECT_DIR/auto-sync.sh" +SYNC_SCRIPT="$PROJECT_DIR/management/sanguo_auto_sync/auto-sync.sh" LOCK_FILE="/tmp/sanguo_sync.lock" # 确保脚本有执行权限 @@ -48,11 +48,20 @@ run_sync() { # 检查fswatch是否可用 if command -v fswatch &> /dev/null; then echo "[$(date)] Using fswatch for file monitoring" >> "$LOG_FILE" - # fswatch: -e 排除.git目录,-r 递归,-0 输出null分隔符 - fswatch -e "\.git" -e "\.log$" -r -0 "$PROJECT_DIR" | while read -d "" event + # fswatch: -e 排除不需要的目录和文件,-r 递归,-0 输出null分隔符 + fswatch \ + -e "\.git" \ + -e "__pycache__" \ + -e "venv" \ + -e "\.venv" \ + -e "node_modules" \ + -e "\.log$" \ + -e "\.pyc$" \ + -e "\.tmp$" \ + -r -0 "$PROJECT_DIR" | while read -d "" event do # 过滤掉一些不必要的文件类型 - if [[ ! "$event" =~ \.log$ ]] && [[ ! "$event" =~ \.tmp$ ]] && [[ ! "$event" =~ ~$ ]]; then + if [[ ! "$event" =~ \.log$ ]] && [[ ! "$event" =~ \.pyc$ ]] && [[ ! "$event" =~ \.tmp$ ]] && [[ ! "$event" =~ ~$ ]]; then run_sync # 添加1秒延迟避免频繁触发 sleep 1 diff --git a/management/sanguo_auto_sync/simple-file-watcher.py b/management/sanguo_auto_sync/simple-file-watcher.py index 425d8cf7b..1cdb8489c 100755 --- a/management/sanguo_auto_sync/simple-file-watcher.py +++ b/management/sanguo_auto_sync/simple-file-watcher.py @@ -16,12 +16,12 @@ from pathlib import Path # 配置 PROJECT_DIR = "/Users/chufeng/.openclaw/sanguo_projects/sanguo_quant_live" SELF_DIR = os.path.dirname(os.path.abspath(__file__)) -LOG_FILE = os.path.join(SELF_DIR, "file-watcher.log") -SYNC_SCRIPT = os.path.join(PROJECT_DIR, "auto-sync.sh") +LOG_FILE = os.path.join(PROJECT_DIR, "simple-watcher.log") +SYNC_SCRIPT = os.path.join(PROJECT_DIR, "management/sanguo_auto_sync/auto-sync.sh") LOCK_FILE = "/tmp/sanguo_sync.lock" CHECK_INTERVAL = 60 # 检查间隔(秒)= 1 分钟 -IGNORE_EXTENSIONS = ['.log', '.tmp', '~'] -IGNORE_DIRS = ['.git'] +IGNORE_EXTENSIONS = ['.log', '.tmp', '~', '.pyc'] +IGNORE_DIRS = ['.git', '__pycache__', 'venv', '.venv', 'node_modules'] # 设置日志 logging.basicConfig( @@ -79,6 +79,7 @@ class FileWatcher: """检查文件变化""" changes_detected = False + # 第一步:遍历当前目录,检测新增和修改 for root, dirs, files in os.walk(self.directory): # 跳过忽略的目录 dirs[:] = [d for d in dirs if d not in IGNORE_DIRS] @@ -113,6 +114,23 @@ class FileWatcher: changes_detected = True logger.info(f"File deleted: {filepath.relative_to(self.directory)}") + # 第二步:反向检查 - 已记录的文件是否还存在 + # 找出已不存在的文件(删除检测) + existing_files = list(self.last_modified.keys()) + for filepath_str in existing_files: + filepath = Path(filepath_str) + if self._should_ignore(filepath): + continue + if not filepath.exists(): + # 文件已被删除 + del self.last_modified[filepath_str] + changes_detected = True + try: + rel_path = filepath.relative_to(self.directory) + logger.info(f"File deleted: {rel_path}") + except: + logger.info(f"File deleted: {filepath_str}") + return changes_detected def run_sync(self): diff --git a/management/sanguo_auto_sync/start-simple-watcher.sh b/management/sanguo_auto_sync/start-simple-watcher.sh index fa1367304..f8f1a1ebb 100755 --- a/management/sanguo_auto_sync/start-simple-watcher.sh +++ b/management/sanguo_auto_sync/start-simple-watcher.sh @@ -3,7 +3,7 @@ # 启动简单文件监控脚本 PROJECT_DIR="/Users/chufeng/.openclaw/sanguo_projects/sanguo_quant_live" -WATCHER_SCRIPT="$PROJECT_DIR/simple-file-watcher.py" +WATCHER_SCRIPT="$PROJECT_DIR/management/sanguo_auto_sync/simple-file-watcher.py" PID_FILE="$PROJECT_DIR/simple-watcher.pid" LOG_FILE="$PROJECT_DIR/simple-watcher.log" @@ -45,4 +45,4 @@ echo "To view logs:" echo " tail -f $LOG_FILE" echo "" echo "Watcher is now monitoring: $PROJECT_DIR" -echo "Files changed will trigger: $PROJECT_DIR/auto-sync.sh" \ No newline at end of file +echo "Files changed will trigger: $PROJECT_DIR/management/sanguo_auto_sync/auto-sync.sh" \ No newline at end of file diff --git a/management/sanguo_auto_sync/watcher.pid b/management/sanguo_auto_sync/watcher.pid index c7386f1c6..8e9651efd 100644 --- a/management/sanguo_auto_sync/watcher.pid +++ b/management/sanguo_auto_sync/watcher.pid @@ -1 +1 @@ -91142 +4052 diff --git a/test.md b/test.md deleted file mode 100644 index 6f41862db..000000000 --- a/test.md +++ /dev/null @@ -1,7 +0,0 @@ - -# Second test - -This is the second test to verify auto-sync is working correctly. - -- Created at: $(date) -- This should be automatically synced to Gitee within 1 minute. diff --git a/test1.md b/test1.md new file mode 100644 index 000000000..0c027b4a4 --- /dev/null +++ b/test1.md @@ -0,0 +1,4 @@ +# Test 1 + +这是第一个测试文件。 +用来测试 fswatch 实时监控是否正常工作。 diff --git a/test2.md b/test2.md new file mode 100644 index 000000000..bc1284940 --- /dev/null +++ b/test2.md @@ -0,0 +1,4 @@ +# Test 2 + +这是第二个测试文件。 +测试新增多个文件是否能一次检测到。