Skip to content
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

Track origin for setup.py files and friends #3481

Merged
merged 2 commits into from
May 9, 2024
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
40 changes: 16 additions & 24 deletions crates/distribution-types/src/annotation.rs
Original file line number Diff line number Diff line change
@@ -1,46 +1,38 @@
use std::collections::{BTreeMap, BTreeSet};
use std::path::PathBuf;

use url::Url;

use pep508_rs::VerbatimUrl;
use pep508_rs::{RequirementOrigin, VerbatimUrl};
use uv_fs::Simplified;
use uv_normalize::PackageName;

/// Source of a dependency, e.g., a `-r requirements.txt` file.
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum SourceAnnotation {
/// A `pyproject.toml` file.
PyProject {
path: PathBuf,
project_name: Option<String>,
},
/// A `-c constraints.txt` file.
Constraint(PathBuf),
Constraint(RequirementOrigin),
/// An `--override overrides.txt` file.
Override(PathBuf),
Override(RequirementOrigin),
/// A `-r requirements.txt` file.
Requirement(PathBuf),
Requirement(RequirementOrigin),
}

impl std::fmt::Display for SourceAnnotation {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Requirement(path) => {
write!(f, "-r {}", path.user_display())
}
Self::Constraint(path) => {
write!(f, "-c {}", path.user_display())
}
Self::Override(path) => {
write!(f, "--override {}", path.user_display())
}
Self::PyProject { path, project_name } => {
if let Some(project_name) = project_name {
write!(f, "{} ({})", project_name, path.user_display())
} else {
write!(f, "{}", path.user_display())
Self::Requirement(origin) => match origin {
RequirementOrigin::File(path) => {
write!(f, "-r {}", path.user_display())
}
RequirementOrigin::Project(path, project_name) => {
write!(f, "{project_name} ({})", path.user_display())
}
},
Self::Constraint(origin) => {
write!(f, "-c {}", origin.path().user_display())
}
Self::Override(origin) => {
write!(f, "--override {}", origin.path().user_display())
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions crates/distribution-types/src/requirement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use indexmap::IndexMap;
use url::Url;

use pep440_rs::VersionSpecifiers;
use pep508_rs::{MarkerEnvironment, MarkerTree, VerbatimUrl, VersionOrUrl};
use pep508_rs::{MarkerEnvironment, MarkerTree, RequirementOrigin, VerbatimUrl, VersionOrUrl};
use uv_git::GitReference;
use uv_normalize::{ExtraName, PackageName};

Expand All @@ -28,7 +28,7 @@ pub struct Requirement {
pub extras: Vec<ExtraName>,
pub marker: Option<MarkerTree>,
pub source: RequirementSource,
pub path: Option<PathBuf>,
pub origin: Option<RequirementOrigin>,
}

impl Requirement {
Expand Down Expand Up @@ -63,7 +63,7 @@ impl Requirement {
extras: requirement.extras,
marker: requirement.marker,
source,
path: requirement.source,
origin: requirement.origin,
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/distribution-types/src/resolution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ impl From<&ResolvedDist> for Requirement {
extras: vec![],
marker: None,
source,
path: None,
origin: None,
}
}
}
2 changes: 0 additions & 2 deletions crates/distribution-types/src/specified_requirement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ pub struct UnresolvedRequirementSpecification {
pub requirement: UnresolvedRequirement,
/// Hashes of the downloadable packages.
pub hashes: Vec<String>,
/// Path of the source of the requirement
pub path: Option<String>,
}

/// A requirement read from a `requirements.txt` or `pyproject.toml` file.
Expand Down
20 changes: 11 additions & 9 deletions crates/pep508-rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use std::fmt::{Debug, Display, Formatter};
use std::hash::{Hash, Hasher};
#[cfg(feature = "pyo3")]
use std::ops::Deref;
use std::path::{Path, PathBuf};
use std::path::Path;
use std::str::FromStr;

#[cfg(feature = "pyo3")]
Expand All @@ -49,13 +49,15 @@ use pep440_rs::{Version, VersionSpecifier, VersionSpecifiers};
#[cfg(feature = "non-pep508-extensions")]
pub use unnamed::UnnamedRequirement;
// Parity with the crates.io version of pep508_rs
pub use origin::RequirementOrigin;
pub use uv_normalize::{ExtraName, InvalidNameError, PackageName};
pub use verbatim_url::{
expand_env_vars, split_scheme, strip_host, Scheme, VerbatimUrl, VerbatimUrlError,
};

mod cursor;
mod marker;
mod origin;
#[cfg(feature = "non-pep508-extensions")]
mod unnamed;
mod verbatim_url;
Expand Down Expand Up @@ -149,14 +151,14 @@ pub struct Requirement<T: Pep508Url = VerbatimUrl> {
/// Those are a nested and/or tree.
pub marker: Option<MarkerTree>,
/// The source file containing the requirement.
pub source: Option<PathBuf>,
pub origin: Option<RequirementOrigin>,
}

impl Requirement {
/// Set the source file containing the requirement.
#[must_use]
pub fn with_source(self, source: Option<PathBuf>) -> Self {
Self { source, ..self }
pub fn with_origin(self, origin: Option<RequirementOrigin>) -> Self {
Self { origin, ..self }
}
}

Expand Down Expand Up @@ -492,7 +494,7 @@ impl<T: Pep508Url> Requirement<T> {
extras,
version_or_url,
marker,
source,
origin,
} = self;
Requirement {
name,
Expand All @@ -505,7 +507,7 @@ impl<T: Pep508Url> Requirement<T> {
Some(VersionOrUrl::Url(url)) => Some(VersionOrUrl::Url(U::from(url))),
},
marker,
source,
origin,
}
}
}
Expand Down Expand Up @@ -1029,7 +1031,7 @@ fn parse_pep508_requirement<T: Pep508Url>(
extras,
version_or_url: requirement_kind,
marker,
source: None,
origin: None,
})
}

Expand Down Expand Up @@ -1171,7 +1173,7 @@ mod tests {
operator: MarkerOperator::LessThan,
r_value: MarkerValue::QuotedString("2.7".to_string()),
})),
source: None,
origin: None,
};
assert_eq!(requests, expected);
}
Expand Down Expand Up @@ -1397,7 +1399,7 @@ mod tests {
extras: vec![],
marker: None,
version_or_url: Some(VersionOrUrl::Url(Url::parse(url).unwrap())),
source: None,
origin: None,
};
assert_eq!(pip_url, expected);
}
Expand Down
22 changes: 22 additions & 0 deletions crates/pep508-rs/src/origin.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use std::path::{Path, PathBuf};

