freeze_support.py 1.3 KB

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