install_egg_info.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. from distutils import log, dir_util
  2. import os, sys
  3. from setuptools import Command
  4. from setuptools import namespaces
  5. from setuptools.archive_util import unpack_archive
  6. import pkg_resources
  7. class install_egg_info(namespaces.Installer, 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. self.install_layout = None
  16. self.prefix_option = None
  17. def finalize_options(self):
  18. self.set_undefined_options('install_lib',
  19. ('install_dir', 'install_dir'))
  20. self.set_undefined_options('install',('install_layout','install_layout'))
  21. if sys.hexversion > 0x2060000:
  22. self.set_undefined_options('install',('prefix_option','prefix_option'))
  23. ei_cmd = self.get_finalized_command("egg_info")
  24. basename = pkg_resources.Distribution(
  25. None, None, ei_cmd.egg_name, ei_cmd.egg_version
  26. ).egg_name() + '.egg-info'
  27. if self.install_layout:
  28. if not self.install_layout.lower() in ['deb']:
  29. raise DistutilsOptionError("unknown value for --install-layout")
  30. self.install_layout = self.install_layout.lower()
  31. basename = basename.replace('-py%s' % pkg_resources.PY_MAJOR, '')
  32. elif self.prefix_option or 'real_prefix' in sys.__dict__:
  33. # don't modify for virtualenv
  34. pass
  35. else:
  36. basename = basename.replace('-py%s' % pkg_resources.PY_MAJOR, '')
  37. self.source = ei_cmd.egg_info
  38. self.target = os.path.join(self.install_dir, basename)
  39. self.outputs = []
  40. def run(self):
  41. self.run_command('egg_info')
  42. if os.path.isdir(self.target) and not os.path.islink(self.target):
  43. dir_util.remove_tree(self.target, dry_run=self.dry_run)
  44. elif os.path.exists(self.target):
  45. self.execute(os.unlink, (self.target,), "Removing " + self.target)
  46. if not self.dry_run:
  47. pkg_resources.ensure_directory(self.target)
  48. self.execute(
  49. self.copytree, (), "Copying %s to %s" % (self.source, self.target)
  50. )
  51. self.install_namespaces()
  52. def get_outputs(self):
  53. return self.outputs
  54. def copytree(self):
  55. # Copy the .egg-info tree to site-packages
  56. def skimmer(src, dst):
  57. # filter out source-control directories; note that 'src' is always
  58. # a '/'-separated path, regardless of platform. 'dst' is a
  59. # platform-specific path.
  60. for skip in '.svn/', 'CVS/':
  61. if src.startswith(skip) or '/' + skip in src:
  62. return None
  63. if self.install_layout and self.install_layout in ['deb'] and src.startswith('SOURCES.txt'):
  64. log.info("Skipping SOURCES.txt")
  65. return None
  66. self.outputs.append(dst)
  67. log.debug("Copying %s to %s", src, dst)
  68. return dst
  69. unpack_archive(self.source, self.target, skimmer)