Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 30 additions & 29 deletions crates/uv-cli/src/options.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::env;
use std::error::Error;
use std::fmt;

Expand Down Expand Up @@ -231,7 +232,7 @@ impl TryFrom<RefreshArgs> for Refresh {
}

/// Extract the `--index` and `--default-index` values from [`IndexArgs`].
pub fn indexes_from_args(
fn indexes_from_args(
default_index: Option<&Maybe<Index>>,
index: Option<&[Vec<Maybe<Index>>]>,
) -> Option<Vec<Index>> {
Expand Down Expand Up @@ -319,7 +320,7 @@ impl TryFrom<ResolverArgs> for PipOptions {
} else {
Some(no_sources_package)
},
..Self::from(index_args)
..Self::try_from(index_args)?
})
}
}
Expand Down Expand Up @@ -380,7 +381,7 @@ impl TryFrom<InstallerArgs> for PipOptions {
} else {
Some(no_sources_package)
},
..Self::from(index_args)
..Self::try_from(index_args)?
})
}
}
Expand Down Expand Up @@ -467,13 +468,15 @@ impl TryFrom<ResolverInstallerArgs> for PipOptions {
} else {
Some(no_sources_package)
},
..Self::from(index_args)
..Self::try_from(index_args)?
})
}
}

impl From<FetchArgs> for PipOptions {
fn from(args: FetchArgs) -> Self {
impl TryFrom<FetchArgs> for PipOptions {
type Error = anyhow::Error;

fn try_from(args: FetchArgs) -> anyhow::Result<Self> {
let FetchArgs {
index_args,
registry_client:
Expand All @@ -484,17 +487,19 @@ impl From<FetchArgs> for PipOptions {
exclude_newer,
} = args;

Self {
Ok(Self {
index_strategy,
keyring_provider,
exclude_newer,
..Self::from(index_args)
}
..Self::try_from(index_args)?
})
}
}

impl From<IndexArgs> for PipOptions {
fn from(args: IndexArgs) -> Self {
impl TryFrom<IndexArgs> for PipOptions {
type Error = anyhow::Error;

fn try_from(args: IndexArgs) -> anyhow::Result<Self> {
let IndexArgs {
default_index,
index,
Expand Down Expand Up @@ -522,6 +527,8 @@ impl From<IndexArgs> for PipOptions {
}),
..Self::default()
}
.relative_to(&env::current_dir()?)
.map_err(Into::into)
}
}

Expand Down Expand Up @@ -566,7 +573,7 @@ pub fn resolver_options(
no_binary_package,
} = build_args;

Ok(ResolverOptions {
ResolverOptions {
index: indexes_from_args(
index_args.default_index.as_ref(),
index_args.index.as_deref(),
Expand Down Expand Up @@ -639,26 +646,15 @@ pub fn resolver_options(
} else {
Some(no_sources_package)
},
})
}
.relative_to(&env::current_dir()?)
.map_err(Into::into)
}

/// Construct the [`ResolverInstallerOptions`] from the [`ResolverInstallerArgs`] and [`BuildOptionsArgs`].
pub fn resolver_installer_options(
resolver_installer_args: ResolverInstallerArgs,
build_args: BuildOptionsArgs,
) -> anyhow::Result<ResolverInstallerOptions> {
let index = indexes_from_args(
resolver_installer_args.index_args.default_index.as_ref(),
resolver_installer_args.index_args.index.as_deref(),
);
resolver_installer_options_with_indexes(resolver_installer_args, build_args, index)
}

/// Construct the [`ResolverInstallerOptions`] with a precomputed list of indexes.
pub fn resolver_installer_options_with_indexes(
resolver_installer_args: ResolverInstallerArgs,
build_args: BuildOptionsArgs,
index: Option<Vec<Index>>,
) -> anyhow::Result<ResolverInstallerOptions> {
let ResolverInstallerArgs {
index_args,
Expand Down Expand Up @@ -707,8 +703,11 @@ pub fn resolver_installer_options_with_indexes(
no_binary_package,
} = build_args;

Ok(ResolverInstallerOptions {
index,
ResolverInstallerOptions {
index: indexes_from_args(
index_args.default_index.as_ref(),
index_args.index.as_deref(),
),
index_url: index_args.index_url.and_then(Maybe::into_option),
extra_index_url: index_args.extra_index_url.map(|extra_index_url| {
extra_index_url
Expand Down Expand Up @@ -782,5 +781,7 @@ pub fn resolver_installer_options_with_indexes(
Some(no_sources_package)
},
torch_backend: None,
})
}
.relative_to(&env::current_dir()?)
.map_err(Into::into)
}
163 changes: 93 additions & 70 deletions crates/uv-settings/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,49 @@ impl TryFrom<GlobalOptionsWire> for GlobalOptions {
}
}

/// Resolve registry indexes and find-links relative to the given root directory.
fn rebase_indexes(
root_dir: &Path,
indexes: &mut Option<Vec<Index>>,
index_url: &mut Option<PipIndex>,
extra_index_urls: &mut Option<Vec<PipExtraIndex>>,
find_links: &mut Option<Vec<PipFindLinks>>,
) -> Result<(), IndexUrlError> {
*indexes = indexes
.take()
.map(|indexes| {
indexes
.into_iter()
.map(|index| index.relative_to(root_dir))
.collect::<Result<Vec<_>, _>>()
})
.transpose()?;
*index_url = index_url
.take()
.map(|index| index.relative_to(root_dir))
.transpose()?;
*extra_index_urls = extra_index_urls
.take()
.map(|indexes| {
indexes
.into_iter()
.map(|index| index.relative_to(root_dir))
.collect::<Result<Vec<_>, _>>()
})
.transpose()?;
*find_links = find_links
.take()
.map(|find_links| {
find_links
.into_iter()
.map(|find_link| find_link.relative_to(root_dir))
.collect::<Result<Vec<_>, _>>()
})
.transpose()?;

Ok(())
}

/// Settings relevant to all installer operations.
#[derive(Debug, Clone, Default, CombineOptions)]
pub struct InstallerOptions {
Expand Down Expand Up @@ -566,6 +609,21 @@ pub struct ResolverOptions {
pub no_sources_package: Option<Vec<PackageName>>,
}

impl ResolverOptions {
/// Resolve the [`ResolverOptions`] relative to the given root directory.
pub fn relative_to(mut self, root_dir: &Path) -> Result<Self, IndexUrlError> {
rebase_indexes(
root_dir,
&mut self.index,
&mut self.index_url,
&mut self.extra_index_url,
&mut self.find_links,
)?;

Ok(self)
}
}

/// Shared settings, relevant to all operations that must resolve and install dependencies. The
/// union of [`InstallerOptions`] and [`ResolverOptions`].
#[derive(Debug, Clone, Default, CombineOptions)]
Expand Down Expand Up @@ -601,6 +659,21 @@ pub struct ResolverInstallerOptions {
pub no_binary_package: Option<Vec<PackageName>>,
}

impl ResolverInstallerOptions {
/// Resolve the [`ResolverInstallerOptions`] relative to the given root directory.
pub fn relative_to(mut self, root_dir: &Path) -> Result<Self, IndexUrlError> {
rebase_indexes(
root_dir,
&mut self.index,
&mut self.index_url,
&mut self.extra_index_url,
&mut self.find_links,
)?;

Ok(self)
}
}

impl From<ResolverInstallerSchema> for ResolverInstallerOptions {
fn from(value: ResolverInstallerSchema) -> Self {
let ResolverInstallerSchema {
Expand Down Expand Up @@ -684,41 +757,16 @@ impl From<ResolverInstallerSchema> for ResolverInstallerOptions {

impl ResolverInstallerSchema {
/// Resolve the [`ResolverInstallerSchema`] relative to the given root directory.
fn relative_to(self, root_dir: &Path) -> Result<Self, IndexUrlError> {
Ok(Self {
index: self
.index
.map(|index| {
index
.into_iter()
.map(|index| index.relative_to(root_dir))
.collect::<Result<Vec<_>, _>>()
})
.transpose()?,
index_url: self
.index_url
.map(|index_url| index_url.relative_to(root_dir))
.transpose()?,
extra_index_url: self
.extra_index_url
.map(|extra_index_url| {
extra_index_url
.into_iter()
.map(|extra_index_url| extra_index_url.relative_to(root_dir))
.collect::<Result<Vec<_>, _>>()
})
.transpose()?,
find_links: self
.find_links
.map(|find_links| {
find_links
.into_iter()
.map(|find_link| find_link.relative_to(root_dir))
.collect::<Result<Vec<_>, _>>()
})
.transpose()?,
..self
})
fn relative_to(mut self, root_dir: &Path) -> Result<Self, IndexUrlError> {
rebase_indexes(
root_dir,
&mut self.index,
&mut self.index_url,
&mut self.extra_index_url,
&mut self.find_links,
)?;

Ok(self)
}
}

Expand Down Expand Up @@ -2079,41 +2127,16 @@ pub struct PipOptions {

impl PipOptions {
/// Resolve the [`PipOptions`] relative to the given root directory.
fn relative_to(self, root_dir: &Path) -> Result<Self, IndexUrlError> {
Ok(Self {
index: self
.index
.map(|index| {
index
.into_iter()
.map(|index| index.relative_to(root_dir))
.collect::<Result<Vec<_>, _>>()
})
.transpose()?,
index_url: self
.index_url
.map(|index_url| index_url.relative_to(root_dir))
.transpose()?,
extra_index_url: self
.extra_index_url
.map(|extra_index_url| {
extra_index_url
.into_iter()
.map(|extra_index_url| extra_index_url.relative_to(root_dir))
.collect::<Result<Vec<_>, _>>()
})
.transpose()?,
find_links: self
.find_links
.map(|find_links| {
find_links
.into_iter()
.map(|find_link| find_link.relative_to(root_dir))
.collect::<Result<Vec<_>, _>>()
})
.transpose()?,
..self
})
pub fn relative_to(mut self, root_dir: &Path) -> Result<Self, IndexUrlError> {
rebase_indexes(
root_dir,
&mut self.index,
&mut self.index_url,
&mut self.extra_index_url,
&mut self.find_links,
)?;

Ok(self)
}
}

Expand Down
Loading
Loading