importer.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. """Implements an importer that looks only in specific path (ignoring
  2. sys.path), and uses a per-path cache in addition to sys.modules. This is
  3. necessary because test modules in different directories frequently have the
  4. same names, which means that the first loaded would mask the rest when using
  5. the builtin importer.
  6. """
  7. import logging
  8. import os
  9. import sys
  10. from nose.config import Config
  11. from imp import find_module, load_module, acquire_lock, release_lock
  12. log = logging.getLogger(__name__)
  13. try:
  14. _samefile = os.path.samefile
  15. except AttributeError:
  16. def _samefile(src, dst):
  17. return (os.path.normcase(os.path.realpath(src)) ==
  18. os.path.normcase(os.path.realpath(dst)))
  19. class Importer(object):
  20. """An importer class that does only path-specific imports. That
  21. is, the given module is not searched for on sys.path, but only at
  22. the path or in the directory specified.
  23. """
  24. def __init__(self, config=None):
  25. if config is None:
  26. config = Config()
  27. self.config = config
  28. def importFromPath(self, path, fqname):
  29. """Import a dotted-name package whose tail is at path. In other words,
  30. given foo.bar and path/to/foo/bar.py, import foo from path/to/foo then
  31. bar from path/to/foo/bar, returning bar.
  32. """
  33. # find the base dir of the package
  34. path_parts = os.path.normpath(os.path.abspath(path)).split(os.sep)
  35. name_parts = fqname.split('.')
  36. if path_parts[-1] == '__init__.py':
  37. path_parts.pop()
  38. path_parts = path_parts[:-(len(name_parts))]
  39. dir_path = os.sep.join(path_parts)
  40. # then import fqname starting from that dir
  41. return self.importFromDir(dir_path, fqname)
  42. def importFromDir(self, dir, fqname):
  43. """Import a module *only* from path, ignoring sys.path and
  44. reloading if the version in sys.modules is not the one we want.
  45. """
  46. dir = os.path.normpath(os.path.abspath(dir))
  47. log.debug("Import %s from %s", fqname, dir)
  48. # FIXME reimplement local per-dir cache?
  49. # special case for __main__
  50. if fqname == '__main__':
  51. return sys.modules[fqname]
  52. if self.config.addPaths:
  53. add_path(dir, self.config)
  54. path = [dir]
  55. parts = fqname.split('.')
  56. part_fqname = ''
  57. mod = parent = fh = None
  58. for part in parts:
  59. if part_fqname == '':
  60. part_fqname = part
  61. else:
  62. part_fqname = "%s.%s" % (part_fqname, part)
  63. try:
  64. acquire_lock()
  65. log.debug("find module part %s (%s) in %s",
  66. part, part_fqname, path)
  67. fh, filename, desc = find_module(part, path)
  68. old = sys.modules.get(part_fqname)
  69. if old is not None:
  70. # test modules frequently have name overlap; make sure
  71. # we get a fresh copy of anything we are trying to load
  72. # from a new path
  73. log.debug("sys.modules has %s as %s", part_fqname, old)
  74. if (self.sameModule(old, filename)
  75. or (self.config.firstPackageWins and
  76. getattr(old, '__path__', None))):
  77. mod = old
  78. else:
  79. del sys.modules[part_fqname]
  80. mod = load_module(part_fqname, fh, filename, desc)
  81. else:
  82. mod = load_module(part_fqname, fh, filename, desc)
  83. finally:
  84. if fh:
  85. fh.close()
  86. release_lock()
  87. if parent:
  88. setattr(parent, part, mod)
  89. if hasattr(mod, '__path__'):
  90. path = mod.__path__
  91. parent = mod
  92. return mod
  93. def _dirname_if_file(self, filename):
  94. # We only take the dirname if we have a path to a non-dir,
  95. # because taking the dirname of a symlink to a directory does not
  96. # give the actual directory parent.
  97. if os.path.isdir(filename):
  98. return filename
  99. else:
  100. return os.path.dirname(filename)
  101. def sameModule(self, mod, filename):
  102. mod_paths = []
  103. if hasattr(mod, '__path__'):
  104. for path in mod.__path__:
  105. mod_paths.append(self._dirname_if_file(path))
  106. elif hasattr(mod, '__file__'):
  107. mod_paths.append(self._dirname_if_file(mod.__file__))
  108. else:
  109. # builtin or other module-like object that
  110. # doesn't have __file__; must be new
  111. return False
  112. new_path = self._dirname_if_file(filename)
  113. for mod_path in mod_paths:
  114. log.debug(
  115. "module already loaded? mod: %s new: %s",
  116. mod_path, new_path)
  117. if _samefile(mod_path, new_path):
  118. return True
  119. return False
  120. def add_path(path, config=None):
  121. """Ensure that the path, or the root of the current package (if
  122. path is in a package), is in sys.path.
  123. """
  124. # FIXME add any src-looking dirs seen too... need to get config for that
  125. log.debug('Add path %s' % path)
  126. if not path:
  127. return []
  128. added = []
  129. parent = os.path.dirname(path)
  130. if (parent
  131. and os.path.exists(os.path.join(path, '__init__.py'))):
  132. added.extend(add_path(parent, config))
  133. elif not path in sys.path:
  134. log.debug("insert %s into sys.path", path)
  135. sys.path.insert(0, path)
  136. added.append(path)
  137. if config and config.srcDirs:
  138. for dirname in config.srcDirs:
  139. dirpath = os.path.join(path, dirname)
  140. if os.path.isdir(dirpath):
  141. sys.path.insert(0, dirpath)
  142. added.append(dirpath)
  143. return added
  144. def remove_path(path):
  145. log.debug('Remove path %s' % path)
  146. if path in sys.path:
  147. sys.path.remove(path)