install_egg_info.py 2.7 KB

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