metadata_legacy.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. """Metadata generation logic for legacy source distributions.
  2. """
  3. import logging
  4. import os
  5. from pip._internal.build_env import BuildEnvironment
  6. from pip._internal.cli.spinners import open_spinner
  7. from pip._internal.exceptions import (
  8. InstallationError,
  9. InstallationSubprocessError,
  10. MetadataGenerationFailed,
  11. )
  12. from pip._internal.utils.setuptools_build import make_setuptools_egg_info_args
  13. from pip._internal.utils.subprocess import call_subprocess
  14. from pip._internal.utils.temp_dir import TempDirectory
  15. logger = logging.getLogger(__name__)
  16. def _find_egg_info(directory: str) -> str:
  17. """Find an .egg-info subdirectory in `directory`."""
  18. filenames = [f for f in os.listdir(directory) if f.endswith(".egg-info")]
  19. if not filenames:
  20. raise InstallationError(f"No .egg-info directory found in {directory}")
  21. if len(filenames) > 1:
  22. raise InstallationError(
  23. "More than one .egg-info directory found in {}".format(directory)
  24. )
  25. return os.path.join(directory, filenames[0])
  26. def generate_metadata(
  27. build_env: BuildEnvironment,
  28. setup_py_path: str,
  29. source_dir: str,
  30. isolated: bool,
  31. details: str,
  32. ) -> str:
  33. """Generate metadata using setup.py-based defacto mechanisms.
  34. Returns the generated metadata directory.
  35. """
  36. logger.debug(
  37. "Running setup.py (path:%s) egg_info for package %s",
  38. setup_py_path,
  39. details,
  40. )
  41. egg_info_dir = TempDirectory(kind="pip-egg-info", globally_managed=True).path
  42. args = make_setuptools_egg_info_args(
  43. setup_py_path,
  44. egg_info_dir=egg_info_dir,
  45. no_user_config=isolated,
  46. )
  47. with build_env:
  48. with open_spinner("Preparing metadata (setup.py)") as spinner:
  49. try:
  50. call_subprocess(
  51. args,
  52. cwd=source_dir,
  53. command_desc="python setup.py egg_info",
  54. spinner=spinner,
  55. )
  56. except InstallationSubprocessError as error:
  57. raise MetadataGenerationFailed(package_details=details) from error
  58. # Return the .egg-info directory.
  59. return _find_egg_info(egg_info_dir)