1 | n/a | #! /usr/bin/env python3 |
---|
2 | n/a | |
---|
3 | n/a | # pdeps |
---|
4 | n/a | # |
---|
5 | n/a | # Find dependencies between a bunch of Python modules. |
---|
6 | n/a | # |
---|
7 | n/a | # Usage: |
---|
8 | n/a | # pdeps file1.py file2.py ... |
---|
9 | n/a | # |
---|
10 | n/a | # Output: |
---|
11 | n/a | # Four tables separated by lines like '--- Closure ---': |
---|
12 | n/a | # 1) Direct dependencies, listing which module imports which other modules |
---|
13 | n/a | # 2) The inverse of (1) |
---|
14 | n/a | # 3) Indirect dependencies, or the closure of the above |
---|
15 | n/a | # 4) The inverse of (3) |
---|
16 | n/a | # |
---|
17 | n/a | # To do: |
---|
18 | n/a | # - command line options to select output type |
---|
19 | n/a | # - option to automatically scan the Python library for referenced modules |
---|
20 | n/a | # - option to limit output to particular modules |
---|
21 | n/a | |
---|
22 | n/a | |
---|
23 | n/a | import sys |
---|
24 | n/a | import re |
---|
25 | n/a | import os |
---|
26 | n/a | |
---|
27 | n/a | |
---|
28 | n/a | # Main program |
---|
29 | n/a | # |
---|
30 | n/a | def main(): |
---|
31 | n/a | args = sys.argv[1:] |
---|
32 | n/a | if not args: |
---|
33 | n/a | print('usage: pdeps file.py file.py ...') |
---|
34 | n/a | return 2 |
---|
35 | n/a | # |
---|
36 | n/a | table = {} |
---|
37 | n/a | for arg in args: |
---|
38 | n/a | process(arg, table) |
---|
39 | n/a | # |
---|
40 | n/a | print('--- Uses ---') |
---|
41 | n/a | printresults(table) |
---|
42 | n/a | # |
---|
43 | n/a | print('--- Used By ---') |
---|
44 | n/a | inv = inverse(table) |
---|
45 | n/a | printresults(inv) |
---|
46 | n/a | # |
---|
47 | n/a | print('--- Closure of Uses ---') |
---|
48 | n/a | reach = closure(table) |
---|
49 | n/a | printresults(reach) |
---|
50 | n/a | # |
---|
51 | n/a | print('--- Closure of Used By ---') |
---|
52 | n/a | invreach = inverse(reach) |
---|
53 | n/a | printresults(invreach) |
---|
54 | n/a | # |
---|
55 | n/a | return 0 |
---|
56 | n/a | |
---|
57 | n/a | |
---|
58 | n/a | # Compiled regular expressions to search for import statements |
---|
59 | n/a | # |
---|
60 | n/a | m_import = re.compile('^[ \t]*from[ \t]+([^ \t]+)[ \t]+') |
---|
61 | n/a | m_from = re.compile('^[ \t]*import[ \t]+([^#]+)') |
---|
62 | n/a | |
---|
63 | n/a | |
---|
64 | n/a | # Collect data from one file |
---|
65 | n/a | # |
---|
66 | n/a | def process(filename, table): |
---|
67 | n/a | fp = open(filename, 'r') |
---|
68 | n/a | mod = os.path.basename(filename) |
---|
69 | n/a | if mod[-3:] == '.py': |
---|
70 | n/a | mod = mod[:-3] |
---|
71 | n/a | table[mod] = list = [] |
---|
72 | n/a | while 1: |
---|
73 | n/a | line = fp.readline() |
---|
74 | n/a | if not line: break |
---|
75 | n/a | while line[-1:] == '\\': |
---|
76 | n/a | nextline = fp.readline() |
---|
77 | n/a | if not nextline: break |
---|
78 | n/a | line = line[:-1] + nextline |
---|
79 | n/a | m_found = m_import.match(line) or m_from.match(line) |
---|
80 | n/a | if m_found: |
---|
81 | n/a | (a, b), (a1, b1) = m_found.regs[:2] |
---|
82 | n/a | else: continue |
---|
83 | n/a | words = line[a1:b1].split(',') |
---|
84 | n/a | # print '#', line, words |
---|
85 | n/a | for word in words: |
---|
86 | n/a | word = word.strip() |
---|
87 | n/a | if word not in list: |
---|
88 | n/a | list.append(word) |
---|
89 | n/a | fp.close() |
---|
90 | n/a | |
---|
91 | n/a | |
---|
92 | n/a | # Compute closure (this is in fact totally general) |
---|
93 | n/a | # |
---|
94 | n/a | def closure(table): |
---|
95 | n/a | modules = list(table.keys()) |
---|
96 | n/a | # |
---|
97 | n/a | # Initialize reach with a copy of table |
---|
98 | n/a | # |
---|
99 | n/a | reach = {} |
---|
100 | n/a | for mod in modules: |
---|
101 | n/a | reach[mod] = table[mod][:] |
---|
102 | n/a | # |
---|
103 | n/a | # Iterate until no more change |
---|
104 | n/a | # |
---|
105 | n/a | change = 1 |
---|
106 | n/a | while change: |
---|
107 | n/a | change = 0 |
---|
108 | n/a | for mod in modules: |
---|
109 | n/a | for mo in reach[mod]: |
---|
110 | n/a | if mo in modules: |
---|
111 | n/a | for m in reach[mo]: |
---|
112 | n/a | if m not in reach[mod]: |
---|
113 | n/a | reach[mod].append(m) |
---|
114 | n/a | change = 1 |
---|
115 | n/a | # |
---|
116 | n/a | return reach |
---|
117 | n/a | |
---|
118 | n/a | |
---|
119 | n/a | # Invert a table (this is again totally general). |
---|
120 | n/a | # All keys of the original table are made keys of the inverse, |
---|
121 | n/a | # so there may be empty lists in the inverse. |
---|
122 | n/a | # |
---|
123 | n/a | def inverse(table): |
---|
124 | n/a | inv = {} |
---|
125 | n/a | for key in table.keys(): |
---|
126 | n/a | if key not in inv: |
---|
127 | n/a | inv[key] = [] |
---|
128 | n/a | for item in table[key]: |
---|
129 | n/a | store(inv, item, key) |
---|
130 | n/a | return inv |
---|
131 | n/a | |
---|
132 | n/a | |
---|
133 | n/a | # Store "item" in "dict" under "key". |
---|
134 | n/a | # The dictionary maps keys to lists of items. |
---|
135 | n/a | # If there is no list for the key yet, it is created. |
---|
136 | n/a | # |
---|
137 | n/a | def store(dict, key, item): |
---|
138 | n/a | if key in dict: |
---|
139 | n/a | dict[key].append(item) |
---|
140 | n/a | else: |
---|
141 | n/a | dict[key] = [item] |
---|
142 | n/a | |
---|
143 | n/a | |
---|
144 | n/a | # Tabulate results neatly |
---|
145 | n/a | # |
---|
146 | n/a | def printresults(table): |
---|
147 | n/a | modules = sorted(table.keys()) |
---|
148 | n/a | maxlen = 0 |
---|
149 | n/a | for mod in modules: maxlen = max(maxlen, len(mod)) |
---|
150 | n/a | for mod in modules: |
---|
151 | n/a | list = sorted(table[mod]) |
---|
152 | n/a | print(mod.ljust(maxlen), ':', end=' ') |
---|
153 | n/a | if mod in list: |
---|
154 | n/a | print('(*)', end=' ') |
---|
155 | n/a | for ref in list: |
---|
156 | n/a | print(ref, end=' ') |
---|
157 | n/a | print() |
---|
158 | n/a | |
---|
159 | n/a | |
---|
160 | n/a | # Call main and honor exit status |
---|
161 | n/a | if __name__ == '__main__': |
---|
162 | n/a | try: |
---|
163 | n/a | sys.exit(main()) |
---|
164 | n/a | except KeyboardInterrupt: |
---|
165 | n/a | sys.exit(1) |
---|