freeze_support.py 1.3 KB

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