namespaces.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import os
  2. from distutils import log
  3. import itertools
  4. from setuptools.extern.six.moves import map
  5. flatten = itertools.chain.from_iterable
  6. class Installer:
  7. nspkg_ext = '-nspkg.pth'
  8. def install_namespaces(self):
  9. nsp = self._get_all_ns_packages()
  10. if not nsp:
  11. return
  12. filename, ext = os.path.splitext(self._get_target())
  13. filename += self.nspkg_ext
  14. self.outputs.append(filename)
  15. log.info("Installing %s", filename)
  16. lines = map(self._gen_nspkg_line, nsp)
  17. if self.dry_run:
  18. # always generate the lines, even in dry run
  19. list(lines)
  20. return
  21. with open(filename, 'wt') as f:
  22. f.writelines(lines)
  23. def uninstall_namespaces(self):
  24. filename, ext = os.path.splitext(self._get_target())
  25. filename += self.nspkg_ext
  26. if not os.path.exists(filename):
  27. return
  28. log.info("Removing %s", filename)
  29. os.remove(filename)
  30. def _get_target(self):
  31. return self.target
  32. _nspkg_tmpl = (
  33. "import sys, types, os",
  34. "has_mfs = sys.version_info > (3, 5)",
  35. "p = os.path.join(%(root)s, *%(pth)r)",
  36. "importlib = has_mfs and __import__('importlib.util')",
  37. "has_mfs and __import__('importlib.machinery')",
  38. "m = has_mfs and "
  39. "sys.modules.setdefault(%(pkg)r, "
  40. "importlib.util.module_from_spec("
  41. "importlib.machinery.PathFinder.find_spec(%(pkg)r, "
  42. "[os.path.dirname(p)])))",
  43. "m = m or "
  44. "sys.modules.setdefault(%(pkg)r, types.ModuleType(%(pkg)r))",
  45. "mp = (m or []) and m.__dict__.setdefault('__path__',[])",
  46. "(p not in mp) and mp.append(p)",
  47. )
  48. "lines for the namespace installer"
  49. _nspkg_tmpl_multi = (
  50. 'm and setattr(sys.modules[%(parent)r], %(child)r, m)',
  51. )
  52. "additional line(s) when a parent package is indicated"
  53. def _get_root(self):
  54. return "sys._getframe(1).f_locals['sitedir']"
  55. def _gen_nspkg_line(self, pkg):
  56. # ensure pkg is not a unicode string under Python 2.7
  57. pkg = str(pkg)
  58. pth = tuple(pkg.split('.'))
  59. root = self._get_root()
  60. tmpl_lines = self._nspkg_tmpl
  61. parent, sep, child = pkg.rpartition('.')
  62. if parent:
  63. tmpl_lines += self._nspkg_tmpl_multi
  64. return ';'.join(tmpl_lines) % locals() + '\n'
  65. def _get_all_ns_packages(self):
  66. """Return sorted list of all package namespaces"""
  67. pkgs = self.distribution.namespace_packages or []
  68. return sorted(flatten(map(self._pkg_names, pkgs)))
  69. @staticmethod
  70. def _pkg_names(pkg):
  71. """
  72. Given a namespace package, yield the components of that
  73. package.
  74. >>> names = Installer._pkg_names('a.b.c')
  75. >>> set(names) == set(['a', 'a.b', 'a.b.c'])
  76. True
  77. """
  78. parts = pkg.split('.')
  79. while parts:
  80. yield '.'.join(parts)
  81. parts.pop()
  82. class DevelopInstaller(Installer):
  83. def _get_root(self):
  84. return repr(str(self.egg_path))
  85. def _get_target(self):
  86. return self.egg_link