Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/pip/_internal/req/req_install.py: 24%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

389 statements  

1from __future__ import annotations 

2 

3import functools 

4import logging 

5import os 

6import shutil 

7import sys 

8import uuid 

9import zipfile 

10from collections.abc import Collection, Iterable 

11from optparse import Values 

12from pathlib import Path 

13from typing import Any 

14 

15from pip._vendor.packaging.markers import Marker 

16from pip._vendor.packaging.requirements import Requirement 

17from pip._vendor.packaging.specifiers import SpecifierSet 

18from pip._vendor.packaging.utils import canonicalize_name 

19from pip._vendor.packaging.version import Version 

20from pip._vendor.packaging.version import parse as parse_version 

21from pip._vendor.pyproject_hooks import BuildBackendHookCaller 

22 

23from pip._internal.build_env import BuildEnvironment, NoOpBuildEnvironment 

24from pip._internal.exceptions import InstallationError, PreviousBuildDirError 

25from pip._internal.locations import get_scheme 

26from pip._internal.metadata import ( 

27 BaseDistribution, 

28 get_default_environment, 

29 get_directory_distribution, 

30 get_wheel_distribution, 

31) 

32from pip._internal.metadata.base import FilesystemWheel 

33from pip._internal.models.direct_url import DirectUrl 

34from pip._internal.models.link import Link 

35from pip._internal.operations.build.metadata import generate_metadata 

36from pip._internal.operations.build.metadata_editable import generate_editable_metadata 

37from pip._internal.operations.install.wheel import install_wheel 

38from pip._internal.pyproject import load_pyproject_toml, make_pyproject_path 

39from pip._internal.req.req_uninstall import UninstallPathSet 

40from pip._internal.utils.deprecation import deprecated 

41from pip._internal.utils.hashes import Hashes 

42from pip._internal.utils.misc import ( 

43 ConfiguredBuildBackendHookCaller, 

44 ask_path_exists, 

45 backup_dir, 

46 display_path, 

47 hide_url, 

48 is_installable_dir, 

49 redact_auth_from_requirement, 

50 redact_auth_from_url, 

51) 

52from pip._internal.utils.packaging import get_requirement 

53from pip._internal.utils.subprocess import runner_with_spinner_message 

54from pip._internal.utils.temp_dir import TempDirectory, tempdir_kinds 

55from pip._internal.utils.unpacking import unpack_file 

56from pip._internal.utils.virtualenv import running_under_virtualenv 

57from pip._internal.vcs import vcs 

58 

59logger = logging.getLogger(__name__) 

60 

61 

62class InstallRequirement: 

63 """ 

64 Represents something that may be installed later on, may have information 

65 about where to fetch the relevant requirement and also contains logic for 

66 installing the said requirement. 

67 """ 

68 

69 def __init__( 

70 self, 

71 req: Requirement | None, 

72 comes_from: str | InstallRequirement | None, 

73 editable: bool = False, 

74 link: Link | None = None, 

75 markers: Marker | None = None, 

76 isolated: bool = False, 

77 *, 

78 hash_options: dict[str, list[str]] | None = None, 

79 config_settings: dict[str, str | list[str]] | None = None, 

80 constraint: bool = False, 

81 extras: Collection[str] = (), 

82 user_supplied: bool = False, 

83 permit_editable_wheels: bool = False, 

84 ) -> None: 

85 assert req is None or isinstance(req, Requirement), req 

86 self.req = req 

87 self.comes_from = comes_from 

88 self.constraint = constraint 

89 self.editable = editable 

90 self.permit_editable_wheels = permit_editable_wheels 

91 

92 # source_dir is the local directory where the linked requirement is 

93 # located, or unpacked. In case unpacking is needed, creating and 

94 # populating source_dir is done by the RequirementPreparer. Note this 

95 # is not necessarily the directory where pyproject.toml or setup.py is 

96 # located - that one is obtained via unpacked_source_directory. 

97 self.source_dir: str | None = None 

98 if self.editable: 

99 assert link 

100 if link.is_file: 

101 self.source_dir = os.path.normpath(os.path.abspath(link.file_path)) 

102 

103 # original_link is the direct URL that was provided by the user for the 

104 # requirement, either directly or via a constraints file. 

105 if link is None and req and req.url: 

106 # PEP 508 URL requirement 

107 link = Link(req.url) 

