-
Notifications
You must be signed in to change notification settings - Fork 10
/
find_all_modules.py
executable file
·66 lines (52 loc) · 1.83 KB
/
find_all_modules.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python3
"""Searches for Python module in a given path and prints them to STDOUT."""
import os
import sys
from pkgutil import iter_modules
from typing import Any, Union, Set, List
from setuptools import find_packages
def error_print(*args: Any, **kwargs: Any) -> None:
"""Prints to STDERR.
Args:
*args: A list of arguments
**kwargs: A dict of key-value arguments
"""
print(*args, file=sys.stderr, **kwargs)
def flatten_nested_list(l: List[Union[Any, List]]) -> List:
"""
"""
l_flat = []
for x in l:
if isinstance(x, list):
flat_x = flatten_nested_list(x)
l_flat.extend(flat_x)
else:
l_flat.append(x)
return l_flat
def find_modules(path: Union[str, os.PathLike], prefix=None) -> List[str]:
if prefix is None:
prefix = []
modules = [
".".join(prefix + [module.name])
if not module.ispkg else
find_modules(f"{path}/{module.name}", prefix=(prefix + [module.name]))
for module in iter_modules([path])
]
return flatten_nested_list(modules)
# for package in find_packages(path):
# pkg_path = "{}/{}".format(path, package.replace(".", "/"))
# for info in iter_modules([pkg_path]):
# if not info.ispkg:
# modules.add(f"{package}.{info.name}")
# return modules
if __name__ == '__main__':
if sys.version_info < (3, 6, 0):
error_print("Requires at least Python 3.6.0")
sys.exit(1)
if not len(sys.argv) == 2:
error_print("Usage: find_all_packages.py <path/to/project/root>")
error_print("Searches for all Python modules in <path/to/project/root> and")
error_print("returns them one module name per line")
sys.exit(1)
for module in find_modules(sys.argv[1].strip()):
print(module)