~hardware-certification/zope3/certify-staging-2.5

« back to all changes in this revision

Viewing changes to utilities/importchecker.py

  • Committer: Marc Tardif
  • Date: 2008-04-26 19:03:34 UTC
  • Revision ID: cr3@lime-20080426190334-u16xo4llz56vliqf
Initial import.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python2.4
 
2
##############################################################################
 
3
#
 
4
# Copyright (c) 2003 Zope Corporation and Contributors.
 
5
# All Rights Reserved.
 
6
#
 
7
# This software is subject to the provisions of the Zope Public License,
 
8
# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
 
9
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
 
10
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 
11
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
 
12
# FOR A PARTICULAR PURPOSE.
 
13
#
 
14
##############################################################################
 
15
"""Import checker
 
16
 
 
17
This utility finds unused imports in Python modules.  Its output is
 
18
grep-like and thus emacs-friendly.
 
19
 
 
20
$Id: importchecker.py 38688 2005-09-29 16:33:36Z fdrake $
 
21
"""
 
22
 
 
23
import compiler
 
24
import os, os.path
 
25
import sys
 
26
 
 
27
def _findDottedNamesHelper(node, result):
 
28
    more_node = node
 
29
    name = node.__class__.__name__
 
30
    if name == 'Getattr':
 
31
        dotted = []
 
32
        while name == 'Getattr':
 
33
            dotted.append(node.attrname)
 
34
            node = node.expr
 
35
            name = node.__class__.__name__
 
36
        if name == 'Name':
 
37
            dotted.append(node.name)
 
38
            dotted.reverse()
 
39
            for i in range(1, len(dotted)):
 
40
                result.append('.'.join(dotted[:i]))
 
41
            result.append('.'.join(dotted))
 
42
            return
 
43
    elif name == 'Name':
 
44
        result.append(node.name)
 
45
        return
 
46
    elif name == 'AssAttr':
 
47
        # Can be on an import as well.
 
48
        # for instance
 
49
        # from x import y
 
50
        # y.k = v
 
51
        expr = node.expr
 
52
        result.append(getattr(expr, 'name', ''))
 
53
        return
 
54
    for child in more_node.getChildNodes():
 
55
        _findDottedNamesHelper(child, result)
 
56
 
 
57
 
 
58
def findDottedNames(node):
 
59
    """Find dotted names in an AST tree node
 
60
    """
 
61
    result = []
 
62
    _findDottedNamesHelper(node, result)
 
63
    return result
 
64
 
 
65
 
 
66
class ImportFinder:
 
67
    """An instance of this class will be used to walk over a compiler AST
 
68
    tree (a module). During that operation, the appropriate methods of
 
69
    this visitor will be called
 
70
    """
 
71
 
 
72
    def __init__(self):
 
73
        self._map = {}
 
74
 
 
75
    def visitFrom(self, stmt):
 
76
        """Will be called for 'from foo import bar' statements
 
77
        """
 
78
        module_name, names = stmt.asList()
 
79
        if module_name == '__future__':
 
80
            # we don't care what's imported from the future
 
81
            return
 
82
        names_dict = {}
 
83
        for orig_name, as_name in names:
 
84
            # we don't care about from import *
 
85
            if orig_name == '*':
 
86
                continue
 
87
            if as_name is None:
 
88
                name = orig_name
 
89
            else:
 
90
                name = as_name
 
91
            names_dict[name] = orig_name
 
92
        self._map.setdefault(module_name, {'names': names_dict,
 
93
                                           'lineno': stmt.lineno})
 
94
 
 
95
    def visitImport(self, stmt):
 
96
        """Will be called for 'import foo.bar' statements
 
97
        """
 
98
        for orig_name, as_name in stmt.names:
 
99
            if as_name is None:
 
100
                name = orig_name
 
101
            else:
 
102
                name = as_name
 
103
            self._map.setdefault(orig_name, {'names': {name: orig_name},
 
104
                                             'lineno': stmt.lineno})
 
105
 
 
106
    def getMap(self):
 
107
        return self._map
 
