修复:Windows 离线启动误校验 Python 可再生缓存,提供原地修复工具
This commit is contained in:
@@ -0,0 +1,135 @@
|
||||
"""原地修复旧 Windows 包把可再生 Python 缓存列入固定校验的问题。"""
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path, PurePosixPath
|
||||
import re
|
||||
import shutil
|
||||
import tempfile
|
||||
import uuid
|
||||
|
||||
|
||||
def cache_source(name):
|
||||
"""只识别随包解释器的缓存;业务数据和普通程序文件不属于缓存。"""
|
||||
if not isinstance(name, str) or '\\' in name:
|
||||
return None
|
||||
path = PurePosixPath(name)
|
||||
if path.parts[:2] != ('runtimes', 'python') or any(p in ('.', '..') for p in name.split('/')):
|
||||
return None
|
||||
if path.parent.name != '__pycache__':
|
||||
return None
|
||||
match = re.fullmatch(r'(.+)\.cpython-\d+(?:\.opt-\d+)?\.pyc', path.name)
|
||||
return (path.parent.parent / (match[1] + '.py')).as_posix() if match else None
|
||||
|
||||
|
||||
def local_file(root, name):
|
||||
path = root.joinpath(*PurePosixPath(name).parts)
|
||||
if path.is_symlink() or not path.resolve().is_relative_to(root.resolve()):
|
||||
raise RuntimeError('文件路径异常,已停止修复:' + name)
|
||||
return path
|
||||
|
||||
|
||||
def digest(path):
|
||||
with path.open('rb') as stream:
|
||||
return hashlib.file_digest(stream, 'sha256').hexdigest()
|
||||
|
||||
|
||||
def repair(root):
|
||||
root = Path(root).resolve()
|
||||
manifest_path = root / 'manifest.json'
|
||||
if not manifest_path.is_file() or not (root / 'deploy/bootstrap/windows-start.ps1').is_file():
|
||||
raise RuntimeError('请把本修复文件放到 QualityCenter 目录,与“一键部署启动.cmd”放在一起后双击。')
|
||||
original_manifest = manifest_path.read_bytes()
|
||||
manifest = json.loads(original_manifest.decode('utf-8-sig'))
|
||||
if manifest.get('platform') != 'windows':
|
||||
raise RuntimeError('本修复仅适用于 Windows 离线部署包。')
|
||||
entries = manifest['files']
|
||||
by_name = {entry['path']: entry for entry in entries}
|
||||
if len(by_name) != len(entries):
|
||||
raise RuntimeError('文件清单存在重复路径,已停止修复。')
|
||||
caches = [(entry, cache_source(entry['path'])) for entry in entries if cache_source(entry['path'])]
|
||||
# 先验证全部对应源码,不能用修复操作掩盖源码丢失或真正的内容变更。
|
||||
for entry, source in caches:
|
||||
expected = by_name.get(source, {}).get('sha256')
|
||||
source_path = local_file(root, source)
|
||||
if not expected or not source_path.is_file() or digest(source_path) != expected:
|
||||
raise RuntimeError('对应源码缺失或校验失败,未修改文件清单:' + source)
|
||||
local_file(root, entry['path'])
|
||||
if not caches:
|
||||
return {'cacheEntriesRemoved': 0, 'cacheFilesRemoved': 0, 'alreadyRepaired': True}
|
||||
|
||||
backup_dir = root / '.runtime/cache-repair-backups' / (datetime.now().strftime('%Y%m%d-%H%M%S') + '-' + uuid.uuid4().hex[:8])
|
||||
if not backup_dir.resolve().is_relative_to(root):
|
||||
raise RuntimeError('备份目录路径异常,已停止修复。')
|
||||
backup_dir.mkdir(parents=True, mode=0o700)
|
||||
backup = backup_dir / 'manifest.json'
|
||||
shutil.copy2(manifest_path, backup)
|
||||
# 只删除可由已校验源码重建的缓存;不删除数据库、迁移包、配置或日志。
|
||||
removed = 0
|
||||
for entry, _source in caches:
|
||||
path = local_file(root, entry['path'])
|
||||
if path.exists():
|
||||
path.unlink()
|
||||
removed += 1
|
||||
excluded = {entry['path'] for entry, _source in caches}
|
||||
manifest['files'] = [entry for entry in entries if entry['path'] not in excluded]
|
||||
fd, temporary = tempfile.mkstemp(prefix='manifest-cache-repair-', suffix='.tmp', dir=root)
|
||||
try:
|
||||
with os.fdopen(fd, 'w', encoding='utf-8', newline='\n') as stream:
|
||||
json.dump(manifest, stream, ensure_ascii=False, indent=2)
|
||||
stream.write('\n')
|
||||
stream.flush()
|
||||
os.fsync(stream.fileno())
|
||||
if manifest_path.read_bytes() != original_manifest:
|
||||
raise RuntimeError('文件清单同时被其他进程更新,请等待原操作完成后重新运行修复。')
|
||||
os.replace(temporary, manifest_path)
|
||||
finally:
|
||||
Path(temporary).unlink(missing_ok=True)
|
||||
return {'cacheEntriesRemoved': len(caches), 'cacheFilesRemoved': removed,
|
||||
'manifestBackup': str(backup.relative_to(root))}
|
||||
|
||||
|
||||
def command_file():
|
||||
"""生成单个可双击文件;使用原包 Python,不下载、不安装系统组件。"""
|
||||
header = '''@echo off
|
||||
setlocal
|
||||
chcp 65001 >nul
|
||||
set "PYTHONUTF8=1"
|
||||
set "PYTHONIOENCODING=utf-8"
|
||||
set "PYTHONDONTWRITEBYTECODE=1"
|
||||
if not exist "%~dp0runtimes\\python\\python.exe" (
|
||||
echo 请将本文件放到 QualityCenter 目录,与一键部署启动放在一起。
|
||||
pause
|
||||
exit /b 1
|
||||
)
|
||||
"%~dp0runtimes\\python\\python.exe" -I -S -B -X utf8 -c "import sys;from pathlib import Path;p=Path(sys.argv[1]);s=p.read_text(encoding='utf-8').split(chr(10)+'# QUALITY_CACHE_REPAIR_PYTHON'+chr(10),1)[1];n={'__name__':'quality_cache_repair'};exec(compile(s,str(p),'exec'),n);print(n['repair'](p.parent))" "%~f0"
|
||||
if errorlevel 1 (
|
||||
echo 修复未完成,请保留上方报错;现有数据库和配置未覆盖。
|
||||
pause
|
||||
exit /b 1
|
||||
)
|
||||
echo 缓存校验已修复,正在继续原来的一键部署流程。
|
||||
powershell.exe -NoProfile -ExecutionPolicy Bypass -File "%~dp0deploy\\bootstrap\\windows-start.ps1"
|
||||
if errorlevel 1 echo 部署未完成,请保留上方报错。
|
||||
pause
|
||||
exit /b
|
||||
# QUALITY_CACHE_REPAIR_PYTHON
|
||||
'''
|
||||
return header.replace('\n', '\r\n').encode('utf-8') + Path(__file__).read_bytes()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import argparse
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument('--root', type=Path)
|
||||
parser.add_argument('--write-command', type=Path)
|
||||
args = parser.parse_args()
|
||||
if args.write_command:
|
||||
args.write_command.write_bytes(command_file())
|
||||
elif args.root:
|
||||
print(json.dumps(repair(args.root), ensure_ascii=False))
|
||||
else:
|
||||
parser.error('请选择 --root 或 --write-command')
|
||||
Reference in New Issue
Block a user