install_egg_info.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. from distutils import log, dir_util
  2. import os
  3. from setuptools.extern.six.moves import map
  4. from setuptools import Command
  5. from setuptools.archive_util import unpack_archive
  6. import pkg_resources
  7. class install_egg_info(Command):
  8. """Install an .egg-info directory for the package"""
  9. description = "Install an .egg-info directory for the package"
  10. user_options = [
  11. ('install-dir=', 'd', "directory to install to"),
  12. ]
  13. def initialize_options(self):
  14. self.install_dir = None
  15. def finalize_options(self):
  16. self.set_undefined_options('install_lib',
  17. ('install_dir', 'install_dir'))
  18. ei_cmd = self.get_finalized_command("egg_info")
  19. basename = pkg_resources.Distribution(
  20. None, None, ei_cmd.egg_name, ei_cmd.egg_version
  21. ).egg_name() + '.egg-info'
  22. self.source = ei_cmd.egg_info
  23. self.target = os.path.join(self.install_dir, basename)
  24. self.outputs = []
  25. def run(self):
  26. self.run_command('egg_info')
  27. if os.path.isdir(self.target) and not os.path.islink(self.target):
  28. dir_util.remove_tree(self.target, dry_run=self.dry_run)
  29. elif os.path.exists(self.target):
  30. self.execute(os.unlink, (self.target,), "Removing " + self.target)
  31. if not self.dry_run:
  32. pkg_resources.ensure_directory(self.target)
  33. self.execute(
  34. self.copytree, (), "Copying %s to %s" % (self.source, self.target)
  35. )
  36. self.install_namespaces()
  37. def get_outputs(self):
  38. return self.outputs
  39. def copytree(self):
  40. # Copy the .egg-info tree to site-packages
  41. def skimmer(src, dst):
  42. # filter out source-control directories; note that 'src' is always
  43. # a '/'-separated path, regardless of platform. 'dst' is a
  44. # platform-specific path.
  45. for skip in '.svn/', 'CVS/':
  46. if src.startswith(skip) or '/' + skip in src:
  47. return None
  48. self.outputs.append(dst)
  49. log.debug("Copying %s to %s", src, dst)
  50. return dst
  51. unpack_archive(self.source, self.target, skimmer)
  52. def install_namespaces(self):
  53. nsp = self._get_all_ns_packages()
  54. if not nsp:
  55. return
  56. filename, ext = os.path.splitext(self.target)
  57. filename += '-nspkg.pth'
  58. self.outputs.append(filename)
  59. log.info("Installing %s", filename)
  60. lines = map(self._gen_nspkg_line, nsp)
  61. if self.dry_run:
  62. # always generate the lines, even in dry run
  63. list(lines)
  64. return
  65. with open(filename, 'wt') as f:
  66. f.writelines(lines)
  67. _nspkg_tmpl = (
  68. "import sys, types, os",
  69. "p = os.path.join(sys._getframe(1).f_locals['sitedir'], *%(pth)r)",
  70. "ie = os.path.exists(os.path.join(p,'__init__.py'))",
  71. "m = not ie and "
  72. "sys.modules.setdefault(%(pkg)r, types.ModuleType(%(pkg)r))",
  73. "mp = (m or []) and m.__dict__.setdefault('__path__',[])",
  74. "(p not in mp) and mp.append(p)",
  75. )
  76. "lines for the namespace installer"
  77. _nspkg_tmpl_multi = (
  78. 'm and setattr(sys.modules[%(parent)r], %(child)r, m)',
  79. )
  80. "additional line(s) when a parent package is indicated"
  81. @classmethod
  82. def _gen_nspkg_line(cls, pkg):
  83. # ensure pkg is not a unicode string under Python 2.7
  84. pkg = str(pkg)
  85. pth = tuple(pkg.split('.'))
  86. tmpl_lines = cls._nspkg_tmpl
  87. parent, sep, child = pkg.rpartition('.')
  88. if parent:
  89. tmpl_lines += cls._nspkg_tmpl_multi
  90. return ';'.join(tmpl_lines) % locals() + '\n'
  91. def _get_all_ns_packages(self):
  92. """Return sorted list of all package namespaces"""
  93. nsp = set()
  94. for pkg in self.distribution.namespace_packages or []:
  95. pkg = pkg.split('.')
  96. while pkg:
  97. nsp.add('.'.join(pkg))
  98. pkg.pop()
  99. return sorted(nsp)