108 self.link = self.original_link = link 

109 

110 # When this InstallRequirement is a wheel obtained from the cache of locally 

111 # built wheels, this is the source link corresponding to the cache entry, which 

112 # was used to download and build the cached wheel. 

113 self.cached_wheel_source_link: Link | None = None 

114 

115 # Information about the location of the artifact that was downloaded . This 

116 # property is guaranteed to be set in resolver results. 

117 self.download_info: DirectUrl | None = None 

118 

119 # Path to any downloaded or already-existing package. 

120 self.local_file_path: str | None = None 

121 if self.link and self.link.is_file: 

122 self.local_file_path = self.link.file_path 

123 

124 if extras: 

125 self.extras = extras 

126 elif req: 

127 self.extras = req.extras 

128 else: 

129 self.extras = set() 

130 if markers is None and req: 

131 markers = req.marker 

132 self.markers = markers 

133 

134 # This holds the Distribution object if this requirement is already installed. 

135 self.satisfied_by: BaseDistribution | None = None 

136 # Whether the installation process should try to uninstall an existing 

137 # distribution before installing this requirement. 

138 self.should_reinstall = False 

139 # Temporary build location 

140 self._temp_build_dir: TempDirectory | None = None 

141 # Set to True after successful installation 

142 self.install_succeeded: bool | None = None 

143 # Supplied options 

144 self.hash_options = hash_options if hash_options else {} 

145 self.config_settings = config_settings 

146 # Set to True after successful preparation of this requirement 

147 self.prepared = False 

148 # User supplied requirement are explicitly requested for installation 

149 # by the user via CLI arguments or requirements files, as opposed to, 

150 # e.g. dependencies, extras or constraints. 

151 self.user_supplied = user_supplied 

152 

153 self.isolated = isolated 

154 self.build_env: BuildEnvironment = NoOpBuildEnvironment() 

155 

156 # For PEP 517, the directory where we request the project metadata 

157 # gets stored. We need this to pass to build_wheel, so the backend 

158 # can ensure that the wheel matches the metadata (see the PEP for 

159 # details). 

160 self.metadata_directory: str | None = None 

161 

162 # The cached metadata distribution that this requirement represents. 

163 # See get_dist / set_dist. 

164 self._distribution: BaseDistribution | None = None 

165 

166 # The static build requirements (from pyproject.toml) 

167 self.pyproject_requires: list[str] | None = None 

168 

169 # Build requirements that we will check are available 

170 self.requirements_to_check: list[str] = [] 

171 

172 # The PEP 517 backend we should use to build the project 

173 self.pep517_backend: BuildBackendHookCaller | None = None 

174 

175 # This requirement needs more preparation before it can be built 

176 self.needs_more_preparation = False 

177 

178 # This requirement needs to be unpacked before it can be installed. 

179 self._archive_source: Path | None = None 

180 

181 def __str__(self) -> str: 

182 if self.req: 

183 s = redact_auth_from_requirement(self.req) 

184 if self.link: 

185 s += f" from {redact_auth_from_url(self.link.url)}" 

186 elif self.link: 

187 s = redact_auth_from_url(self.link.url) 

188 else: 

189 s = "<InstallRequirement>" 

190 if self.satisfied_by is not None: 

191 if self.satisfied_by.location is not None: 

192 location = display_path(self.satisfied_by.location) 

193 else: 

194 location = "<memory>" 

195 s += f" in {location}" 

196 if self.comes_from: 

197 if isinstance(self.comes_from, str): 

198 comes_from: str | None = self.comes_from 

199 else: 

200 comes_from = self.comes_from.from_path() 

201 if comes_from: 

202 s += f" (from {comes_from})" 

203 return s 

204 

205 def __repr__(self) -> str: 

206 return ( 

207 f"<{self.__class__.__name__} object: " 

208 f"{str(self)} editable={self.editable!r}>" 

209 ) 

210 

211 def format_debug(self) -> str: 

212 """An un-tested helper for getting state, for debugging.""" 

213 attributes = vars(self) 

214 names = sorted(attributes) 

215 

216 state = (f"{attr}={attributes[attr]!r}" for attr in sorted(names)) 

217 return "<{name} object: {{{state}}}>".format( 

218 name=self.__class__.__name__, 

219 state=", ".join(state), 

220 ) 

221 

222 # Things that are valid for all kinds of requirements? 