108
 
 
109
 
 
110
def findImports(mod):
 
111
    """Find import statements in module and put the result in a mapping.
 
112
    """
 
113
    visitor = ImportFinder()
 
114
    compiler.walk(mod, visitor)
 
115
    return visitor.getMap()
 
116
 
 
117
 
 
118
class Module:
 
119
    """This represents a python module.
 
120
    """
 
121
 
 
122
    def __init__(self, path):
 
123
        mod = compiler.parseFile(path)
 
124
        self._path = path
 
125
        self._map = findImports(mod)
 
126
        dottednames = {}
 
127
        self._dottednames = findDottedNames(mod)
 
128
 
 
129
    def getPath(self):
 
130
        """Return the path to this module's file.
 
131
        """
 
132
        return self._path
 
133
 
 
134
    def getImportedModuleNames(self):
 
135
        """Return the names of imported modules.
 
136
        """
 
137
        return self._map.keys()
 
138
 
 
139
    def getImportNames(self):
 
140
        """Return the names of imports; add dottednames as well.
 
141
        """
 
142
        result = []
 
143
        map = self._map
 
144
        for module_name in map.keys():
 
145
            for usedname, originalname in map[module_name]['names'].items():
 
146
                result.append((originalname, module_name))
 
147
                # add any other name that we could be using
 
148
                for dottedname in self._dottednames:
 
149
                    usednamedot = usedname + '.'
 
150
                    if dottedname.startswith(usednamedot):
 
151
                        attrname = dottedname[len(usednamedot):].split('.')[0]
 
152
                        result.append((attrname, module_name))
 
153
        return result
 
154
 
 
155
    def getUnusedImports(self):
 
156
        """Get unused imports of this module (the whole import info).
 
157
        """
 
158
        result = []
 
159
        for value in self._map.values():
 
160
            for usedname, originalname in value['names'].items():
 
161
                if usedname not in self._dottednames:
 
162
                    result.append((originalname, value['lineno']))
 
163
        return result
 
164
 
 
165
 
 
166
class ModuleFinder:
 
167
 
 
168
    def __init__(self):
 
169
        self._files = []
 
170
 
 
171
    def visit(self, arg, dirname, names):
 
172
        """This method will be called when we walk the filesystem
 
173
        tree. It looks for python modules and stored their filenames.
 
174
        """
 
175
        for name in names:
 
176
            # get all .py files that aren't weirdo emacs droppings
 
177
            if name.endswith('.py') and not name.startswith('.#'):
 
178
                self._files.append(os.path.join(dirname, name))
 
179
 
 
180
    def getModuleFilenames(self):
 
181
        return self._files
 
182
 
 
183
 
 
184
def findModules(path):
 
185
    """Find python modules in the given path and return their absolute
 
186
    filenames in a sequence.
 
187
    """
 
188
    finder = ModuleFinder()
 
189
    os.path.walk(path, finder.visit, ())
 
190
    return finder.getModuleFilenames()
 
191
 
 
192
 
 
193
class ImportDatabase:
 
194
    """This database keeps tracks of imports.
 
195
 
 
196
    It allows to NOT report cases where a module imports something
 
197
    just so that another module can import it (import dependencies).
 
198
    """
 
199
 
 
200
    def __init__(self, root_path):
 
201
        self._root_path = root_path
 
202
        self._modules = {}
 
203
        self._names = {}
 
204
 
 
205
    def resolveDottedModuleName(self, dotted_name, module):
 
206
        """Return path to file representing module, or None if no such
 
207
        thing. Can do this relative from module.
 
208
        """
 
209
        dotted_path = dotted_name.replace('.', '/')
 
210
        # try relative import first
 
211
        path = os.path.join(os.path.dirname(module.getPath()), dotted_path)
 
212
        path = self._resolveHelper(path)
 
213
        if path is not None:
 
214
            return path
 
215
        # absolute import (assumed to be from this tree)
 
216
        if os.path.isfile(os.path.join(self._root_path, '__init__.py')):
 
217
            startpath, dummy = os.path.split(self._root_path)
 
