290967dd6d57adef52a4999e92aafceac5760cd7
[SubU] /
1 """Legacy installation process, i.e. `setup.py install`.
2 """
3
4 import logging
5 import os
6 from typing import List, Optional, Sequence
7
8 from pip._internal.build_env import BuildEnvironment
9 from pip._internal.exceptions import InstallationError, LegacyInstallFailure
10 from pip._internal.locations.base import change_root
11 from pip._internal.models.scheme import Scheme
12 from pip._internal.utils.misc import ensure_dir
13 from pip._internal.utils.setuptools_build import make_setuptools_install_args
14 from pip._internal.utils.subprocess import runner_with_spinner_message
15 from pip._internal.utils.temp_dir import TempDirectory
16
17 logger = logging.getLogger(__name__)
18
19
20 def write_installed_files_from_setuptools_record(
21     record_lines: List[str],
22     root: Optional[str],
23     req_description: str,
24 ) -> None:
25     def prepend_root(path: str) -> str:
26         if root is None or not os.path.isabs(path):
27             return path
28         else:
29             return change_root(root, path)
30
31     for line in record_lines:
32         directory = os.path.dirname(line)
33         if directory.endswith(".egg-info"):
34             egg_info_dir = prepend_root(directory)
35             break
36     else:
37         message = (
38             "{} did not indicate that it installed an "
39             ".egg-info directory. Only setup.py projects "
40             "generating .egg-info directories are supported."
41         ).format(req_description)
42         raise InstallationError(message)
43
44     new_lines = []
45     for line in record_lines:
46         filename = line.strip()
47         if os.path.isdir(filename):
48             filename += os.path.sep
49         new_lines.append(os.path.relpath(prepend_root(filename), egg_info_dir))
50     new_lines.sort()
51     ensure_dir(egg_info_dir)
52     inst_files_path = os.path.join(egg_info_dir, "installed-files.txt")
53     with open(inst_files_path, "w") as f:
54         f.write("\n".join(new_lines) + "\n")
55
56
57 def install(
58     install_options: List[str],
59     global_options: Sequence[str],
60     root: Optional[str],
61     home: Optional[str],
62     prefix: Optional[str],
63     use_user_site: bool,
64     pycompile: bool,
65     scheme: Scheme,
66     setup_py_path: str,
67     isolated: bool,
68     req_name: str,
69     build_env: BuildEnvironment,
70     unpacked_source_directory: str,
71     req_description: str,
72 ) -> bool:
73
74     header_dir = scheme.headers
75
76     with TempDirectory(kind="record") as temp_dir:
77         try:
78             record_filename = os.path.join(temp_dir.path, "install-record.txt")
79             install_args = make_setuptools_install_args(
80                 setup_py_path,
81                 global_options=global_options,
82                 install_options=install_options,
83                 record_filename=record_filename,
84                 root=root,
85                 prefix=prefix,
86                 header_dir=header_dir,
87                 home=home,
88                 use_user_site=use_user_site,
89                 no_user_config=isolated,
90                 pycompile=pycompile,
91             )
92
93             runner = runner_with_spinner_message(
94                 f"Running setup.py install for {req_name}"
95             )
96             with build_env:
97                 runner(
98                     cmd=install_args,
99                     cwd=unpacked_source_directory,
100                 )
101
102             if not os.path.exists(record_filename):
103                 logger.debug("Record file %s not found", record_filename)
104                 # Signal to the caller that we didn't install the new package
105                 return False
106
107         except Exception as e:
108             # Signal to the caller that we didn't install the new package
109             raise LegacyInstallFailure(package_details=req_name) from e
110
111         # At this point, we have successfully installed the requirement.
112
113         # We intentionally do not use any encoding to read the file because
114         # setuptools writes the file using distutils.file_util.write_file,
115         # which does not specify an encoding.
116         with open(record_filename) as f:
117             record_lines = f.read().splitlines()
118
119     write_installed_files_from_setuptools_record(record_lines, root, req_description)
120     return True