rattler/
lib.rs

1//! [![Rattler banner](https://github.com/user-attachments/assets/bfd64756-061d-49f5-af4e-388743bdb855)](https://github.com/conda/rattler)
2//!
3//! Rattler is a library and executable to work with [Conda](http://conda.io)
4//! environments. Conda is a cross-platform open-source package management
5//! system and environment management system.
6//!
7//! Conda is originally written in Python and has evolved a lot since it was
8//! first conceived. Rattler is an attempt at reimplementing a lot of the
9//! machinery supporting Conda but making it available to a wider range of
10//! languages. The goal is to be able to integrate the Conda ecosystem in a wide
11//! variety of tools that do not rely on Python. Rust has excellent support for
12//! interfacing with many other languages (WASM, Javascript, Python, C, etc) and
13//! is therefore a good candidate for a reimplementation.
14#![deny(missing_docs)]
15
16use std::path::PathBuf;
17
18#[cfg(feature = "cli-tools")]
19pub mod cli;
20pub mod install;
21pub use rattler_cache::{package_cache, validation};
22
23/// A helper function that returns a [`Channel`] instance that points to an
24/// empty channel on disk that is bundled with this repository.
25#[cfg(any(doctest, test))]
26pub fn empty_channel() -> rattler_conda_types::Channel {
27    let manifest_dir = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"));
28    let channel_path = manifest_dir.join("../../test-data/channels/empty");
29    rattler_conda_types::Channel::from_str(
30        format!("file://{}[noarch]", channel_path.display()),
31        &rattler_conda_types::ChannelConfig::default_with_root_dir(
32            std::env::current_dir().unwrap(),
33        ),
34    )
35    .unwrap()
36}
37
38#[cfg(test)]
39pub(crate) fn get_test_data_dir() -> PathBuf {
40    std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../test-data")
41}
42
43/// Returns the default cache directory used by rattler.
44pub fn default_cache_dir() -> anyhow::Result<PathBuf> {
45    rattler_cache::default_cache_dir()
46}
47
48#[cfg(test)]
49use rattler_conda_types::RepoDataRecord;
50
51#[cfg(test)]
52pub(crate) fn get_repodata_record(package_path: impl AsRef<std::path::Path>) -> RepoDataRecord {
53    use std::fs;
54
55    use rattler_conda_types::{package::IndexJson, PackageRecord};
56    use rattler_digest::{Md5, Sha256};
57    use rattler_package_streaming::seek::read_package_file;
58
59    let package_path = package_path.as_ref();
60    let index_json = read_package_file::<IndexJson>(&package_path).unwrap();
61
62    // find size and hash
63    let size = fs::metadata(package_path).unwrap().len();
64    let sha256 = rattler_digest::compute_file_digest::<Sha256>(&package_path).unwrap();
65    let md5 = rattler_digest::compute_file_digest::<Md5>(&package_path).unwrap();
66
67    RepoDataRecord {
68        package_record: PackageRecord::from_index_json(
69            index_json,
70            Some(size),
71            Some(sha256),
72            Some(md5),
73        )
74        .unwrap(),
75        file_name: package_path
76            .file_name()
77            .and_then(|f| f.to_str())
78            .unwrap()
79            .to_string(),
80        url: url::Url::from_file_path(package_path).unwrap(),
81        channel: Some(String::from("test")),
82    }
83}