metadata.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. """Metadata generation logic for source distributions.
  2. """
  3. import os
  4. from pip._vendor.pyproject_hooks import BuildBackendHookCaller
  5. from pip._internal.build_env import BuildEnvironment
  6. from pip._internal.exceptions import (
  7. InstallationSubprocessError,
  8. MetadataGenerationFailed,
  9. )
  10. from pip._internal.utils.subprocess import runner_with_spinner_message
  11. from pip._internal.utils.temp_dir import TempDirectory
  12. def generate_metadata(
  13. build_env: BuildEnvironment, backend: BuildBackendHookCaller, details: str
  14. ) -> str:
  15. """Generate metadata using mechanisms described in PEP 517.
  16. Returns the generated metadata directory.
  17. """
  18. metadata_tmpdir = TempDirectory(kind="modern-metadata", globally_managed=True)
  19. metadata_dir = metadata_tmpdir.path
  20. with build_env:
  21. # Note that BuildBackendHookCaller implements a fallback for
  22. # prepare_metadata_for_build_wheel, so we don't have to
  23. # consider the possibility that this hook doesn't exist.
  24. runner = runner_with_spinner_message("Preparing metadata (pyproject.toml)")
  25. with backend.subprocess_runner(runner):
  26. try:
  27. distinfo_dir = backend.prepare_metadata_for_build_wheel(metadata_dir)
  28. except InstallationSubprocessError as error:
  29. raise MetadataGenerationFailed(package_details=details) from error
  30. return os.path.join(metadata_dir, distinfo_dir)