use uv_normalize::PackageName;

/// The origin of a dependency, e.g., a `-r requirements.txt` file.
#[derive(Hash, Debug, Clone, Eq, PartialEq, PartialOrd, Ord)]
pub enum RequirementOrigin {
/// The requirement was provided via a standalone file (e.g., a `requirements.txt` file).
File(PathBuf),
/// The requirement was provided via a local project (e.g., a `pyproject.toml` file).
Project(PathBuf, PackageName),
}

impl RequirementOrigin {
/// Returns the path of the requirement origin.
pub fn path(&self) -> &Path {
match self {
RequirementOrigin::File(path) => path.as_path(),
RequirementOrigin::Project(path, _) => path.as_path(),
}
}
}
14 changes: 7 additions & 7 deletions crates/pep508-rs/src/unnamed.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::fmt::{Display, Formatter};
use std::path::{Path, PathBuf};
use std::path::Path;
use std::str::FromStr;

use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
Expand All @@ -10,8 +10,8 @@ use uv_normalize::ExtraName;
use crate::marker::parse_markers_cursor;
use crate::{
expand_env_vars, parse_extras_cursor, split_extras, split_scheme, strip_host, Cursor,
MarkerEnvironment, MarkerTree, Pep508Error, Pep508ErrorSource, Scheme, VerbatimUrl,
VerbatimUrlError,
MarkerEnvironment, MarkerTree, Pep508Error, Pep508ErrorSource, RequirementOrigin, Scheme,
VerbatimUrl, VerbatimUrlError,
};

/// A PEP 508-like, direct URL dependency specifier without a package name.
Expand All @@ -31,7 +31,7 @@ pub struct UnnamedRequirement {
/// Those are a nested and/or tree.
pub marker: Option<MarkerTree>,
/// The source file containing the requirement.
pub source: Option<PathBuf>,
pub origin: Option<RequirementOrigin>,
}

impl UnnamedRequirement {
Expand All @@ -46,8 +46,8 @@ impl UnnamedRequirement {

/// Set the source file containing the requirement.
#[must_use]
pub fn with_source(self, source: Option<PathBuf>) -> Self {
Self { source, ..self }
pub fn with_origin(self, origin: Option<RequirementOrigin>) -> Self {
Self { origin, ..self }
}
}

Expand Down Expand Up @@ -167,7 +167,7 @@ fn parse_unnamed_requirement(
url,
extras,
marker,
source: None,
origin: None,
})
}

Expand Down
Loading
Loading