From 7c4741753e489b280742ac6024c1f3e076c38caf Mon Sep 17 00:00:00 2001 From: steven-omaha <35634100+steven-omaha@users.noreply.github.com> Date: Mon, 27 Feb 2023 17:21:56 +0100 Subject: [PATCH] add debian backend --- Cargo.lock | 185 +++++++++++++++++- crates/pacdef_core/Cargo.toml | 10 +- .../pacdef_core/src/backend/actual/debian.rs | 67 +++++++ crates/pacdef_core/src/backend/actual/mod.rs | 2 + crates/pacdef_core/src/backend/mod.rs | 2 + crates/pacdef_core/src/core.rs | 2 +- 6 files changed, 261 insertions(+), 7 deletions(-) create mode 100644 crates/pacdef_core/src/backend/actual/debian.rs diff --git a/Cargo.lock b/Cargo.lock index d074894..2c1cd7f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -36,6 +36,17 @@ version = "1.0.69" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "224afbd727c3d6e4b90103ece64b8d1b67fbb1973b1046c2281eed3f3803f800" +[[package]] +name = "atty" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +dependencies = [ + "hermit-abi 0.1.19", + "libc", + "winapi 0.3.9", +] + [[package]] name = "autocfg" version = "1.1.0" @@ -76,6 +87,16 @@ dependencies = [ "os_str_bytes", ] +[[package]] +name = "codespan-reporting" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3538270d33cc669650c4b093848450d380def10c331d38c768e34cac80576e6e" +dependencies = [ + "termcolor", + "unicode-width", +] + [[package]] name = "const_format" version = "0.2.30" @@ -96,6 +117,50 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "cxx" +version = "1.0.91" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "86d3488e7665a7a483b57e25bdd90d0aeb2bc7608c8d0346acf2ad3f1caf1d62" +dependencies = [ + "cc", + "cxxbridge-flags", + "cxxbridge-macro", + "link-cplusplus", +] + +[[package]] +name = "cxx-build" +version = "1.0.91" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "48fcaf066a053a41a81dfb14d57d99738b767febb8b735c3016e469fac5da690" +dependencies = [ + "cc", + "codespan-reporting", + "once_cell", + "proc-macro2", + "quote", + "scratch", + "syn", +] + +[[package]] +name = "cxxbridge-flags" +version = "1.0.91" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2ef98b8b717a829ca5603af80e1f9e2e48013ab227b68ef37872ef84ee479bf" + +[[package]] +name = "cxxbridge-macro" +version = "1.0.91" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "086c685979a698443656e5cf7856c95c642295a38599f12fb1ff76fb28d19892" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "errno" version = "0.2.8" @@ -104,7 +169,7 @@ checksum = "f639046355ee4f37944e44f60642c6f3a7efa3cf6b78c78a0d989a8ce6c396a1" dependencies = [ "errno-dragonfly", "libc", - "winapi", + "winapi 0.3.9", ] [[package]] @@ -123,6 +188,15 @@ version = "0.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" +[[package]] +name = "hermit-abi" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" +dependencies = [ + "libc", +] + [[package]] name = "hermit-abi" version = "0.3.1" @@ -155,7 +229,7 @@ version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "22e18b0a45d56fe973d6db23972bf5bc46f988a4a2385deac9cc29572f09daef" dependencies = [ - "hermit-abi", + "hermit-abi 0.3.1", "io-lifetimes", "rustix", "windows-sys", @@ -167,12 +241,31 @@ version = "1.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fad582f4b9e86b6caa621cabeb0963332d92eea04729ab12892c2533951e6440" +[[package]] +name = "kernel32-sys" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d" +dependencies = [ + "winapi 0.2.8", + "winapi-build", +] + [[package]] name = "libc" version = "0.2.139" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "201de327520df007757c1f0adce6e827fe8562fbc28bfd9c15571c66ca1f5f79" +[[package]] +name = "link-cplusplus" +version = "1.0.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ecd207c9c713c34f95a097a5b029ac2ce6010530c7b49d7fea24d977dede04f5" +dependencies = [ + "cc", +] + [[package]] name = "linux-raw-sys" version = "0.1.4" @@ -185,6 +278,12 @@ version = "2.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" +[[package]] +name = "numtoa" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8f8bdf33df195859076e54ab11ee78a1b208382d3a26ec40d142ffc1ecc49ef" + [[package]] name = "once_cell" version = "1.17.1" @@ -216,6 +315,7 @@ dependencies = [ "pacdef_macros", "path-absolutize", "regex", + "rust-apt", "serde", "serde_derive", "serde_json", @@ -274,6 +374,24 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "redox_syscall" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fb5a58c1855b4b6819d59012155603f0b22ad30cad752600aadfcb695265519a" +dependencies = [ + "bitflags", +] + +[[package]] +name = "redox_termios" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8440d8acb4fd3d277125b4bd01a6f38aee8d814b3b5fc09b3f2b825d37d3fe8f" +dependencies = [ + "redox_syscall", +] + [[package]] name = "regex" version = "1.7.1" @@ -291,6 +409,18 @@ version = "0.6.28" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "456c603be3e8d448b072f410900c09faf164fbce2d480456f50eea6e25f9c848" +[[package]] +name = "rust-apt" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "51033bc4f66a0c5cf22dd7df910786bf4b2a8a041509332e961e471929955444" +dependencies = [ + "cxx", + "cxx-build", + "once_cell", + "termsize", +] + [[package]] name = "rustix" version = "0.36.8" @@ -311,6 +441,12 @@ version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7b4b9743ed687d4b4bcedf9ff5eaa7398495ae14e61cba0a295704edbc7decde" +[[package]] +name = "scratch" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ddccb15bcce173023b3fedd9436f882a0739b8dfb45e4f6b6002bee5929f61b2" + [[package]] name = "serde" version = "1.0.152" @@ -378,6 +514,18 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "termion" +version = "1.5.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "077185e2eac69c3f8379a4298e1e07cd36beb962290d4a51199acf0fdc10607e" +dependencies = [ + "libc", + "numtoa", + "redox_syscall", + "redox_termios", +] + [[package]] name = "termios" version = "0.3.3" @@ -387,12 +535,31 @@ dependencies = [ "libc", ] +[[package]] +name = "termsize" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e86d824a8e90f342ad3ef4bd51ef7119a9b681b0cc9f8ee7b2852f02ccd2517" +dependencies = [ + "atty", + "kernel32-sys", + "libc", + "termion", + "winapi 0.2.8", +] + [[package]] name = "unicode-ident" version = "1.0.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "84a22b9f218b40614adcb3f4ff08b703773ad44fa9423e4e0d346d5db86e4ebc" +[[package]] +name = "unicode-width" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c0edd1e5b14653f783770bce4a4dabb4a5108a5370a5f5d8cfe8710c361f6c8b" + [[package]] name = "unicode-xid" version = "0.2.4" @@ -405,6 +572,12 @@ version = "0.2.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bc7ed8ba44ca06be78ea1ad2c3682a43349126c8818054231ee6f4748012aed2" +[[package]] +name = "winapi" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a" + [[package]] name = "winapi" version = "0.3.9" @@ -415,6 +588,12 @@ dependencies = [ "winapi-x86_64-pc-windows-gnu", ] +[[package]] +name = "winapi-build" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc" + [[package]] name = "winapi-i686-pc-windows-gnu" version = "0.4.0" @@ -427,7 +606,7 @@ version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" dependencies = [ - "winapi", + "winapi 0.3.9", ] [[package]] diff --git a/crates/pacdef_core/Cargo.toml b/crates/pacdef_core/Cargo.toml index 59445d5..d7afbf8 100644 --- a/crates/pacdef_core/Cargo.toml +++ b/crates/pacdef_core/Cargo.toml @@ -13,7 +13,6 @@ categories = ["command-line-utilities"] features = ["alpm/docs-rs"] [dependencies] -alpm = { version = "2.2", optional = true } anyhow = { workspace = true } clap = "4.1" const_format = { version = "0.2", default-features = false } @@ -21,13 +20,18 @@ path-absolutize = "3.0" regex = "1.7" termios = "0.3" +serde = "1.0" +serde_derive = "1.0" serde_json = "1.0" serde_yaml = "0.9" -serde_derive = "1.0" -serde = "1.0" pacdef_macros = { path = "../pacdef_macros", version = "0.1" } +# backends +alpm = { version = "2.2", optional = true } +rust-apt = { version = "0.5", optional = true } + [features] default = ["arch"] arch = ["dep:alpm"] +debian = ["dep:rust-apt"] diff --git a/crates/pacdef_core/src/backend/actual/debian.rs b/crates/pacdef_core/src/backend/actual/debian.rs new file mode 100644 index 0000000..824a501 --- /dev/null +++ b/crates/pacdef_core/src/backend/actual/debian.rs @@ -0,0 +1,67 @@ +use std::collections::HashSet; +use std::process::Command; +use std::process::ExitStatus; + +use anyhow::{Context, Result}; +use rust_apt::cache::PackageSort; +use rust_apt::new_cache; + +use crate::backend::backend_trait::{Backend, Switches, Text}; +use crate::{impl_backend_constants, Group, Package}; + +#[derive(Debug, Clone)] +pub struct Debian { + pub(crate) packages: HashSet, +} + +const BINARY: Text = "apt"; +const SECTION: Text = "debian"; + +const SWITCHES_INFO: Switches = &["show"]; +const SWITCHES_INSTALL: Switches = &["install"]; +const SWITCHES_MAKE_DEPENDENCY: Switches = &[]; // not needed +const SWITCHES_REMOVE: Switches = &["remove"]; + +impl Backend for Debian { + impl_backend_constants!(); + + fn get_all_installed_packages(&self) -> Result> { + let cache = new_cache!()?; + let sort = PackageSort::default().installed(); + + let mut result = HashSet::new(); + for pkg in cache.packages(&sort) { + result.insert(Package::from(pkg.name().to_string())); + } + Ok(result) + } + + fn get_explicitly_installed_packages(&self) -> Result> { + let cache = new_cache!()?; + let sort = PackageSort::default().installed().manually_installed(); + + let mut result = HashSet::new(); + for pkg in cache.packages(&sort) { + result.insert(Package::from(pkg.name().to_string())); + } + Ok(result) + } + + fn make_dependency(&self, packages: &[Package]) -> Result { + let mut cmd = Command::new("apt-mark"); + cmd.arg("auto"); + for p in packages { + cmd.arg(format!("{p}")); + } + cmd.status() + .with_context(|| format!("running command [{cmd:?}]")) + } +} + +impl Debian { + pub(crate) fn new() -> Self { + Self { + packages: HashSet::new(), + } + } +} diff --git a/crates/pacdef_core/src/backend/actual/mod.rs b/crates/pacdef_core/src/backend/actual/mod.rs index e8bd3ad..9e1cf5c 100644 --- a/crates/pacdef_core/src/backend/actual/mod.rs +++ b/crates/pacdef_core/src/backend/actual/mod.rs @@ -1,3 +1,5 @@ #[cfg(feature = "arch")] pub mod arch; +#[cfg(feature = "debian")] +pub mod debian; pub mod rust; diff --git a/crates/pacdef_core/src/backend/mod.rs b/crates/pacdef_core/src/backend/mod.rs index 2aed837..7a25663 100644 --- a/crates/pacdef_core/src/backend/mod.rs +++ b/crates/pacdef_core/src/backend/mod.rs @@ -14,5 +14,7 @@ use pacdef_macros::Register; pub enum Backends { #[cfg(feature = "arch")] Arch, + #[cfg(feature = "debian")] + Debian, Rust, } diff --git a/crates/pacdef_core/src/core.rs b/crates/pacdef_core/src/core.rs index 74b3139..5050de8 100644 --- a/crates/pacdef_core/src/core.rs +++ b/crates/pacdef_core/src/core.rs @@ -432,7 +432,7 @@ pub const fn get_version_string() -> &'static str { fn get_included_backends() -> Vec<&'static str> { let mut result = vec![]; for backend in Backends::iter() { - result.push(backend.get_section()) + result.push(backend.get_section()); } result.sort_unstable(); result