Let us walk on the 3-isogeny graph
Loading...
Searching...
No Matches
direct_url_helpers.py
Go to the documentation of this file.
1from typing import Optional
2
3from pip._internal.models.direct_url import ArchiveInfo, DirectUrl, DirInfo, VcsInfo
4from pip._internal.models.link import Link
5from pip._internal.utils.urls import path_to_url
6from pip._internal.vcs import vcs
7
8
9def direct_url_as_pep440_direct_reference(direct_url: DirectUrl, name: str) -> str:
10 """Convert a DirectUrl to a pip requirement string."""
11 direct_url.validate() # if invalid, this is a pip bug
12 requirement = name + " @ "
13 fragments = []
14 if isinstance(direct_url.info, VcsInfo):
15 requirement += "{}+{}@{}".format(
17 )
18 elif isinstance(direct_url.info, ArchiveInfo):
19 requirement += direct_url.url
22 else:
23 assert isinstance(direct_url.info, DirInfo)
24 requirement += direct_url.url
27 if fragments:
28 requirement += "#" + "&".join(fragments)
29 return requirement
30
31
32def direct_url_for_editable(source_dir: str) -> DirectUrl:
33 return DirectUrl(
34 url=path_to_url(source_dir),
35 info=DirInfo(editable=True),
36 )
37
38
39def direct_url_from_link(
40 link: Link, source_dir: Optional[str] = None, link_is_in_wheel_cache: bool = False
41) -> DirectUrl:
42 if link.is_vcs:
44 assert vcs_backend
45 url, requested_revision, _ = vcs_backend.get_url_rev_and_auth(
47 )
48 # For VCS links, we need to find out and add commit_id.
49 if link_is_in_wheel_cache:
50 # If the requested VCS link corresponds to a cached
51 # wheel, it means the requested revision was an
52 # immutable commit hash, otherwise it would not have
53 # been cached. In that case we don't have a source_dir
54 # with the VCS checkout.
55 assert requested_revision
56 commit_id = requested_revision
57 else:
58 # If the wheel was not in cache, it means we have
59 # had to checkout from VCS to build and we have a source_dir
60 # which we can inspect to find out the commit id.
61 assert source_dir
62 commit_id = vcs_backend.get_revision(source_dir)
63 return DirectUrl(
64 url=url,
65 info=VcsInfo(
67 commit_id=commit_id,
68 requested_revision=requested_revision,
69 ),
70 subdirectory=link.subdirectory_fragment,
71 )
73 return DirectUrl(
75 info=DirInfo(),
76 subdirectory=link.subdirectory_fragment,
77 )
78 else:
79 hash = None
80 hash_name = link.hash_name
81 if hash_name:
82 hash = f"{hash_name}={link.hash}"
83 return DirectUrl(
85 info=ArchiveInfo(hash=hash),
86 subdirectory=link.subdirectory_fragment,
87 )
for i