wheel_legacy.py 3.3 KB

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