-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsummarize_dependencies.py
More file actions
59 lines (55 loc) · 1.86 KB
/
summarize_dependencies.py
File metadata and controls
59 lines (55 loc) · 1.86 KB
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
'''
Examines a directory of .py files and find all imports.
'''
import os
from os import path
from indentprinter import indentPrinter, print
from pprint import pprint
def main():
summary = set()
warnings = []
examineDir(summary, warnings)
pprint(summary)
pprint(warnings)
def examineDir(summary, warnings):
for i in os.listdir():
if i in '.git __pycache__':
continue
if path.isdir(i):
print(i)
with indentPrinter:
os.chdir(i)
examineDir(summary, warnings)
os.chdir('..')
else:
if i[-3:].lower() in '.py .pyw':
examineFile(i, summary, warnings)
else:
print('ignore', i)
def examineFile(filename, summary, warnings):
with open(filename, 'r') as f:
try:
for line in f:
if 'import' in line:
try:
if 'from' in line:
parts = line.split('from ')
assert parts[0].strip() == ''
assert len(parts) == 2
parts = parts[1].split(' import ')
assert len(parts) == 2
name = parts[0]
else:
parts = line.split('import ')
assert len(parts) == 2
name = parts[1].split(' as ')[0]
name = name.strip()
if name[0] == '.':
pass
else:
summary.add(name)
except AssertionError:
warnings.append((filename, line))
except UnicodeDecodeError:
warnings.append((filename, 'UNICODE Error'))
main()