Skip to content

Commit

Permalink
feat: return pybytes for sha256 and md5 everywhere and use md5 hash f…
Browse files Browse the repository at this point in the history
…or legacy bz2 md5 (#752)

cc @jaimergp
  • Loading branch information
wolfv authored Jun 17, 2024
1 parent c18aec9 commit 7cbf2c4
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 18 deletions.
3 changes: 2 additions & 1 deletion crates/rattler_conda_types/src/repo_data/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ pub struct PackageRecord {
pub features: Option<String>,

/// A deprecated md5 hash
pub legacy_bz2_md5: Option<String>,
#[serde_as(as = "Option<SerializableHash::<rattler_digest::Md5>>")]
pub legacy_bz2_md5: Option<Md5Hash>,

/// A deprecated package archive size.
pub legacy_bz2_size: Option<u64>,
Expand Down
7 changes: 4 additions & 3 deletions crates/rattler_lock/src/utils/serde/raw_conda_package_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ pub(crate) struct RawCondaPackageData<'a> {
#[serde_as(as = "Option<SerializableHash::<rattler_digest::Md5>>")]
pub md5: Option<Md5Hash>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub legacy_bz2_md5: Cow<'a, Option<String>>,
#[serde_as(as = "Option<SerializableHash::<rattler_digest::Md5>>")]
pub legacy_bz2_md5: Option<Md5Hash>,

// Dependencies
#[serde(default, skip_serializing_if = "Vec::is_empty")]
Expand Down Expand Up @@ -109,7 +110,7 @@ impl<'a> From<RawCondaPackageData<'a>> for CondaPackageData {
constrains: value.constrains.into_owned(),
depends: value.depends.into_owned(),
features: value.features.into_owned(),
legacy_bz2_md5: value.legacy_bz2_md5.into_owned(),
legacy_bz2_md5: value.legacy_bz2_md5,
legacy_bz2_size: value.legacy_bz2_size.into_owned(),
license: value.license.into_owned(),
license_family: value.license_family.into_owned(),
Expand Down Expand Up @@ -151,7 +152,7 @@ impl<'a> From<&'a CondaPackageData> for RawCondaPackageData<'a> {
platform: Cow::Borrowed(&value.package_record.platform),
arch: Cow::Borrowed(&value.package_record.arch),
md5: value.package_record.md5,
legacy_bz2_md5: Cow::Borrowed(&value.package_record.legacy_bz2_md5),
legacy_bz2_md5: value.package_record.legacy_bz2_md5,
sha256: value.package_record.sha256,
size: Cow::Borrowed(&value.package_record.size),
legacy_bz2_size: Cow::Borrowed(&value.package_record.legacy_bz2_size),
Expand Down
14 changes: 7 additions & 7 deletions py-rattler/rattler/repo_data/package_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ def features(self) -> Optional[str]:
return self._record.features

@property
def legacy_bz2_md5(self) -> Optional[str]:
def legacy_bz2_md5(self) -> Optional[bytes]:
"""
A deprecated md5 hash.
"""
Expand Down Expand Up @@ -295,7 +295,7 @@ def license_family(self) -> Optional[str]:
return self._record.license_family

@property
def md5(self) -> Optional[str]:
def md5(self) -> Optional[bytes]:
"""
Optionally a MD5 hash of the package archive.
Expand All @@ -306,8 +306,8 @@ def md5(self) -> Optional[str]:
>>> record = PrefixRecord.from_path(
... "../test-data/conda-meta/libsqlite-3.40.0-hcfcfb64_0.json"
... )
>>> record.md5
'5E5A97795DE72F8CC3BAF3D9EA6327A2'
>>> record.md5.hex()
'5e5a97795de72f8cc3baf3d9ea6327a2'
>>>
```
"""
Expand Down Expand Up @@ -380,7 +380,7 @@ def platform(self) -> Optional[str]:
return self._record.platform

@property
def sha256(self) -> Optional[str]:
def sha256(self) -> Optional[bytes]:
"""
Optionally a SHA256 hash of the package archive.
Expand All @@ -391,8 +391,8 @@ def sha256(self) -> Optional[str]:
>>> record = PrefixRecord.from_path(
... "../test-data/conda-meta/libsqlite-3.40.0-hcfcfb64_0.json"
... )
>>> record.sha256
'4E50B3D90A351C9D47D239D3F90FCE4870DF2526E4F7FEF35203AB3276A6DFC9'
>>> record.sha256.hex()
'4e50b3d90a351c9d47d239d3f90fce4870df2526e4f7fef35203ab3276a6dfc9'
>>>
"""
return self._record.sha256
Expand Down
19 changes: 12 additions & 7 deletions py-rattler/src/record.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::path::PathBuf;

use pyo3::{
exceptions::PyTypeError, intern, pyclass, pymethods, FromPyObject, PyAny, PyErr, PyResult,
exceptions::PyTypeError, intern, pyclass, pymethods, types::PyBytes, FromPyObject, PyAny,
PyErr, PyResult, Python,
};
use rattler_conda_types::{
package::{IndexJson, PackageFile},
Expand Down Expand Up @@ -138,8 +139,10 @@ impl PyRecord {

/// A deprecated md5 hash.
#[getter]
pub fn legacy_bz2_md5(&self) -> Option<String> {
self.as_package_record().legacy_bz2_md5.clone()
pub fn legacy_bz2_md5<'a>(&self, py: Python<'a>) -> Option<&'a PyBytes> {
self.as_package_record()
.legacy_bz2_md5
.map(|md5| PyBytes::new(py, &md5))
}

/// A deprecated package archive size.
Expand All @@ -162,8 +165,10 @@ impl PyRecord {

/// Optionally a MD5 hash of the package archive.
#[getter]
pub fn md5(&self) -> Option<String> {
self.as_package_record().md5.map(|md5| format!("{md5:X}"))
pub fn md5<'a>(&self, py: Python<'a>) -> Option<&'a PyBytes> {
self.as_package_record()
.md5
.map(|md5| PyBytes::new(py, &md5))
}

/// Package name of the Record.
Expand All @@ -180,10 +185,10 @@ impl PyRecord {

/// Optionally a SHA256 hash of the package archive.
#[getter]
pub fn sha256(&self) -> Option<String> {
pub fn sha256<'a>(&self, py: Python<'a>) -> Option<&'a PyBytes> {
self.as_package_record()
.sha256
.map(|sha| format!("{sha:X}"))
.map(|sha| PyBytes::new(py, &sha))
}

/// Optionally the size of the package archive in bytes.
Expand Down

0 comments on commit 7cbf2c4

Please sign in to comment.