From a78bd150fb7ebec8edd64ffa6fa1ab4547860295 Mon Sep 17 00:00:00 2001 From: ripytide <62516857+ripytide@users.noreply.github.com> Date: Thu, 29 Aug 2024 14:23:06 +0100 Subject: [PATCH] remove rust-apt dependency and un-comment the apt backend (#1) --- Cargo.toml | 2 - src/backend/apt.rs | 153 ++++++++++++++++++++++----------------------- src/backend/mod.rs | 9 +-- 3 files changed, 79 insertions(+), 85 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a38e7e9..5cde4cb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,8 +26,6 @@ toml = "0.8" derive_more = "0.99" itertools = "0.12" -# rust-apt = "0.7" - [profile.release] lto = "off" opt-level = "z" diff --git a/src/backend/apt.rs b/src/backend/apt.rs index e14c1f2..94e0cae 100644 --- a/src/backend/apt.rs +++ b/src/backend/apt.rs @@ -1,91 +1,86 @@ -// use std::collections::BTreeMap; +use std::collections::BTreeMap; -// use anyhow::Result; -// use rust_apt::cache::{PackageSort, Sort}; -// use rust_apt::new_cache; +use anyhow::Result; -// use crate::cmd::{command_found, run_args}; -// use crate::prelude::*; +use crate::cmd::{command_found, run_args, run_args_for_stdout}; +use crate::prelude::*; -// #[derive(Debug, Copy, Clone, derive_more::Display)] -// pub struct Apt; +#[derive(Debug, Copy, Clone, derive_more::Display)] +pub struct Apt; -// #[derive(Debug, Clone)] -// pub struct AptQueryInfo { -// explicit: bool, -// } +#[derive(Debug, Clone)] +pub struct AptQueryInfo { + explicit: bool, +} -// pub struct AptMakeImplicit; +pub struct AptMakeImplicit; -// impl Backend for Apt { -// type PackageId = String; -// type RemoveOptions = (); -// type InstallOptions = (); -// type QueryInfo = AptQueryInfo; -// type Modification = AptMakeImplicit; +impl Backend for Apt { + type PackageId = String; + type RemoveOptions = (); + type InstallOptions = (); + type QueryInfo = AptQueryInfo; + type Modification = AptMakeImplicit; -// fn query_installed_packages(_: &Config) -> Result> { -// if !command_found("apt") { -// return Ok(BTreeMap::new()); -// } + fn query_installed_packages(_: &Config) -> Result> { + if !command_found("apt-mark") { + return Ok(BTreeMap::new()); + } -// let cache = new_cache!()?; + // See https://askubuntu.com/questions/2389/how-to-list-manually-installed-packages + // for a run-down of methods for finding lists of + // explicit/dependency packages. It doesn't seem as if apt was + // designed with this use-case in mind so there are lots and + // lots of different methods all of which seem to have + // caveats. + let explicit = run_args_for_stdout(["apt-mark", "showmanual"])?; + let dependency = run_args_for_stdout(["apt-mark", "showauto"])?; -// let packages = cache.packages(&PackageSort { -// names: true, -// upgradable: Sort::Enable, -// virtual_pkgs: Sort::Enable, -// installed: Sort::Enable, -// auto_installed: Sort::Enable, -// auto_removable: Sort::Enable, -// })?; + Ok(dependency + .lines() + .map(|x| (x.to_string(), AptQueryInfo { explicit: false })) + .chain( + explicit + .lines() + .map(|x| (x.to_string(), AptQueryInfo { explicit: true })), + ) + .collect()) + } -// Ok(packages -// .map(|x| { -// ( -// x.name().to_string(), -// AptQueryInfo { -// explicit: !x.is_auto_installed(), -// }, -// ) -// }) -// .collect()) -// } + fn install_packages( + packages: &BTreeMap, + no_confirm: bool, + _: &Config, + ) -> Result<()> { + run_args( + ["apt-get", "install"] + .into_iter() + .chain(Some("--yes").filter(|_| no_confirm)) + .chain(packages.keys().map(String::as_str)), + ) + } -// fn install_packages( -// packages: &BTreeMap, -// no_confirm: bool, -// _: &Config, -// ) -> Result<()> { -// run_args( -// ["apt", "install"] -// .into_iter() -// .chain(Some("--yes").filter(|_| no_confirm)) -// .chain(packages.keys().map(String::as_str)), -// ) -// } + fn modify_packages( + packages: &BTreeMap, + _: &Config, + ) -> Result<()> { + run_args( + ["apt-mark", "auto"] + .into_iter() + .chain(packages.keys().map(String::as_str)), + ) + } -// fn modify_packages( -// packages: &BTreeMap, -// _: &Config, -// ) -> Result<()> { -// run_args( -// ["apt-mark", "auto"] -// .into_iter() -// .chain(packages.keys().map(String::as_str)), -// ) -// } - -// fn remove_packages( -// packages: &BTreeMap, -// no_confirm: bool, -// _: &Config, -// ) -> Result<()> { -// run_args( -// ["apt", "remove"] -// .into_iter() -// .chain(Some("--yes").filter(|_| no_confirm)) -// .chain(packages.keys().map(String::as_str)), -// ) -// } -// } + fn remove_packages( + packages: &BTreeMap, + no_confirm: bool, + _: &Config, + ) -> Result<()> { + run_args( + ["apt-get", "remove"] + .into_iter() + .chain(Some("--yes").filter(|_| no_confirm)) + .chain(packages.keys().map(String::as_str)), + ) + } +} diff --git a/src/backend/mod.rs b/src/backend/mod.rs index d74dc26..8d0e580 100644 --- a/src/backend/mod.rs +++ b/src/backend/mod.rs @@ -1,4 +1,4 @@ -// pub mod apt; +pub mod apt; pub mod arch; pub mod cargo; pub mod dnf; @@ -12,10 +12,11 @@ use std::{collections::BTreeMap, str::FromStr}; use crate::prelude::*; use anyhow::{Context, Result}; +use apt::Apt; #[derive(Debug, Copy, Clone, derive_more::Display)] pub enum AnyBackend { - // Apt(Apt), + Apt(Apt), Arch(Arch), Cargo(Cargo), Dnf(Dnf), @@ -27,8 +28,8 @@ pub enum AnyBackend { } impl AnyBackend { - pub const ALL: [Self; 8] = [ - // Self::Apt(Apt), + pub const ALL: [Self; 9] = [ + Self::Apt(Apt), Self::Arch(Arch), Self::Cargo(Cargo), Self::Dnf(Dnf),