From 242af523b4c513a93a1bf7ed1b88deee5bd6f6b5 Mon Sep 17 00:00:00 2001 From: innocentzero Date: Wed, 24 Jan 2024 20:21:05 +0530 Subject: [PATCH] FIX iterate through json generated by pipx While it is now able to iterate through JSON created by pipx, it is unable to read the JSON with the error `EOF while parsing a value at line 1 column 0` Signed-off-by: innocentzero --- .../pacdef_core/src/backend/actual/python.rs | 26 ++++++++++++++----- 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/crates/pacdef_core/src/backend/actual/python.rs b/crates/pacdef_core/src/backend/actual/python.rs index f309600..68f0636 100644 --- a/crates/pacdef_core/src/backend/actual/python.rs +++ b/crates/pacdef_core/src/backend/actual/python.rs @@ -39,7 +39,7 @@ impl Backend for Python { 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_pipx(output) } fn get_explicitly_installed_packages(&self) -> Result> { @@ -49,7 +49,7 @@ impl Backend for Python { &["list", "--format", "json", "--not-required", "--user"], )?; - extract_pacdef_packages(output) + extract_pacdef_packages_pipx(output) } fn make_dependency(&self, _packages: &[Package]) -> Result { @@ -73,10 +73,10 @@ impl Python { } fn get_switches_runtime(&self) -> Switches { - if self.get_binary().eq("pip") { - &["list", "--format", "json", "--user"] - } else { - &["list", "--json"] + match self.get_binary() { + "pip" => &["list", "--format", "json", "--user"], + "pipx" => &["list", "--json"], + _ => panic!("Cannot use {} for package management in python. Please use a valid package manager like pip or pipx", self.get_binary()), } } } @@ -91,3 +91,17 @@ fn extract_pacdef_packages(value: Value) -> Result> { .collect(); Ok(result) } + +fn extract_pacdef_packages_pipx(value: Value) -> Result> { + let result = value["venvs"] + .as_object() + .context("getting inner json object")? + .iter() + .map(|(name, _)| { + println!("{name}"); + Package::from(name.as_str()) + }) + // .map(|(name, _)| Package::from(name.as_str())) + .collect(); + Ok(result) +}