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 <isfarulhaque@proton.me>
This commit is contained in:
innocentzero
2024-01-24 20:21:05 +05:30
parent 050a844bc2
commit 242af523b4
@@ -39,7 +39,7 @@ impl Backend for Python {
let mut cmd = Command::new(self.get_binary()); let mut cmd = Command::new(self.get_binary());
let output = run_pip_command(&mut cmd, self.get_switches_runtime())?; 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<HashSet<Package>> { fn get_explicitly_installed_packages(&self) -> Result<HashSet<Package>> {
@@ -49,7 +49,7 @@ impl Backend for Python {
&["list", "--format", "json", "--not-required", "--user"], &["list", "--format", "json", "--not-required", "--user"],
)?; )?;
extract_pacdef_packages(output) extract_pacdef_packages_pipx(output)
} }
fn make_dependency(&self, _packages: &[Package]) -> Result<ExitStatus> { fn make_dependency(&self, _packages: &[Package]) -> Result<ExitStatus> {
@@ -73,10 +73,10 @@ impl Python {
} }
fn get_switches_runtime(&self) -> Switches { fn get_switches_runtime(&self) -> Switches {
if self.get_binary().eq("pip") { match self.get_binary() {
&["list", "--format", "json", "--user"] "pip" => &["list", "--format", "json", "--user"],
} else { "pipx" => &["list", "--json"],
&["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<HashSet<Package>> {
.collect(); .collect();
Ok(result) Ok(result)
} }
fn extract_pacdef_packages_pipx(value: Value) -> Result<HashSet<Package>> {
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)
}