Debian Codegraph @

util/winresource.py

raw
1 #
2 # Copyright (C) 2009, Florian Hoech
3 #
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, 5th Floor, Boston, MA 02110-1301, USA
17
18 """
19 winresource.py
20
21 Read and write resources from/to Win32 PE files.
22
23 Commandline usage:
24 winresource.py <dstpath> <srcpath>
25 Updates or adds resources from file <srcpath> in file <dstpath>.
26
27 2009-03 Florian Hoech
28
29 """
30
31 import pywintypes
32 import win32api
33
34 silent = False # True suppresses all messages
35
36 LOAD_LIBRARY_AS_DATAFILE = 2
37 ERROR_BAD_EXE_FORMAT = 193
38 ERROR_RESOURCE_DATA_NOT_FOUND = 1812
39 ERROR_RESOURCE_TYPE_NOT_FOUND = 1813
40 ERROR_RESOURCE_NAME_NOT_FOUND = 1814
41 ERROR_RESOURCE_LANG_NOT_FOUND = 1815
42
43
44 class File:
45
46 """Win32 PE file class."""
47
48 def __init__(self, filename):
49 self.filename = filename
50
51 def get_resources(self, types=None, names=None, languages=None):
52 """Get resources.
53
54 types = a list of resource types to search for (None = all)
55 names = a list of resource names to search for (None = all)
56 languages = a list of resource languages to search for (None = all)
57 Return a dict of the form {type_: {name: {language: data}}} which
58 might also be empty if no matching resources were found.
59
60 """
61 return GetResources(self.filename, types, names, languages)
62
63 def update_resources(self, data, type_, names=None, languages=None):
64 """Update or add resource data.
65
66 type_ = resource type to update
67 names = a list of resource names to update (None = all)
68 languages = a list of resource languages to update (None = all)
69
70 """
71 UpdateResources(self.filename, data, type_, names, languages)
72
73 def update_resources_from_datafile(
74 self, srcpath, type_, names=None, languages=None
75 ):
76 """Update or add resource data from file srcpath.
77
78 type_ = resource type to update
79 names = a list of resource names to update (None = all)
80 languages = a list of resource languages to update (None = all)
81
82 """
83 UpdateResourcesFromDataFile(self.filename, srcpath, type_, names, languages)
84
85 def update_resources_from_dict(self, res, types=None, names=None, languages=None):
86 """Update or add resources from resource dict.
87
88 types = a list of resource types to update (None = all)
89 names = a list of resource names to update (None = all)
90 languages = a list of resource languages to update (None = all)
91
92 """
93 UpdateResourcesFromDict(self.filename, res, types, names, languages)
94
95 def update_resources_from_resfile(
96 self, srcpath, types=None, names=None, languages=None
97 ):
98 """Update or add resources from dll/exe file srcpath.
99
100 types = a list of resource types to update (None = all)
101 names = a list of resource names to update (None = all)
102 languages = a list of resource languages to update (None = all)
103
104 """
105 UpdateResourcesFromResFile(self.filename, srcpath, types, names, languages)
106
107
108 def _GetResources(hsrc, types=None, names=None, languages=None):
109 """Get resources from hsrc.
110
111 types = a list of resource types to search for (None = all)
112 names = a list of resource names to search for (None = all)
113 languages = a list of resource languages to search for (None = all)
114 Return a dict of the form {type_: {name: {language: data}}} which
115 might also be empty if no matching resources were found.
116
117 """
118 res = {}
119 try:
120 # print "I: Enumerating resource types"
121 enum_types = win32api.EnumResourceTypes(hsrc)
122 if types and "*" not in types:
123 enum_types = filter(lambda type_: type_ in types, enum_types)
124 for type_ in enum_types:
125 # print "I: Enumerating resources of type", type_
126 enum_names = win32api.EnumResourceNames(hsrc, type_)
127 if names and "*" not in names:
128 enum_names = filter(lambda name: name in names, enum_names)
129 for name in enum_names:
130 # print "I: Enumerating resources of type", type_, "name", name
131 enum_languages = win32api.EnumResourceLanguages(hsrc, type_, name)
132 if languages and "*" not in languages:
133 enum_languages = filter(
134 lambda language: language in languages, enum_languages
135 )
136 for language in enum_languages:
137 data = win32api.LoadResource(hsrc, type_, name, language)
138 if type_ not in res:
139 res[type_] = {}
140 if name not in res[type_]:
141 res[type_][name] = {}
142 res[type_][name][language] = data
143 except pywintypes.error as exception:
144 if exception.args[0] in (
145 ERROR_RESOURCE_DATA_NOT_FOUND,
146 ERROR_RESOURCE_TYPE_NOT_FOUND,
147 ERROR_RESOURCE_NAME_NOT_FOUND,
148 ERROR_RESOURCE_LANG_NOT_FOUND,
149 ):
150 # print "I:", exception.args[1] + ":", \
151 # exception.args[2]
152 pass
153 else:
154 raise exception
155 return res
156
157
158 def GetResources(filename, types=None, names=None, languages=None):
159 """Get resources from dll/exe file.
160
161 types = a list of resource types to search for (None = all)
162 names = a list of resource names to search for (None = all)
163 languages = a list of resource languages to search for (None = all)
164 Return a dict of the form {type_: {name: {language: data}}} which
165 might also be empty if no matching resources were found.
166
167 """
168 hsrc = win32api.LoadLibraryEx(filename, 0, LOAD_LIBRARY_AS_DATAFILE)
169 res = _GetResources(hsrc, types, names, languages)
170 win32api.FreeLibrary(hsrc)
171 return res
172
173
174 def UpdateResources(dstpath, data, type_, names=None, languages=None):
175 """Update or add resource data in dll/exe file dstpath.
176
177 type_ = resource type to update
178 names = a list of resource names to update (None = all)
179 languages = a list of resource languages to update (None = all)
180
181 """
182 # look for existing resources
183 res = GetResources(dstpath, [type_], names, languages)
184 # add type_, names and languages not already present in existing resources
185 if type_ not in res and type_ != "*":
186 res[type_] = {}
187 if names:
188 for name in names:
189 if name not in res[type_] and name != "*":
190 res[type_][name] = []
191 if languages:
192 for language in languages:
193 if language not in res[type_][name] and language != "*":
194 res[type_][name].append(language)
195 # add resource to destination, overwriting existing resources
196 hdst = win32api.BeginUpdateResource(dstpath, 0)
197 for type_ in res:
198 for name in res[type_]:
199 for language in res[type_][name]:
200 if not silent:
201 print(
202 "I: Updating resource type",
203 type_,
204 "name",
205 name,
206 "language",
207 language,
208 )
209 win32api.UpdateResource(hdst, type_, name, data, language)
210 win32api.EndUpdateResource(hdst, 0)
211
212
213 def UpdateResourcesFromDataFile(dstpath, srcpath, type_, names=None, languages=None):
214 """Update or add resource data from file srcpath in dll/exe file dstpath.
215
216 type_ = resource type to update
217 names = a list of resource names to update (None = all)
218 languages = a list of resource languages to update (None = all)
219
220 """
221 src = open(srcpath, "rb")
222 data = src.read()
223 src.close()
224 UpdateResources(dstpath, data, type_, names, languages)
225
226
227 def UpdateResourcesFromDict(dstpath, res, types=None, names=None, languages=None):
228 """Update or add resources from resource dict in dll/exe file dstpath.
229
230 types = a list of resource types to update (None = all)
231 names = a list of resource names to update (None = all)
232 languages = a list of resource languages to update (None = all)
233
234 """
235 for type_ in res:
236 if not types or type_ in types:
237 for name in res[type_]:
238 if not names or name in names:
239 for language in res[type_][name]:
240 if not languages or language in languages:
241 UpdateResources(
242 dstpath,
243 res[type_][name][language],
244 [type_],
245 [name],
246 [language],
247 )
248
249
250 def UpdateResourcesFromResFile(
251 dstpath, srcpath, types=None, names=None, languages=None
252 ):
253 """Update or add resources from dll/exe file srcpath in dll/exe file dstpath.
254
255 types = a list of resource types to update (None = all)
256 names = a list of resource names to update (None = all)
257 languages = a list of resource languages to update (None = all)
258
259 """
260 res = GetResources(srcpath, types, names, languages)
261 UpdateResourcesFromDict(dstpath, res)