File indexing completed on 2024-11-24 05:05:58

0001 import logging
0002 import os.path
0003 from typing import List, Optional
0004 
0005 from pip._internal.cli.spinners import open_spinner
0006 from pip._internal.utils.setuptools_build import make_setuptools_bdist_wheel_args
0007 from pip._internal.utils.subprocess import call_subprocess, format_command_args
0008 
0009 logger = logging.getLogger(__name__)
0010 
0011 
0012 def format_command_result(
0013     command_args: List[str],
0014     command_output: str,
0015 ) -> str:
0016     """Format command information for logging."""
0017     command_desc = format_command_args(command_args)
0018     text = f"Command arguments: {command_desc}\n"
0019 
0020     if not command_output:
0021         text += "Command output: None"
0022     elif logger.getEffectiveLevel() > logging.DEBUG:
0023         text += "Command output: [use --verbose to show]"
0024     else:
0025         if not command_output.endswith("\n"):
0026             command_output += "\n"
0027         text += f"Command output:\n{command_output}"
0028 
0029     return text
0030 
0031 
0032 def get_legacy_build_wheel_path(
0033     names: List[str],
0034     temp_dir: str,
0035     name: str,
0036     command_args: List[str],
0037     command_output: str,
0038 ) -> Optional[str]:
0039     """Return the path to the wheel in the temporary build directory."""
0040     # Sort for determinism.
0041     names = sorted(names)
0042     if not names:
0043         msg = ("Legacy build of wheel for {!r} created no files.\n").format(name)
0044         msg += format_command_result(command_args, command_output)
0045         logger.warning(msg)
0046         return None
0047 
0048     if len(names) > 1:
0049         msg = (
0050             "Legacy build of wheel for {!r} created more than one file.\n"
0051             "Filenames (choosing first): {}\n"
0052         ).format(name, names)
0053         msg += format_command_result(command_args, command_output)
0054         logger.warning(msg)
0055 
0056     return os.path.join(temp_dir, names[0])
0057 
0058 
0059 def build_wheel_legacy(
0060     name: str,
0061     setup_py_path: str,
0062     source_dir: str,
0063     global_options: List[str],
0064     build_options: List[str],
0065     tempd: str,
0066 ) -> Optional[str]:
0067     """Build one unpacked package using the "legacy" build process.
0068 
0069     Returns path to wheel if successfully built. Otherwise, returns None.
0070     """
0071     wheel_args = make_setuptools_bdist_wheel_args(
0072         setup_py_path,
0073         global_options=global_options,
0074         build_options=build_options,
0075         destination_dir=tempd,
0076     )
0077 
0078     spin_message = f"Building wheel for {name} (setup.py)"
0079     with open_spinner(spin_message) as spinner:
0080         logger.debug("Destination directory: %s", tempd)
0081 
0082         try:
0083             output = call_subprocess(
0084                 wheel_args,
0085                 command_desc="python setup.py bdist_wheel",
0086                 cwd=source_dir,
0087                 spinner=spinner,
0088             )
0089         except Exception:
0090             spinner.finish("error")
0091             logger.error("Failed building wheel for %s", name)
0092             return None
0093 
0094         names = os.listdir(tempd)
0095         wheel_path = get_legacy_build_wheel_path(
0096             names=names,
0097             temp_dir=tempd,
0098             name=name,
0099             command_args=wheel_args,
0100             command_output=output,
0101         )
0102         return wheel_path