#!/usr/bin/env python3 """ 法律库更新检查脚本 功能: 1. 检查 LawRefBook/Laws 仓库是否有更新 2. 检测新增法律和修订内容 3. 过滤过期法律 4. 生成更新报告 用法: python scripts/check_law_updates.py """ import os import sys import json import subprocess from datetime import datetime from pathlib import Path # 配置 REPO_URL = "https://github.com/LawRefBook/Laws.git" REPO_DIR = "temp-lawrefbook-updates" REPORT_FILE = "project-docs/phase14-legal-fulltext-library/update-check-report.json" DATABASE_URL = os.environ.get("DATABASE_URL", "") def check_repo_updates(): """检查仓库是否有新提交""" print("Checking LawRefBook/Laws repository for updates...") # 克隆或更新仓库 if os.path.exists(REPO_DIR): print(f" Updating existing clone: {REPO_DIR}") subprocess.run(["git", "fetch"], cwd=REPO_DIR, capture_output=True) subprocess.run(["git", "reset", "--hard", "origin/main"], cwd=REPO_DIR, capture_output=True) else: print(f" Cloning repository: {REPO_URL}") subprocess.run(["git", "clone", REPO_URL, REPO_DIR], capture_output=True) # 获取最新提交信息 result = subprocess.run( ["git", "log", "-1", "--format=%H|%ci|%s"], cwd=REPO_DIR, capture_output=True, text=True ) if result.returncode == 0: commit_hash, commit_date, commit_msg = result.stdout.strip().split("|") print(f" Latest commit: {commit_hash[:8]} ({commit_date})") print(f" Message: {commit_msg}") return commit_hash, commit_date, commit_msg return None, None, None def check_database_laws(): """检查数据库中已有的法律""" if not DATABASE_URL: print(" Warning: DATABASE_URL not set, skipping DB check") return [] import psycopg conn = psycopg.connect(DATABASE_URL) cur = conn.cursor() cur.execute("SELECT DISTINCT source_id FROM legal_articles") existing = [row[0] for row in cur.fetchall()] conn.close() return existing def scan_new_laws(existing_sources): """扫描新法律""" print("Scanning for new laws...") def check_validity(file_path): try: with open(file_path, 'r', encoding='utf-8') as f: content = f.read() markers = ['已废止', '已失效', '已修改', '已修订', '被废止', '被修改', '废止', '失效'] return not any(m in content for m in markers) except: return True new_laws = [] expired_laws = [] for root, dirs, files in os.walk(REPO_DIR): for file in files: if not file.endswith('.md') or file.startswith('_'): continue file_path = os.path.join(root, file) rel_path = os.path.relpath(file_path, REPO_DIR) # 跳过DLC目录(可选) if rel_path.startswith('DLC/'): continue # 生成source_id import re with open(file_path, 'r', encoding='utf-8') as f: content = f.read() title_match = re.search(r'^#\s+(.+)$', content, re.MULTILINE) title = title_match.group(1).strip() if title_match else file source_id = re.sub(r'[^\w]', '-', title[:50]).strip('-') # 检查是否已存在 if source_id in existing_sources: continue # 检查时效性 if check_validity(file_path): new_laws.append({ 'source_id': source_id, 'title': title, 'path': rel_path }) else: expired_laws.append({ 'source_id': source_id, 'title': title, 'path': rel_path }) return new_laws, expired_laws def generate_report(commit_hash, commit_date, new_laws, expired_laws): """生成更新报告""" report = { 'check_time': datetime.now().isoformat(), 'repo_commit': commit_hash, 'repo_date': commit_date, 'new_laws_count': len(new_laws), 'expired_laws_count': len(expired_laws), 'new_laws': new_laws[:20], # 只保存前20个作为示例 'expired_laws': expired_laws[:20], 'action_required': len(new_laws) > 0 or len(expired_laws) > 0 } with open(REPORT_FILE, 'w', encoding='utf-8') as f: json.dump(report, f, ensure_ascii=False, indent=2) print(f"Report saved: {REPORT_FILE}") return report def main(): print("=" * 60) print("Law Library Update Checker") print("=" * 60) print() # 1. 检查仓库更新 commit_hash, commit_date, commit_msg = check_repo_updates() # 2. 检查数据库已有法律 existing_sources = check_database_laws() print(f" Existing laws in database: {len(existing_sources)}") # 3. 扫描新法律 new_laws, expired_laws = scan_new_laws(existing_sources) print() print("=" * 60) print("Update Check Results") print("=" * 60) print(f"New laws found: {len(new_laws)}") print(f"Expired laws detected: {len(expired_laws)}") # 4. 生成报告 report = generate_report(commit_hash, commit_date, new_laws, expired_laws) # 5. 建议 if report['action_required']: print() print("Action required:") if new_laws: print(f" - Import {len(new_laws)} new laws") if expired_laws: print(f" - Remove {len(expired_laws)} expired laws") else: print() print("No updates needed. Law library is up-to-date.") return 0 if __name__ == "__main__": sys.exit(main())