223 @property 

224 def name(self) -> str | None: 

225 if self.req is None: 

226 return None 

227 return self.req.name 

228 

229 @functools.cached_property 

230 def supports_pyproject_editable(self) -> bool: 

231 assert self.pep517_backend 

232 with self.build_env: 

233 runner = runner_with_spinner_message( 

234 "Checking if build backend supports build_editable" 

235 ) 

236 with self.pep517_backend.subprocess_runner(runner): 

237 return "build_editable" in self.pep517_backend._supported_features() 

238 

239 @property 

240 def specifier(self) -> SpecifierSet: 

241 assert self.req is not None 

242 return self.req.specifier 

243 

244 @property 

245 def is_direct(self) -> bool: 

246 """Whether this requirement was specified as a direct URL.""" 

247 return self.original_link is not None 

248 

249 @property 

250 def is_pinned(self) -> bool: 

251 """Return whether I am pinned to an exact version. 

252 

253 For example, some-package==1.2 is pinned; some-package>1.2 is not. 

254 """ 

255 assert self.req is not None 

256 specifiers = self.req.specifier 

257 return len(specifiers) == 1 and next(iter(specifiers)).operator in {"==", "==="} 

258 

259 def match_markers(self, extras_requested: Iterable[str] | None = None) -> bool: 

260 if not extras_requested: 

261 # Provide an extra to safely evaluate the markers 

262 # without matching any extra 

263 extras_requested = ("",) 

264 if self.markers is not None: 

265 return any( 

266 self.markers.evaluate({"extra": extra}) for extra in extras_requested 

267 ) 

268 else: 

269 return True 

270 

271 @property 

272 def has_hash_options(self) -> bool: 

273 """Return whether any known-good hashes are specified as options. 

274 

275 These activate --require-hashes mode; hashes specified as part of a 

276 URL do not. 

277 

278 """ 

279 return bool(self.hash_options) 

280 

281 def hashes(self, trust_internet: bool = True) -> Hashes: 

282 """Return a hash-comparer that considers my option- and URL-based 

283 hashes to be known-good. 

284 

285 Hashes in URLs--ones embedded in the requirements file, not ones 

286 downloaded from an index server--are almost peers with ones from 

287 flags. They satisfy --require-hashes (whether it was implicitly or 

288 explicitly activated) but do not activate it. md5 and sha224 are not 

289 allowed in flags, which should nudge people toward good algos. We 

290 always OR all hashes together, even ones from URLs. 

291 

292 :param trust_internet: Whether to trust URL-based (#md5=...) hashes 

293 downloaded from the internet, as by populate_link() 

294 

295 """ 

296 good_hashes = self.hash_options.copy() 

297 if trust_internet: 

298 link = self.link 

299 elif self.is_direct and self.user_supplied: 

300 link = self.original_link 

301 else: 

302 link = None 

303 if link and link.hash: 

304 assert link.hash_name is not None 

305 good_hashes.setdefault(link.hash_name, []).append(link.hash) 

306 return Hashes(good_hashes) 

307 

308 def from_path(self) -> str | None: 

309 """Format a nice indicator to show where this "comes from" """ 

310 if self.req is None: 

311 return None 

312 s = str(self.req) 

313 if self.comes_from: 

314 comes_from: str | None 

315 if isinstance(self.comes_from, str): 

316 comes_from = self.comes_from 

317 else: 

318 comes_from = self.comes_from.from_path() 

319 if comes_from: 

320 s += "->" + comes_from 

321 return s 

322 

323 def ensure_build_location( 

324 self, build_dir: str, autodelete: bool, parallel_builds: bool 

325 ) -> str: 

326 assert build_dir is not None 

327 if self._temp_build_dir is not None: 

328 assert self._temp_build_dir.path 

329 return self._temp_build_dir.path 

330 if self.req is None: 

331 # Some systems have /tmp as a symlink which confuses custom 

332 # builds (such as numpy). Thus, we ensure that the real path 

333 # is returned. 

334 self._temp_build_dir = TempDirectory( 

335 kind=tempdir_kinds.REQ_BUILD, globally_managed=True 

336 ) 

337 

338 return self._temp_build_dir.path 

339 

340 # This is the only remaining place where we manually determine the path 

341 # for the temporary directory. It is only needed for editables where 

342 # it is the value of the --src option. 

