wheel_legacy.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import logging
  2. import os.path
  3. from typing import List, Optional
  4. from pip._internal.cli.spinners import open_spinner
  5. from pip._internal.utils.setuptools_build import make_setuptools_bdist_wheel_args
  6. from pip._internal.utils.subprocess import call_subprocess, format_command_args
  7. logger = logging.getLogger(__name__)
  8. def format_command_result(
  9. command_args: List[str],
  10. command_output: str,
  11. ) -> str:
  12. """Format command information for logging."""
  13. command_desc = format_command_args(command_args)
  14. text = f"Command arguments: {command_desc}\n"
  15. if not command_output:
  16. text += "Command output: None"
  17. elif logger.getEffectiveLevel() > logging.DEBUG:
  18. text += "Command output: [use --verbose to show]"
  19. else:
  20. if not command_output.endswith("\n"):
  21. command_output += "\n"
  22. text += f"Command output:\n{command_output}"
  23. return text
  24. def get_legacy_build_wheel_path(
  25. names: List[str],
  26. temp_dir: str,
  27. name: str,
  28. command_args: List[str],
  29. command_output: str,
  30. ) -> Optional[str]:
  31. """Return the path to the wheel in the temporary build directory."""
  32. # Sort for determinism.
  33. names = sorted(names)
  34. if not names:
  35. msg = ("Legacy build of wheel for {!r} created no files.\n").format(name)
  36. msg += format_command_result(command_args, command_output)
  37. logger.warning(msg)
  38. return None
  39. if len(names) > 1:
  40. msg = (
  41. "Legacy build of wheel for {!r} created more than one file.\n"
  42. "Filenames (choosing first): {}\n"
  43. ).format(name, names)
  44. msg += format_command_result(command_args, command_output)
  45. logger.warning(msg)
  46. return os.path.join(temp_dir, names[0])
  47. def build_wheel_legacy(
  48. name: str,
  49. setup_py_path: str,
  50. source_dir: str,
  51. global_options: List[str],
  52. build_options: List[str],
  53. tempd: str,
  54. ) -> Optional[str]:
  55. """Build one unpacked package using the "legacy" build process.
  56. Returns path to wheel if successfully built. Otherwise, returns None.
  57. """
  58. wheel_args = make_setuptools_bdist_wheel_args(
  59. setup_py_path,
  60. global_options=global_options,
  61. build_options=build_options,
  62. destination_dir=tempd,
  63. )
  64. spin_message = f"Building wheel for {name} (setup.py)"
  65. with open_spinner(spin_message) as spinner:
  66. logger.debug("Destination directory: %s", tempd)
  67. try:
  68. output = call_subprocess(
  69. wheel_args,
  70. command_desc="python setup.py bdist_wheel",
  71. cwd=source_dir,
  72. spinner=spinner,
  73. )
  74. except Exception:
  75. spinner.finish("error")
  76. logger.error("Failed building wheel for %s", name)
  77. return None
  78. names = os.listdir(tempd)
  79. wheel_path = get_legacy_build_wheel_path(
  80. names=names,
  81. temp_dir=tempd,
  82. name=name,
  83. command_args=wheel_args,
  84. command_output=output,
  85. )
  86. return wheel_path