From 0a6c659e454ef2d689d221d05bc3d5ef4ee1f861 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=9F=A5=E5=BE=AE?= Date: Tue, 30 Jun 2026 23:12:53 +0800 Subject: [PATCH] =?UTF-8?q?sync:=20system=5Faudit.py=20cache=E5=AE=A1?= =?UTF-8?q?=E8=AE=A1(=E7=AC=91=E7=AC=91)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- system_audit.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/system_audit.py b/system_audit.py index ff1f8b5..738f1b5 100644 --- a/system_audit.py +++ b/system_audit.py @@ -139,6 +139,33 @@ def audit_portfolio(conn): log_issue("组合", "MEDIUM", f"查询失败: {e}") +# ── 8. 编译缓存审计 ── +def audit_cache(): + """检查 __pycache__ 中是否有比 .py 源文件更老的 .pyc(陈旧缓存)。""" + try: + base = Path(__file__).resolve().parent + stale = [] + for pyc in base.rglob("__pycache__/*.pyc"): + py = pyc.with_suffix("") # remove .cpython-*.pyc extension + # The .py file is at parent_of___pycache__ / stem_without_cpython_suffix + # e.g., __pycache__/foo.cpython-312.pyc -> ../foo.py + stem = pyc.stem # e.g. "foo.cpython-312" + # Remove the .cpython-NNN suffix to get original module name + import re + m = re.match(r"^(.*?)\.cpython-\d+", stem) + if not m: + continue + py_path = pyc.parent.parent / f"{m.group(1)}.py" + if py_path.exists() and pyc.stat().st_mtime < py_path.stat().st_mtime: + stale.append(str(py_path.name)) + if stale: + log_issue("编译缓存", "MEDIUM", f"{len(stale)}个陈旧.pyc:{', '.join(stale)}", "删除对应__pycache__/.pyc") + else: + log_ok("编译缓存", "所有.pyc文件与源文件一致") + except Exception as e: + log_issue("编译缓存", "LOW", f"检查失败: {e}") + + # ── 6. 数据管道审计 ── def audit_pipeline(): # 检查DB市场数据是否今天更新 @@ -195,6 +222,7 @@ def main(): audit_portfolio(conn) audit_pipeline() audit_services() + audit_cache() conn.close()