343 

344 # When parallel builds are enabled, add a UUID to the build directory 

345 # name so multiple builds do not interfere with each other. 

346 dir_name: str = canonicalize_name(self.req.name) 

347 if parallel_builds: 

348 dir_name = f"{dir_name}_{uuid.uuid4().hex}" 

349 

350 # FIXME: Is there a better place to create the build_dir? (hg and bzr 

351 # need this) 

352 if not os.path.exists(build_dir): 

353 logger.debug("Creating directory %s", build_dir) 

354 os.makedirs(build_dir) 

355 actual_build_dir = os.path.join(build_dir, dir_name) 

356 # `None` indicates that we respect the globally-configured deletion 

357 # settings, which is what we actually want when auto-deleting. 

358 delete_arg = None if autodelete else False 

359 return TempDirectory( 

360 path=actual_build_dir, 

361 delete=delete_arg, 

362 kind=tempdir_kinds.REQ_BUILD, 

363 globally_managed=True, 

364 ).path 

365 

366 def _set_requirement(self) -> None: 

367 """Set requirement after generating metadata.""" 

368 assert self.req is None 

369 assert self.metadata is not None 

370 assert self.source_dir is not None 

371 

372 # Construct a Requirement object from the generated metadata 

373 if isinstance(parse_version(self.metadata["Version"]), Version): 

374 op = "==" 

375 else: 

376 op = "===" 

377 

378 self.req = get_requirement( 

379 "".join( 

380 [ 

381 self.metadata["Name"], 

382 op, 

383 self.metadata["Version"], 

384 ] 

385 ) 

386 ) 

387 

388 def warn_on_mismatching_name(self) -> None: 

389 assert self.req is not None 

390 metadata_name = canonicalize_name(self.metadata["Name"]) 

391 if canonicalize_name(self.req.name) == metadata_name: 

392 # Everything is fine. 

393 return 

394 

395 # If we're here, there's a mismatch. Log a warning about it. 

396 logger.warning( 

397 "Generating metadata for package %s " 

398 "produced metadata for project name %s. Fix your " 

399 "#egg=%s fragments.", 

400 self.name, 

401 metadata_name, 

402 self.name, 

403 ) 

404 self.req = get_requirement(metadata_name) 

405 

406 def check_if_exists(self, use_user_site: bool) -> None: 

407 """Find an installed distribution that satisfies or conflicts 

408 with this requirement, and set self.satisfied_by or 

409 self.should_reinstall appropriately. 

410 """ 

411 if self.req is None: 

412 return 

413 existing_dist = get_default_environment().get_distribution(self.req.name) 

414 if not existing_dist: 

415 return 

416 

417 version_compatible = self.req.specifier.contains( 

418 existing_dist.version, 

419 prereleases=True, 

420 ) 

421 if not version_compatible: 

422 self.satisfied_by = None 

423 if use_user_site: 

424 if existing_dist.in_usersite: 

425 self.should_reinstall = True 

426 elif running_under_virtualenv() and existing_dist.in_site_packages: 

427 raise InstallationError( 

428 f"Will not install to the user site because it will " 

429 f"lack sys.path precedence to {existing_dist.raw_name} " 

430 f"in {existing_dist.location}" 

431 ) 

432 else: 

433 self.should_reinstall = True 

434 else: 

435 if self.editable: 

436 self.should_reinstall = True 

437 # when installing editables, nothing pre-existing should ever 

438 # satisfy 

439 self.satisfied_by = None 

440 else: 

441 self.satisfied_by = existing_dist 

442 

443 # Things valid for wheels 

444 @property 

445 def is_wheel(self) -> bool: 

446 if not self.link: 

447 return False 

448 return self.link.is_wheel 

449 

450 @property 

451 def is_wheel_from_cache(self) -> bool: 

452 # When True, it means that this InstallRequirement is a local wheel file in the 

453 # cache of locally built wheels. 

454 return self.cached_wheel_source_link is not None 

455 

456 # Things valid for sdists 

457 @property 

458 def unpacked_source_directory(self) -> str: 

459 assert self.source_dir, f"No source dir for {self}" 

460 return os.path.join( 

461 self.source_dir, self.link and self.link.subdirectory_fragment or "" 

462 ) 

463 

464 @property 

465 def setup_py_path(self) -> str: 

