install_egg_info.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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, dry_run=self.dry_run)
  25. elif os.path.exists(self.target):
  26. self.execute(os.unlink, (self.target,), "Removing " + self.target)
  27. if not self.dry_run:
  28. ensure_directory(self.target)
  29. self.execute(self.copytree, (), f"Copying {self.source} to {self.target}")
  30. self.install_namespaces()
  31. def get_outputs(self):
  32. return self.outputs
  33. def copytree(self) -> None:
  34. # Copy the .egg-info tree to site-packages
  35. def skimmer(src, dst):
  36. # filter out source-control directories; note that 'src' is always
  37. # a '/'-separated path, regardless of platform. 'dst' is a
  38. # platform-specific path.
  39. for skip in '.svn/', 'CVS/':
  40. if src.startswith(skip) or '/' + skip in src:
  41. return None
  42. self.outputs.append(dst)
  43. log.debug("Copying %s to %s", src, dst)
  44. return dst
  45. unpack_archive(self.source, self.target, skimmer)