Files
dxdesigner-part-replacement/tools/package_portable.py
T

47 lines
2.3 KiB
Python

"""Create a reviewable portable archive, excluding local user data and logs."""
from importlib import metadata
from pathlib import Path
import hashlib
import json
import shutil
import zipfile
ROOT = Path(__file__).resolve().parents[1]
def main():
bundle = ROOT / 'dist/DxPartReplace'
if not (bundle / 'DxPartReplace.exe').is_file():
raise RuntimeError('Build the executable first')
shutil.copyfile(ROOT / 'packaging/README-便携版.txt', bundle / '使用说明.txt')
shutil.copyfile(ROOT / '版本问题与修改记录.md', bundle / '版本问题与修改记录.md')
notices = bundle / 'THIRD_PARTY'
versions = {}
for name in ('PySide6-Essentials', 'shiboken6', 'pywin32', 'pywinauto', 'openpyxl', 'et_xmlfile', 'comtypes', 'six', 'Pillow'):
distribution = metadata.distribution(name)
versions[name] = distribution.version
for file in distribution.files or ():
if any(token in file.name.casefold() for token in ('license', 'copying', 'notice')):
source = Path(distribution.locate_file(file))
if source.is_file():
destination = notices / name / Path(*file.parts[-3:])
destination.parent.mkdir(parents=True, exist_ok=True)
shutil.copyfile(source, destination)
python_license = Path(__import__('sys').base_prefix) / 'LICENSE.txt'
if python_license.is_file():
shutil.copyfile(python_license, notices / 'Python-LICENSE.txt')
(notices / 'versions.json').write_text(json.dumps(versions, indent=2), encoding='utf-8')
archive = ROOT / 'dist/DxPartReplace-1.2.0-beta.31-Windows-x64.zip'
with zipfile.ZipFile(archive, 'w', zipfile.ZIP_DEFLATED, compresslevel=6) as output:
for path in sorted(bundle.rglob('*')):
relative = path.relative_to(bundle)
if path.is_file() and relative.parts[0].casefold() != 'data':
output.write(path, Path('DxPartReplace') / relative)
digest = hashlib.sha256(archive.read_bytes()).hexdigest()
archive.with_suffix('.zip.sha256').write_text(f'{digest} {archive.name}\n', encoding='ascii')
print(json.dumps({'archive': str(archive), 'bytes': archive.stat().st_size, 'sha256': digest}, indent=2))
if __name__ == '__main__':
main()