diff --git a/crates/pacdef_core/src/backend/actual/python.rs b/crates/pacdef_core/src/backend/actual/python.rs index 8ba3438..f309600 100644 --- a/crates/pacdef_core/src/backend/actual/python.rs +++ b/crates/pacdef_core/src/backend/actual/python.rs @@ -12,6 +12,7 @@ use crate::{Group, Package}; #[derive(Debug, Clone)] pub struct Python { + pub(crate) binary: String, pub(crate) packages: HashSet, } @@ -29,14 +30,24 @@ const SUPPORTS_AS_DEPENDENCY: bool = false; impl Backend for Python { impl_backend_constants!(); + fn get_binary(&self) -> Text { + let r#box = self.binary.clone().into_boxed_str(); + Box::leak(r#box) + } + fn get_all_installed_packages(&self) -> Result> { - let output = run_pip_command(&["list", "--format", "json", "--user"])?; + let mut cmd = Command::new(self.get_binary()); + let output = run_pip_command(&mut cmd, self.get_switches_runtime())?; extract_pacdef_packages(output) } fn get_explicitly_installed_packages(&self) -> Result> { - let output = run_pip_command(&["list", "--format", "json", "--not-required", "--user"])?; + let mut cmd = Command::new(self.get_binary()); + let output = run_pip_command( + &mut cmd, + &["list", "--format", "json", "--not-required", "--user"], + )?; extract_pacdef_packages(output) } @@ -46,8 +57,7 @@ impl Backend for Python { } } -fn run_pip_command(args: &[&str]) -> Result { - let mut cmd = Command::new(BINARY); +fn run_pip_command(cmd: &mut Command, args: &[&str]) -> Result { cmd.args(args); let output = String::from_utf8(cmd.output()?.stdout)?; let val: Value = serde_json::from_str(&output)?; @@ -57,9 +67,18 @@ fn run_pip_command(args: &[&str]) -> Result { impl Python { pub(crate) fn new() -> Self { Self { + binary: BINARY.to_string(), packages: HashSet::new(), } } + + fn get_switches_runtime(&self) -> Switches { + if self.get_binary().eq("pip") { + &["list", "--format", "json", "--user"] + } else { + &["list", "--json"] + } + } } fn extract_pacdef_packages(value: Value) -> Result> { diff --git a/crates/pacdef_core/src/config.rs b/crates/pacdef_core/src/config.rs index dbbec27..dbcafc1 100644 --- a/crates/pacdef_core/src/config.rs +++ b/crates/pacdef_core/src/config.rs @@ -24,6 +24,9 @@ pub struct Config { /// Backends the user does not want to use even though the binary exists. #[serde(default)] pub disabled_backends: Vec, + /// Choose whether to use pipx instead of pip for python package management + #[serde(default)] + pub pip_binary: String, } fn yes() -> bool { @@ -86,6 +89,7 @@ impl Default for Config { flatpak_systemwide: true, warn_not_symlinks: true, disabled_backends: vec![], + pip_binary: "pip".into(), } } } diff --git a/crates/pacdef_core/src/core.rs b/crates/pacdef_core/src/core.rs index ccf2839..b79b6e8 100644 --- a/crates/pacdef_core/src/core.rs +++ b/crates/pacdef_core/src/core.rs @@ -140,6 +140,13 @@ impl Pacdef { { flatpak.systemwide = self.config.flatpak_systemwide; } + + if let Some(python) = backend + .as_any_mut() + .downcast_mut::() + { + python.binary = self.config.pip_binary.clone(); + } } fn install_packages(&mut self, noconfirm: bool) -> Result<()> {