install_egg_info.py 2.5 KB

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