metadata_legacy.py 1.9 KB

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