test_ok2/src/_pytest/freeze_support.py

47 lines
1.4 KiB
Python
Raw Normal View History

"""Provides a function to report all internal modules for using freezing
tools."""
2020-06-25 20:15:08 +08:00
import types
from typing import Iterator
from typing import List
from typing import Union
2020-06-25 20:15:08 +08:00
def freeze_includes() -> List[str]:
"""Return a list of module names used by pytest that should be
included by cx_freeze."""
import py
import _pytest
2018-05-23 22:48:46 +08:00
result = list(_iter_all_modules(py))
result += list(_iter_all_modules(_pytest))
return result
2020-06-25 20:15:08 +08:00
def _iter_all_modules(
package: Union[str, types.ModuleType],
prefix: str = "",
2020-06-25 20:15:08 +08:00
) -> Iterator[str]:
"""Iterate over the names of all modules that can be found in the given
package, recursively.
>>> import _pytest
>>> list(_iter_all_modules(_pytest))
['_pytest._argcomplete', '_pytest._code.code', ...]
"""
import os
import pkgutil
2018-05-23 22:48:46 +08:00
2020-06-25 20:15:08 +08:00
if isinstance(package, str):
path = package
2020-06-25 20:15:08 +08:00
else:
# Type ignored because typeshed doesn't define ModuleType.__path__
# (only defined on packages).
package_path = package.__path__ # type: ignore[attr-defined]
path, prefix = package_path[0], package.__name__ + "."
for _, name, is_package in pkgutil.iter_modules([path]):
if is_package:
2018-05-23 22:48:46 +08:00
for m in _iter_all_modules(os.path.join(path, name), prefix=name + "."):
yield prefix + m
else:
yield prefix + name