1 """Legacy installation process, i.e. `setup.py install`.
6 from typing import List, Optional, Sequence
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
17 logger = logging.getLogger(__name__)
20 def write_installed_files_from_setuptools_record(
21 record_lines: List[str],
25 def prepend_root(path: str) -> str:
26 if root is None or not os.path.isabs(path):
29 return change_root(root, path)
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)
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)
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))
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")
58 install_options: List[str],
59 global_options: Sequence[str],
62 prefix: Optional[str],
69 build_env: BuildEnvironment,
70 unpacked_source_directory: str,
74 header_dir = scheme.headers
76 with TempDirectory(kind="record") as temp_dir:
78 record_filename = os.path.join(temp_dir.path, "install-record.txt")
79 install_args = make_setuptools_install_args(
81 global_options=global_options,
82 install_options=install_options,
83 record_filename=record_filename,
86 header_dir=header_dir,
88 use_user_site=use_user_site,
89 no_user_config=isolated,
93 runner = runner_with_spinner_message(
94 f"Running setup.py install for {req_name}"
99 cwd=unpacked_source_directory,
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
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
111 # At this point, we have successfully installed the requirement.
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()
119 write_installed_files_from_setuptools_record(record_lines, root, req_description)