File indexing completed on 2024-11-24 05:05:58
0001 """Metadata generation logic for legacy source distributions. 0002 """ 0003 0004 import logging 0005 import os 0006 0007 from pip._internal.build_env import BuildEnvironment 0008 from pip._internal.cli.spinners import open_spinner 0009 from pip._internal.exceptions import ( 0010 InstallationError, 0011 InstallationSubprocessError, 0012 MetadataGenerationFailed, 0013 ) 0014 from pip._internal.utils.setuptools_build import make_setuptools_egg_info_args 0015 from pip._internal.utils.subprocess import call_subprocess 0016 from pip._internal.utils.temp_dir import TempDirectory 0017 0018 logger = logging.getLogger(__name__) 0019 0020 0021 def _find_egg_info(directory: str) -> str: 0022 """Find an .egg-info subdirectory in `directory`.""" 0023 filenames = [f for f in os.listdir(directory) if f.endswith(".egg-info")] 0024 0025 if not filenames: 0026 raise InstallationError(f"No .egg-info directory found in {directory}") 0027 0028 if len(filenames) > 1: 0029 raise InstallationError( 0030 "More than one .egg-info directory found in {}".format(directory) 0031 ) 0032 0033 return os.path.join(directory, filenames[0]) 0034 0035 0036 def generate_metadata( 0037 build_env: BuildEnvironment, 0038 setup_py_path: str, 0039 source_dir: str, 0040 isolated: bool, 0041 details: str, 0042 ) -> str: 0043 """Generate metadata using setup.py-based defacto mechanisms. 0044 0045 Returns the generated metadata directory. 0046 """ 0047 logger.debug( 0048 "Running setup.py (path:%s) egg_info for package %s", 0049 setup_py_path, 0050 details, 0051 ) 0052 0053 egg_info_dir = TempDirectory(kind="pip-egg-info", globally_managed=True).path 0054 0055 args = make_setuptools_egg_info_args( 0056 setup_py_path, 0057 egg_info_dir=egg_info_dir, 0058 no_user_config=isolated, 0059 ) 0060 0061 with build_env: 0062 with open_spinner("Preparing metadata (setup.py)") as spinner: 0063 try: 0064 call_subprocess( 0065 args, 0066 cwd=source_dir, 0067 command_desc="python setup.py egg_info", 0068 spinner=spinner, 0069 ) 0070 except InstallationSubprocessError as error: 0071 raise MetadataGenerationFailed(package_details=details) from error 0072 0073 # Return the .egg-info directory. 0074 return _find_egg_info(egg_info_dir)