-
Notifications
You must be signed in to change notification settings - Fork 7
Adds support for pulling packages from PyPI #273
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
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,216 @@ | ||
| use std::{cmp::Ordering, str::FromStr}; | ||
|
|
||
| use rkyv::Archive; | ||
| use zpm_semver::VersionRc; | ||
| use zpm_utils::{DataType, EcoVec, FromFileString, ToFileString, ToHumanString, impl_file_string_from_str, impl_file_string_serialization}; | ||
|
|
||
| #[derive(thiserror::Error, Clone, Debug, PartialEq, Eq)] | ||
| pub enum PypiError { | ||
| #[error("Invalid PEP 440 version: {0}")] | ||
| InvalidVersion(String), | ||
|
|
||
| #[error("Invalid PEP 440 specifier set: {0}")] | ||
| InvalidSpecifier(String), | ||
|
|
||
| #[error("Cannot project PEP 440 version to semver: {0}")] | ||
| InvalidSemverProjection(String), | ||
| } | ||
|
|
||
| #[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Archive, rkyv::Serialize, rkyv::Deserialize)] | ||
| #[rkyv(derive(PartialEq, Eq, PartialOrd, Ord, Hash))] | ||
| pub struct PypiVersion { | ||
| raw: String, | ||
| } | ||
|
|
||
| impl PypiVersion { | ||
| pub fn as_str(&self) -> &str { | ||
| &self.raw | ||
| } | ||
|
|
||
| pub fn is_stable(&self) -> Result<bool, PypiError> { | ||
| Ok(self.parse()?.is_stable()) | ||
| } | ||
|
|
||
| pub fn cmp_pep440(&self, other: &Self) -> Result<Ordering, PypiError> { | ||
| Ok(self.parse()?.cmp(&other.parse()?)) | ||
| } | ||
|
|
||
| pub fn satisfies(&self, specifiers: &PypiSpecifierSet) -> Result<bool, PypiError> { | ||
| specifiers.contains(self) | ||
| } | ||
|
|
||
| pub fn to_lossy_semver(&self) -> Result<zpm_semver::Version, PypiError> { | ||
| let parsed = self.parse()?; | ||
| let release = parsed.release(); | ||
|
|
||
| let to_u32 = |n: Option<u64>| -> Result<u32, PypiError> { | ||
| let n = n.unwrap_or(0); | ||
| n.try_into().map_err(|_| PypiError::InvalidSemverProjection(self.raw.clone())) | ||
| }; | ||
|
|
||
| let major = to_u32(release.first().copied())?; | ||
| let minor = to_u32(release.get(1).copied())?; | ||
| let patch = to_u32(release.get(2).copied())?; | ||
|
|
||
| let mut prerelease_segments = Vec::new(); | ||
|
|
||
| if let Some(pre) = parsed.pre() { | ||
| prerelease_segments.push(pre.kind.to_string()); | ||
| prerelease_segments.push(pre.number.to_string()); | ||
| } | ||
|
|
||
| if let Some(dev) = parsed.dev() { | ||
| prerelease_segments.push("dev".to_string()); | ||
| prerelease_segments.push(dev.to_string()); | ||
| } | ||
|
|
||
| if let Some(post) = parsed.post() { | ||
| prerelease_segments.push("post".to_string()); | ||
| prerelease_segments.push(post.to_string()); | ||
| } | ||
|
|
||
| if !parsed.local().is_empty() { | ||
| prerelease_segments.push("local".to_string()); | ||
|
|
||
| for segment in parsed.local() { | ||
| prerelease_segments.push(segment.to_string().to_ascii_lowercase()); | ||
| } | ||
| } | ||
|
|
||
| let rc = if prerelease_segments.is_empty() { | ||
| None | ||
| } else { | ||
| let rc_segments = prerelease_segments.into_iter().map(|segment| { | ||
| match segment.parse::<u32>() { | ||
| Ok(number) => VersionRc::Number(number), | ||
| Err(_) => VersionRc::String(segment.into()), | ||
| } | ||
| }).collect::<Vec<_>>(); | ||
|
|
||
| Some(EcoVec::from(rc_segments)) | ||
| }; | ||
|
|
||
| Ok(zpm_semver::Version::new_from_components(major, minor, patch, rc)) | ||
| } | ||
|
|
||
| fn parse(&self) -> Result<pep440_rs::Version, PypiError> { | ||
| pep440_rs::Version::from_str(&self.raw) | ||
| .map_err(|_| PypiError::InvalidVersion(self.raw.clone())) | ||
| } | ||
| } | ||
|
|
||
| impl FromFileString for PypiVersion { | ||
| type Error = PypiError; | ||
|
|
||
| fn from_file_string(src: &str) -> Result<Self, Self::Error> { | ||
| let src = src.trim(); | ||
|
|
||
| let parsed = pep440_rs::Version::from_str(src) | ||
| .map_err(|_| PypiError::InvalidVersion(src.to_string()))?; | ||
|
|
||
| Ok(Self { | ||
| raw: parsed.to_string(), | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| impl ToFileString for PypiVersion { | ||
| fn to_file_string(&self) -> String { | ||
| self.raw.clone() | ||
| } | ||
| } | ||
|
|
||
| impl ToHumanString for PypiVersion { | ||
| fn to_print_string(&self) -> String { | ||
| DataType::Reference.colorize(&self.raw) | ||
| } | ||
| } | ||
|
|
||
| impl_file_string_from_str!(PypiVersion); | ||
| impl_file_string_serialization!(PypiVersion); | ||
|
|
||
| #[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Archive, rkyv::Serialize, rkyv::Deserialize)] | ||
| #[rkyv(derive(PartialEq, Eq, PartialOrd, Ord, Hash))] | ||
| pub struct PypiSpecifierSet { | ||
| raw: String, | ||
| } | ||
|
|
||
| impl PypiSpecifierSet { | ||
| pub fn any() -> Self { | ||
| Self { | ||
| raw: "*".to_string(), | ||
| } | ||
| } | ||
|
|
||
| pub fn is_any(&self) -> bool { | ||
| self.raw == "*" | ||
| } | ||
|
|
||
| pub fn as_str(&self) -> &str { | ||
| &self.raw | ||
| } | ||
|
|
||
| pub fn contains(&self, version: &PypiVersion) -> Result<bool, PypiError> { | ||
| if self.is_any() { | ||
| return Ok(true); | ||
| } | ||
|
|
||
| let parsed_version = pep440_rs::Version::from_str(version.as_str()) | ||
| .map_err(|_| PypiError::InvalidVersion(version.as_str().to_string()))?; | ||
|
|
||
| if let Ok(specifiers) = pep440_rs::VersionSpecifiers::from_str(&self.raw) { | ||
| return Ok(specifiers.contains(&parsed_version)); | ||
| } | ||
|
|
||
| let pinned = pep440_rs::Version::from_str(&self.raw) | ||
| .map_err(|_| PypiError::InvalidSpecifier(self.raw.clone()))?; | ||
|
|
||
| Ok(parsed_version == pinned) | ||
| } | ||
| } | ||
|
|
||
| impl Default for PypiSpecifierSet { | ||
| fn default() -> Self { | ||
| Self::any() | ||
| } | ||
| } | ||
|
|
||
| impl FromFileString for PypiSpecifierSet { | ||
| type Error = PypiError; | ||
|
|
||
| fn from_file_string(src: &str) -> Result<Self, Self::Error> { | ||
| let src = src.trim(); | ||
|
|
||
| if src.is_empty() || src == "*" { | ||
| return Ok(Self::any()); | ||
| } | ||
|
|
||
| if let Ok(specifiers) = pep440_rs::VersionSpecifiers::from_str(src) { | ||
| return Ok(Self { | ||
| raw: specifiers.to_string(), | ||
| }); | ||
| } | ||
|
|
||
| let version = pep440_rs::Version::from_str(src) | ||
| .map_err(|_| PypiError::InvalidSpecifier(src.to_string()))?; | ||
|
|
||
| Ok(Self { | ||
| raw: version.to_string(), | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| impl ToFileString for PypiSpecifierSet { | ||
| fn to_file_string(&self) -> String { | ||
| self.raw.clone() | ||
| } | ||
| } | ||
|
|
||
| impl ToHumanString for PypiSpecifierSet { | ||
| fn to_print_string(&self) -> String { | ||
| DataType::Range.colorize(&self.raw) | ||
| } | ||
| } | ||
|
|
||
| impl_file_string_from_str!(PypiSpecifierSet); | ||
| impl_file_string_serialization!(PypiSpecifierSet); | ||
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.
Post-release versions mapped to semver pre-releases inverts ordering
Medium Severity
to_lossy_semverplaces PEP 440postreleases into the semver prerelease segment, which inverts their ordering. In PEP 440,1.0.0.post1is NEWER than1.0.0, but the projected semver1.0.0-post.1is OLDER than1.0.0per semver ordering rules. This projected version is stored inResolutionviaproject_pep440_to_semverand could cause incorrect behavior in any downstream code that compares resolution versions using semver semantics.Additional Locations (1)
packages/zpm/src/resolvers/pypi.rs#L100-L106Reviewed by Cursor Bugbot for commit 5f0b38e. Configure here.