add python support

This commit is contained in:
steven-omaha
2023-02-27 19:39:25 +01:00
parent 7c4741753e
commit 184f86ebae
6 changed files with 81 additions and 4 deletions
-1
View File
@@ -1,4 +1,3 @@
# To Do
- tutorial
- support apt
@@ -2,4 +2,5 @@
pub mod arch;
#[cfg(feature = "debian")]
pub mod debian;
pub mod python;
pub mod rust;
@@ -0,0 +1,70 @@
use std::collections::HashSet;
use std::process::Command;
use std::process::ExitStatus;
use anyhow::Context;
use anyhow::Result;
use serde_json::Value;
use crate::backend::backend_trait::{Backend, Switches, Text};
use crate::{impl_backend_constants, Group, Package};
#[derive(Debug, Clone)]
pub struct Python {
pub(crate) packages: HashSet<Package>,
}
const BINARY: Text = "pip";
const SECTION: Text = "python";
const SWITCHES_INFO: Switches = &["show"];
const SWITCHES_INSTALL: Switches = &["install"];
const SWITCHES_MAKE_DEPENDENCY: Switches = &[]; // not needed
const SWITCHES_REMOVE: Switches = &["uninstall"];
impl Backend for Python {
impl_backend_constants!();
fn get_all_installed_packages(&self) -> Result<HashSet<Package>> {
let output = run_pip_command(&["list", "--format", "json", "--user"])?;
extract_pacdef_packages(output)
}
fn get_explicitly_installed_packages(&self) -> Result<HashSet<Package>> {
let output = run_pip_command(&["list", "--format", "json", "--not-required", "--user"])?;
extract_pacdef_packages(output)
}
fn make_dependency(&self, _packages: &[Package]) -> Result<ExitStatus> {
todo!()
}
}
fn run_pip_command(args: &[&str]) -> Result<Value> {
let mut cmd = Command::new(BINARY);
cmd.args(args);
let output = String::from_utf8(cmd.output()?.stdout)?;
let val: Value = serde_json::from_str(&output)?;
Ok(val)
}
impl Python {
pub(crate) fn new() -> Self {
Self {
packages: HashSet::new(),
}
}
}
fn extract_pacdef_packages(value: Value) -> Result<HashSet<Package>> {
let result = value
.as_array()
.context("getting inner json array")?
.iter()
.map(|node| node["name"].as_str().unwrap())
.map(Package::from)
.collect();
Ok(result)
}
+1
View File
@@ -16,5 +16,6 @@ pub enum Backends {
Arch,
#[cfg(feature = "debian")]
Debian,
Python,
Rust,
}
@@ -24,6 +24,12 @@ impl From<String> for Package {
}
}
impl From<&str> for Package {
fn from(value: &str) -> Self {
Self::from(value.to_string())
}
}
impl Package {
fn split_into_name_and_repo(s: &str) -> (String, Option<String>) {
let mut iter = s.split('/').rev();
+3 -3
View File
@@ -1,6 +1,6 @@
use std::rc::Rc;
use anyhow::Result;
use anyhow::{ensure, Result};
use crate::backend::Backend;
use crate::{Group, Package};
@@ -30,11 +30,11 @@ impl Strategy {
pub(super) fn execute(self) -> Result<()> {
if !self.delete.is_empty() {
self.backend.remove_packages(&self.delete)?;
ensure!(self.backend.remove_packages(&self.delete)?.success());
}
if !self.as_dependency.is_empty() {
self.backend.make_dependency(&self.as_dependency)?;
ensure!(self.backend.make_dependency(&self.as_dependency)?.success());
}
if !self.assign_group.is_empty() {