imphookapi.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. #-----------------------------------------------------------------------------
  2. # Copyright (c) 2005-2023, 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. Classes facilitating communication between PyInstaller and import hooks.
  13. PyInstaller passes instances of classes defined by this module to corresponding functions defined by external import
  14. hooks, which commonly modify the contents of these instances before returning. PyInstaller then detects and converts
  15. these modifications into appropriate operations on the current `PyiModuleGraph` instance, thus modifying which
  16. modules will be frozen into the executable.
  17. """
  18. from PyInstaller.building.utils import format_binaries_and_datas
  19. from PyInstaller.lib.modulegraph.modulegraph import (RuntimeModule, RuntimePackage)
  20. class PreSafeImportModuleAPI:
  21. """
  22. Metadata communicating changes made by the current **pre-safe import module hook** (i.e., hook run immediately
  23. _before_ a call to `ModuleGraph._safe_import_module()` recursively adding the hooked module, package,
  24. or C extension and all transitive imports thereof to the module graph) back to PyInstaller.
  25. Pre-safe import module hooks _must_ define a `pre_safe_import_module()` function accepting an instance of this
  26. class, whose attributes describe the subsequent `ModuleGraph._safe_import_module()` call creating the hooked
  27. module's graph node.
  28. Each pre-safe import module hook is run _only_ on the first attempt to create the hooked module's graph node and
  29. then subsequently ignored. If this hook successfully creates that graph node, the subsequent
  30. `ModuleGraph._safe_import_module()` call will observe this fact and silently return without attempting to
  31. recreate that graph node.
  32. Pre-safe import module hooks are typically used to create graph nodes for **runtime modules** (i.e.,
  33. modules dynamically defined at runtime). Most modules are physically defined in external `.py`-suffixed scripts.
  34. Some modules, however, are dynamically defined at runtime (e.g., `six.moves`, dynamically defined by the
  35. physically defined `six.py` module). However, `ModuleGraph` only parses `import` statements residing in external
  36. scripts. `ModuleGraph` is _not_ a full-fledged, Turing-complete Python interpreter and hence has no means of
  37. parsing `import` statements performed by runtime modules existing only in-memory.
  38. 'With great power comes great responsibility.'
  39. Attributes (Immutable)
  40. ----------------------------
  41. The following attributes are **immutable** (i.e., read-only). For safety, any attempts to change these attributes
  42. _will_ result in a raised exception:
  43. module_graph : PyiModuleGraph
  44. Current module graph.
  45. parent_package : Package
  46. Graph node for the package providing this module _or_ `None` if this module is a top-level module.
  47. Attributes (Mutable)
  48. -----------------------------
  49. The following attributes are editable.
  50. module_basename : str
  51. Unqualified name of the module to be imported (e.g., `text`).
  52. module_name : str
  53. Fully-qualified name of this module (e.g., `email.mime.text`).
  54. """
  55. def __init__(self, module_graph, module_basename, module_name, parent_package):
  56. self._module_graph = module_graph
  57. self.module_basename = module_basename
  58. self.module_name = module_name
  59. self._parent_package = parent_package
  60. # Immutable properties. No corresponding setters are defined.
  61. @property
  62. def module_graph(self):
  63. """
  64. Current module graph.
  65. """
  66. return self._module_graph
  67. @property
  68. def parent_package(self):
  69. """
  70. Parent Package of this node.
  71. """
  72. return self._parent_package
  73. def add_runtime_module(self, module_name):
  74. """
  75. Add a graph node representing a non-package Python module with the passed name dynamically defined at runtime.
  76. Most modules are statically defined on-disk as standard Python files. Some modules, however, are dynamically
  77. defined in-memory at runtime (e.g., `gi.repository.Gst`, dynamically defined by the statically defined
  78. `gi.repository.__init__` module).
  79. This method adds a graph node representing such a runtime module. Since this module is _not_ a package,
  80. all attempts to import submodules from this module in `from`-style import statements (e.g., the `queue`
  81. submodule in `from six.moves import queue`) will be silently ignored. To circumvent this, simply call
  82. `add_runtime_package()` instead.
  83. Parameters
  84. ----------
  85. module_name : str
  86. Fully-qualified name of this module (e.g., `gi.repository.Gst`).
  87. Examples
  88. ----------
  89. This method is typically called by `pre_safe_import_module()` hooks, e.g.:
  90. def pre_safe_import_module(api):
  91. api.add_runtime_module(api.module_name)
  92. """
  93. self._module_graph.add_module(RuntimeModule(module_name))
  94. def add_runtime_package(self, package_name):
  95. """
  96. Add a graph node representing a non-namespace Python package with the passed name dynamically defined at
  97. runtime.
  98. Most packages are statically defined on-disk as standard subdirectories containing `__init__.py` files. Some
  99. packages, however, are dynamically defined in-memory at runtime (e.g., `six.moves`, dynamically defined by
  100. the statically defined `six` module).
  101. This method adds a graph node representing such a runtime package. All attributes imported from this package
  102. in `from`-style import statements that are submodules of this package (e.g., the `queue` submodule in `from
  103. six.moves import queue`) will be imported rather than ignored.
  104. Parameters
  105. ----------
  106. package_name : str
  107. Fully-qualified name of this package (e.g., `six.moves`).
  108. Examples
  109. ----------
  110. This method is typically called by `pre_safe_import_module()` hooks, e.g.:
  111. def pre_safe_import_module(api):
  112. api.add_runtime_package(api.module_name)
  113. """
  114. self._module_graph.add_module(RuntimePackage(package_name))
  115. def add_alias_module(self, real_module_name, alias_module_name):
  116. """
  117. Alias the source module to the target module with the passed names.
  118. This method ensures that the next call to findNode() given the target module name will resolve this alias.
  119. This includes importing and adding a graph node for the source module if needed as well as adding a reference
  120. from the target to the source module.
  121. Parameters
  122. ----------
  123. real_module_name : str
  124. Fully-qualified name of the **existing module** (i.e., the module being aliased).
  125. alias_module_name : str
  126. Fully-qualified name of the **non-existent module** (i.e., the alias to be created).
  127. """
  128. self._module_graph.alias_module(real_module_name, alias_module_name)
  129. def append_package_path(self, directory):
  130. """
  131. Modulegraph does a good job at simulating Python's, but it cannot handle packagepath `__path__` modifications
  132. packages make at runtime.
  133. Therefore there is a mechanism whereby you can register extra paths in this map for a package, and it will be
  134. honored.
  135. Parameters
  136. ----------
  137. directory : str
  138. Absolute or relative path of the directory to be appended to this package's `__path__` attribute.
  139. """
  140. self._module_graph.append_package_path(self.module_name, directory)
  141. class PreFindModulePathAPI:
  142. """
  143. Metadata communicating changes made by the current **pre-find module path hook** (i.e., hook run immediately
  144. _before_ a call to `ModuleGraph._find_module_path()` finding the hooked module's absolute path) back to PyInstaller.
  145. Pre-find module path hooks _must_ define a `pre_find_module_path()` function accepting an instance of this class,
  146. whose attributes describe the subsequent `ModuleGraph._find_module_path()` call to be performed.
  147. Pre-find module path hooks are typically used to change the absolute path from which a module will be
  148. subsequently imported and thus frozen into the executable. To do so, hooks may overwrite the default
  149. `search_dirs` list of the absolute paths of all directories to be searched for that module: e.g.,
  150. def pre_find_module_path(api):
  151. api.search_dirs = ['/the/one/true/package/providing/this/module']
  152. Each pre-find module path hook is run _only_ on the first call to `ModuleGraph._find_module_path()` for the
  153. corresponding module.
  154. Attributes
  155. ----------
  156. The following attributes are **mutable** (i.e., modifiable). All changes to these attributes will be immediately
  157. respected by PyInstaller:
  158. search_dirs : list
  159. List of the absolute paths of all directories to be searched for this module (in order). Searching will halt
  160. at the first directory containing this module.
  161. Attributes (Immutable)
  162. ----------
  163. The following attributes are **immutable** (i.e., read-only). For safety, any attempts to change these attributes
  164. _will_ result in a raised exception:
  165. module_name : str
  166. Fully-qualified name of this module.
  167. module_graph : PyiModuleGraph
  168. Current module graph. For efficiency, this attribute is technically mutable. To preserve graph integrity,
  169. this attribute should nonetheless _never_ be modified. While read-only `PyiModuleGraph` methods (e.g.,
  170. `findNode()`) are safely callable from within pre-find module path hooks, methods modifying the graph are
  171. _not_. If graph modifications are required, consider an alternative type of hook (e.g., pre-import module
  172. hooks).
  173. """
  174. def __init__(
  175. self,
  176. module_graph,
  177. module_name,
  178. search_dirs,
  179. ):
  180. # Mutable attributes.
  181. self.search_dirs = search_dirs
  182. # Immutable attributes.
  183. self._module_graph = module_graph
  184. self._module_name = module_name
  185. # Immutable properties. No corresponding setters are defined.
  186. @property
  187. def module_graph(self):
  188. """
  189. Current module graph.
  190. """
  191. return self._module_graph
  192. @property
  193. def module_name(self):
  194. """
  195. Fully-qualified name of this module.
  196. """
  197. return self._module_name
  198. class PostGraphAPI:
  199. """
  200. Metadata communicating changes made by the current **post-graph hook** (i.e., hook run for a specific module
  201. transitively imported by the current application _after_ the module graph of all `import` statements performed by
  202. this application has been constructed) back to PyInstaller.
  203. Post-graph hooks may optionally define a `post_graph()` function accepting an instance of this class,
  204. whose attributes describe the current state of the module graph and the hooked module's graph node.
  205. Attributes (Mutable)
  206. ----------
  207. The following attributes are **mutable** (i.e., modifiable). All changes to these attributes will be immediately
  208. respected by PyInstaller:
  209. module_graph : PyiModuleGraph
  210. Current module graph.
  211. module : Node
  212. Graph node for the currently hooked module.
  213. 'With great power comes great responsibility.'
  214. Attributes (Immutable)
  215. ----------
  216. The following attributes are **immutable** (i.e., read-only). For safety, any attempts to change these attributes
  217. _will_ result in a raised exception:
  218. __name__ : str
  219. Fully-qualified name of this module (e.g., `six.moves.tkinter`).
  220. __file__ : str
  221. Absolute path of this module. If this module is:
  222. * A standard (rather than namespace) package, this is the absolute path of this package's directory.
  223. * A namespace (rather than standard) package, this is the abstract placeholder `-`. (Don't ask. Don't tell.)
  224. * A non-package module or C extension, this is the absolute path of the corresponding file.
  225. __path__ : list
  226. List of the absolute paths of all directories comprising this package if this module is a package _or_ `None`
  227. otherwise. If this module is a standard (rather than namespace) package, this list contains only the absolute
  228. path of this package's directory.
  229. co : code
  230. Code object compiled from the contents of `__file__` (e.g., via the `compile()` builtin).
  231. analysis: build_main.Analysis
  232. The Analysis that load the hook.
  233. Attributes (Private)
  234. ----------
  235. The following attributes are technically mutable but private, and hence should _never_ be externally accessed or
  236. modified by hooks. Call the corresponding public methods instead:
  237. _added_datas : list
  238. List of the `(name, path)` 2-tuples or TOC objects of all external data files required by the current hook,
  239. defaulting to the empty list. This is equivalent to the global `datas` hook attribute.
  240. _added_imports : list
  241. List of the fully-qualified names of all modules imported by the current hook, defaulting to the empty list.
  242. This is equivalent to the global `hiddenimports` hook attribute.
  243. _added_binaries : list
  244. List of the `(name, path)` 2-tuples or TOC objects of all external C extensions imported by the current hook,
  245. defaulting to the empty list. This is equivalent to the global `binaries` hook attribute.
  246. _module_collection_mode : dict
  247. Dictionary of package/module names and their corresponding collection mode strings. This is equivalent to the
  248. global `module_collection_mode` hook attribute.
  249. _bindepend_symlink_suppression : set
  250. A set of paths or path patterns corresponding to shared libraries for which binary dependency analysis should
  251. not generate symbolic links into top-level application directory.
  252. """
  253. def __init__(self, module_name, module_graph, analysis):
  254. # Mutable attributes.
  255. self.module_graph = module_graph
  256. self.module = module_graph.find_node(module_name)
  257. assert self.module is not None # should not occur
  258. # Immutable attributes.
  259. self.___name__ = module_name
  260. self.___file__ = self.module.filename
  261. self._co = self.module.code
  262. self._analysis = analysis
  263. # To enforce immutability, convert this module's package path if any into an immutable tuple.
  264. self.___path__ = tuple(self.module.packagepath) \
  265. if self.module.packagepath is not None else None
  266. #FIXME: Refactor "_added_datas", "_added_binaries", and "_deleted_imports" into sets. Since order of
  267. #import is important, "_added_imports" must remain a list.
  268. # Private attributes.
  269. self._added_binaries = []
  270. self._added_datas = []
  271. self._added_imports = []
  272. self._deleted_imports = []
  273. self._module_collection_mode = {}
  274. self._bindepend_symlink_suppression = set()
  275. # Immutable properties. No corresponding setters are defined.
  276. @property
  277. def __file__(self):
  278. """
  279. Absolute path of this module's file.
  280. """
  281. return self.___file__
  282. @property
  283. def __path__(self):
  284. """
  285. List of the absolute paths of all directories comprising this package if this module is a package _or_ `None`
  286. otherwise. If this module is a standard (rather than namespace) package, this list contains only the absolute
  287. path of this package's directory.
  288. """
  289. return self.___path__
  290. @property
  291. def __name__(self):
  292. """
  293. Fully-qualified name of this module (e.g., `six.moves.tkinter`).
  294. """
  295. return self.___name__
  296. @property
  297. def co(self):
  298. """
  299. Code object compiled from the contents of `__file__` (e.g., via the `compile()` builtin).
  300. """
  301. return self._co
  302. @property
  303. def analysis(self):
  304. """
  305. build_main.Analysis that calls the hook.
  306. """
  307. return self._analysis
  308. # Obsolete immutable properties provided to preserve backward compatibility.
  309. @property
  310. def name(self):
  311. """
  312. Fully-qualified name of this module (e.g., `six.moves.tkinter`).
  313. **This property has been deprecated by the `__name__` property.**
  314. """
  315. return self.___name__
  316. @property
  317. def graph(self):
  318. """
  319. Current module graph.
  320. **This property has been deprecated by the `module_graph` property.**
  321. """
  322. return self.module_graph
  323. @property
  324. def node(self):
  325. """
  326. Graph node for the currently hooked module.
  327. **This property has been deprecated by the `module` property.**
  328. """
  329. return self.module
  330. # TODO: This incorrectly returns the list of the graph nodes of all modules *TRANSITIVELY* (rather than directly)
  331. # imported by this module. Unfortunately, this implies that most uses of this property are currently broken
  332. # (e.g., "hook-PIL.SpiderImagePlugin.py"). We only require this for the aforementioned hook, so contemplate
  333. # alternative approaches.
  334. @property
  335. def imports(self):
  336. """
  337. List of the graph nodes of all modules directly imported by this module.
  338. """
  339. return self.module_graph.iter_graph(start=self.module)
  340. def add_imports(self, *module_names):
  341. """
  342. Add all Python modules whose fully-qualified names are in the passed list as "hidden imports" upon which the
  343. current module depends.
  344. This is equivalent to appending such names to the hook-specific `hiddenimports` attribute.
  345. """
  346. # Append such names to the current list of all such names.
  347. self._added_imports.extend(module_names)
  348. def del_imports(self, *module_names):
  349. """
  350. Remove the named fully-qualified modules from the set of imports (either hidden or visible) upon which the
  351. current module depends.
  352. This is equivalent to appending such names to the hook-specific `excludedimports` attribute.
  353. """
  354. self._deleted_imports.extend(module_names)
  355. def add_binaries(self, binaries):
  356. """
  357. Add all external dynamic libraries in the passed list of `(src_name, dest_name)` 2-tuples as dependencies of the
  358. current module. This is equivalent to adding to the global `binaries` hook attribute.
  359. For convenience, the `binaries` may also be a list of TOC-style 3-tuples `(dest_name, src_name, typecode)`.
  360. """
  361. # Detect TOC 3-tuple list by checking the length of the first entry
  362. if binaries and len(binaries[0]) == 3:
  363. self._added_binaries.extend(entry[:2] for entry in binaries)
  364. else:
  365. # NOTE: `format_binaries_and_datas` changes tuples from input format `(src_name, dest_name)` to output
  366. # format `(dest_name, src_name)`.
  367. self._added_binaries.extend(format_binaries_and_datas(binaries))
  368. def add_datas(self, datas):
  369. """
  370. Add all external data files in the passed list of `(src_name, dest_name)` 2-tuples as dependencies of the
  371. current module. This is equivalent to adding to the global `datas` hook attribute.
  372. For convenience, the `datas` may also be a list of TOC-style 3-tuples `(dest_name, src_name, typecode)`.
  373. """
  374. # Detect TOC 3-tuple list by checking the length of the first entry
  375. if datas and len(datas[0]) == 3:
  376. self._added_datas.extend(entry[:2] for entry in datas)
  377. else:
  378. # NOTE: `format_binaries_and_datas` changes tuples from input format `(src_name, dest_name)` to output
  379. # format `(dest_name, src_name)`.
  380. self._added_datas.extend(format_binaries_and_datas(datas))
  381. def set_module_collection_mode(self, name, mode):
  382. """"
  383. Set the package/module collection mode for the specified module name. If `name` is `None`, the hooked
  384. module/package name is used. `mode` can be one of valid mode strings (`'pyz'`, `'pyc'`, `'py'`, `'pyz+py'`,
  385. `'py+pyz'`) or `None`, which clears the setting for the module/package - but only within this hook's context!
  386. """
  387. if name is None:
  388. name = self.__name__
  389. if mode is None:
  390. self._module_collection_mode.pop(name)
  391. else:
  392. self._module_collection_mode[name] = mode
  393. def add_bindepend_symlink_suppression_pattern(self, pattern):
  394. """
  395. Add the given path or path pattern to the set of patterns that prevent binary dependency analysis from creating
  396. a symbolic link to the top-level application directory.
  397. """
  398. self._bindepend_symlink_suppression.add(pattern)