Skip to content

Commit

Permalink
Compiling with unified source
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Sep 27, 2024
1 parent 6a40f9a commit 4cffabe
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 91 deletions.
11 changes: 11 additions & 0 deletions crates/distribution-types/src/index_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,14 @@ pub struct IndexSource {
// /// An index containing a list of links to distributions (e.g., `--find-links`).
// Flat,
// }

impl From<IndexUrl> for IndexSource {
fn from(url: IndexUrl) -> Self {
Self {
url,
name: None,
explicit: false,
default: false,
}
}
}
80 changes: 14 additions & 66 deletions crates/distribution-types/src/index_url.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,24 +300,21 @@ impl From<VerbatimUrl> for FlatIndexLocation {
#[derive(Default, Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
pub struct IndexLocations {
sources: Vec<IndexSource>,
index: Option<IndexUrl>,
extra_index: Vec<IndexUrl>,
index: Option<IndexSource>,
extra_index: Vec<IndexSource>,
flat_index: Vec<FlatIndexLocation>,
no_index: bool,
}

impl IndexLocations {
/// Determine the index URLs to use for fetching packages.
pub fn new(
sources: Vec<IndexSource>,
index: Option<IndexUrl>,
extra_index: Vec<IndexUrl>,
index: Option<IndexSource>,
extra_index: Vec<IndexSource>,
flat_index: Vec<FlatIndexLocation>,
no_index: bool,
) -> Self {
Self {
sources,
index,
extra_index,
flat_index,
Expand All @@ -334,14 +331,12 @@ impl IndexLocations {
#[must_use]
pub fn combine(
self,
sources: Vec<IndexSource>,
index: Option<IndexUrl>,
extra_index: Vec<IndexUrl>,
index: Option<IndexSource>,
extra_index: Vec<IndexSource>,
flat_index: Vec<FlatIndexLocation>,
no_index: bool,
) -> Self {
Self {
sources: self.sources.into_iter().chain(sources).collect(),
index: self.index.or(index),
extra_index: self.extra_index.into_iter().chain(extra_index).collect(),
flat_index: self.flat_index.into_iter().chain(flat_index).collect(),
Expand All @@ -357,27 +352,6 @@ impl IndexLocations {
}

impl<'a> IndexLocations {
/// Return an iterator over the `tool.uv.index` sources, prioritizing the default index.
fn sources(&'a self) -> impl Iterator<Item = &'a IndexUrl> + 'a {
if self.no_index {
Either::Left(std::iter::empty())
} else {
Either::Right(
self.sources
.iter()
.filter(|source| !source.explicit)
.filter(|source| source.default)
.chain(
self.sources
.iter()
.filter(|source| !source.explicit)
.filter(|source| !source.default),
)
.map(|source| &source.url),
)
}
}

/// Return the primary [`IndexUrl`] entry.
///
/// If `--no-index` is set, return `None`.
Expand All @@ -388,8 +362,7 @@ impl<'a> IndexLocations {
None
} else {
match self.index.as_ref() {
Some(index) => Some(index),
None if self.sources.iter().any(|source| source.default) => None,
Some(index) => Some(&index.url),
None => Some(&DEFAULT_INDEX_URL),
}
}
Expand All @@ -400,7 +373,7 @@ impl<'a> IndexLocations {
if self.no_index {
Either::Left(std::iter::empty())
} else {
Either::Right(self.extra_index.iter())
Either::Right(self.extra_index.iter().map(|index| &index.url))
}
}

Expand All @@ -411,7 +384,7 @@ impl<'a> IndexLocations {
/// If `no_index` was enabled, then this always returns an empty
/// iterator.
pub fn indexes(&'a self) -> impl Iterator<Item = &'a IndexUrl> + 'a {
self.sources().chain(self.extra_index()).chain(self.index())
self.extra_index().chain(self.index())
}

/// Return an iterator over the [`FlatIndexLocation`] entries.
Expand All @@ -427,7 +400,6 @@ impl<'a> IndexLocations {
/// Clone the index locations into a [`IndexUrls`] instance.
pub fn index_urls(&'a self) -> IndexUrls {
IndexUrls {
sources: self.sources.clone(),
index: self.index.clone(),
extra_index: self.extra_index.clone(),
no_index: self.no_index,
Expand All @@ -447,34 +419,12 @@ impl<'a> IndexLocations {
/// From a pip perspective, this type merges `--index-url` and `--extra-index-url`.
#[derive(Default, Debug, Clone, PartialEq, Eq)]
pub struct IndexUrls {
sources: Vec<IndexSource>,
index: Option<IndexUrl>,
extra_index: Vec<IndexUrl>,
index: Option<IndexSource>,
extra_index: Vec<IndexSource>,
no_index: bool,
}

impl<'a> IndexUrls {
/// Return an iterator over the `tool.uv.index` sources, prioritizing the default index.
fn sources(&'a self) -> impl Iterator<Item = &'a IndexUrl> + 'a {
if self.no_index {
Either::Left(std::iter::empty())
} else {
Either::Right(
self.sources
.iter()
.filter(|source| !source.explicit)
.filter(|source| source.default)
.chain(
self.sources
.iter()
.filter(|source| !source.explicit)
.filter(|source| !source.default),
)
.map(|source| &source.url),
)
}
}

/// Return the fallback [`IndexUrl`] entry.
///
/// If `--no-index` is set, return `None`.
Expand All @@ -485,8 +435,7 @@ impl<'a> IndexUrls {
None
} else {
match self.index.as_ref() {
Some(index) => Some(index),
None if self.sources.iter().any(|source| source.default) => None,
Some(index) => Some(&index.url),
None => Some(&DEFAULT_INDEX_URL),
}
}
Expand All @@ -497,7 +446,7 @@ impl<'a> IndexUrls {
if self.no_index {
Either::Left(std::iter::empty())
} else {
Either::Right(self.extra_index.iter())
Either::Right(self.extra_index.iter().map(|index| &index.url))
}
}

Expand All @@ -509,14 +458,13 @@ impl<'a> IndexUrls {
/// If `no_index` was enabled, then this always returns an empty
/// iterator.
pub fn indexes(&'a self) -> impl Iterator<Item = &'a IndexUrl> + 'a {
self.sources().chain(self.extra_index()).chain(self.index())
self.extra_index().chain(self.index())
}
}

impl From<IndexLocations> for IndexUrls {
fn from(locations: IndexLocations) -> Self {
Self {
sources: locations.sources,
index: locations.index,
extra_index: locations.extra_index,
no_index: locations.no_index,
Expand Down
29 changes: 29 additions & 0 deletions crates/uv-settings/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,35 @@ pub struct PipOptions {
"#
)]
pub prefix: Option<PathBuf>,
/// The indexes to use when resolving dependencies.
///
/// Accepts either a repository compliant with [PEP 503](https://peps.python.org/pep-0503/)
/// (the simple repository API), or a local directory laid out in the same format.
///
/// Indexes are considered in the order in which they're defined, such that the first-defined
/// index has the highest priority. Further, the indexes provided by this setting are given
/// higher priority than any indexes specified via [`index_url`](#index-url) or
/// [`extra_index_url`](#extra-index-url).
///
/// If an index is marked as `explicit = true`, it will be used exclusively for those
/// dependencies that select it explicitly via `[tool.uv.sources]`, as in:
///
/// ```toml
/// [[tool.uv.index]]
/// name = "pytorch"
/// url = "https://download.pytorch.org/whl/cu121"
/// explicit = true
///
/// [tool.uv.sources]
/// torch = { index = "pytorch" }
/// ```
///
/// If an index is marked as `default = true`, it will be moved to the front of the list of
/// the list of indexes, such that it is given the highest priority when resolving packages.
/// Additionally, marking an index as default will disable the PyPI default index.
#[serde(skip)]
#[cfg_attr(feature = "schemars", schemars(skip))]
pub index: Option<Vec<IndexSource>>,
/// The URL of the Python package index (by default: <https://pypi.org/simple>).
///
/// Accepts either a repository compliant with [PEP 503](https://peps.python.org/pep-0503/)
Expand Down
12 changes: 7 additions & 5 deletions crates/uv/src/commands/pip/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use owo_colors::OwoColorize;
use tracing::debug;

use distribution_types::{
DependencyMetadata, IndexCapabilities, IndexLocations, NameRequirementSpecification,
UnresolvedRequirementSpecification, Verbatim,
DependencyMetadata, IndexCapabilities, IndexLocations, IndexSource,
NameRequirementSpecification, UnresolvedRequirementSpecification, Verbatim,
};
use install_wheel_rs::linker::LinkMode;
use pypi_types::{Requirement, SupportedEnvironments};
Expand Down Expand Up @@ -275,9 +275,11 @@ pub(crate) async fn pip_compile(

// Incorporate any index locations from the provided sources.
let index_locations = index_locations.combine(
Vec::default(),
index_url,
extra_index_urls,
index_url.map(IndexSource::from),
extra_index_urls
.into_iter()
.map(IndexSource::from)
.collect(),
find_links,
no_index,
);
Expand Down
10 changes: 6 additions & 4 deletions crates/uv/src/commands/pip/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use owo_colors::OwoColorize;
use tracing::{debug, enabled, Level};

use distribution_types::{
DependencyMetadata, IndexLocations, NameRequirementSpecification, Resolution,
DependencyMetadata, IndexLocations, IndexSource, NameRequirementSpecification, Resolution,
UnresolvedRequirementSpecification,
};
use install_wheel_rs::linker::LinkMode;
Expand Down Expand Up @@ -273,9 +273,11 @@ pub(crate) async fn pip_install(

// Incorporate any index locations from the provided sources.
let index_locations = index_locations.combine(
Vec::default(),
index_url,
extra_index_urls,
index_url.map(IndexSource::from),
extra_index_urls
.into_iter()
.map(IndexSource::from)
.collect(),
find_links,
no_index,
);
Expand Down
10 changes: 6 additions & 4 deletions crates/uv/src/commands/pip/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use anyhow::Result;
use owo_colors::OwoColorize;
use tracing::debug;

use distribution_types::{DependencyMetadata, IndexLocations, Resolution};
use distribution_types::{DependencyMetadata, IndexLocations, IndexSource, Resolution};
use install_wheel_rs::linker::LinkMode;
use pep508_rs::PackageName;
use uv_auth::store_credentials_from_url;
Expand Down Expand Up @@ -216,9 +216,11 @@ pub(crate) async fn pip_sync(

// Incorporate any index locations from the provided sources.
let index_locations = index_locations.combine(
Vec::default(),
index_url,
extra_index_urls,
index_url.map(IndexSource::from),
extra_index_urls
.into_iter()
.map(IndexSource::from)
.collect(),
find_links,
no_index,
);
Expand Down
Loading

0 comments on commit 4cffabe

Please sign in to comment.