"modulefinder" — スクリプト中で使われているモジュールを検索する
***************************************************************

バージョン 2.3 で追加.

**ソースコード:** Lib/modulefinder.py

======================================================================

このモジュールでは、スクリプト中で import されているモジュールセットを
調べるために使える "ModuleFinder" クラスを提供しています。
"modulefinder.py" はまた、Python スクリプトのファイル名を引数に指定し
てスクリプトとして実行し、 import されているモジュールのレポートを出力
させることもできます。

modulefinder.AddPackagePath(pkg_name, path)

   *pkg_name* という名前のパッケージの在り処が *path* であることを記録
   します。

modulefinder.ReplacePackage(oldname, newname)

   Allows specifying that the module named *oldname* is in fact the
   package named *newname*.  The most common usage would be  to handle
   how the "_xmlplus" package replaces the "xml" package.

class modulefinder.ModuleFinder([path=None, debug=0, excludes=[], replace_paths=[]])

   This class provides "run_script()" and "report()" methods to
   determine the set of modules imported by a script. *path* can be a
   list of directories to search for modules; if not specified,
   "sys.path" is used.  *debug* sets the debugging level; higher
   values make the class print  debugging messages about what it’s
   doing. *excludes* is a list of module names to exclude from the
   analysis. *replace_paths* is a list of "(oldpath, newpath)" tuples
   that will be replaced in module paths.

   report()

      スクリプトで import しているモジュールと、そのパスからなるリスト
      を列挙したレポートを標準出力に出力します。モジュールを見つけられ
      なかったり、モジュールがないように見える場合にも報告します。

   run_script(pathname)

      *pathname* に指定したファイルの内容を解析します。ファイルには
      Python コードが入っていなければなりません。

   modules

      モジュール名をモジュールに結びつける辞書。 ModuleFinder の使用例
      を参照して下さい。


"ModuleFinder" の使用例
=======================

解析対象のスクリプトはこれ (bacon.py) です:

   import re, itertools

   try:
       import baconhameggs
   except ImportError:
       pass

   try:
       import guido.python.ham
   except ImportError:
       pass

bacon.py のレポートを出力するスクリプトです:

   from modulefinder import ModuleFinder

   finder = ModuleFinder()
   finder.run_script('bacon.py')

   print 'Loaded modules:'
   for name, mod in finder.modules.iteritems():
       print '%s: ' % name,
       print ','.join(mod.globalnames.keys()[:3])

   print '-'*50
   print 'Modules not imported:'
   print '\n'.join(finder.badmodules.iterkeys())

出力例です (アーキテクチャに依って違ってくるかもしれません):

   Loaded modules:
   _types:
   copy_reg:  _inverted_registry,_slotnames,__all__
   sre_compile:  isstring,_sre,_optimize_unicode
   _sre:
   sre_constants:  REPEAT_ONE,makedict,AT_END_LINE
   sys:
   re:  __module__,finditer,_expand
   itertools:
   __main__:  re,itertools,baconhameggs
   sre_parse:  __getslice__,_PATTERNENDERS,SRE_FLAG_UNICODE
   array:
   types:  __module__,IntType,TypeType
   ---------------------------------------------------
   Modules not imported:
   guido.python.ham
   baconhameggs
