wheel_legacy.py 3.3 KB

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