Skip to content

Commit

Permalink
Isolate the one external use of raw_url.
Browse files Browse the repository at this point in the history
Outside of the Locker, the raw_url should not be needed except for the
single case of VCS artifacts, which need the extra data (fragment) to
form a valid requirement URL that Pip will accept for downloading at
lock resolve time.
  • Loading branch information
jsirois committed Feb 23, 2023
1 parent f567440 commit df8869a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 17 deletions.
2 changes: 1 addition & 1 deletion pex/resolve/downloads.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def _download_and_fingerprint(self, url):

def _to_file_artifact(self, artifact):
# type: (PartialArtifact) -> SpawnedJob[FileArtifact]
url = artifact.url.raw_url
url = artifact.url.normalized_url
fingerprint = artifact.fingerprint
if fingerprint:
return SpawnedJob.completed(
Expand Down
54 changes: 38 additions & 16 deletions pex/resolve/locked_resolve.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,10 @@ def from_artifact_url(
):
# type: (...) -> Union[FileArtifact, LocalProjectArtifact, VCSArtifact]
if isinstance(artifact_url.scheme, VCSScheme):
return VCSArtifact(
url=artifact_url.raw_url,
return VCSArtifact.from_artifact_url(
artifact_url=artifact_url,
fingerprint=fingerprint,
verified=verified,
vcs=artifact_url.scheme.vcs,
)

if "file" == artifact_url.scheme and os.path.isdir(artifact_url.path):
Expand Down Expand Up @@ -149,19 +148,6 @@ def from_url(
fingerprint = attr.ib() # type: Fingerprint
verified = attr.ib() # type: bool

def as_unparsed_requirement(self, project_name):
# type: (ProjectName) -> str
url_info = urlparse.urlparse(self.url)
if url_info.fragment:
fragment_parameters = urlparse.parse_qs(url_info.fragment)
names = fragment_parameters.get("egg")
if names and ProjectName(names[-1]) == project_name:
# A Pip proprietary VCS requirement.
return self.url
# A PEP-440 direct reference VCS requirement with the project name stripped from earlier
# processing. See: https://peps.python.org/pep-0440/#direct-references
return "{project_name} @ {url}".format(project_name=project_name, url=self.url)


@attr.s(frozen=True)
class FileArtifact(Artifact):
Expand Down Expand Up @@ -191,12 +177,48 @@ def is_source(self):

@attr.s(frozen=True)
class VCSArtifact(Artifact):
@classmethod
def from_artifact_url(
cls,
artifact_url, # type: ArtifactURL
fingerprint, # type: Fingerprint
verified=False, # type: bool
):
# type: (...) -> VCSArtifact
if not isinstance(artifact_url.scheme, VCSScheme):
raise ValueError(
"The given artifact URL is not that of a VCS artifact: {url}".format(
url=artifact_url.raw_url
)
)
return cls(
# N.B.: We need the raw URL in order to have access to the fragment needed for
# `as_unparsed_requirement`.
url=artifact_url.raw_url,
fingerprint=fingerprint,
verified=verified,
vcs=artifact_url.scheme.vcs,
)

vcs = attr.ib() # type: VCS.Value

@property
def is_source(self):
return True

def as_unparsed_requirement(self, project_name):
# type: (ProjectName) -> str
url_info = urlparse.urlparse(self.url)
if url_info.fragment:
fragment_parameters = urlparse.parse_qs(url_info.fragment)
names = fragment_parameters.get("egg")
if names and ProjectName(names[-1]) == project_name:
# A Pip proprietary VCS requirement.
return self.url
# A PEP-440 direct reference VCS requirement with the project name stripped from earlier
# processing. See: https://peps.python.org/pep-0440/#direct-references
return "{project_name} @ {url}".format(project_name=project_name, url=self.url)


@attr.s(frozen=True)
class RankedArtifact(object):
Expand Down

0 comments on commit df8869a

Please sign in to comment.