Rename ancillary direct url types to parsed url (#3211)

Followup to #3187. Renaming only
This commit is contained in:
konsti 2024-04-23 16:51:23 +02:00 committed by GitHub
parent 645d0399fd
commit 4a49ff4372
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 54 additions and 56 deletions

View file

@ -7,11 +7,9 @@ use pep508_rs::VerbatimUrl;
use pypi_types::HashDigest; use pypi_types::HashDigest;
use uv_normalize::PackageName; use uv_normalize::PackageName;
use crate::direct_url::{LocalFileUrl, ParsedUrl};
use crate::hash::Hashed;
use crate::{ use crate::{
BuiltDist, Dist, DistributionMetadata, InstalledMetadata, InstalledVersion, Name, SourceDist, BuiltDist, Dist, DistributionMetadata, Hashed, InstalledMetadata, InstalledVersion, Name,
VersionOrUrl, ParsedLocalFileUrl, ParsedUrl, SourceDist, VersionOrUrl,
}; };
/// A built distribution (wheel) that exists in the local cache. /// A built distribution (wheel) that exists in the local cache.
@ -111,7 +109,7 @@ impl CachedDist {
Self::Url(dist) => { Self::Url(dist) => {
if dist.editable { if dist.editable {
assert_eq!(dist.url.scheme(), "file", "{}", dist.url); assert_eq!(dist.url.scheme(), "file", "{}", dist.url);
Ok(Some(ParsedUrl::LocalFile(LocalFileUrl { Ok(Some(ParsedUrl::LocalFile(ParsedLocalFileUrl {
url: dist.url.raw().clone(), url: dist.url.raw().clone(),
editable: dist.editable, editable: dist.editable,
}))) })))

View file

@ -47,7 +47,6 @@ use uv_normalize::PackageName;
pub use crate::any::*; pub use crate::any::*;
pub use crate::buildable::*; pub use crate::buildable::*;
pub use crate::cached::*; pub use crate::cached::*;
pub use crate::direct_url::*;
pub use crate::editable::*; pub use crate::editable::*;
pub use crate::error::*; pub use crate::error::*;
pub use crate::file::*; pub use crate::file::*;
@ -55,6 +54,7 @@ pub use crate::hash::*;
pub use crate::id::*; pub use crate::id::*;
pub use crate::index_url::*; pub use crate::index_url::*;
pub use crate::installed::*; pub use crate::installed::*;
pub use crate::parsed_url::*;
pub use crate::prioritized_distribution::*; pub use crate::prioritized_distribution::*;
pub use crate::resolution::*; pub use crate::resolution::*;
pub use crate::resolved::*; pub use crate::resolved::*;
@ -63,7 +63,6 @@ pub use crate::traits::*;
mod any; mod any;
mod buildable; mod buildable;
mod cached; mod cached;
mod direct_url;
mod editable; mod editable;
mod error; mod error;
mod file; mod file;
@ -71,6 +70,7 @@ mod hash;
mod id; mod id;
mod index_url; mod index_url;
mod installed; mod installed;
mod parsed_url;
mod prioritized_distribution; mod prioritized_distribution;
mod resolution; mod resolution;
mod resolved; mod resolved;

View file

@ -6,7 +6,7 @@ use url::Url;
use uv_git::{GitSha, GitUrl}; use uv_git::{GitSha, GitUrl};
#[derive(Debug, Error)] #[derive(Debug, Error)]
pub enum DirectUrlError { pub enum ParsedUrlError {
#[error("Unsupported URL prefix `{prefix}` in URL: `{url}`")] #[error("Unsupported URL prefix `{prefix}` in URL: `{url}`")]
UnsupportedUrlPrefix { prefix: String, url: Url }, UnsupportedUrlPrefix { prefix: String, url: Url },
#[error("Invalid path in file URL: `{0}`")] #[error("Invalid path in file URL: `{0}`")]
@ -28,11 +28,11 @@ pub enum DirectUrlError {
#[derive(Debug)] #[derive(Debug)]
pub enum ParsedUrl { pub enum ParsedUrl {
/// The direct URL is a path to a local directory or file. /// The direct URL is a path to a local directory or file.
LocalFile(LocalFileUrl), LocalFile(ParsedLocalFileUrl),
/// The direct URL is path to a Git repository. /// The direct URL is path to a Git repository.
Git(DirectGitUrl), Git(ParsedGitUrl),
/// The direct URL is a URL to an archive. /// The direct URL is a URL to an archive.
Archive(DirectArchiveUrl), Archive(ParsedArchiveUrl),
} }
/// A local path url /// A local path url
@ -40,7 +40,7 @@ pub enum ParsedUrl {
/// Examples: /// Examples:
/// * `file:///home/ferris/my_project` /// * `file:///home/ferris/my_project`
#[derive(Debug)] #[derive(Debug)]
pub struct LocalFileUrl { pub struct ParsedLocalFileUrl {
pub url: Url, pub url: Url,
pub editable: bool, pub editable: bool,
} }
@ -51,7 +51,7 @@ pub struct LocalFileUrl {
/// * `git+https://git.example.com/MyProject.git` /// * `git+https://git.example.com/MyProject.git`
/// * `git+https://git.example.com/MyProject.git@v1.0#egg=pkg&subdirectory=pkg_dir` /// * `git+https://git.example.com/MyProject.git@v1.0#egg=pkg&subdirectory=pkg_dir`
#[derive(Debug)] #[derive(Debug)]
pub struct DirectGitUrl { pub struct ParsedGitUrl {
pub url: GitUrl, pub url: GitUrl,
pub subdirectory: Option<PathBuf>, pub subdirectory: Option<PathBuf>,
} }
@ -63,13 +63,13 @@ pub struct DirectGitUrl {
/// * source dist, correctly named: `https://files.pythonhosted.org/packages/62/06/d5604a70d160f6a6ca5fd2ba25597c24abd5c5ca5f437263d177ac242308/tqdm-4.66.1.tar.gz` /// * source dist, correctly named: `https://files.pythonhosted.org/packages/62/06/d5604a70d160f6a6ca5fd2ba25597c24abd5c5ca5f437263d177ac242308/tqdm-4.66.1.tar.gz`
/// * source dist, only extension recognizable: `https://github.com/foo-labs/foo/archive/master.zip#egg=pkg&subdirectory=packages/bar` /// * source dist, only extension recognizable: `https://github.com/foo-labs/foo/archive/master.zip#egg=pkg&subdirectory=packages/bar`
#[derive(Debug)] #[derive(Debug)]
pub struct DirectArchiveUrl { pub struct ParsedArchiveUrl {
pub url: Url, pub url: Url,
pub subdirectory: Option<PathBuf>, pub subdirectory: Option<PathBuf>,
} }
impl TryFrom<&Url> for DirectGitUrl { impl TryFrom<&Url> for ParsedGitUrl {
type Error = DirectUrlError; type Error = ParsedUrlError;
fn try_from(url_in: &Url) -> Result<Self, Self::Error> { fn try_from(url_in: &Url) -> Result<Self, Self::Error> {
let subdirectory = get_subdirectory(url_in); let subdirectory = get_subdirectory(url_in);
@ -77,15 +77,15 @@ impl TryFrom<&Url> for DirectGitUrl {
let url = url_in let url = url_in
.as_str() .as_str()
.strip_prefix("git+") .strip_prefix("git+")
.ok_or_else(|| DirectUrlError::MissingUrlPrefix(url_in.clone()))?; .ok_or_else(|| ParsedUrlError::MissingUrlPrefix(url_in.clone()))?;
let url = Url::parse(url).map_err(|err| DirectUrlError::UrlParse(url.to_string(), err))?; let url = Url::parse(url).map_err(|err| ParsedUrlError::UrlParse(url.to_string(), err))?;
let url = GitUrl::try_from(url) let url = GitUrl::try_from(url)
.map_err(|err| DirectUrlError::GitShaParse(url_in.clone(), err))?; .map_err(|err| ParsedUrlError::GitShaParse(url_in.clone(), err))?;
Ok(Self { url, subdirectory }) Ok(Self { url, subdirectory })
} }
} }
impl From<&Url> for DirectArchiveUrl { impl From<&Url> for ParsedArchiveUrl {
fn from(url: &Url) -> Self { fn from(url: &Url) -> Self {
Self { Self {
url: url.clone(), url: url.clone(),
@ -110,29 +110,29 @@ fn get_subdirectory(url: &Url) -> Option<PathBuf> {
/// Return the Git reference of the given URL, if it exists. /// Return the Git reference of the given URL, if it exists.
pub fn git_reference(url: &Url) -> Result<Option<GitSha>, Error> { pub fn git_reference(url: &Url) -> Result<Option<GitSha>, Error> {
let DirectGitUrl { url, .. } = DirectGitUrl::try_from(url)?; let ParsedGitUrl { url, .. } = ParsedGitUrl::try_from(url)?;
Ok(url.precise()) Ok(url.precise())
} }
impl TryFrom<&Url> for ParsedUrl { impl TryFrom<&Url> for ParsedUrl {
type Error = DirectUrlError; type Error = ParsedUrlError;
fn try_from(url: &Url) -> Result<Self, Self::Error> { fn try_from(url: &Url) -> Result<Self, Self::Error> {
if let Some((prefix, ..)) = url.scheme().split_once('+') { if let Some((prefix, ..)) = url.scheme().split_once('+') {
match prefix { match prefix {
"git" => Ok(Self::Git(DirectGitUrl::try_from(url)?)), "git" => Ok(Self::Git(ParsedGitUrl::try_from(url)?)),
_ => Err(DirectUrlError::UnsupportedUrlPrefix { _ => Err(ParsedUrlError::UnsupportedUrlPrefix {
prefix: prefix.to_string(), prefix: prefix.to_string(),
url: url.clone(), url: url.clone(),
}), }),
} }
} else if url.scheme().eq_ignore_ascii_case("file") { } else if url.scheme().eq_ignore_ascii_case("file") {
Ok(Self::LocalFile(LocalFileUrl { Ok(Self::LocalFile(ParsedLocalFileUrl {
url: url.clone(), url: url.clone(),
editable: false, editable: false,
})) }))
} else { } else {
Ok(Self::Archive(DirectArchiveUrl::from(url))) Ok(Self::Archive(ParsedArchiveUrl::from(url)))
} }
} }
} }
@ -149,10 +149,10 @@ impl TryFrom<&ParsedUrl> for pypi_types::DirectUrl {
} }
} }
impl TryFrom<&LocalFileUrl> for pypi_types::DirectUrl { impl TryFrom<&ParsedLocalFileUrl> for pypi_types::DirectUrl {
type Error = Error; type Error = Error;
fn try_from(value: &LocalFileUrl) -> Result<Self, Self::Error> { fn try_from(value: &ParsedLocalFileUrl) -> Result<Self, Self::Error> {
Ok(Self::LocalDirectory { Ok(Self::LocalDirectory {
url: value.url.to_string(), url: value.url.to_string(),
dir_info: pypi_types::DirInfo { dir_info: pypi_types::DirInfo {
@ -162,10 +162,10 @@ impl TryFrom<&LocalFileUrl> for pypi_types::DirectUrl {
} }
} }
impl TryFrom<&DirectArchiveUrl> for pypi_types::DirectUrl { impl TryFrom<&ParsedArchiveUrl> for pypi_types::DirectUrl {
type Error = Error; type Error = Error;
fn try_from(value: &DirectArchiveUrl) -> Result<Self, Self::Error> { fn try_from(value: &ParsedArchiveUrl) -> Result<Self, Self::Error> {
Ok(Self::ArchiveUrl { Ok(Self::ArchiveUrl {
url: value.url.to_string(), url: value.url.to_string(),
archive_info: pypi_types::ArchiveInfo { archive_info: pypi_types::ArchiveInfo {
@ -177,10 +177,10 @@ impl TryFrom<&DirectArchiveUrl> for pypi_types::DirectUrl {
} }
} }
impl TryFrom<&DirectGitUrl> for pypi_types::DirectUrl { impl TryFrom<&ParsedGitUrl> for pypi_types::DirectUrl {
type Error = Error; type Error = Error;
fn try_from(value: &DirectGitUrl) -> Result<Self, Self::Error> { fn try_from(value: &ParsedGitUrl) -> Result<Self, Self::Error> {
Ok(Self::VcsUrl { Ok(Self::VcsUrl {
url: value.url.repository().to_string(), url: value.url.repository().to_string(),
vcs_info: pypi_types::VcsInfo { vcs_info: pypi_types::VcsInfo {
@ -203,14 +203,14 @@ impl From<ParsedUrl> for Url {
} }
} }
impl From<LocalFileUrl> for Url { impl From<ParsedLocalFileUrl> for Url {
fn from(value: LocalFileUrl) -> Self { fn from(value: ParsedLocalFileUrl) -> Self {
value.url value.url
} }
} }
impl From<DirectArchiveUrl> for Url { impl From<ParsedArchiveUrl> for Url {
fn from(value: DirectArchiveUrl) -> Self { fn from(value: ParsedArchiveUrl) -> Self {
let mut url = value.url; let mut url = value.url;
if let Some(subdirectory) = value.subdirectory { if let Some(subdirectory) = value.subdirectory {
url.set_fragment(Some(&format!("subdirectory={}", subdirectory.display()))); url.set_fragment(Some(&format!("subdirectory={}", subdirectory.display())));
@ -219,8 +219,8 @@ impl From<DirectArchiveUrl> for Url {
} }
} }
impl From<DirectGitUrl> for Url { impl From<ParsedGitUrl> for Url {
fn from(value: DirectGitUrl) -> Self { fn from(value: ParsedGitUrl) -> Self {
let mut url = Self::parse(&format!("{}{}", "git+", Self::from(value.url).as_str())) let mut url = Self::parse(&format!("{}{}", "git+", Self::from(value.url).as_str()))
.expect("Git URL is invalid"); .expect("Git URL is invalid");
if let Some(subdirectory) = value.subdirectory { if let Some(subdirectory) = value.subdirectory {
@ -235,7 +235,7 @@ mod tests {
use anyhow::Result; use anyhow::Result;
use url::Url; use url::Url;
use crate::direct_url::ParsedUrl; use crate::parsed_url::ParsedUrl;
#[test] #[test]
fn direct_url_from_url() -> Result<()> { fn direct_url_from_url() -> Result<()> {

View file

@ -3,7 +3,7 @@ use tokio::task::JoinError;
use zip::result::ZipError; use zip::result::ZipError;
use distribution_filename::WheelFilenameError; use distribution_filename::WheelFilenameError;
use distribution_types::DirectUrlError; use distribution_types::ParsedUrlError;
use pep440_rs::Version; use pep440_rs::Version;
use pypi_types::HashDigest; use pypi_types::HashDigest;
use uv_client::BetterReqwestError; use uv_client::BetterReqwestError;
@ -24,7 +24,7 @@ pub enum Error {
#[error("Git operation failed")] #[error("Git operation failed")]
Git(#[source] anyhow::Error), Git(#[source] anyhow::Error),
#[error(transparent)] #[error(transparent)]
DirectUrl(#[from] Box<DirectUrlError>), DirectUrl(#[from] Box<ParsedUrlError>),
#[error(transparent)] #[error(transparent)]
Reqwest(#[from] BetterReqwestError), Reqwest(#[from] BetterReqwestError),
#[error(transparent)] #[error(transparent)]

View file

@ -9,7 +9,7 @@ use tracing::debug;
use url::Url; use url::Url;
use cache_key::{CanonicalUrl, RepositoryUrl}; use cache_key::{CanonicalUrl, RepositoryUrl};
use distribution_types::DirectGitUrl; use distribution_types::ParsedGitUrl;
use uv_cache::{Cache, CacheBucket}; use uv_cache::{Cache, CacheBucket};
use uv_fs::LockedFile; use uv_fs::LockedFile;
use uv_git::{Fetch, GitReference, GitSha, GitSource, GitUrl}; use uv_git::{Fetch, GitReference, GitSha, GitSource, GitUrl};
@ -67,7 +67,7 @@ pub(crate) async fn fetch_git_archive(
) )
.map_err(Error::CacheWrite)?; .map_err(Error::CacheWrite)?;
let DirectGitUrl { url, subdirectory } = DirectGitUrl::try_from(url).map_err(Box::new)?; let ParsedGitUrl { url, subdirectory } = ParsedGitUrl::try_from(url).map_err(Box::new)?;
// Fetch the Git repository. // Fetch the Git repository.
let source = if let Some(reporter) = reporter { let source = if let Some(reporter) = reporter {
@ -95,7 +95,7 @@ pub(crate) async fn resolve_precise(
cache: &Cache, cache: &Cache,
reporter: Option<&Arc<dyn Reporter>>, reporter: Option<&Arc<dyn Reporter>>,
) -> Result<Option<Url>, Error> { ) -> Result<Option<Url>, Error> {
let DirectGitUrl { url, subdirectory } = DirectGitUrl::try_from(url).map_err(Box::new)?; let ParsedGitUrl { url, subdirectory } = ParsedGitUrl::try_from(url).map_err(Box::new)?;
// If the Git reference already contains a complete SHA, short-circuit. // If the Git reference already contains a complete SHA, short-circuit.
if url.precise().is_some() { if url.precise().is_some() {
@ -107,7 +107,7 @@ pub(crate) async fn resolve_precise(
let resolved_git_refs = RESOLVED_GIT_REFS.lock().unwrap(); let resolved_git_refs = RESOLVED_GIT_REFS.lock().unwrap();
let reference = RepositoryReference::new(&url); let reference = RepositoryReference::new(&url);
if let Some(precise) = resolved_git_refs.get(&reference) { if let Some(precise) = resolved_git_refs.get(&reference) {
return Ok(Some(Url::from(DirectGitUrl { return Ok(Some(Url::from(ParsedGitUrl {
url: url.with_precise(*precise), url: url.with_precise(*precise),
subdirectory, subdirectory,
}))); })));
@ -136,7 +136,7 @@ pub(crate) async fn resolve_precise(
} }
// Re-encode as a URL. // Re-encode as a URL.
Ok(Some(Url::from(DirectGitUrl { Ok(Some(Url::from(ParsedGitUrl {
url: git, url: git,
subdirectory, subdirectory,
}))) })))
@ -154,11 +154,11 @@ pub(crate) async fn resolve_precise(
/// This method will only return precise URLs for URLs that have already been resolved via /// This method will only return precise URLs for URLs that have already been resolved via
/// [`resolve_precise`]. /// [`resolve_precise`].
pub fn to_precise(url: &Url) -> Option<Url> { pub fn to_precise(url: &Url) -> Option<Url> {
let DirectGitUrl { url, subdirectory } = DirectGitUrl::try_from(url).ok()?; let ParsedGitUrl { url, subdirectory } = ParsedGitUrl::try_from(url).ok()?;
let resolved_git_refs = RESOLVED_GIT_REFS.lock().unwrap(); let resolved_git_refs = RESOLVED_GIT_REFS.lock().unwrap();
let reference = RepositoryReference::new(&url); let reference = RepositoryReference::new(&url);
let precise = resolved_git_refs.get(&reference)?; let precise = resolved_git_refs.get(&reference)?;
Some(Url::from(DirectGitUrl { Some(Url::from(ParsedGitUrl {
url: url.with_precise(*precise), url: url.with_precise(*precise),
subdirectory, subdirectory,
})) }))
@ -182,12 +182,12 @@ fn is_same_reference_impl<'a>(
resolved_refs: &FxHashMap<RepositoryReference, GitSha>, resolved_refs: &FxHashMap<RepositoryReference, GitSha>,
) -> bool { ) -> bool {
// Convert `a` to a Git URL, if possible. // Convert `a` to a Git URL, if possible.
let Ok(a_git) = DirectGitUrl::try_from(&Url::from(CanonicalUrl::new(a))) else { let Ok(a_git) = ParsedGitUrl::try_from(&Url::from(CanonicalUrl::new(a))) else {
return false; return false;
}; };
// Convert `b` to a Git URL, if possible. // Convert `b` to a Git URL, if possible.
let Ok(b_git) = DirectGitUrl::try_from(&Url::from(CanonicalUrl::new(b))) else { let Ok(b_git) = ParsedGitUrl::try_from(&Url::from(CanonicalUrl::new(b))) else {
return false; return false;
}; };

View file

@ -16,8 +16,8 @@ use zip::ZipArchive;
use distribution_filename::WheelFilename; use distribution_filename::WheelFilename;
use distribution_types::{ use distribution_types::{
BuildableSource, DirectArchiveUrl, Dist, FileLocation, GitSourceUrl, HashPolicy, Hashed, BuildableSource, Dist, FileLocation, GitSourceUrl, HashPolicy, Hashed, LocalEditable,
LocalEditable, PathSourceDist, PathSourceUrl, RemoteSource, SourceDist, SourceUrl, ParsedArchiveUrl, PathSourceDist, PathSourceUrl, RemoteSource, SourceDist, SourceUrl,
}; };
use install_wheel_rs::metadata::read_archive_metadata; use install_wheel_rs::metadata::read_archive_metadata;
use platform_tags::Tags; use platform_tags::Tags;
@ -135,7 +135,7 @@ impl<'a, T: BuildContext> SourceDistributionBuilder<'a, T> {
} }
BuildableSource::Dist(SourceDist::DirectUrl(dist)) => { BuildableSource::Dist(SourceDist::DirectUrl(dist)) => {
let filename = dist.filename().expect("Distribution must have a filename"); let filename = dist.filename().expect("Distribution must have a filename");
let DirectArchiveUrl { url, subdirectory } = DirectArchiveUrl::from(dist.url.raw()); let ParsedArchiveUrl { url, subdirectory } = ParsedArchiveUrl::from(dist.url.raw());
// For direct URLs, cache directly under the hash of the URL itself. // For direct URLs, cache directly under the hash of the URL itself.
let cache_shard = self let cache_shard = self
@ -186,7 +186,7 @@ impl<'a, T: BuildContext> SourceDistributionBuilder<'a, T> {
.url .url
.filename() .filename()
.expect("Distribution must have a filename"); .expect("Distribution must have a filename");
let DirectArchiveUrl { url, subdirectory } = DirectArchiveUrl::from(resource.url); let ParsedArchiveUrl { url, subdirectory } = ParsedArchiveUrl::from(resource.url);
// For direct URLs, cache directly under the hash of the URL itself. // For direct URLs, cache directly under the hash of the URL itself.
let cache_shard = self let cache_shard = self
@ -284,7 +284,7 @@ impl<'a, T: BuildContext> SourceDistributionBuilder<'a, T> {
} }
BuildableSource::Dist(SourceDist::DirectUrl(dist)) => { BuildableSource::Dist(SourceDist::DirectUrl(dist)) => {
let filename = dist.filename().expect("Distribution must have a filename"); let filename = dist.filename().expect("Distribution must have a filename");
let DirectArchiveUrl { url, subdirectory } = DirectArchiveUrl::from(dist.url.raw()); let ParsedArchiveUrl { url, subdirectory } = ParsedArchiveUrl::from(dist.url.raw());
// For direct URLs, cache directly under the hash of the URL itself. // For direct URLs, cache directly under the hash of the URL itself.
let cache_shard = self let cache_shard = self
@ -328,7 +328,7 @@ impl<'a, T: BuildContext> SourceDistributionBuilder<'a, T> {
.url .url
.filename() .filename()
.expect("Distribution must have a filename"); .expect("Distribution must have a filename");
let DirectArchiveUrl { url, subdirectory } = DirectArchiveUrl::from(resource.url); let ParsedArchiveUrl { url, subdirectory } = ParsedArchiveUrl::from(resource.url);
// For direct URLs, cache directly under the hash of the URL itself. // For direct URLs, cache directly under the hash of the URL itself.
let cache_shard = self let cache_shard = self