annotate Tools/scripts/pdeps.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents 4727f260f6f8
children 46625e6122e9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4573
73758db2f464 /usr/local/bin/python -> /usr/bin/env python
Guido van Rossum <guido@python.org>
parents: 1105
diff changeset
1 #! /usr/bin/env python
274
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 # pdeps
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4 #
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 # Find dependencies between a bunch of Python modules.
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 #
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 # Usage:
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
8 # pdeps file1.py file2.py ...
274
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 #
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 # Output:
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 # Four tables separated by lines like '--- Closure ---':
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 # 1) Direct dependencies, listing which module imports which other modules
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 # 2) The inverse of (1)
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 # 3) Indirect dependencies, or the closure of the above
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 # 4) The inverse of (3)
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
16 #
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 # To do:
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 # - command line options to select output type
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 # - option to automatically scan the Python library for referenced modules
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 # - option to limit output to particular modules
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
21
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
22
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 import sys
37021
4727f260f6f8 Remove regsub, reconvert, regex, regex_syntax and everything under lib-old.
Neal Norwitz <nnorwitz@gmail.com>
parents: 33074
diff changeset
24 import re
1105
6f2a6efd019e Adapt to modern times...
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
25 import os
274
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
26
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
27
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 # Main program
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 #
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 def main():
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
31 args = sys.argv[1:]
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
32 if not args:
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
33 print 'usage: pdeps file.py file.py ...'
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
34 return 2
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
35 #
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
36 table = {}
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
37 for arg in args:
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
38 process(arg, table)
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
39 #
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
40 print '--- Uses ---'
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
41 printresults(table)
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
42 #
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
43 print '--- Used By ---'
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
44 inv = inverse(table)
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
45 printresults(inv)
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
46 #
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
47 print '--- Closure of Uses ---'
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
48 reach = closure(table)
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
49 printresults(reach)
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
50 #
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
51 print '--- Closure of Used By ---'
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
52 invreach = inverse(reach)
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
53 printresults(invreach)
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
54 #
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
55 return 0
274
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
56
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
57
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 # Compiled regular expressions to search for import statements
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
59 #
37021
4727f260f6f8 Remove regsub, reconvert, regex, regex_syntax and everything under lib-old.
Neal Norwitz <nnorwitz@gmail.com>
parents: 33074
diff changeset
60 m_import = re.compile('^[ \t]*from[ \t]+([^ \t]+)[ \t]+')
4727f260f6f8 Remove regsub, reconvert, regex, regex_syntax and everything under lib-old.
Neal Norwitz <nnorwitz@gmail.com>
parents: 33074
diff changeset
61 m_from = re.compile('^[ \t]*import[ \t]+([^#]+)')
274
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
62
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
63
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
64 # Collect data from one file
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 #
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
66 def process(filename, table):
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
67 fp = open(filename, 'r')
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
68 mod = os.path.basename(filename)
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
69 if mod[-3:] == '.py':
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
70 mod = mod[:-3]
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
71 table[mod] = list = []
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
72 while 1:
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
73 line = fp.readline()
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
74 if not line: break
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
75 while line[-1:] == '\\':
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
76 nextline = fp.readline()
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
77 if not nextline: break
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
78 line = line[:-1] + nextline
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
79 if m_import.match(line) >= 0:
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
80 (a, b), (a1, b1) = m_import.regs[:2]
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
81 elif m_from.match(line) >= 0:
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
82 (a, b), (a1, b1) = m_from.regs[:2]
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
83 else: continue
25219
8a7a2f1f88f0 Apply diff2.txt from SF patch http://www.python.org/sf/572113
Walter Dörwald <walter@livinglogic.de>
parents: 15930
diff changeset
84 words = line[a1:b1].split(',')
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
85 # print '#', line, words
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
86 for word in words:
25219
8a7a2f1f88f0 Apply diff2.txt from SF patch http://www.python.org/sf/572113
Walter Dörwald <walter@livinglogic.de>
parents: 15930
diff changeset
87 word = word.strip()
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
88 if word not in list:
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
89 list.append(word)
274
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
90
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
91
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 # Compute closure (this is in fact totally general)
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 #
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
94 def closure(table):
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
95 modules = table.keys()
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
96 #
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
97 # Initialize reach with a copy of table
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
98 #
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
99 reach = {}
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
100 for mod in modules:
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
101 reach[mod] = table[mod][:]
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
102 #
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
103 # Iterate until no more change
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
104 #
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
105 change = 1
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
106 while change:
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
107 change = 0
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
108 for mod in modules:
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
109 for mo in reach[mod]:
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
110 if mo in modules:
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
111 for m in reach[mo]:
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
112 if m not in reach[mod]:
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
113 reach[mod].append(m)
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
114 change = 1
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
115 #
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
116 return reach
274
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
117
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
118
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
119 # Invert a table (this is again totally general).
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
120 # All keys of the original table are made keys of the inverse,
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
121 # so there may be empty lists in the inverse.
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
122 #
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
123 def inverse(table):
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
124 inv = {}
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
125 for key in table.keys():
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
126 if not inv.has_key(key):
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
127 inv[key] = []
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
128 for item in table[key]:
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
129 store(inv, item, key)
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
130 return inv
274
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
131
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
132
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
133 # Store "item" in "dict" under "key".
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
134 # The dictionary maps keys to lists of items.
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
135 # If there is no list for the key yet, it is created.
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
136 #
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
137 def store(dict, key, item):
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
138 if dict.has_key(key):
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
139 dict[key].append(item)
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
140 else:
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
141 dict[key] = [item]
274
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
142
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
143
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
144 # Tabulate results neatly
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
145 #
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
146 def printresults(table):
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
147 modules = table.keys()
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
148 maxlen = 0
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
149 for mod in modules: maxlen = max(maxlen, len(mod))
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
150 modules.sort()
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
151 for mod in modules:
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
152 list = table[mod]
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
153 list.sort()
25219
8a7a2f1f88f0 Apply diff2.txt from SF patch http://www.python.org/sf/572113
Walter Dörwald <walter@livinglogic.de>
parents: 15930
diff changeset
154 print mod.ljust(maxlen), ':',
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
155 if mod in list:
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
156 print '(*)',
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
157 for ref in list:
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
158 print ref,
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
159 print
274
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
160
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
161
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
162 # Call main and honor exit status
33074
981fff7363b9 [Patch #1005491 ] use __name__ == '__main__' in scripts
Andrew M. Kuchling <amk@amk.ca>
parents: 25219
diff changeset
163 if __name__ == '__main__':
981fff7363b9 [Patch #1005491 ] use __name__ == '__main__' in scripts
Andrew M. Kuchling <amk@amk.ca>
parents: 25219
diff changeset
164 try:
981fff7363b9 [Patch #1005491 ] use __name__ == '__main__' in scripts
Andrew M. Kuchling <amk@amk.ca>
parents: 25219
diff changeset
165 sys.exit(main())
981fff7363b9 [Patch #1005491 ] use __name__ == '__main__' in scripts
Andrew M. Kuchling <amk@amk.ca>
parents: 25219
diff changeset
166 except KeyboardInterrupt:
981fff7363b9 [Patch #1005491 ] use __name__ == '__main__' in scripts
Andrew M. Kuchling <amk@amk.ca>
parents: 25219
diff changeset
167 sys.exit(1)