Files
kefu/deploy/protocol-file-identity-20260917/fix_file_identity.py
T
2026-09-21 10:34:06 +08:00

36 lines
1.4 KiB
Python

from pathlib import Path
import os
import shutil
root=Path(r'C:\wechat_rpa')
path=root/'wecom_native_sender.py'
backup=root/'backups/protocol-file-identity-20260917'
backup.mkdir(parents=True,exist_ok=True)
assert not (backup/path.name).exists()
shutil.copy2(path,backup/path.name)
text=path.read_text(encoding='utf-8')
anchor='''def _hash_client_file(path):
'''
helper='''def _path_file_identity(path):
"""Use the same metadata provider for both path and hash-handle checks.
On Windows/Python 3.12, Path.stat().st_ctime_ns can be birth time while
os.fstat().st_ctime_ns is change time. Comparing those reports a stable
installed executable as changed. Reopen the path and fstat that handle;
retain volume/file ID, size and timestamps to detect real replacement.
"""
with Path(path).open('rb') as handle:
return _file_identity(os.fstat(handle.fileno()))
'''
assert text.count(anchor)==1
text=text.replace(anchor,helper+anchor,1)
assert text.count('_file_identity(Path(path).stat())')==2
text=text.replace('_file_identity(Path(path).stat())','_path_file_identity(path)')
compile(text,str(path),'exec')
temp=path.with_suffix('.identity-fix-tmp')
temp.write_text(text,encoding='utf-8');os.replace(temp,path)
shutil.copy2(Path(__file__).parent/'test_windows_build_identity.py',root/'test_windows_build_identity.py')
print('Unified build identities through opened file handles; replacement checks retained.')