466 assert self.source_dir, f"No source dir for {self}" 

467 setup_py = os.path.join(self.unpacked_source_directory, "setup.py") 

468 

469 return setup_py 

470 

471 @property 

472 def pyproject_toml_path(self) -> str: 

473 assert self.source_dir, f"No source dir for {self}" 

474 return make_pyproject_path(self.unpacked_source_directory) 

475 

476 def load_pyproject_toml(self) -> None: 

477 """Load the pyproject.toml file. 

478 

479 After calling this routine, all of the attributes related to PEP 517 

480 processing for this requirement have been set. 

481 """ 

482 pyproject_toml_data = load_pyproject_toml( 

483 self.pyproject_toml_path, self.setup_py_path, str(self) 

484 ) 

485 assert pyproject_toml_data 

486 requires, backend, check, backend_path = pyproject_toml_data 

487 self.requirements_to_check = check 

488 self.pyproject_requires = requires 

489 self.pep517_backend = ConfiguredBuildBackendHookCaller( 

490 self, 

491 self.unpacked_source_directory, 

492 backend, 

493 backend_path=backend_path, 

494 ) 

495 

496 def editable_sanity_check(self) -> None: 

497 """Check that an editable requirement if valid for use with PEP 517/518. 

498 

499 This verifies that an editable has a build backend that supports PEP 660. 

500 """ 

501 if self.editable and not self.supports_pyproject_editable: 

502 raise InstallationError( 

503 f"Project {self} uses a build backend " 

504 f"that is missing the 'build_editable' hook, so " 

505 f"it cannot be installed in editable mode. " 

506 f"Consider using a build backend that supports PEP 660." 

507 ) 

508 

509 def prepare_metadata(self) -> None: 

510 """Ensure that project metadata is available. 

511 

512 Under PEP 517 and PEP 660, call the backend hook to prepare the metadata. 

513 Under legacy processing, call setup.py egg-info. 

514 """ 

515 assert self.source_dir, f"No source dir for {self}" 

516 details = self.name or f"from {self.link}" 

517 

518 assert self.pep517_backend is not None 

519 if ( 

520 self.editable 

521 and self.permit_editable_wheels 

522 and self.supports_pyproject_editable 

523 ): 

524 self.metadata_directory = generate_editable_metadata( 

525 build_env=self.build_env, 

526 backend=self.pep517_backend, 

527 details=details, 

528 ) 

529 else: 

530 self.metadata_directory = generate_metadata( 

531 build_env=self.build_env, 

532 backend=self.pep517_backend, 

533 details=details, 

534 ) 

535 

536 # Act on the newly generated metadata, based on the name and version. 

537 if not self.name: 

538 self._set_requirement() 

539 else: 

540 self.warn_on_mismatching_name() 

541 

542 self.assert_source_matches_version() 

543 

544 @property 

545 def metadata(self) -> Any: 

546 if not hasattr(self, "_metadata"): 

547 self._metadata = self.get_dist().metadata 

548 

549 return self._metadata 

550 

551 def set_dist(self, distribution: BaseDistribution) -> None: 

552 self._distribution = distribution 

553 

554 def get_dist(self) -> BaseDistribution: 

555 if self._distribution is not None: 

556 return self._distribution 

557 elif self.metadata_directory: 

558 return get_directory_distribution(self.metadata_directory) 

559 elif self.local_file_path and self.is_wheel: 

560 assert self.req is not None 

561 return get_wheel_distribution( 

562 FilesystemWheel(self.local_file_path), 

563 canonicalize_name(self.req.name), 

564 ) 

565 raise AssertionError( 

566 f"InstallRequirement {self} has no metadata directory and no wheel: " 

567 f"can't make a distribution." 

568 ) 

569 

570 def assert_source_matches_version(self) -> None: 

571 assert self.source_dir, f"No source dir for {self}" 

572 version = self.metadata["version"] 

573 if self.req and self.req.specifier and version not in self.req.specifier: 

574 logger.warning( 

575 "Requested %s, but installing version %s", 

576 self, 

577 version, 

578 ) 

579 else: 

580 logger.debug( 

581 "Source in %s has version %s, which satisfies requirement %s", 

582 display_path(self.source_dir), 

583 version, 

584 self, 

585 ) 

586 

587 # For both source distributions and editables 

