install_egg_info.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. """
  2. distutils.command.install_egg_info
  3. Implements the Distutils 'install_egg_info' command, for installing
  4. a package's PKG-INFO metadata.
  5. """
  6. import os
  7. import sys
  8. import re
  9. from distutils.cmd import Command
  10. from distutils import log, dir_util
  11. class install_egg_info(Command):
  12. """Install an .egg-info file for the package"""
  13. description = "Install package's PKG-INFO metadata as an .egg-info file"
  14. user_options = [
  15. ('install-dir=', 'd', "directory to install to"),
  16. ]
  17. def initialize_options(self):
  18. self.install_dir = None
  19. @property
  20. def basename(self):
  21. """
  22. Allow basename to be overridden by child class.
  23. Ref pypa/distutils#2.
  24. """
  25. return "%s-%s-py%d.%d.egg-info" % (
  26. to_filename(safe_name(self.distribution.get_name())),
  27. to_filename(safe_version(self.distribution.get_version())),
  28. *sys.version_info[:2],
  29. )
  30. def finalize_options(self):
  31. self.set_undefined_options('install_lib', ('install_dir', 'install_dir'))
  32. self.target = os.path.join(self.install_dir, self.basename)
  33. self.outputs = [self.target]
  34. def run(self):
  35. target = self.target
  36. if os.path.isdir(target) and not os.path.islink(target):
  37. dir_util.remove_tree(target, dry_run=self.dry_run)
  38. elif os.path.exists(target):
  39. self.execute(os.unlink, (self.target,), "Removing " + target)
  40. elif not os.path.isdir(self.install_dir):
  41. self.execute(
  42. os.makedirs, (self.install_dir,), "Creating " + self.install_dir
  43. )
  44. log.info("Writing %s", target)
  45. if not self.dry_run:
  46. with open(target, 'w', encoding='UTF-8') as f:
  47. self.distribution.metadata.write_pkg_file(f)
  48. def get_outputs(self):
  49. return self.outputs
  50. # The following routines are taken from setuptools' pkg_resources module and
  51. # can be replaced by importing them from pkg_resources once it is included
  52. # in the stdlib.
  53. def safe_name(name):
  54. """Convert an arbitrary string to a standard distribution name
  55. Any runs of non-alphanumeric/. characters are replaced with a single '-'.
  56. """
  57. return re.sub('[^A-Za-z0-9.]+', '-', name)
  58. def safe_version(version):
  59. """Convert an arbitrary string to a standard version string
  60. Spaces become dots, and all other non-alphanumeric characters become
  61. dashes, with runs of multiple dashes condensed to a single dash.
  62. """
  63. version = version.replace(' ', '.')
  64. return re.sub('[^A-Za-z0-9.]+', '-', version)
  65. def to_filename(name):
  66. """Convert a project or version name to its filename-escaped form
  67. Any '-' characters are currently replaced with '_'.
  68. """
  69. return name.replace('-', '_')