[modutils] setuptools pkg_resources support. Closes #8796

NO I won't write a test. Setup is to painful to reproduce for automatic test (require virtualenv, setuptools, python setup.py develop, etc). See description on the ticket.

authorSylvain Thénault <sylvain.thenault@logilab.fr>
changesetcd9817f175b4
branchstable
phasepublic
hiddenno
parent revision#5a8fb4b4a7f5 [modutils] fix python3.3 crash on file_from_modpath. Closes #137244
child revision#878ef72bc997 [modutils] add pkgutil.extend_path support. Closes #8796
files modified by this revision
modutils.py
# HG changeset patch
# User Sylvain Thénault <sylvain.thenault@logilab.fr>
# Date 1371481804 -7200
# Mon Jun 17 17:10:04 2013 +0200
# Branch stable
# Node ID cd9817f175b45acf827df04cc14847b87356aafe
# Parent 5a8fb4b4a7f567b65b8f935282316be0b1f9018e
[modutils] setuptools pkg_resources support. Closes #8796

NO I won't write a test. Setup is to painful to reproduce for automatic test
(require virtualenv, setuptools, python setup.py develop, etc). See description
on the ticket.

diff --git a/modutils.py b/modutils.py
@@ -572,10 +572,15 @@
1                      raise ImportError('No module named %s in %s/%s' % (
2                          '.'.join(modpath[1:]), filepath, modpath))
3                  return ZIPFILE, abspath(filepath) + '/' + '/'.join(modpath), filepath
4      raise ImportError('No module named %s' % '.'.join(modpath))
5 
6 +try:
7 +    import pkg_resources
8 +except ImportError:
9 +    pkg_resources = None
10 +
11  def _module_file(modpath, path=None):
12      """get a module type / file path
13 
14      :type modpath: list or tuple
15      :param modpath:
@@ -602,10 +607,16 @@
16                  except zipimport.ZipImportError:
17                      pic[__path] = None
18          checkeggs = True
19      except AttributeError:
20          checkeggs = False
21 +    # pkg_resources support (aka setuptools namespace packages)
22 +    if pkg_resources is not None and modpath[0] in pkg_resources._namespace_packages and len(modpath) > 1:
23 +        # setuptools has added into sys.modules a module object with proper
24 +        # __path__, get back information from there
25 +        module = sys.modules[modpath.pop(0)]
26 +        path = module.__path__
27      imported = []
28      while modpath:
29          # take care to changes in find_module implementation wrt builtin modules
30          #
31          # Python 2.6.6 (r266:84292, Sep 11 2012, 08:34:23)