588 def ensure_has_source_dir( 

589 self, 

590 parent_dir: str, 

591 autodelete: bool = False, 

592 parallel_builds: bool = False, 

593 ) -> None: 

594 """Ensure that a source_dir is set. 

595 

596 This will create a temporary build dir if the name of the requirement 

597 isn't known yet. 

598 

599 :param parent_dir: The ideal pip parent_dir for the source_dir. 

600 Generally src_dir for editables and build_dir for sdists. 

601 :return: self.source_dir 

602 """ 

603 if self.source_dir is None: 

604 self.source_dir = self.ensure_build_location( 

605 parent_dir, 

606 autodelete=autodelete, 

607 parallel_builds=parallel_builds, 

608 ) 

609 

610 def needs_unpacked_archive(self, archive_source: Path) -> None: 

611 assert self._archive_source is None 

612 self._archive_source = archive_source 

613 

614 def ensure_pristine_source_checkout(self) -> None: 

615 """Ensure the source directory has not yet been built in.""" 

616 assert self.source_dir is not None 

617 if self._archive_source is not None: 

618 unpack_file(str(self._archive_source), self.source_dir) 

619 elif is_installable_dir(self.source_dir): 

620 # If a checkout exists, it's unwise to keep going. 

621 # version inconsistencies are logged later, but do not fail 

622 # the installation. 

623 raise PreviousBuildDirError( 

624 f"pip can't proceed with requirements '{self}' due to a " 

625 f"pre-existing build directory ({self.source_dir}). This is likely " 

626 "due to a previous installation that failed . pip is " 

627 "being responsible and not assuming it can delete this. " 

628 "Please delete it and try again." 

629 ) 

630 

631 # For editable installations 

632 def update_editable(self) -> None: 

633 if not self.link: 

634 logger.debug( 

635 "Cannot update repository at %s; repository location is unknown", 

636 self.source_dir, 

637 ) 

638 return 

639 assert self.editable 

640 assert self.source_dir 

641 if self.link.scheme == "file": 

642 # Static paths don't get updated 

643 return 

644 vcs_backend = vcs.get_backend_for_scheme(self.link.scheme) 

645 # Editable requirements are validated in Requirement constructors. 

646 # So here, if it's neither a path nor a valid VCS URL, it's a bug. 

647 assert vcs_backend, f"Unsupported VCS URL {self.link.url}" 

648 hidden_url = hide_url(self.link.url) 

649 vcs_backend.obtain(self.source_dir, url=hidden_url, verbosity=0) 

650 

651 # Top-level Actions 

652 def uninstall( 

653 self, auto_confirm: bool = False, verbose: bool = False 

654 ) -> UninstallPathSet | None: 

655 """ 

656 Uninstall the distribution currently satisfying this requirement. 

657 

658 Prompts before removing or modifying files unless 

659 ``auto_confirm`` is True. 

660 

661 Refuses to delete or modify files outside of ``sys.prefix`` - 

662 thus uninstallation within a virtual environment can only 

663 modify that virtual environment, even if the virtualenv is 

664 linked to global site-packages. 

665 

666 """ 

667 assert self.req 

668 dist = get_default_environment().get_distribution(self.req.name) 

669 if not dist: 

670 logger.warning("Skipping %s as it is not installed.", self.name) 

671 return None 

672 logger.info("Found existing installation: %s", dist) 

673 

674 uninstalled_pathset = UninstallPathSet.from_dist(dist) 

675 uninstalled_pathset.remove(auto_confirm, verbose) 

676 return uninstalled_pathset 

677 

678 def _get_archive_name(self, path: str, parentdir: str, rootdir: str) -> str: 

679 def _clean_zip_name(name: str, prefix: str) -> str: 

680 assert name.startswith( 

681 prefix + os.path.sep 

682 ), f"name {name!r} doesn't start with prefix {prefix!r}" 

683 name = name[len(prefix) + 1 :] 

684 name = name.replace(os.path.sep, "/") 

685 return name 

686 

687 assert self.req is not None 

688 path = os.path.join(parentdir, path) 

689 name = _clean_zip_name(path, rootdir) 

690 return self.req.name + "/" + name 

691 

692 def archive(self, build_dir: str | None) -> None: 

693 """Saves archive to provided build_dir. 

694 

695 Used for saving downloaded VCS requirements as part of `pip download`. 

696 """ 

697 assert self.source_dir 

698 if build_dir is None: 

