install_egg_info.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import os
  2. from setuptools import Command, namespaces
  3. from setuptools.archive_util import unpack_archive
  4. from .._path import ensure_directory
  5. from distutils import dir_util, log
  6. class install_egg_info(namespaces.Installer, Command):
  7. """Install an .egg-info directory for the package"""
  8. description = "Install an .egg-info directory for the package"
  9. user_options = [
  10. ('install-dir=', 'd', "directory to install to"),
  11. ]
  12. def initialize_options(self):
  13. self.install_dir = None
  14. def finalize_options(self) -> None:
  15. self.set_undefined_options('install_lib', ('install_dir', 'install_dir'))
  16. ei_cmd = self.get_finalized_command("egg_info")
  17. basename = f"{ei_cmd._get_egg_basename()}.egg-info"
  18. self.source = ei_cmd.egg_info
  19. self.target = os.path.join(self.install_dir, basename)
  20. self.outputs: list[str] = []
  21. def run(self) -> None:
  22. self.run_command('egg_info')
  23. if os.path.isdir(self.target) and not os.path.islink(self.target):
  24. dir_util.remove_tree(self.target)
  25. elif os.path.exists(self.target):
  26. self.execute(os.unlink, (self.target,), "Removing " + self.target)
  27. ensure_directory(self.target)
  28. self.execute(self.copytree, (), f"Copying {self.source} to {self.target}")
  29. self.install_namespaces()
  30. def get_outputs(self):
  31. return self.outputs
  32. def copytree(self) -> None:
  33. # Copy the .egg-info tree to site-packages
  34. def skimmer(src, dst):
  35. # filter out source-control directories; note that 'src' is always
  36. # a '/'-separated path, regardless of platform. 'dst' is a
  37. # platform-specific path.
  38. for skip in '.svn/', 'CVS/':
  39. if src.startswith(skip) or '/' + skip in src:
  40. return None
  41. self.outputs.append(dst)
  42. log.debug("Copying %s to %s", src, dst)
  43. return dst
  44. unpack_archive(self.source, self.target, skimmer)