You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
3.1 KiB

4 years ago
  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. (
  39. "m = has_mfs and "
  40. "sys.modules.setdefault(%(pkg)r, "
  41. "importlib.util.module_from_spec("
  42. "importlib.machinery.PathFinder.find_spec(%(pkg)r, "
  43. "[os.path.dirname(p)])))"
  44. ),
  45. (
  46. "m = m or "
  47. "sys.modules.setdefault(%(pkg)r, types.ModuleType(%(pkg)r))"
  48. ),
  49. "mp = (m or []) and m.__dict__.setdefault('__path__',[])",
  50. "(p not in mp) and mp.append(p)",
  51. )
  52. "lines for the namespace installer"
  53. _nspkg_tmpl_multi = (
  54. 'm and setattr(sys.modules[%(parent)r], %(child)r, m)',
  55. )
  56. "additional line(s) when a parent package is indicated"
  57. def _get_root(self):
  58. return "sys._getframe(1).f_locals['sitedir']"
  59. def _gen_nspkg_line(self, pkg):
  60. # ensure pkg is not a unicode string under Python 2.7
  61. pkg = str(pkg)
  62. pth = tuple(pkg.split('.'))
  63. root = self._get_root()
  64. tmpl_lines = self._nspkg_tmpl
  65. parent, sep, child = pkg.rpartition('.')
  66. if parent:
  67. tmpl_lines += self._nspkg_tmpl_multi
  68. return ';'.join(tmpl_lines) % locals() + '\n'
  69. def _get_all_ns_packages(self):
  70. """Return sorted list of all package namespaces"""
  71. pkgs = self.distribution.namespace_packages or []
  72. return sorted(flatten(map(self._pkg_names, pkgs)))
  73. @staticmethod
  74. def _pkg_names(pkg):
  75. """
  76. Given a namespace package, yield the components of that
  77. package.
  78. >>> names = Installer._pkg_names('a.b.c')
  79. >>> set(names) == set(['a', 'a.b', 'a.b.c'])
  80. True
  81. """
  82. parts = pkg.split('.')
  83. while parts:
  84. yield '.'.join(parts)
  85. parts.pop()
  86. class DevelopInstaller(Installer):
  87. def _get_root(self):
  88. return repr(str(self.egg_path))
  89. def _get_target(self):
  90. return self.egg_link