Files
pricing_review/test/界面体检/清浏览器缓存.py

53 lines
1.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# -*- coding: utf-8 -*-
"""清空 playwright 浏览器缓存(2026-10-20 主人定:每次体检收尾自动清,下次体检自动重下)。
- 目标目录:%LOCALAPPDATA%\\ms-playwright(chromium/ffmpeg 等体检浏览器,峰值约 500MB)
- 用法:①体检 run.py 收尾自动调用(默认开);②手动 `python 清浏览器缓存.py` 随时清。
- 跳过开关:环境变量 KEEP_BROWSER_CACHE=1 时本轮不清(想快速连跑两轮体检时用)。
- 只动 playwright 自己的缓存目录,主人的数据、项目文件一概不碰。
"""
import os
import shutil
import sys
def cache_dir():
return os.path.join(os.environ.get("LOCALAPPDATA", ""), "ms-playwright")
def dir_size(path):
total = 0
for root, _dirs, files in os.walk(path):
for f in files:
try:
total += os.path.getsize(os.path.join(root, f))
except OSError:
pass
return total
def clear(verbose=True):
if os.environ.get("KEEP_BROWSER_CACHE") == "1":
if verbose:
print(" KEEP_BROWSER_CACHE=1 → 本轮保留浏览器缓存")
return False
d = cache_dir()
if not os.path.isdir(d):
return False
mb = dir_size(d) / 1024 / 1024
try:
shutil.rmtree(d, ignore_errors=False)
if verbose:
print(" 已清空 playwright 浏览器缓存(%s,释放 %.0fMB);下次体检自动重下" % (d, mb))
return True
except OSError as e:
# 有浏览器进程没退干净/文件被占用:不动它,留给下一轮(不报错、不影响体检结果)
if verbose:
print(" ⚠️ 浏览器缓存清不掉(%s),留待下轮" % e)
return False
if __name__ == "__main__":
ok = clear()
sys.exit(0 if ok or not os.path.isdir(cache_dir()) else 1)