-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlist_modules.py
70 lines (54 loc) · 1.85 KB
/
list_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
67
68
69
70
#!/usr/bin/env python3
"""List modules used in Python files."""
import sys
import pathlib
import re
from collections import Counter
def find_imports(file_content: str, distinct: bool) -> set:
"""Find the imports in python file.
Search the `import ..` and `from .. import ..` for package names.
"""
IMPORT_REGEX = r'^[\s]*(?:import|(?P<type>from))\s+(?P<module>\w*)(?(type)\s+import\s+(?:.*)|(?:.*))$'
imports = re.findall(IMPORT_REGEX, file_content, re.I | re.M)
imports = [_[1] for _ in imports]
if distinct:
return set(imports)
else:
return imports
def search_dir(directory: str) -> str:
"""Recursively search directory."""
p = pathlib.Path(directory)
paths = []
for path in p.iterdir():
paths.append(path)
if path.is_dir():
paths.extend(search_dir(path))
for path in paths:
yield path
def package_collection(distinct: bool):
"""Return the package collection, set or list."""
if distinct:
package = set()
else:
package = []
return package
def print_results(x):
"""Print results."""
print(x)
def main(directory: str, distinct: bool):
"""Find all packages and print to console."""
packages = package_collection(distinct)
for path in search_dir(directory):
if path.as_posix().rpartition('.')[-1] == 'py':
with open(path.as_posix(), 'r') as f:
file_content = "".join(f.readlines())
if distinct:
packages = packages | find_imports(file_content, distinct)
else:
packages.extend(find_imports(file_content, distinct))
print_results(sorted(packages, key=lambda x: x.lower()))
print_results(Counter(packages).most_common())
if __name__ == "__main__":
directory = sys.argv[1]
distinct = True
main(directory, distinct)