Files
cutdex/tool/windows/check_windows_release.py
T
lingjhf 2fb675e64c
Gitea CI / Windows x64 and Android (pull_request) Canceled after 0s
Gitea CI / Gitea CI passed (pull_request) Canceled after 0s
Audit portable Windows DLL dependencies before releasing
2026-10-06 03:11:15 +08:00

93 lines
4.0 KiB
Python

"""Audit Windows x64 PE imports without trusting the build host's System32.
Build-host dependency: pefile==2024.8.26. The target computer needs no Python.
This is a static dependency gate, not a substitute for a clean Windows VM run.
"""
import argparse
import json
from pathlib import Path
# Inbox components on supported, fully updated Windows 10/11 (non-N editions).
# Never infer this list from files installed on the developer's machine.
SYSTEM_DLLS = set('''
advapi32 avicap32 avrt bcrypt bcryptprimitives cfgmgr32 combase comctl32 comdlg32
crypt32 d2d1 d3d11 d3d12 d3d9 d3dcompiler_47 dbghelp dnsapi dwmapi dwrite dxgi
gdi32 imagehlp imm32 iphlpapi kernel32 mf mfplat mfplay mfreadwrite msimg32
ncrypt ntdll ole32 oleacc oleaut32 opengl32 propsys psapi rpcrt4 secur32
setupapi shell32 shlwapi uiautomationcore user32 userenv usp10 winmm wintrust
ws2_32 ucrtbase version
'''.split())
SYSTEM_DLLS = {name + '.dll' for name in SYSTEM_DLLS}
REQUIRED = (
'cutdex.exe', 'flutter_windows.dll', 'cutdex_runtime_plugin.dll',
'deskup_plugin.dll', 'velopack_libc.dll',
'cutdex_windows_ffi.dll', 'sqlite3.dll', 'data/app.so', 'data/icudtl.dat',
'processing/bin/cutdex-processing-worker.exe',
'processing/bin/cutdex_processing_ffi.dll', 'processing/bin/ffmpeg.exe',
'processing/bin/ffprobe.exe', 'processing/bin/onnxruntime.dll',
'processing/bin/DirectML.dll',
'processing/workers/asr/cutdex-asr-worker.exe',
'processing/workers/audio/cutdex-audio-worker.exe',
'processing/workers/translation/cutdex-m2m100-worker.exe',
)
def system_dependency(name):
name = name.lower()
return name in SYSTEM_DLLS or name.startswith(('api-ms-win-', 'ext-ms-win-'))
def pe_imports(path):
import pefile
with pefile.PE(str(path), fast_load=True) as pe:
if pe.FILE_HEADER.Machine != 0x8664:
raise ValueError(f'Not an x64 PE: {path}')
pe.parse_data_directories(directories=[1, 13])
return sorted({item.dll.decode('ascii').lower() for attribute in
('DIRECTORY_ENTRY_IMPORT', 'DIRECTORY_ENTRY_DELAY_IMPORT')
for item in getattr(pe, attribute, [])})
def audit(root, read_imports=pe_imports, *, require_processing=True):
root = Path(root).resolve()
required = REQUIRED if require_processing else tuple(name for name in REQUIRED if not name.startswith('processing/'))
missing = [name for name in required if not (root / name).is_file()]
if missing:
raise ValueError(f'Missing release files: {missing}')
if not (root / 'data/flutter_assets').is_dir():
raise ValueError('Missing Flutter assets')
report = {}
unresolved = []
for binary in sorted(root.rglob('*')):
if binary.suffix.lower() not in ('.dll', '.exe'):
continue
if binary.is_symlink():
raise ValueError(f'External/symlink binary: {binary}')
# Each worker is a separate process. A DLL in another worker directory
# (or only in the main app directory) does not satisfy its dependency.
local = {p.name.lower() for p in binary.parent.iterdir() if p.is_file()}
names = read_imports(binary)
report[binary.relative_to(root).as_posix()] = names
for name in names:
if name.lower() not in local and not system_dependency(name):
unresolved.append(f'{binary.relative_to(root)} -> {name}')
if unresolved:
raise ValueError('Unbundled dependencies: ' + '; '.join(unresolved))
return {'architecture': 'x64', 'imports': report, 'unresolved': [],
'cleanSystemValidated': False}
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--app', type=Path, required=True)
parser.add_argument('--report', type=Path, required=True)
args = parser.parse_args()
result = audit(args.app)
args.report.parent.mkdir(parents=True, exist_ok=True)
args.report.write_text(json.dumps(result, indent=2) + '\n', encoding='utf-8')
print(f'Windows dependency gate passed: {len(result["imports"])} PE files')
if __name__ == '__main__':
main()