1use crate::macros::{error_println, warning_println};
2use colored::*;
3use std::collections::HashMap;
4use std::{env, path::PathBuf};
5
6pub fn append_pwd_to_pythonpath(current_dir: PathBuf) -> HashMap<String, String> {
8 if !current_dir.exists() {
9 error_println!(
10 "Current directory not valid: {}",
11 current_dir.display().to_string().bold()
12 );
13 HashMap::new()
14 } else {
15 let mut path = env::var("PYTHONPATH").unwrap_or_default();
16 if !path.contains(¤t_dir.to_string_lossy().to_string()) {
17 if !path.is_empty() {
18 path.push(':');
19 }
20 path.push_str(current_dir.to_string_lossy().to_string().as_str());
21 return HashMap::from([("PYTHONPATH".to_string(), path)]);
22 }
23 HashMap::new()
24 }
25}
26
27pub fn set_additional_env_var(
29 additional_env_from_args: Vec<String>,
30 quiet: bool,
31) -> HashMap<String, String> {
32 let mut additional_env = HashMap::new();
33
34 let current_dir = env::current_dir().unwrap();
36 additional_env.extend(append_pwd_to_pythonpath(current_dir));
37
38 for env_var in additional_env_from_args {
39 if let Some(pos) = env_var.find('=') {
40 let key = env_var[..pos].to_string();
41 let value = env_var[pos + 1..].to_string();
42 additional_env.insert(key.clone(), value.clone());
43 if !quiet {
44 println!("Setting env: {} = {}", key.bold(), value);
45 }
46 } else {
47 if !quiet {
48 warning_println!(
49 "Warning: Ignoring malformed environment variable: {}",
50 env_var.bold()
51 );
52 }
53 }
54 }
55 additional_env
56}
57
58pub fn get_python_exec_path(venv_path: &PathBuf) -> String {
59 if cfg!(target_os = "windows") {
60 venv_path
61 .join("Scripts")
62 .join("python.exe")
63 .to_string_lossy()
64 .to_string()
65 } else {
66 venv_path
67 .join("bin")
68 .join("python")
69 .to_string_lossy()
70 .to_string()
71 }
72}