|
| 1 | +import os |
| 2 | +import tempfile |
| 3 | +import uuid |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | +import packaging.version as version |
| 7 | +import pefile |
| 8 | +from PyInstaller.building.icon import normalize_icon_type |
| 9 | +from PyInstaller.compat import win32api |
| 10 | +from PyInstaller.utils.win32.icon import IconFile, normalize_icon_type |
| 11 | +from PyInstaller.utils.win32.versioninfo import decode |
| 12 | + |
| 13 | + |
| 14 | +def update_flet_view_icon(exe_path, icon_path): |
| 15 | + print("Updating Flet View icon", exe_path, icon_path) |
| 16 | + |
| 17 | + RT_ICON = 3 |
| 18 | + RT_GROUP_ICON = 14 |
| 19 | + |
| 20 | + normalized_icon_path = normalize_icon_type( |
| 21 | + icon_path, ("exe", "ico"), "ico", os.getcwd() |
| 22 | + ) |
| 23 | + icon = IconFile(normalized_icon_path) |
| 24 | + print("Copying icons from", normalized_icon_path) |
| 25 | + |
| 26 | + hdst = win32api.BeginUpdateResource(exe_path, 0) |
| 27 | + |
| 28 | + iconid = 1 |
| 29 | + # Each step in the following enumerate() will instantiate an IconFile object, as a result of deferred execution |
| 30 | + # of the map() above. |
| 31 | + i = 101 |
| 32 | + data = icon.grp_icon_dir() |
| 33 | + data = data + icon.grp_icondir_entries(iconid) |
| 34 | + win32api.UpdateResource(hdst, RT_GROUP_ICON, i, data, 1033) |
| 35 | + print("Writing RT_GROUP_ICON %d resource with %d bytes", i, len(data)) |
| 36 | + for data in icon.images: |
| 37 | + win32api.UpdateResource(hdst, RT_ICON, iconid, data, 1033) |
| 38 | + print("Writing RT_ICON %d resource with %d bytes", iconid, len(data)) |
| 39 | + iconid = iconid + 1 |
| 40 | + |
| 41 | + win32api.EndUpdateResource(hdst, 0) |
| 42 | + |
| 43 | + |
| 44 | +def update_flet_view_version_info( |
| 45 | + exe_path, |
| 46 | + product_name, |
| 47 | + file_description, |
| 48 | + product_version, |
| 49 | + file_version, |
| 50 | + company_name, |
| 51 | + copyright, |
| 52 | +): |
| 53 | + print("Updating Flet View version info", exe_path) |
| 54 | + |
| 55 | + # load versioninfo from exe |
| 56 | + vs = decode(exe_path) |
| 57 | + |
| 58 | + # update file version |
| 59 | + if file_version: |
| 60 | + pv = version.parse(file_version) |
| 61 | + filevers = (pv.major, pv.minor, pv.micro, 0) |
| 62 | + vs.ffi.fileVersionMS = (filevers[0] << 16) | (filevers[1] & 0xFFFF) |
| 63 | + vs.ffi.fileVersionLS = (filevers[2] << 16) | (filevers[3] & 0xFFFF) |
| 64 | + |
| 65 | + # update string props |
| 66 | + for k in vs.kids[0].kids[0].kids: |
| 67 | + if k.name == "ProductName": |
| 68 | + k.val = product_name if product_name else "" |
| 69 | + elif k.name == "FileDescription": |
| 70 | + k.val = file_description if file_description else "" |
| 71 | + if k.name == "ProductVersion": |
| 72 | + k.val = product_version if product_version else "" |
| 73 | + if k.name == "FileVersion" and file_version: |
| 74 | + k.val = file_version if file_version else "" |
| 75 | + if k.name == "CompanyName": |
| 76 | + k.val = company_name if company_name else "" |
| 77 | + if k.name == "LegalCopyright": |
| 78 | + k.val = copyright if copyright else "" |
| 79 | + |
| 80 | + version_info_path = str(Path(tempfile.gettempdir()).joinpath(str(uuid.uuid4()))) |
| 81 | + with open(version_info_path, "w") as f: |
| 82 | + f.write(str(vs)) |
| 83 | + |
| 84 | + # Remember overlay |
| 85 | + pe = pefile.PE(exe_path, fast_load=True) |
| 86 | + overlay_before = pe.get_overlay() |
| 87 | + pe.close() |
| 88 | + |
| 89 | + hdst = win32api.BeginUpdateResource(exe_path, 0) |
| 90 | + win32api.UpdateResource( |
| 91 | + hdst, pefile.RESOURCE_TYPE["RT_VERSION"], 1, vs.toRaw(), 1033 |
| 92 | + ) |
| 93 | + win32api.EndUpdateResource(hdst, 0) |
| 94 | + |
| 95 | + if overlay_before: |
| 96 | + # Check if the overlay is still present |
| 97 | + pe = pefile.PE(exe_path, fast_load=True) |
| 98 | + overlay_after = pe.get_overlay() |
| 99 | + pe.close() |
| 100 | + |
| 101 | + # If the update removed the overlay data, re-append it |
| 102 | + if not overlay_after: |
| 103 | + with open(exe_path, "ab") as exef: |
| 104 | + exef.write(overlay_before) |
| 105 | + |
| 106 | + return version_info_path |
0 commit comments