218
        else:
 
219
            startpath = self._root_path
 
220
        return self._resolveHelper(os.path.join(startpath, dotted_path))
 
221
 
 
222
    def _resolveHelper(self, path):
 
223
        if os.path.isfile(path + '.py'):
 
224
            return path + '.py'
 
225
        if os.path.isdir(path):
 
226
            path = os.path.join(path, '__init__.py')
 
227
            if os.path.isfile(path):
 
228
                return path
 
229
        return None
 
230
 
 
231
    def findModules(self):
 
232
        """Find modules in the given path.
 
233
        """
 
234
        for modulepath in findModules(self._root_path):
 
235
            module = Module(modulepath)
 
236
            self.addModule(module)
 
237
 
 
238
    def addModule(self, module):
 
239
        """Add information about a module to the database. A module in
 
240
        this case is not a python module object, but an instance of
 
241
        the above defined Module class.w
 
242
        """
 
243
        self_path = module.getPath()
 
244
        # do nothing if we already know about it
 
245
        if self._modules.has_key(self_path):
 
246
            return
 
247
 
 
248
        self._modules[self_path] = module
 
249
 
 
250
        # add imported names to internal names mapping; this will
 
251
        # allow us identify dependent imports later
 
252
        names = self._names
 
253
        for name, from_module_name in module.getImportNames():
 
254
            path = self.resolveDottedModuleName(from_module_name, module)
 
255
            t = (path, name)
 
256
            modulepaths = names.get(t, {})
 
257
            if not modulepaths.has_key(self_path):
 
258
                modulepaths[self_path] = 1
 
259
            names[t] = modulepaths
 
260
 
 
261
    def getUnusedImports(self):
 
262
        """Get unused imports of all known modules.
 
263
        """
 
264
        result = {}
 
265
        for path, module in self._modules.items():
 
266
            result[path] = self.getUnusedImportsInModule(module)
 
267
        return result
 
268
 
 
269
    def getUnusedImportsInModule(self, module):
 
270
        """Get all unused imports in a module.
 
271
        """
 
272
        result = []
 
273
        for name, lineno in module.getUnusedImports():
 
274
            if not self.isNameImportedFrom(name, module):
 
275
                result.append((name, lineno))
 
276
        return result
 
277
 
 
278
    def isNameImportedFrom(self, name, module):
 
279
        """Return true if name is imported from module by another module.
 
280
        """
 
281
        return self._names.has_key((module.getPath(), name))
 
282
 
 
283
    def getModulesImportingNameFrom(self, name, module):
 
284
        """Return list of known modules that import name from module.
 
285
        """
 
286
        result = []
 
287
        for path in self._names.get((module.getPath(), name), {}).keys():
 
288
            result.append(self._modules[path])
 
289
        return result
 
290
 
 
291
 
 
292
def main():
 
293
    try:
 
294
        path = sys.argv[1]
 
295
    except IndexError:
 
296
        print "No path supplied"
 
297
        sys.exit(1)
 
298
 
 
299
    path = os.path.abspath(path)
 
300
    if not os.path.isdir(path):
 
301
        print "Unknown path:", path
 
302
        sys.exit(1)
 
303
 
 
304
    l = len(path) + 1
 
305
    db = ImportDatabase(path)
 
306
    db.findModules()
 
307
    unused_imports = db.getUnusedImports()
 
308
    module_paths = unused_imports.keys()
 
309
    module_paths.sort()
 
310
    for path in module_paths:
 
311
        info = unused_imports[path]
 
312
        path = path[l:]
 
313
        if not info:
 
314
            continue
 
315
        line2names = {}
 
316
        for name, line in info:
 
317
            names = line2names.get(line, [])
 
318
            names.append(name)
 
319
            line2names[line] = names
 
320
        lines = line2names.keys()
 
321
        lines.sort()
 
322
        for line in lines:
 
323
            names = ', '.join(line2names[line])
 
324
            print "%s:%s: %s" % (path, line, names)
 
325
 
 
326
if __name__ == '__main__':
 
327
    main()
 
328