| 1 |
"""Provides a function to report all internal modules for using freezing |
| 2 |
tools.""" |
| 3 |
|
| 4 |
from __future__ import annotations |
| 5 |
|
| 6 |
from collections.abc import Iterator |
| 7 |
import types |
| 8 |
|
| 9 |
|
| 10 |
def freeze_includes() -> list[str]: |
| 11 |
"""Return a list of module names used by pytest that should be |
| 12 |
included by cx_freeze.""" |
| 13 |
import _pytest |
| 14 |
|
| 15 |
result = list(_iter_all_modules(_pytest)) |
| 16 |
return result |
| 17 |
|
| 18 |
|
| 19 |
def _iter_all_modules( |
| 20 |
package: str | types.ModuleType, |
| 21 |
prefix: str = "", |
| 22 |
) -> Iterator[str]: |
| 23 |
"""Iterate over the names of all modules that can be found in the given |
| 24 |
package, recursively. |
| 25 |
|
| 26 |
>>> import _pytest |
| 27 |
>>> list(_iter_all_modules(_pytest)) |
| 28 |
['_pytest._argcomplete', '_pytest._code.code', ...] |
| 29 |
""" |
| 30 |
import os |
| 31 |
import pkgutil |
| 32 |
|
| 33 |
if isinstance(package, str): |
| 34 |
path = package |
| 35 |
else: |
| 36 |
# Type ignored because typeshed doesn't define ModuleType.__path__ |
| 37 |
# (only defined on packages). |
| 38 |
package_path = package.__path__ |
| 39 |
path, prefix = package_path[0], package.__name__ + "." |
| 40 |
for _, name, is_package in pkgutil.iter_modules([path]): |
| 41 |
if is_package: |
| 42 |
for m in _iter_all_modules(os.path.join(path, name), prefix=name + "."): |
| 43 |
yield prefix + m |
| 44 |
else: |
| 45 |
yield prefix + name |