Skip to content

Commit

Permalink
[wip] handle mirrors
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfv committed Mar 7, 2024
1 parent 7eaafa1 commit 67f7620
Show file tree
Hide file tree
Showing 8 changed files with 193 additions and 110 deletions.
146 changes: 83 additions & 63 deletions Cargo.lock

Large diffs are not rendered by default.

36 changes: 19 additions & 17 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -158,24 +158,26 @@ toml = "0.8.10"
pep440_rs = { git = "https://github.com/astral-sh/uv", rev = "9a99aa777618c55d21a9376699a2aeb646501bd6" }
pep508_rs = { git = "https://github.com/astral-sh/uv", rev = "9a99aa777618c55d21a9376699a2aeb646501bd6" }

rattler = { git = "https://github.com/mamba-org/rattler", rev = "7e0a130f0603fc2ff204649df9adba78422de2c8" }
rattler_conda_types = { git = "https://github.com/mamba-org/rattler", rev = "7e0a130f0603fc2ff204649df9adba78422de2c8" }
rattler_digest = { git = "https://github.com/mamba-org/rattler", rev = "7e0a130f0603fc2ff204649df9adba78422de2c8" }
rattler_lock = { git = "https://github.com/mamba-org/rattler", rev = "7e0a130f0603fc2ff204649df9adba78422de2c8" }
rattler_networking = { git = "https://github.com/mamba-org/rattler", rev = "7e0a130f0603fc2ff204649df9adba78422de2c8" }
rattler_repodata_gateway = { git = "https://github.com/mamba-org/rattler", rev = "7e0a130f0603fc2ff204649df9adba78422de2c8" }
rattler_shell = { git = "https://github.com/mamba-org/rattler", rev = "7e0a130f0603fc2ff204649df9adba78422de2c8" }
rattler_solve = { git = "https://github.com/mamba-org/rattler", rev = "7e0a130f0603fc2ff204649df9adba78422de2c8" }
rattler_virtual_packages = { git = "https://github.com/mamba-org/rattler", rev = "7e0a130f0603fc2ff204649df9adba78422de2c8" }
# rattler = { git = "https://github.com/mamba-org/rattler", rev = "7e0a130f0603fc2ff204649df9adba78422de2c8" }
# rattler_conda_types = { git = "https://github.com/mamba-org/rattler", rev = "7e0a130f0603fc2ff204649df9adba78422de2c8" }
# rattler_digest = { git = "https://github.com/mamba-org/rattler", rev = "7e0a130f0603fc2ff204649df9adba78422de2c8" }
# rattler_lock = { git = "https://github.com/mamba-org/rattler", rev = "7e0a130f0603fc2ff204649df9adba78422de2c8" }
# rattler_networking = { git = "https://github.com/mamba-org/rattler", rev = "7e0a130f0603fc2ff204649df9adba78422de2c8" }
# rattler_repodata_gateway = { git = "https://github.com/mamba-org/rattler", rev = "7e0a130f0603fc2ff204649df9adba78422de2c8" }
# rattler_shell = { git = "https://github.com/mamba-org/rattler", rev = "7e0a130f0603fc2ff204649df9adba78422de2c8" }
# rattler_solve = { git = "https://github.com/mamba-org/rattler", rev = "7e0a130f0603fc2ff204649df9adba78422de2c8" }
# rattler_virtual_packages = { git = "https://github.com/mamba-org/rattler", rev = "7e0a130f0603fc2ff204649df9adba78422de2c8" }
#deno_task_shell = { path = "../deno_task_shell" }#rattler = { path = "../rattler/crates/rattler" }
#rattler_conda_types = { path = "../rattler/crates/rattler_conda_types" }
#rattler_digest = { path = "../rattler/crates/rattler_digest" }
#rattler_networking = { path = "../rattler/crates/rattler_networking" }
#rattler_repodata_gateway = { path = "../rattler/crates/rattler_repodata_gateway" }
#rattler_shell = { path = "../rattler/crates/rattler_shell" }
#rattler_solve = { path = "../rattler/crates/rattler_solve" }
#rattler_virtual_packages = { path = "../rattler/crates/rattler_virtual_packages" }
#rattler_lock = { path = "../rattler/crates/rattler_lock" }

