| 1 |
#----------------------------------------------------------------------------- |
| 2 |
# Copyright (c) 2013, PyInstaller Development Team. |
| 3 |
# |
| 4 |
# Distributed under the terms of the GNU General Public License with exception |
| 5 |
# for distributing bootloader. |
| 6 |
# |
| 7 |
# The full license is in the file COPYING.txt, distributed with this software. |
| 8 |
#----------------------------------------------------------------------------- |
| 9 |
|
| 10 |
|
| 11 |
RT_ICON = 3 |
| 12 |
RT_GROUP_ICON = 14 |
| 13 |
LOAD_LIBRARY_AS_DATAFILE = 2 |
| 14 |
|
| 15 |
import struct |
| 16 |
import types |
| 17 |
try: |
| 18 |
StringTypes = types.StringTypes |
| 19 |
except AttributeError: |
| 20 |
StringTypes = (str) |
| 21 |
|
| 22 |
import logging |
| 23 |
logger = logging.getLogger() |
| 24 |
|
| 25 |
|
| 26 |
class Structure: |
| 27 |
def __init__(self): |
| 28 |
size = self._sizeInBytes = struct.calcsize(self._format_) |
| 29 |
self._fields_ = list(struct.unpack(self._format_, '\000' * size)) |
| 30 |
indexes = self._indexes_ = {} |
| 31 |
for i, nm in enumerate(self._names_): |
| 32 |
indexes[nm] = i |
| 33 |
|
| 34 |
def dump(self): |
| 35 |
logger.info("DUMP of %s", self) |
| 36 |
for name in self._names_: |
| 37 |
if not name.startswith('_'): |
| 38 |
logger.info("%20s = %s", name, getattr(self, name)) |
| 39 |
logger.info("") |
| 40 |
|
| 41 |
def __getattr__(self, name): |
| 42 |
if name in self._names_: |
| 43 |
index = self._indexes_[name] |
| 44 |
return self._fields_[index] |
| 45 |
try: |
| 46 |
return self.__dict__[name] |
| 47 |
except KeyError: |
| 48 |
raise AttributeError(name) |
| 49 |
|
| 50 |
def __setattr__(self, name, value): |
| 51 |
if name in self._names_: |
| 52 |
index = self._indexes_[name] |
| 53 |
self._fields_[index] = value |
| 54 |
else: |
| 55 |
self.__dict__[name] = value |
| 56 |
|
| 57 |
def tostring(self): |
| 58 |
args = [self._format_,] + self._fields_ |
| 59 |
return struct.pack(*args) |
| 60 |
|
| 61 |
def fromfile(self, file): |
| 62 |
data = file.read(self._sizeInBytes) |
| 63 |
self._fields_ = list(struct.unpack(self._format_, data)) |
| 64 |
|
| 65 |
|
| 66 |
class ICONDIRHEADER(Structure): |
| 67 |
_names_ = "idReserved", "idType", "idCount" |
| 68 |
_format_ = "hhh" |
| 69 |
|
| 70 |
class ICONDIRENTRY(Structure): |
| 71 |
_names_ = ("bWidth", "bHeight", "bColorCount", "bReserved", "wPlanes", |
| 72 |
"wBitCount", "dwBytesInRes", "dwImageOffset") |
| 73 |
_format_ = "bbbbhhii" |
| 74 |
|
| 75 |
class GRPICONDIR(Structure): |
| 76 |
_names_ = "idReserved", "idType", "idCount" |
| 77 |
_format_ = "hhh" |
| 78 |
|
| 79 |
class GRPICONDIRENTRY(Structure): |
| 80 |
_names_ = ("bWidth", "bHeight", "bColorCount", "bReserved", "wPlanes", |
| 81 |
"wBitCount", "dwBytesInRes", "nID") |
| 82 |
_format_ = "bbbbhhih" |
| 83 |
|
| 84 |
class IconFile: |
| 85 |
def __init__(self, path): |
| 86 |
self.path = path |
| 87 |
file = open(path, "rb") |
| 88 |
self.entries = [] |
| 89 |
self.images = [] |
| 90 |
header = self.header = ICONDIRHEADER() |
| 91 |
header.fromfile(file) |
| 92 |
for i in range(header.idCount): |
| 93 |
entry = ICONDIRENTRY() |
| 94 |
entry.fromfile(file) |
| 95 |
self.entries.append(entry) |
| 96 |
for e in self.entries: |
| 97 |
file.seek(e.dwImageOffset, 0) |
| 98 |
self.images.append(file.read(e.dwBytesInRes)) |
| 99 |
|
| 100 |
def grp_icon_dir(self): |
| 101 |
return self.header.tostring() |
| 102 |
|
| 103 |
def grp_icondir_entries(self, id=1): |
| 104 |
data = "" |
| 105 |
for entry in self.entries: |
| 106 |
e = GRPICONDIRENTRY() |
| 107 |
for n in e._names_[:-1]: |
| 108 |
setattr(e, n, getattr(entry, n)) |
| 109 |
e.nID = id |
| 110 |
id = id + 1 |
| 111 |
data = data + e.tostring() |
| 112 |
return data |
| 113 |
|
| 114 |
|
| 115 |
def CopyIcons_FromIco(dstpath, srcpath, id=1): |
| 116 |
import win32api #, win32con |
| 117 |
icons = map(IconFile, srcpath) |
| 118 |
logger.info("Updating icons from %s to %s", srcpath, dstpath) |
| 119 |
|
| 120 |
hdst = win32api.BeginUpdateResource(dstpath, 0) |
| 121 |
|
| 122 |
iconid = 1 |
| 123 |
for i, f in enumerate(icons): |
| 124 |
data = f.grp_icon_dir() |
| 125 |
data = data + f.grp_icondir_entries(iconid) |
| 126 |
win32api.UpdateResource(hdst, RT_GROUP_ICON, i, data) |
| 127 |
logger.info("Writing RT_GROUP_ICON %d resource with %d bytes", i, len(data)) |
| 128 |
for data in f.images: |
| 129 |
win32api.UpdateResource(hdst, RT_ICON, iconid, data) |
| 130 |
logger.info("Writing RT_ICON %d resource with %d bytes", iconid, len(data)) |
| 131 |
iconid = iconid + 1 |
| 132 |
|
| 133 |
win32api.EndUpdateResource(hdst, 0) |
| 134 |
|
| 135 |
def CopyIcons(dstpath, srcpath): |
| 136 |
import os.path |
| 137 |
|
| 138 |
if isinstance(srcpath, StringTypes): |
| 139 |
srcpath = [ srcpath ] |
| 140 |
|
| 141 |
def splitter(s): |
| 142 |
try: |
| 143 |
srcpath, index = s.split(',') |
| 144 |
return srcpath.strip(), int(index) |
| 145 |
except ValueError: |
| 146 |
return s, None |
| 147 |
|
| 148 |
srcpath = map(splitter, srcpath) |
| 149 |
logger.info("SRCPATH %s", srcpath) |
| 150 |
|
| 151 |
if len(srcpath) > 1: |
| 152 |
# At the moment, we support multiple icons only from .ico files |
| 153 |
srcs = [] |
| 154 |
for s in srcpath: |
| 155 |
e = os.path.splitext(s[0])[1] |
| 156 |
if e.lower() != '.ico': |
| 157 |
raise ValueError("multiple icons supported only from .ico files") |
| 158 |
if s[1] is not None: |
| 159 |
raise ValueError("index not allowed for .ico files") |
| 160 |
srcs.append(s[0]) |
| 161 |
return CopyIcons_FromIco(dstpath, srcs) |
| 162 |
|
| 163 |
srcpath,index = srcpath[0] |
| 164 |
srcext = os.path.splitext(srcpath)[1] |
| 165 |
if srcext.lower() == '.ico': |
| 166 |
return CopyIcons_FromIco(dstpath, [srcpath]) |
| 167 |
if index is not None: |
| 168 |
logger.info("Updating icons from %s, %d to %s", srcpath, index, dstpath) |
| 169 |
else: |
| 170 |
logger.info("Updating icons from %s to %s", srcpath, dstpath) |
| 171 |
import win32api #, win32con |
| 172 |
hdst = win32api.BeginUpdateResource(dstpath, 0) |
| 173 |
hsrc = win32api.LoadLibraryEx(srcpath, 0, LOAD_LIBRARY_AS_DATAFILE) |
| 174 |
if index is None: |
| 175 |
grpname = win32api.EnumResourceNames(hsrc, RT_GROUP_ICON)[0] |
| 176 |
elif index >= 0: |
| 177 |
grpname = win32api.EnumResourceNames(hsrc, RT_GROUP_ICON)[index] |
| 178 |
else: |
| 179 |
grpname = -index |
| 180 |
data = win32api.LoadResource(hsrc, RT_GROUP_ICON, grpname) |
| 181 |
win32api.UpdateResource(hdst, RT_GROUP_ICON, grpname, data) |
| 182 |
for iconname in win32api.EnumResourceNames(hsrc, RT_ICON): |
| 183 |
data = win32api.LoadResource(hsrc, RT_ICON, iconname) |
| 184 |
win32api.UpdateResource(hdst, RT_ICON, iconname, data) |
| 185 |
win32api.FreeLibrary(hsrc) |
| 186 |
win32api.EndUpdateResource(hdst, 0) |
| 187 |
|
| 188 |
|
| 189 |
def add_icon(exe_path, ico_filename, ico_id): |
| 190 |
CopyIcons(exe_path, "%s,%i" % (ico_filename, ico_id)) |
| 191 |
|
| 192 |
if __name__ == "__main__": |
| 193 |
import sys |
| 194 |
|
| 195 |
dstpath = sys.argv[1] |
| 196 |
srcpath = sys.argv[2:] |
| 197 |
CopyIcons(dstpath, srcpath) |