Skip to content

Commit

Permalink
Restrict to workspaces
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Jan 17, 2025
1 parent ec5f721 commit cc3c14a
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 17 deletions.
26 changes: 14 additions & 12 deletions crates/uv-distribution/src/metadata/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,17 +236,13 @@ impl LoweredRequirement {
.find(|Index { name, .. }| {
name.as_ref().is_some_and(|name| *name == index)
})
.map(|Index { url: index, .. }| index.clone())
else {
return Err(LoweringError::MissingIndex(
requirement.name.clone(),
index,
));
};
let url = if let Some(credentials) = index.credentials() {
credentials.apply(index.url.clone().into_url())
} else {
index.url.clone().into_url()
};
let conflict = project_name.and_then(|project_name| {
if let Some(extra) = extra {
Some(ConflictItem::from((project_name.clone(), extra)))
Expand All @@ -256,7 +252,12 @@ impl LoweredRequirement {
})
}
});
let source = registry_source(&requirement, url, conflict, lower_bound);
let source = registry_source(
&requirement,
index.into_url(),
conflict,
lower_bound,
);
(source, marker)
}
Source::Workspace {
Expand Down Expand Up @@ -464,19 +465,20 @@ impl LoweredRequirement {
.find(|Index { name, .. }| {
name.as_ref().is_some_and(|name| *name == index)
})
.map(|Index { url: index, .. }| index.clone())
else {
return Err(LoweringError::MissingIndex(
requirement.name.clone(),
index,
));
};
let url = if let Some(credentials) = index.credentials() {
credentials.apply(index.url.clone().into_url())
} else {
index.url.clone().into_url()
};
let conflict = None;
let source = registry_source(&requirement, url, conflict, lower_bound);
let source = registry_source(
&requirement,
index.into_url(),
conflict,
lower_bound,
);
(source, marker)
}
Source::Workspace { .. } => {
Expand Down
34 changes: 33 additions & 1 deletion crates/uv/src/commands/project/install_target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::path::Path;
use std::str::FromStr;

use itertools::Either;

use uv_distribution_types::Index;
use uv_normalize::PackageName;
use uv_pypi_types::{LenientRequirement, VerbatimParsedUrl};
use uv_resolver::{Installable, Lock, Package};
Expand Down Expand Up @@ -88,6 +88,38 @@ impl<'lock> Installable<'lock> for InstallTarget<'lock> {
}

impl<'lock> InstallTarget<'lock> {
/// Return an iterator over the [`Index`] definitions in the target.
pub(crate) fn indexes(self) -> impl Iterator<Item = &'lock Index> {
match self {
Self::Project { workspace, .. }
| Self::Workspace { workspace, .. }
| Self::NonProjectWorkspace { workspace, .. } => {
Either::Left(workspace.indexes().iter().chain(
workspace.packages().values().flat_map(|member| {
member
.pyproject_toml()
.tool
.as_ref()
.and_then(|tool| tool.uv.as_ref())
.and_then(|uv| uv.index.as_ref())
.into_iter()
.flatten()
}),
))
}
Self::Script { script, .. } => Either::Right(
script
.metadata
.tool
.as_ref()
.and_then(|tool| tool.uv.as_ref())
.and_then(|uv| uv.top_level.index.as_deref())
.into_iter()
.flatten(),
),
}
}

/// Return an iterator over all [`Sources`] defined by the target.
pub(crate) fn sources(&self) -> impl Iterator<Item = &Source> {
match self {
Expand Down
6 changes: 6 additions & 0 deletions crates/uv/src/commands/project/lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,12 @@ async fn do_lock(
}
}

for index in target.indexes() {
if let Some(credentials) = index.credentials() {
uv_auth::store_credentials(index.raw_url(), credentials);
}
}

// Initialize the registry client.
let client = RegistryClientBuilder::new(cache.clone())
.native_tls(native_tls)
Expand Down
30 changes: 29 additions & 1 deletion crates/uv/src/commands/project/lock_target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use itertools::Either;

use uv_configuration::{LowerBound, SourceStrategy};
use uv_distribution::LoweredRequirement;
use uv_distribution_types::IndexLocations;
use uv_distribution_types::{Index, IndexLocations};
use uv_normalize::{GroupName, PackageName};
use uv_pep508::RequirementOrigin;
use uv_pypi_types::{Conflicts, Requirement, SupportedEnvironments, VerbatimParsedUrl};
Expand Down Expand Up @@ -159,6 +159,34 @@ impl<'lock> LockTarget<'lock> {
}
}

/// Return an iterator over the [`Index`] definitions in the [`LockTarget`].
pub(crate) fn indexes(self) -> impl Iterator<Item = &'lock Index> {
match self {
Self::Workspace(workspace) => Either::Left(workspace.indexes().iter().chain(
workspace.packages().values().flat_map(|member| {
member
.pyproject_toml()
.tool
.as_ref()
.and_then(|tool| tool.uv.as_ref())
.and_then(|uv| uv.index.as_ref())
.into_iter()
.flatten()
}),
)),
Self::Script(script) => Either::Right(
script
.metadata
.tool
.as_ref()
.and_then(|tool| tool.uv.as_ref())
.and_then(|uv| uv.top_level.index.as_deref())
.into_iter()
.flatten(),
),
}
}

/// Return the `Requires-Python` bound for the [`LockTarget`].
pub(crate) fn requires_python(self) -> Option<RequiresPython> {
match self {
Expand Down
13 changes: 10 additions & 3 deletions crates/uv/src/commands/project/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,8 +363,8 @@ pub(super) async fn do_sync(
}
}

// Populate credentials from the workspace.
store_credentials_from_workspace(target);
// Populate credentials from the target.
store_credentials_from_target(target);

// Initialize the registry client.
let client = RegistryClientBuilder::new(cache.clone())
Expand Down Expand Up @@ -522,7 +522,14 @@ fn apply_editable_mode(resolution: Resolution, editable: EditableMode) -> Resolu
///
/// These credentials can come from any of `tool.uv.sources`, `tool.uv.dev-dependencies`,
/// `project.dependencies`, and `project.optional-dependencies`.
fn store_credentials_from_workspace(target: InstallTarget<'_>) {
fn store_credentials_from_target(target: InstallTarget<'_>) {
// Iterate over any idnexes in the target.
for index in target.indexes() {
if let Some(credentials) = index.credentials() {
store_credentials(index.raw_url(), credentials);
}
}

// Iterate over any sources in the target.
for source in target.sources() {
match source {
Expand Down

0 comments on commit cc3c14a

Please sign in to comment.