-
Notifications
You must be signed in to change notification settings - Fork 187
feat: add run_exports cache #1060
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
Merged
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
e4e3a51
feat: add archive cache
nichmor c00c711
misc: change cargo.toml
nichmor 8f667d4
misc: change to archivecacheerror
nichmor be1c03e
Merge branch 'main' into feat/partial-unpacking
nichmor e28ca21
misc: remove ugly unwrap
nichmor 87add40
misc: throw up the error
nichmor 8bd810e
misc: use ArchiveCache in doc links
nichmor 9f9c384
misc: re-export CacheKey
nichmor 2cfb9cb
misc: add public exposed constant
nichmor 4116882
misc: change cachekey to take filename
nichmor 2db3c16
misc: refactor to cache run_exports.json
nichmor 69054a7
misc: remove insta
nichmor 15c5881
misc: add some docstrings
nichmor 9dd1cf0
misc: rename to run-exports cache
nichmor 7120f7e
misc: rename to run-exports cache
nichmor 3a76eee
misc: make writing file atomic
nichmor fbc5708
misc: deduplicate some code
nichmor 64b5fb7
misc: refactor to run_exports constant
nichmor 642742e
misc: apply changes
nichmor 44656bf
Merge branch 'main' into feat/partial-unpacking
nichmor e61fcbf
misc: rewind after writing
nichmor f7e2c56
misc: remove unit return
nichmor 45002a1
Update crates/rattler_cache/src/run_exports_cache/cache_key.rs
nichmor 35f44a3
misc: remove some outdated comments
nichmor bb02a0c
misc: apply changes
nichmor 8f076d9
Merge branch 'main' into feat/partial-unpacking
nichmor 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
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 |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| /// The location in the main cache folder where the conda package cache is stored. | ||
| pub const PACKAGE_CACHE_DIR: &str = "pkgs"; | ||
| pub const RUN_EXPORTS_CACHE_DIR: &str = "run_exports"; | ||
| /// The location in the main cache folder where the repodata cache is stored. | ||
| pub const REPODATA_CACHE_DIR: &str = "repodata"; |
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,86 @@ | ||
| use rattler_conda_types::{package::ArchiveIdentifier, PackageRecord}; | ||
| use rattler_digest::{Md5Hash, Sha256Hash}; | ||
| use std::fmt::{Display, Formatter}; | ||
|
|
||
| /// Provides a unique identifier for packages in the cache. | ||
| #[derive(Debug, Hash, Clone, Eq, PartialEq)] | ||
| pub struct CacheKey { | ||
| pub(crate) name: String, | ||
| pub(crate) version: String, | ||
| pub(crate) build_string: String, | ||
| pub(crate) sha256: Option<Sha256Hash>, | ||
| pub(crate) md5: Option<Md5Hash>, | ||
| pub(crate) extension: String, | ||
| } | ||
|
|
||
| impl CacheKey { | ||
| /// Potentially adds a sha256 hash of the archive. | ||
| pub fn with_opt_sha256(mut self, sha256: Option<Sha256Hash>) -> Self { | ||
| self.sha256 = sha256; | ||
| self | ||
| } | ||
|
|
||
| /// Potentially adds a md5 hash of the archive. | ||
| pub fn with_opt_md5(mut self, md5: Option<Md5Hash>) -> Self { | ||
| self.md5 = md5; | ||
| self | ||
| } | ||
| } | ||
|
|
||
| impl CacheKey { | ||
| /// Return the sha256 hash of the package if it is known. | ||
| pub fn sha256(&self) -> Option<Sha256Hash> { | ||
| self.sha256 | ||
| } | ||
|
|
||
| /// Return the md5 hash of the package if it is known. | ||
| pub fn md5(&self) -> Option<Md5Hash> { | ||
| self.md5 | ||
| } | ||
|
|
||
| /// Return the sha256 hash string of the package if it is known. | ||
| pub fn sha256_str(&self) -> String { | ||
| self.sha256() | ||
| .map(|hash| format!("{hash:x}")) | ||
| .unwrap_or_default() | ||
| } | ||
|
|
||
| /// Try to create a new cache key from a package record and a filename. | ||
| pub fn create(record: &PackageRecord, filename: &str) -> Result<Self, CacheKeyError> { | ||
| let archive_identifier = ArchiveIdentifier::try_from_filename(filename) | ||
| .ok_or_else(|| CacheKeyError::InvalidArchiveIdentifier(filename.to_string()))?; | ||
|
|
||
| Ok(Self { | ||
| name: record.name.as_normalized().to_string(), | ||
| version: record.version.to_string(), | ||
| build_string: record.build.clone(), | ||
| sha256: record.sha256, | ||
| md5: record.md5, | ||
| extension: archive_identifier.archive_type.extension().to_string(), | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| #[derive(Debug, thiserror::Error)] | ||
| pub enum CacheKeyError { | ||
| #[error("could not identify the archive type from the name: {0}")] | ||
| InvalidArchiveIdentifier(String), | ||
| } | ||
|
|
||
| impl Display for CacheKey { | ||
| fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { | ||
| // we need to use either sha256 or md5 hash to display the key | ||
| // if both are none, we ignore them | ||
| let display_key = match (self.sha256(), self.md5()) { | ||
| (Some(sha256), _) => format!("-{sha256:x}"), | ||
| (_, Some(md5)) => format!("-{md5:x}"), | ||
| _ => "".to_string(), | ||
| }; | ||
|
|
||
| write!( | ||
| f, | ||
| "{}-{}-{}{}{}", | ||
| &self.name, &self.version, &self.build_string, display_key, self.extension | ||
| ) | ||
| } | ||
| } |
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,66 @@ | ||
| use std::sync::Arc; | ||
|
|
||
| use ::tokio::io::{AsyncSeekExt, AsyncWriteExt}; | ||
| use fs_err::tokio; | ||
| use futures::StreamExt; | ||
| use rattler_package_streaming::DownloadReporter; | ||
| use tempfile::NamedTempFile; | ||
| use url::Url; | ||
|
|
||
| /// Download the contents of the archive from the specified remote location | ||
| /// and store it in a temporary file. | ||
| pub(crate) async fn download( | ||
| client: reqwest_middleware::ClientWithMiddleware, | ||
| url: Url, | ||
| suffix: &str, | ||
| reporter: Option<Arc<dyn DownloadReporter>>, | ||
| ) -> Result<NamedTempFile, DownloadError> { | ||
| let temp_file = NamedTempFile::with_suffix(suffix)?; | ||
|
|
||
| // Send the request for the file | ||
| let response = client.get(url.clone()).send().await?.error_for_status()?; | ||
|
|
||
| if let Some(reporter) = &reporter { | ||
| reporter.on_download_start(); | ||
| } | ||
|
|
||
| let total_bytes = response.content_length(); | ||
| let (tmp_file_handle, tmp_path) = temp_file.into_parts(); | ||
| // Convert the named temp file into a tokio file | ||
| let mut file = tokio::File::from_std(fs_err::File::from_parts(tmp_file_handle, &tmp_path)); | ||
|
|
||
| let mut stream = response.bytes_stream(); | ||
|
|
||
| let mut bytes_received = 0; | ||
| while let Some(chunk_result) = stream.next().await { | ||
| let chunk = chunk_result?; | ||
|
|
||
| if let Some(reporter) = &reporter { | ||
| bytes_received += chunk.len() as u64; | ||
| reporter.on_download_progress(bytes_received, total_bytes); | ||
| } | ||
| file.write_all(&chunk).await?; | ||
| } | ||
|
|
||
| file.flush().await?; | ||
|
|
||
| file.rewind().await?; | ||
|
|
||
| let file_handle = file.into_parts().0.into_std().await; | ||
|
|
||
| Ok(NamedTempFile::from_parts(file_handle, tmp_path)) | ||
| } | ||
|
|
||
| /// An error that can occur when downloading an archive. | ||
| #[derive(thiserror::Error, Debug)] | ||
| #[allow(missing_docs)] | ||
| pub enum DownloadError { | ||
| #[error("an io error occurred: {0}")] | ||
| Io(#[from] std::io::Error), | ||
|
|
||
| #[error(transparent)] | ||
| ReqwestMiddleware(#[from] ::reqwest_middleware::Error), | ||
|
|
||
| #[error(transparent)] | ||
| Reqwest(#[from] ::reqwest::Error), | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.