diff options
author | Jason R. Coombs <jaraco@jaraco.com> | 2016-01-02 14:02:35 -0500 |
---|---|---|
committer | Jason R. Coombs <jaraco@jaraco.com> | 2016-01-02 14:02:35 -0500 |
commit | 9d9d74deb049ce7cf367a1baff5c6f13ea3b457e (patch) | |
tree | 88d9981bceb2d7e3a2f9adef8786965debcd9d67 /setuptools/extern/__init__.py | |
parent | 9a531b50020849193a55262b3f9cfc375ad3f37c (diff) | |
download | external_python_setuptools-9d9d74deb049ce7cf367a1baff5c6f13ea3b457e.tar.gz external_python_setuptools-9d9d74deb049ce7cf367a1baff5c6f13ea3b457e.tar.bz2 external_python_setuptools-9d9d74deb049ce7cf367a1baff5c6f13ea3b457e.zip |
Create a PEP 302 importer for managing conditional import of vendored packages from the 'extern' namespace. This technique avoids the use of 'imp' and works even when setuptools is installed as a zipped egg. Ref #229.
--HG--
branch : feature/issue-229
Diffstat (limited to 'setuptools/extern/__init__.py')
-rw-r--r-- | setuptools/extern/__init__.py | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/setuptools/extern/__init__.py b/setuptools/extern/__init__.py index e69de29b..803d9e9a 100644 --- a/setuptools/extern/__init__.py +++ b/setuptools/extern/__init__.py @@ -0,0 +1,42 @@ +import sys + +_VENDORED_NAMES = 'six', +_SEARCH_PATH = 'setuptools._vendor.', '' + +class VendorImporter: + """ + A PEP 302 meta path importer for finding optionally-vendored + or otherwise naturally-installed packages from __name__. + """ + def find_module(self, fullname, path=None): + root, base, target = fullname.partition(__name__ + '.') + if root: + return + if not any(map(target.startswith, _VENDORED_NAMES)): + return + return self + + def load_module(self, fullname): + root, base, target = fullname.partition(__name__ + '.') + for prefix in _SEARCH_PATH: + try: + __import__(prefix + target) + mod = sys.modules[prefix + target] + sys.modules[fullname] = mod + return mod + except ImportError: + pass + else: + raise ImportError( + "The '{target}' package is required; " + "normally this is bundled with this package so if you get " + "this warning, consult the packager of your " + "distribution.".format(**locals()) + ) + + @classmethod + def install(cls): + if not any(isinstance(imp, cls) for imp in sys.meta_path): + sys.meta_path.append(cls()) + +VendorImporter.install() |