699 return 

700 

701 create_archive = True 

702 archive_name = "{}-{}.zip".format(self.name, self.metadata["version"]) 

703 archive_path = os.path.join(build_dir, archive_name) 

704 

705 if os.path.exists(archive_path): 

706 response = ask_path_exists( 

707 f"The file {display_path(archive_path)} exists. (i)gnore, (w)ipe, " 

708 "(b)ackup, (a)bort ", 

709 ("i", "w", "b", "a"), 

710 ) 

711 if response == "i": 

712 create_archive = False 

713 elif response == "w": 

714 logger.warning("Deleting %s", display_path(archive_path)) 

715 os.remove(archive_path) 

716 elif response == "b": 

717 dest_file = backup_dir(archive_path) 

718 logger.warning( 

719 "Backing up %s to %s", 

720 display_path(archive_path), 

721 display_path(dest_file), 

722 ) 

723 shutil.move(archive_path, dest_file) 

724 elif response == "a": 

725 sys.exit(-1) 

726 

727 if not create_archive: 

728 return 

729 

730 zip_output = zipfile.ZipFile( 

731 archive_path, 

732 "w", 

733 zipfile.ZIP_DEFLATED, 

734 allowZip64=True, 

735 ) 

736 with zip_output: 

737 dir = os.path.normcase(os.path.abspath(self.unpacked_source_directory)) 

738 for dirpath, dirnames, filenames in os.walk(dir): 

739 for dirname in dirnames: 

740 dir_arcname = self._get_archive_name( 

741 dirname, 

742 parentdir=dirpath, 

743 rootdir=dir, 

744 ) 

745 zipdir = zipfile.ZipInfo(dir_arcname + "/") 

746 zipdir.external_attr = 0x1ED << 16 # 0o755 

747 zip_output.writestr(zipdir, "") 

748 for filename in filenames: 

749 file_arcname = self._get_archive_name( 

750 filename, 

751 parentdir=dirpath, 

752 rootdir=dir, 

753 ) 

754 filename = os.path.join(dirpath, filename) 

755 zip_output.write(filename, file_arcname) 

756 

757 logger.info("Saved %s", display_path(archive_path)) 

758 

759 def install( 

760 self, 

761 root: str | None = None, 

762 home: str | None = None, 

763 prefix: str | None = None, 

764 warn_script_location: bool = True, 

765 use_user_site: bool = False, 

766 pycompile: bool = True, 

767 ) -> None: 

768 assert self.req is not None 

769 scheme = get_scheme( 

770 self.req.name, 

771 user=use_user_site, 

772 home=home, 

773 root=root, 

774 isolated=self.isolated, 

775 prefix=prefix, 

776 ) 

777 

778 assert self.is_wheel 

779 assert self.local_file_path 

780 

781 install_wheel( 

782 self.req.name, 

783 self.local_file_path, 

784 scheme=scheme, 

785 req_description=str(self.req), 

786 pycompile=pycompile, 

787 warn_script_location=warn_script_location, 

788 direct_url=self.download_info if self.is_direct else None, 

789 requested=self.user_supplied, 

790 ) 

791 self.install_succeeded = True 

792 

793 

794def check_invalid_constraint_type(req: InstallRequirement) -> str: 

795 # Check for unsupported forms 

796 problem = "" 

797 if not req.name: 

798 problem = "Unnamed requirements are not allowed as constraints" 

799 elif req.editable: 

800 problem = "Editable requirements are not allowed as constraints" 

801 elif req.extras: 

802 problem = "Constraints cannot have extras" 

803 

804 if problem: 

805 deprecated( 

806 reason=( 

807 "Constraints are only allowed to take the form of a package " 

808 "name and a version specifier. Other forms were originally " 

809 "permitted as an accident of the implementation, but were " 

810 "undocumented. The new implementation of the resolver no " 

811 "longer supports these forms." 

812 ), 

813 replacement="replacing the constraint with a requirement", 

814 # No plan yet for when the new resolver becomes default 

815 gone_in=None, 

816 issue=8210, 

817 ) 

818 

819 return problem 

820 

821 

822def _has_option(options: Values, reqs: list[InstallRequirement], option: str) -> bool: 

823 if getattr(options, option, None): 

824 return True 

825 for req in reqs: 

826 if getattr(req, option, None): 

827 return True 

828 return False