You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
1.4KB

  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 py
  11. import _pytest
  12. result = list(_iter_all_modules(py))
  13. result += list(_iter_all_modules(_pytest))
  14. return result
  15. def _iter_all_modules(
  16. package: Union[str, types.ModuleType], prefix: str = "",
  17. ) -> Iterator[str]:
  18. """Iterate over the names of all modules that can be found in the given
  19. package, recursively.
  20. >>> import _pytest
  21. >>> list(_iter_all_modules(_pytest))
  22. ['_pytest._argcomplete', '_pytest._code.code', ...]
  23. """
  24. import os
  25. import pkgutil
  26. if isinstance(package, str):
  27. path = package
  28. else:
  29. # Type ignored because typeshed doesn't define ModuleType.__path__
  30. # (only defined on packages).
  31. package_path = package.__path__ # type: ignore[attr-defined]
  32. path, prefix = package_path[0], package.__name__ + "."
  33. for _, name, is_package in pkgutil.iter_modules([path]):
  34. if is_package:
  35. for m in _iter_all_modules(os.path.join(path, name), prefix=name + "."):
  36. yield prefix + m
  37. else:
  38. yield prefix + name