feat(pipx support): Add support for pipx

This is a series of commits intended to add support for pipx in pacdef.
WIP.

Signed-off-by: innocentzero <isfarulhaque@proton.me>
This commit is contained in:
innocentzero
2024-01-23 20:42:54 +05:30
parent ec1aab4b43
commit 050a844bc2
3 changed files with 34 additions and 4 deletions
@@ -12,6 +12,7 @@ use crate::{Group, Package};
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Python { pub struct Python {
pub(crate) binary: String,
pub(crate) packages: HashSet<Package>, pub(crate) packages: HashSet<Package>,
} }
@@ -29,14 +30,24 @@ const SUPPORTS_AS_DEPENDENCY: bool = false;
impl Backend for Python { impl Backend for Python {
impl_backend_constants!(); 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<HashSet<Package>> { fn get_all_installed_packages(&self) -> Result<HashSet<Package>> {
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) extract_pacdef_packages(output)
} }
fn get_explicitly_installed_packages(&self) -> Result<HashSet<Package>> { fn get_explicitly_installed_packages(&self) -> Result<HashSet<Package>> {
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) extract_pacdef_packages(output)
} }
@@ -46,8 +57,7 @@ impl Backend for Python {
} }
} }
fn run_pip_command(args: &[&str]) -> Result<Value> { fn run_pip_command(cmd: &mut Command, args: &[&str]) -> Result<Value> {
let mut cmd = Command::new(BINARY);
cmd.args(args); cmd.args(args);
let output = String::from_utf8(cmd.output()?.stdout)?; let output = String::from_utf8(cmd.output()?.stdout)?;
let val: Value = serde_json::from_str(&output)?; let val: Value = serde_json::from_str(&output)?;
@@ -57,9 +67,18 @@ fn run_pip_command(args: &[&str]) -> Result<Value> {
impl Python { impl Python {
pub(crate) fn new() -> Self { pub(crate) fn new() -> Self {
Self { Self {
binary: BINARY.to_string(),
packages: HashSet::new(), 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<HashSet<Package>> { fn extract_pacdef_packages(value: Value) -> Result<HashSet<Package>> {
+4
View File
@@ -24,6 +24,9 @@ pub struct Config {
/// Backends the user does not want to use even though the binary exists. /// Backends the user does not want to use even though the binary exists.
#[serde(default)] #[serde(default)]
pub disabled_backends: Vec<String>, pub disabled_backends: Vec<String>,
/// Choose whether to use pipx instead of pip for python package management
#[serde(default)]
pub pip_binary: String,
} }
fn yes() -> bool { fn yes() -> bool {
@@ -86,6 +89,7 @@ impl Default for Config {
flatpak_systemwide: true, flatpak_systemwide: true,
warn_not_symlinks: true, warn_not_symlinks: true,
disabled_backends: vec![], disabled_backends: vec![],
pip_binary: "pip".into(),
} }
} }
} }
+7
View File
@@ -140,6 +140,13 @@ impl Pacdef {
{ {
flatpak.systemwide = self.config.flatpak_systemwide; flatpak.systemwide = self.config.flatpak_systemwide;
} }
if let Some(python) = backend
.as_any_mut()
.downcast_mut::<crate::backend::Python>()
{
python.binary = self.config.pip_binary.clone();
}
} }
fn install_packages(&mut self, noconfirm: bool) -> Result<()> { fn install_packages(&mut self, noconfirm: bool) -> Result<()> {