dist_info.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. """
  2. Create a dist_info directory
  3. As defined in the wheel specification
  4. """
  5. import os
  6. import re
  7. import warnings
  8. from inspect import cleandoc
  9. from distutils.core import Command
  10. from distutils import log
  11. from setuptools.extern import packaging
  12. class dist_info(Command):
  13. description = 'create a .dist-info directory'
  14. user_options = [
  15. ('egg-base=', 'e', "directory containing .egg-info directories"
  16. " (default: top of the source tree)"),
  17. ]
  18. def initialize_options(self):
  19. self.egg_base = None
  20. def finalize_options(self):
  21. pass
  22. def run(self):
  23. egg_info = self.get_finalized_command('egg_info')
  24. egg_info.egg_base = self.egg_base
  25. egg_info.finalize_options()
  26. egg_info.run()
  27. name = _safe(self.distribution.get_name())
  28. version = _version(self.distribution.get_version())
  29. base = self.egg_base or os.curdir
  30. dist_info_dir = os.path.join(base, f"{name}-{version}.dist-info")
  31. log.info("creating '{}'".format(os.path.abspath(dist_info_dir)))
  32. bdist_wheel = self.get_finalized_command('bdist_wheel')
  33. bdist_wheel.egg2dist(egg_info.egg_info, dist_info_dir)
  34. def _safe(component: str) -> str:
  35. """Escape a component used to form a wheel name according to PEP 491"""
  36. return re.sub(r"[^\w\d.]+", "_", component)
  37. def _version(version: str) -> str:
  38. """Convert an arbitrary string to a version string."""
  39. v = version.replace(' ', '.')
  40. try:
  41. return str(packaging.version.Version(v)).replace("-", "_")
  42. except packaging.version.InvalidVersion:
  43. msg = f"""!!\n\n
  44. ###################
  45. # Invalid version #
  46. ###################
  47. {version!r} is not valid according to PEP 440.\n
  48. Please make sure specify a valid version for your package.
  49. Also note that future releases of setuptools may halt the build process
  50. if an invalid version is given.
  51. \n\n!!
  52. """
  53. warnings.warn(cleandoc(msg))
  54. return _safe(v).strip("_")