|
| 1 | +//! This module deals with `micromamba` - obtaining it, calling it, etc. |
| 2 | +
|
| 3 | +use crate::csmrc::Config; |
| 4 | +use log::debug; |
| 5 | +use std::collections::HashMap; |
| 6 | +use std::io::{Error, ErrorKind}; |
| 7 | +use std::path::PathBuf; |
| 8 | +use std::process::Command; |
| 9 | + |
| 10 | +pub enum MicromambaLocation { |
| 11 | + /// A working `micromamba` was found in `$PATH` already. |
| 12 | + InPath, |
| 13 | + |
| 14 | + /// A working `micromamba` exists at the given path. |
| 15 | + Path(PathBuf), |
| 16 | +} |
| 17 | + |
| 18 | +/// Return a [`Command`] ready to shell out to `micromamba` with the appropriate |
| 19 | +/// environment variables set based on configuration. |
| 20 | +pub fn micromamba_at(location: MicromambaLocation, config: &Config, args: Vec<&str>) -> Command { |
| 21 | + let mut env_vars: HashMap<&str, String> = HashMap::new(); |
| 22 | + |
| 23 | + if let Some(mamba_root_prefix) = &config.mamba_root_prefix { |
| 24 | + env_vars.insert("MAMBA_ROOT_PREFIX", mamba_root_prefix.to_string()); |
| 25 | + } |
| 26 | + |
| 27 | + let mut cmd = Command::new("micromamba"); |
| 28 | + cmd.args(args); |
| 29 | + cmd.envs(env_vars); |
| 30 | + debug!("About to run: {:?}", cmd); |
| 31 | + cmd |
| 32 | +} |
| 33 | + |
| 34 | +pub fn micromamba(config: &Config, args: Vec<&str>) -> Result<Command, Error> { |
| 35 | + match micromamba_path(config) { |
| 36 | + None => Err(Error::from(ErrorKind::NotFound)), |
| 37 | + Some(location) => Ok(micromamba_at(location, config, args)) |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +/// We need a `micromamba` binary to work with. If one is not present, attempt |
| 42 | +/// to download and install `micromamba` into the user's cache directory. Return |
| 43 | +/// a [`MicromambaLocation`] with the result if we found a working `micromamba`. |
| 44 | +/// |
| 45 | +/// 1. If there is already a `micromamba` command in $PATH, we use it. |
| 46 | +/// 2. Otherwise, download micromamba and install it somewhere in the user |
| 47 | +/// cache directory. (We cannot rely on this - it could be that the user's |
| 48 | +/// cache directory is mounted noexec or similar). |
| 49 | +/// |
| 50 | +/// Alternative approaches that we do not take here currently: |
| 51 | +/// - On Linux, we *could* in theory use memfd_create + fexecve to embed the app |
| 52 | +/// and run it from memory. This won't work on Windows. |
| 53 | +/// |
| 54 | +/// - We *could* embed the micromamba binary in our binary (Windows or Linux |
| 55 | +/// based on compile target) and write it to the user cache directory rather |
| 56 | +/// than downloading it. But this inflates our binary size. |
| 57 | +pub fn micromamba_path(config: &Config) -> Option<MicromambaLocation> { |
| 58 | + // Assume it's in $PATH already and see what happens. |
| 59 | + if let Ok(output) = micromamba_at(MicromambaLocation::InPath, config, vec!["--version"]).output() { |
| 60 | + // The full path doesn't matter, it's in $PATH |
| 61 | + debug!("micromamba reported version {}", String::from_utf8_lossy(&output.stdout)); |
| 62 | + return Some(MicromambaLocation::InPath); |
| 63 | + } |
| 64 | + None |
| 65 | +} |
0 commit comments