winresource.py 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. #-----------------------------------------------------------------------------
  2. # Copyright (c) 2013-2021, PyInstaller Development Team.
  3. #
  4. # Distributed under the terms of the GNU General Public License (version 2
  5. # or later) with exception for distributing the bootloader.
  6. #
  7. # The full license is in the file COPYING.txt, distributed with this software.
  8. #
  9. # SPDX-License-Identifier: (GPL-2.0-or-later WITH Bootloader-exception)
  10. #-----------------------------------------------------------------------------
  11. """
  12. Read and write resources from/to Win32 PE files.
  13. Commandline usage:
  14. winresource.py <dstpath> <srcpath>
  15. Updates or adds resources from file <srcpath> in file <dstpath>.
  16. """
  17. from PyInstaller.compat import pywintypes, win32api
  18. import PyInstaller.log as logging
  19. logger = logging.getLogger(__name__)
  20. LOAD_LIBRARY_AS_DATAFILE = 2
  21. ERROR_BAD_EXE_FORMAT = 193
  22. ERROR_RESOURCE_DATA_NOT_FOUND = 1812
  23. ERROR_RESOURCE_TYPE_NOT_FOUND = 1813
  24. ERROR_RESOURCE_NAME_NOT_FOUND = 1814
  25. ERROR_RESOURCE_LANG_NOT_FOUND = 1815
  26. class File(object):
  27. """
  28. Win32 PE file class.
  29. """
  30. def __init__(self, filename):
  31. self.filename = filename
  32. def get_resources(self, types=None, names=None, languages=None):
  33. """
  34. Get resources.
  35. types = a list of resource types to search for (None = all)
  36. names = a list of resource names to search for (None = all)
  37. languages = a list of resource languages to search for (None = all)
  38. Return a dict of the form {type_: {name: {language: data}}} which
  39. might also be empty if no matching resources were found.
  40. """
  41. return GetResources(self.filename, types, names, languages)
  42. def update_resources(self, data, type_, names=None, languages=None):
  43. """
  44. Update or add resource data.
  45. type_ = resource type to update
  46. names = a list of resource names to update (None = all)
  47. languages = a list of resource languages to update (None = all)
  48. """
  49. UpdateResources(self.filename, data, type_, names, languages)
  50. def update_resources_from_datafile(self, srcpath, type_, names=None,
  51. languages=None):
  52. """
  53. Update or add resource data from file srcpath.
  54. type_ = resource type to update
  55. names = a list of resource names to update (None = all)
  56. languages = a list of resource languages to update (None = all)
  57. """
  58. UpdateResourcesFromDataFile(self.filename, srcpath, type_, names,
  59. languages)
  60. def update_resources_from_dict(self, res, types=None, names=None,
  61. languages=None):
  62. """
  63. Update or add resources from resource dict.
  64. types = a list of resource types to update (None = all)
  65. names = a list of resource names to update (None = all)
  66. languages = a list of resource languages to update (None = all)
  67. """
  68. UpdateResourcesFromDict(self.filename, res, types, names,
  69. languages)
  70. def update_resources_from_resfile(self, srcpath, types=None, names=None,
  71. languages=None):
  72. """
  73. Update or add resources from dll/exe file srcpath.
  74. types = a list of resource types to update (None = all)
  75. names = a list of resource names to update (None = all)
  76. languages = a list of resource languages to update (None = all)
  77. """
  78. UpdateResourcesFromResFile(self.filename, srcpath, types, names,
  79. languages)
  80. def _GetResources(hsrc, types=None, names=None, languages=None):
  81. """
  82. Get resources from hsrc.
  83. types = a list of resource types to search for (None = all)
  84. names = a list of resource names to search for (None = all)
  85. languages = a list of resource languages to search for (None = all)
  86. Return a dict of the form {type_: {name: {language: data}}} which
  87. might also be empty if no matching resources were found.
  88. """
  89. if types:
  90. types = set(types)
  91. if names:
  92. names = set(names)
  93. if languages:
  94. languages = set(languages)
  95. res = {}
  96. try:
  97. # logger.debug("Enumerating resource types")
  98. enum_types = win32api.EnumResourceTypes(hsrc)
  99. if types and not "*" in types:
  100. enum_types = filter(lambda type_:
  101. type_ in types,
  102. enum_types)
  103. for type_ in enum_types:
  104. # logger.debug("Enumerating resources of type %s", type_)
  105. enum_names = win32api.EnumResourceNames(hsrc, type_)
  106. if names and not "*" in names:
  107. enum_names = filter(lambda name:
  108. name in names,
  109. enum_names)
  110. for name in enum_names:
  111. # logger.debug("Enumerating resources of type %s name %s", type_, name)
  112. enum_languages = win32api.EnumResourceLanguages(hsrc,
  113. type_,
  114. name)
  115. if languages and not "*" in languages:
  116. enum_languages = filter(lambda language:
  117. language in languages,
  118. enum_languages)
  119. for language in enum_languages:
  120. data = win32api.LoadResource(hsrc, type_, name, language)
  121. if not type_ in res:
  122. res[type_] = {}
  123. if not name in res[type_]:
  124. res[type_][name] = {}
  125. res[type_][name][language] = data
  126. except pywintypes.error as exception:
  127. if exception.args[0] in (ERROR_RESOURCE_DATA_NOT_FOUND,
  128. ERROR_RESOURCE_TYPE_NOT_FOUND,
  129. ERROR_RESOURCE_NAME_NOT_FOUND,
  130. ERROR_RESOURCE_LANG_NOT_FOUND):
  131. # logger.info('%s: %s', exception.args[1:3])
  132. pass
  133. else:
  134. raise exception
  135. return res
  136. def GetResources(filename, types=None, names=None, languages=None):
  137. """
  138. Get resources from dll/exe file.
  139. types = a list of resource types to search for (None = all)
  140. names = a list of resource names to search for (None = all)
  141. languages = a list of resource languages to search for (None = all)
  142. Return a dict of the form {type_: {name: {language: data}}} which
  143. might also be empty if no matching resources were found.
  144. """
  145. hsrc = win32api.LoadLibraryEx(filename, 0, LOAD_LIBRARY_AS_DATAFILE)
  146. res = _GetResources(hsrc, types, names, languages)
  147. win32api.FreeLibrary(hsrc)
  148. return res
  149. def UpdateResources(dstpath, data, type_, names=None, languages=None):
  150. """
  151. Update or add resource data in dll/exe file dstpath.
  152. type_ = resource type to update
  153. names = a list of resource names to update (None = all)
  154. languages = a list of resource languages to update (None = all)
  155. """
  156. # Look for existing resources.
  157. res = GetResources(dstpath, [type_], names, languages)
  158. # add type_, names and languages not already present in existing resources
  159. if not type_ in res and type_ != "*":
  160. res[type_] = {}
  161. if names:
  162. for name in names:
  163. if not name in res[type_] and name != "*":
  164. res[type_][name] = []
  165. if languages:
  166. for language in languages:
  167. if not language in res[type_][name] and language != "*":
  168. res[type_][name].append(language)
  169. # add resource to destination, overwriting existing resources
  170. hdst = win32api.BeginUpdateResource(dstpath, 0)
  171. for type_ in res:
  172. for name in res[type_]:
  173. for language in res[type_][name]:
  174. logger.info("Updating resource type %s name %s language %s",
  175. type_, name, language)
  176. win32api.UpdateResource(hdst, type_, name, data, language)
  177. win32api.EndUpdateResource(hdst, 0)
  178. def UpdateResourcesFromDataFile(dstpath, srcpath, type_, names=None,
  179. languages=None):
  180. """
  181. Update or add resource data from file srcpath in dll/exe file dstpath.
  182. type_ = resource type to update
  183. names = a list of resource names to update (None = all)
  184. languages = a list of resource languages to update (None = all)
  185. """
  186. with open(srcpath, "rb") as src:
  187. data = src.read()
  188. UpdateResources(dstpath, data, type_, names, languages)
  189. def UpdateResourcesFromDict(dstpath, res, types=None, names=None,
  190. languages=None):
  191. """
  192. Update or add resources from resource dict in dll/exe file dstpath.
  193. types = a list of resource types to update (None = all)
  194. names = a list of resource names to update (None = all)
  195. languages = a list of resource languages to update (None = all)
  196. """
  197. if types:
  198. types = set(types)
  199. if names:
  200. names = set(names)
  201. if languages:
  202. languages = set(languages)
  203. for type_ in res:
  204. if not types or type_ in types:
  205. for name in res[type_]:
  206. if not names or name in names:
  207. for language in res[type_][name]:
  208. if not languages or language in languages:
  209. UpdateResources(dstpath,
  210. res[type_][name][language],
  211. type_, [name], [language])
  212. def UpdateResourcesFromResFile(dstpath, srcpath, types=None, names=None,
  213. languages=None):
  214. """
  215. Update or add resources from dll/exe file srcpath in dll/exe file dstpath.
  216. types = a list of resource types to update (None = all)
  217. names = a list of resource names to update (None = all)
  218. languages = a list of resource languages to update (None = all)
  219. """
  220. res = GetResources(srcpath, types, names, languages)
  221. UpdateResourcesFromDict(dstpath, res)