rattler = { path = "../rattler/crates/rattler" }
rattler_conda_types = { path = "../rattler/crates/rattler_conda_types" }
rattler_digest = { path = "../rattler/crates/rattler_digest" }
rattler_networking = { path = "../rattler/crates/rattler_networking" }
rattler_repodata_gateway = { path = "../rattler/crates/rattler_repodata_gateway" }
rattler_shell = { path = "../rattler/crates/rattler_shell" }
rattler_solve = { path = "../rattler/crates/rattler_solve" }
rattler_virtual_packages = { path = "../rattler/crates/rattler_virtual_packages" }
rattler_lock = { path = "../rattler/crates/rattler_lock" }


# Change these lines if you want a patched version of astral
Expand Down
7 changes: 3 additions & 4 deletions src/cli/global/install.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::install::execute_transaction;
use crate::repodata::friendly_channel_name;
use crate::utils::reqwest::build_reqwest_clients;
use crate::{config, prefix::Prefix, progress::await_in_progress, repodata::fetch_sparse_repodata};
use clap::Parser;
use dirs::home_dir;
Expand All @@ -9,7 +10,6 @@ use miette::IntoDiagnostic;
use rattler::install::Transaction;
use rattler::package_cache::PackageCache;
use rattler_conda_types::{Channel, ChannelConfig, MatchSpec, PackageName, Platform, PrefixRecord};
use rattler_networking::AuthenticationMiddleware;
use rattler_repodata_gateway::sparse::SparseRepoData;
use rattler_shell::{
activation::{ActivationVariables, Activator, PathModificationBehavior},
Expand Down Expand Up @@ -333,9 +333,8 @@ pub async fn execute(args: Args) -> miette::Result<()> {
.map(|c| Channel::from_str(c, &channel_config))
.collect::<Result<Vec<Channel>, _>>()
.into_diagnostic()?;
let authenticated_client = reqwest_middleware::ClientBuilder::new(reqwest::Client::new())
.with_arc(Arc::new(AuthenticationMiddleware::default()))
.build();

let (_client, authenticated_client) = build_reqwest_clients();

// Find the MatchSpec we want to install
let specs = args
Expand Down
7 changes: 2 additions & 5 deletions src/cli/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ use indexmap::IndexMap;
use itertools::Itertools;
use miette::IntoDiagnostic;
use rattler_conda_types::{Channel, ChannelConfig, PackageName, Platform, RepoDataRecord};
use rattler_networking::AuthenticationMiddleware;
use rattler_repodata_gateway::sparse::SparseRepoData;
use regex::Regex;

use strsim::jaro;
use tokio::task::spawn_blocking;

use crate::utils::reqwest::build_reqwest_clients;
use crate::{progress::await_in_progress, repodata::fetch_sparse_repodata, Project};

/// Search a package, output will list the latest version of package
Expand Down Expand Up @@ -105,10 +105,7 @@ pub async fn execute(args: Args) -> miette::Result<()> {
};

let package_name_filter = args.package;

let authenticated_client = reqwest_middleware::ClientBuilder::new(reqwest::Client::new())
.with_arc(Arc::new(AuthenticationMiddleware::default()))
.build();
let (_client, authenticated_client) = build_reqwest_clients();
let repo_data = Arc::new(
fetch_sparse_repodata(
channels.iter().map(AsRef::as_ref),
Expand Down
38 changes: 37 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use std::path::PathBuf;
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};

use miette::{Context, IntoDiagnostic};
use serde::Deserialize;

/// Determines the default author based on the default git author. Both the name and the email
/// address of the author are returned.
pub fn get_default_author() -> Option<(String, String)> {
Expand Down Expand Up @@ -43,3 +47,35 @@ pub fn get_cache_dir() -> miette::Result<PathBuf> {
.map_err(|_| miette::miette!("could not determine default cache directory"))
})
}

#[derive(Debug, Deserialize)]
pub struct Channel {
pub name: String,
pub url: String,
pub mirrors: Vec<String>,
}

#[derive(Debug, Deserialize)]
pub struct Config {
pub default_channels: Vec<String>,
pub channels: Vec<Channel>,
}

impl Config {
pub fn from_path(p: &Path) -> miette::Result<Config> {
let config: Config =
toml_edit::de::from_str(&std::fs::read_to_string(p).into_diagnostic()?)
.into_diagnostic()
.context("Failed to parse config")?;

Ok(config)
}

pub fn mirror_map(&self) -> HashMap<String, Vec<String>> {
let mut mirror_map = HashMap::new();
for channel in &self.channels {
mirror_map.insert(channel.url.clone(), channel.mirrors.clone());
}
mirror_map
}
}
21 changes: 1 addition & 20 deletions src/project/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ use indexmap::{Equivalent, IndexMap, IndexSet};
use miette::{IntoDiagnostic, NamedSource, WrapErr};

use rattler_conda_types::{Channel, GenericVirtualPackage, Platform, Version};
use rattler_networking::AuthenticationMiddleware;
use reqwest::Client;
use reqwest_middleware::ClientWithMiddleware;
use std::hash::Hash;

Expand All @@ -30,6 +28,7 @@ use std::{
use crate::activation::{get_environment_variables, run_activation};
use crate::project::grouped_environment::GroupedEnvironment;
use crate::task::TaskName;
use crate::utils::reqwest::build_reqwest_clients;
use crate::{
consts::{self, PROJECT_MANIFEST},
task::Task,
Expand Down Expand Up @@ -459,24 +458,6 @@ impl Project {
}
}

fn build_reqwest_clients() -> (Client, ClientWithMiddleware) {
static APP_USER_AGENT: &str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"),);

let timeout = 5 * 60;
let client = Client::builder()
.pool_max_idle_per_host(20)
.user_agent(APP_USER_AGENT)
.timeout(std::time::Duration::from_secs(timeout))
.build()
.expect("failed to create reqwest Client");

let authenticated_client = reqwest_middleware::ClientBuilder::new(client.clone())
.with_arc(Arc::new(AuthenticationMiddleware::default()))
.build();

(client, authenticated_client)
}

/// Iterates over the current directory and all its parent directories and returns the first
/// directory path that contains the [`consts::PROJECT_MANIFEST`].
pub fn find_project_root() -> Option<PathBuf> {
Expand Down
1 change: 1 addition & 0 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod barrier_cell;
pub mod conda_environment_file;
pub mod reqwest;
pub mod spanned;

pub use barrier_cell::BarrierCell;
47 changes: 47 additions & 0 deletions src/utils/reqwest.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use std::path::PathBuf;
use std::{sync::Arc, time::Duration};

use rattler_networking::AuthenticationMiddleware;
use rattler_networking::{MirrorMiddleware, OciMiddleware};
use reqwest::Client;
use reqwest_middleware::{ClientBuilder, ClientWithMiddleware};

use crate::config::Config;

fn mirror_middleware() -> MirrorMiddleware {
let config = Config::from_path(&PathBuf::from("./config.toml")).expect("Failed to load config");
let mirror_map = config.mirror_map();
// let mut mirror_map = HashMap::new();
// mirror_map.insert(
// "https://conda.anaconda.org/conda-forge".to_string(),
// vec![
// // "https://repo.prefix.dev/conda-forge".to_string()
// "oci://ghcr.io/channel-mirrors/conda-forge".to_string(),
// ],
// );
MirrorMiddleware::from_map(mirror_map)
}

fn oci_middleware() -> OciMiddleware {
OciMiddleware::default()
}

pub(crate) fn build_reqwest_clients() -> (Client, ClientWithMiddleware) {
static APP_USER_AGENT: &str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"),);

let timeout = 5 * 60;
let client = Client::builder()
.pool_max_idle_per_host(20)
.user_agent(APP_USER_AGENT)
.timeout(Duration::from_secs(timeout))
.build()
.expect("failed to create reqwest Client");

let authenticated_client = ClientBuilder::new(client.clone())
.with_arc(Arc::new(AuthenticationMiddleware::default()))
.with(mirror_middleware())
.with(oci_middleware())
.build();

(client, authenticated_client)
}

0 comments on commit 67f7620

Please sign in to comment.