-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use PEP 517 to extract non-static pyproject.toml
metadata
#2633
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
pub use crate::resolver::*; | ||
pub use crate::source_tree::*; | ||
pub use crate::sources::*; | ||
pub use crate::specification::*; | ||
|
||
mod confirm; | ||
mod resolver; | ||
mod source_tree; | ||
mod sources; | ||
mod specification; | ||
pub mod upgrade; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
use std::borrow::Cow; | ||
use std::path::{Path, PathBuf}; | ||
use std::sync::Arc; | ||
|
||
use anyhow::{Context, Result}; | ||
use futures::{StreamExt, TryStreamExt}; | ||
use url::Url; | ||
|
||
use distribution_types::{BuildableSource, PathSourceUrl, SourceUrl}; | ||
use pep508_rs::Requirement; | ||
use uv_client::RegistryClient; | ||
use uv_distribution::{Reporter, SourceDistCachedBuilder}; | ||
use uv_traits::BuildContext; | ||
|
||
use crate::ExtrasSpecification; | ||
|
||
/// A resolver for requirements specified via source trees. | ||
/// | ||
/// Used, e.g., to determine the the input requirements when a user specifies a `pyproject.toml` | ||
/// file, which may require running PEP 517 build hooks to extract metadata. | ||
pub struct SourceTreeResolver<'a> { | ||
/// The requirements for the project. | ||
source_trees: Vec<PathBuf>, | ||
/// The extras to include when resolving requirements. | ||
extras: &'a ExtrasSpecification<'a>, | ||
/// The reporter to use when building source distributions. | ||
reporter: Option<Arc<dyn Reporter>>, | ||
} | ||
|
||
impl<'a> SourceTreeResolver<'a> { | ||
/// Instantiate a new [`SourceTreeResolver`] for a given set of `source_trees`. | ||
pub fn new(source_trees: Vec<PathBuf>, extras: &'a ExtrasSpecification<'a>) -> Self { | ||
Self { | ||
source_trees, | ||
extras, | ||
reporter: None, | ||
} | ||
} | ||
|
||
/// Set the [`Reporter`] to use for this resolver. | ||
#[must_use] | ||
pub fn with_reporter(self, reporter: impl Reporter + 'static) -> Self { | ||
let reporter: Arc<dyn Reporter> = Arc::new(reporter); | ||
Self { | ||
reporter: Some(reporter), | ||
..self | ||
} | ||
} | ||
|
||
/// Resolve the requirements from the provided source trees. | ||
pub async fn resolve<T: BuildContext>( | ||
self, | ||
context: &T, | ||
client: &RegistryClient, | ||
) -> Result<Vec<Requirement>> { | ||
let requirements: Vec<_> = futures::stream::iter(self.source_trees.iter()) | ||
.map(|source_tree| async { | ||
self.resolve_source_tree(source_tree, context, client).await | ||
}) | ||
.buffered(50) | ||
.try_collect() | ||
.await?; | ||
Ok(requirements.into_iter().flatten().collect()) | ||
} | ||
|
||
/// Infer the package name for a given "unnamed" requirement. | ||
async fn resolve_source_tree<T: BuildContext>( | ||
&self, | ||
source_tree: &Path, | ||
context: &T, | ||
client: &RegistryClient, | ||
) -> Result<Vec<Requirement>> { | ||
// Convert to a buildable source. | ||
let path = fs_err::canonicalize(source_tree).with_context(|| { | ||
format!( | ||
"Failed to canonicalize path to source tree: {}", | ||
source_tree.display() | ||
) | ||
})?; | ||
let Ok(url) = Url::from_directory_path(&path) else { | ||
return Err(anyhow::anyhow!("Failed to convert path to URL")); | ||
}; | ||
let source = BuildableSource::Url(SourceUrl::Path(PathSourceUrl { | ||
url: &url, | ||
path: Cow::Owned(path), | ||
})); | ||
|
||
// Run the PEP 517 build process to extract metadata from the source distribution. | ||
let builder = if let Some(reporter) = self.reporter.clone() { | ||
SourceDistCachedBuilder::new(context, client).with_reporter(reporter) | ||
} else { | ||
SourceDistCachedBuilder::new(context, client) | ||
}; | ||
|
||
let metadata = builder | ||
.download_and_build_metadata(&source) | ||
.await | ||
.context("Failed to build source distribution")?; | ||
|
||
// Determine the appropriate requirements to return based on the extras. This involves | ||
// evaluating the `extras` expression in any markers, but preserving the remaining marker | ||
// conditions. | ||
match self.extras { | ||
ExtrasSpecification::None => Ok(metadata.requires_dist), | ||
ExtrasSpecification::All => Ok(metadata | ||
.requires_dist | ||
.into_iter() | ||
.map(|requirement| Requirement { | ||
marker: requirement | ||
.marker | ||
.and_then(|marker| marker.simplify_extras(&metadata.provides_extras)), | ||
..requirement | ||
}) | ||
.collect()), | ||
ExtrasSpecification::Some(extras) => Ok(metadata | ||
.requires_dist | ||
.into_iter() | ||
.map(|requirement| Requirement { | ||
marker: requirement | ||
.marker | ||
.and_then(|marker| marker.simplify_extras(extras)), | ||
..requirement | ||
}) | ||
.collect()), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe @BurntSushi should review this marker code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, feel free @BurntSushi.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Messaged with Andrew, I want to build on this branch (but not related to the markers) so I'm going to merge and address any review comments post-merge.