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