"""Implements an importer that looks only in specific path (ignoring sys.path), and uses a per-path cache in addition to sys.modules. This is necessary because test modules in different directories frequently have the same names, which means that the first loaded would mask the rest when using the builtin importer. """
except AttributeError: def _samefile(src, dst): return (os.path.normcase(os.path.realpath(src)) == os.path.normcase(os.path.realpath(dst)))
"""An importer class that does only path-specific imports. That is, the given module is not searched for on sys.path, but only at the path or in the directory specified. """ config = Config()
"""Import a dotted-name package whose tail is at path. In other words, given foo.bar and path/to/foo/bar.py, import foo from path/to/foo then bar from path/to/foo/bar, returning bar. """ # find the base dir of the package path_parts.pop() # then import fqname starting from that dir
"""Import a module *only* from path, ignoring sys.path and reloading if the version in sys.modules is not the one we want. """
# FIXME reimplement local per-dir cache?
# special case for __main__ return sys.modules[fqname]
else: part, part_fqname, path) # test modules frequently have name overlap; make sure # we get a fresh copy of anything we are trying to load # from a new path or (self.config.firstPackageWins and getattr(old, '__path__', None))): else: del sys.modules[part_fqname] mod = load_module(part_fqname, fh, filename, desc) else: finally:
# We only take the dirname if we have a path to a non-dir, # because taking the dirname of a symlink to a directory does not # give the actual directory parent. else: return os.path.dirname(filename)
elif hasattr(mod, '__file__'): mod_paths.append(self._dirname_if_file(mod.__file__)) else: # builtin or other module-like object that # doesn't have __file__; must be new return False "module already loaded? mod: %s new: %s", mod_path, new_path) return False
"""Ensure that the path, or the root of the current package (if path is in a package), is in sys.path. """
# FIXME add any src-looking dirs seen too... need to get config for that
return [] and os.path.exists(os.path.join(path, '__init__.py'))): log.debug("insert %s into sys.path", path) sys.path.insert(0, path) added.append(path)
|