/Doc/library/modulefinder.rst

http://unladen-swallow.googlecode.com/ · ReStructuredText · 116 lines · 76 code · 40 blank · 0 comment · 0 complexity · e34df551a92ddb070b0c4830a2479d07 MD5 · raw file

  1. :mod:`modulefinder` --- Find modules used by a script
  2. =====================================================
  3. .. sectionauthor:: A.M. Kuchling <amk@amk.ca>
  4. .. module:: modulefinder
  5. :synopsis: Find modules used by a script.
  6. .. versionadded:: 2.3
  7. This module provides a :class:`ModuleFinder` class that can be used to determine
  8. the set of modules imported by a script. ``modulefinder.py`` can also be run as
  9. a script, giving the filename of a Python script as its argument, after which a
  10. report of the imported modules will be printed.
  11. .. function:: AddPackagePath(pkg_name, path)
  12. Record that the package named *pkg_name* can be found in the specified *path*.
  13. .. function:: ReplacePackage(oldname, newname)
  14. Allows specifying that the module named *oldname* is in fact the package named
  15. *newname*. The most common usage would be to handle how the :mod:`_xmlplus`
  16. package replaces the :mod:`xml` package.
  17. .. class:: ModuleFinder([path=None, debug=0, excludes=[], replace_paths=[]])
  18. This class provides :meth:`run_script` and :meth:`report` methods to determine
  19. the set of modules imported by a script. *path* can be a list of directories to
  20. search for modules; if not specified, ``sys.path`` is used. *debug* sets the
  21. debugging level; higher values make the class print debugging messages about
  22. what it's doing. *excludes* is a list of module names to exclude from the
  23. analysis. *replace_paths* is a list of ``(oldpath, newpath)`` tuples that will
  24. be replaced in module paths.
  25. .. method:: report()
  26. Print a report to standard output that lists the modules imported by the
  27. script and their paths, as well as modules that are missing or seem to be
  28. missing.
  29. .. method:: run_script(pathname)
  30. Analyze the contents of the *pathname* file, which must contain Python
  31. code.
  32. .. attribute:: modules
  33. A dictionary mapping module names to modules. See
  34. :ref:`modulefinder-example`
  35. .. _modulefinder-example:
  36. Example usage of :class:`ModuleFinder`
  37. --------------------------------------
  38. The script that is going to get analyzed later on (bacon.py)::
  39. import re, itertools
  40. try:
  41. import baconhameggs
  42. except ImportError:
  43. pass
  44. try:
  45. import guido.python.ham
  46. except ImportError:
  47. pass
  48. The script that will output the report of bacon.py::
  49. from modulefinder import ModuleFinder
  50. finder = ModuleFinder()
  51. finder.run_script('bacon.py')
  52. print 'Loaded modules:'
  53. for name, mod in finder.modules.iteritems():
  54. print '%s: ' % name,
  55. print ','.join(mod.globalnames.keys()[:3])
  56. print '-'*50
  57. print 'Modules not imported:'
  58. print '\n'.join(finder.badmodules.iterkeys())
  59. Sample output (may vary depending on the architecture)::
  60. Loaded modules:
  61. _types:
  62. copy_reg: _inverted_registry,_slotnames,__all__
  63. sre_compile: isstring,_sre,_optimize_unicode
  64. _sre:
  65. sre_constants: REPEAT_ONE,makedict,AT_END_LINE
  66. sys:
  67. re: __module__,finditer,_expand
  68. itertools:
  69. __main__: re,itertools,baconhameggs
  70. sre_parse: __getslice__,_PATTERNENDERS,SRE_FLAG_UNICODE
  71. array:
  72. types: __module__,IntType,TypeType
  73. ---------------------------------------------------
  74. Modules not imported:
  75. guido.python.ham
  76. baconhameggs