import struct
import sys
from pathlib import Path
SIZE = 16
BLUE_BGRA = bytes((0xCC, 0x52, 0x00, 0xFF))
ICON_PATH = Path(__file__).resolve().parent.parent / "packaging" / "windows" / "icon.ico"
def build_ico() -> bytes:
pixels = BLUE_BGRA * (SIZE * SIZE)
and_mask = bytes(SIZE * 4)
bmp_header = struct.pack(
"<IiiHHIIiiII",
40, SIZE, SIZE * 2, 1, 32, 0, len(pixels) + len(and_mask), 0, 0, 0, 0
)
image = bmp_header + pixels + and_mask
icon_dir = struct.pack("<HHH", 0, 1, 1)
dir_entry = struct.pack(
"<BBBBHHII",
SIZE, SIZE, 0, 0, 1, 32, len(image), len(icon_dir) + 16
)
return icon_dir + dir_entry + image
def main() -> int:
data = build_ico()
if "--check" in sys.argv[1:]:
if ICON_PATH.read_bytes() == data:
print(f"ok: {ICON_PATH} matches ({len(data)} bytes)")
return 0
print(f"MISMATCH: {ICON_PATH} does not match the generated bytes")
return 1
ICON_PATH.write_bytes(data)
print(f"wrote: {ICON_PATH} ({len(data)} bytes)")
return 0
if __name__ == "__main__":
sys.exit(main())