#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum InstallMethod {
Global,
Venv,
Auto,
}
impl std::fmt::Display for InstallMethod {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
InstallMethod::Global => write!(f, "global"),
InstallMethod::Venv => write!(f, "venv"),
InstallMethod::Auto => write!(f, "auto"),
}
}
}
#[derive(Debug, Clone)]
pub struct WandbConfig {
pub python_bin: String,
pub method: InstallMethod,
pub dry_run: bool,
pub strict_rocm: bool,
}
impl Default for WandbConfig {
fn default() -> Self {
Self {
python_bin: "python3".to_string(),
method: InstallMethod::Auto,
dry_run: false,
strict_rocm: true,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ShellCommand {
pub program: String,
pub args: Vec<String>,
pub env: Vec<(String, String)>,
}
impl ShellCommand {
pub fn to_command_string(&self) -> String {
let env_prefix = self
.env
.iter()
.map(|(k, v)| format!("{k}={v}"))
.collect::<Vec<_>>()
.join(" ");
let cmd = if self.args.is_empty() {
self.program.clone()
} else {
format!("{} {}", self.program, self.args.join(" "))
};
if env_prefix.is_empty() {
cmd
} else {
format!("{env_prefix} {cmd}")
}
}
}
pub struct WandbInstaller {
config: WandbConfig,
}
impl WandbInstaller {
pub fn new(config: WandbConfig) -> Self {
Self { config }
}
pub fn with_defaults() -> Self {
Self::new(WandbConfig::default())
}
pub fn build_install_command(&self) -> ShellCommand {
let use_break = self.config.method == InstallMethod::Global
|| self.config.method == InstallMethod::Auto;
let mut args = vec!["-m".to_string(), "pip".to_string(), "install".to_string()];
if use_break {
args.push("--break-system-packages".to_string());
}
args.push("wandb".to_string());
ShellCommand {
program: self.config.python_bin.clone(),
args,
env: vec![],
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_install_command_global() {
let installer = WandbInstaller::new(WandbConfig {
method: InstallMethod::Global,
..Default::default()
});
let cmd = installer.build_install_command();
assert_eq!(cmd.program, "python3");
assert!(cmd.args.contains(&"-m".to_string()));
assert!(cmd.args.contains(&"pip".to_string()));
assert!(cmd.args.contains(&"install".to_string()));
assert!(cmd.args.contains(&"--break-system-packages".to_string()));
assert!(cmd.args.contains(&"wandb".to_string()));
}
#[test]
fn test_install_command_venv() {
let installer = WandbInstaller::new(WandbConfig {
method: InstallMethod::Venv,
..Default::default()
});
let cmd = installer.build_install_command();
assert!(!cmd.args.contains(&"--break-system-packages".to_string()));
assert!(cmd.args.contains(&"wandb".to_string()));
}
#[test]
fn test_install_command_auto() {
let installer = WandbInstaller::new(WandbConfig {
method: InstallMethod::Auto,
..Default::default()
});
let cmd = installer.build_install_command();
assert!(cmd.args.contains(&"--break-system-packages".to_string()));
assert!(cmd.args.contains(&"wandb".to_string()));
}
#[test]
fn test_command_string_format() {
let cmd = ShellCommand {
program: "python3".to_string(),
args: vec![
"-m".to_string(),
"pip".to_string(),
"install".to_string(),
"wandb".to_string(),
],
env: vec![],
};
assert_eq!(cmd.to_command_string(), "python3 -m pip install wandb");
}
#[test]
fn test_command_string_with_env() {
let cmd = ShellCommand {
program: "python3".to_string(),
args: vec![
"-m".to_string(),
"pip".to_string(),
"install".to_string(),
"wandb".to_string(),
],
env: vec![("ROCM_PATH".to_string(), "/opt/rocm".to_string())],
};
let s = cmd.to_command_string();
assert!(s.contains("ROCM_PATH=/opt/rocm"));
assert!(s.contains("python3 -m pip install wandb"));
}
}