Detect Unused Modules

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Kamilche

    #1

    Detect Unused Modules

    '''
    DetectUnusedMod ules.py - Detect modules that were imported but not used
    in a file.
    When run directly, this class will check all files in the current
    directory.
    '''

    import os
    import tokenize

    class PrintUnusedModu les(object):
    state = 0
    importlist = None
    linenumbers = None

    def __init__(self, filename):
    self.filename = filename
    self.importlist = {}
    self.linenumber s = {}
    fd = open(filename, 'rU')
    tokenize.tokeni ze(fd.readline, self.FindImport s)
    fd = open(filename, 'rU')
    tokenize.tokeni ze(fd.readline, self.FindRefere nces)
    for mod, ctr in self.importlist .items():
    if ctr == 0:
    print ' File "%s", line %d, "%s" is imported but not
    referenced.' % (self.filename, self.linenumber s[mod], mod)

    def FindImports(sel f, toktype, tokval, tokstart, tokend, tokline):
    ' Build a list of modules this module imports'
    if self.state == 0:
    if toktype == tokenize.NAME:
    if tokval == 'import':
    self.state = 1
    self.mods = []
    elif self.state == 1:
    if toktype == tokenize.NAME:
    if tokval == 'import':
    pass
    elif tokval == 'as':
    del self.mods[-1]
    else:
    self.mods.appen d((tokval, tokstart[0]))
    elif toktype == tokenize.OP:
    pass
    elif toktype == tokenize.NEWLIN E:
    for mod, linenum in self.mods:
    self.importlist[mod] = 0
    self.linenumber s[mod] = linenum
    self.state = 0

    def FindReferences( self, toktype, tokval, tokstart, tokend,
    tokline):
    ' Find all references to the imported modules'
    if self.state == 0:
    if toktype == tokenize.NAME:
    if tokval == 'import':
    self.state = 1
    elif tokval in self.importlist :
    self.importlist[tokval] += 1
    elif self.state == 1:
    if toktype == tokenize.NEWLIN E:
    self.state = 0

    def ProcessDir(path name = None):
    if pathname == None:
    pathname = os.getcwd()
    else:
    pathname = os.path.abspath (pathname)
    for shortname in os.listdir(path name):
    filename = os.path.join(pa thname, shortname)
    if filename[-3:] == '.py':
    print 'Checking %s...' % filename
    PrintUnusedModu les(filename)

    def main():
    print 'Unused Module List'
    ProcessDir()
    print 'Done!'

    if __name__ == '__main__':
    main()

  • Sybren Stuvel

    #2
    Re: Detect Unused Modules

    Kamilche enlightened us with:
    DetectUnusedMod ules.py - Detect modules that were imported but not
    used in a file. When run directly, this class will check all files
    in the current directory.
    Nice as it is, but why not use pylint to check this and many other
    coding style issues?

    Sybren
    --
    Sybren Stüvel
    Stüvel IT - http://www.stuvel.eu/

    Comment

    • Kamilche

      #3
      Re: Detect Unused Modules

      Nice as it is, but why not use pylint to check this and many other
      coding style issues?
      I made this the first time I mangled some code because pychecker said
      some modules were not used when they really were. The module wasn't
      that complex, only 302 lines, but it got it wrong.

      Comment

      Working...