sync: system_audit.py cache审计(笑笑)

This commit is contained in:
知微
2026-06-30 23:12:53 +08:00
parent 28c001684e
commit 0a6c659e45
+28
View File
@@ -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()