From ad34e8bf8b627b7a87e1ca5a898be98273de8533 Mon Sep 17 00:00:00 2001 From: trivernis Date: Fri, 2 Sep 2022 21:29:21 +0200 Subject: [PATCH] Add filtering for already installed dependencies --- Cargo.lock | 72 +++++++++++----------- Cargo.toml | 2 +- src/builder/makepkg.rs | 1 + src/builder/pacman.rs | 13 +++- src/internal/clean.rs | 4 +- src/internal/dependencies.rs | 50 +++++++++++++--- src/internal/initialise.rs | 112 ----------------------------------- src/internal/mod.rs | 4 -- src/internal/utils.rs | 20 +++++-- src/logging/handler.rs | 2 +- src/logging/output.rs | 4 +- src/main.rs | 4 +- 12 files changed, 111 insertions(+), 177 deletions(-) delete mode 100644 src/internal/initialise.rs diff --git a/Cargo.lock b/Cargo.lock index fbbed4a..0acdd4e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -158,9 +158,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "clap" -version = "3.2.17" +version = "3.2.20" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "29e724a68d9319343bb3328c9cc2dfde263f4b3142ee1059a9980580171c954b" +checksum = "23b71c3ce99b7611011217b366d923f1d0a7e07a92bb2dbf1e84508c673ca3bd" dependencies = [ "atty", "bitflags", @@ -185,9 +185,9 @@ dependencies = [ [[package]] name = "clap_derive" -version = "3.2.17" +version = "3.2.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "13547f7012c01ab4a0e8f8967730ada8f9fdf419e8b6c792788f39cf4e46eefa" +checksum = "ea0c8bce528c4be4da13ea6fead8965e95b6073585a2f05204bd8f4119f82a65" dependencies = [ "heck", "proc-macro-error", @@ -403,9 +403,9 @@ dependencies = [ [[package]] name = "futures" -version = "0.3.23" +version = "0.3.24" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ab30e97ab6aacfe635fad58f22c2bb06c8b685f7421eb1e064a729e2a5f481fa" +checksum = "7f21eda599937fba36daeb58a22e8f5cee2d14c4a17b5b7739c7c8e5e3b8230c" dependencies = [ "futures-channel", "futures-core", @@ -418,9 +418,9 @@ dependencies = [ [[package]] name = "futures-channel" -version = "0.3.23" +version = "0.3.24" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2bfc52cbddcfd745bf1740338492bb0bd83d76c67b445f91c5fb29fae29ecaa1" +checksum = "30bdd20c28fadd505d0fd6712cdfcb0d4b5648baf45faef7f852afb2399bb050" dependencies = [ "futures-core", "futures-sink", @@ -428,15 +428,15 @@ dependencies = [ [[package]] name = "futures-core" -version = "0.3.23" +version = "0.3.24" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d2acedae88d38235936c3922476b10fced7b2b68136f5e3c03c2d5be348a1115" +checksum = "4e5aa3de05362c3fb88de6531e6296e85cde7739cccad4b9dfeeb7f6ebce56bf" [[package]] name = "futures-executor" -version = "0.3.23" +version = "0.3.24" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1d11aa21b5b587a64682c0094c2bdd4df0076c5324961a40cc3abd7f37930528" +checksum = "9ff63c23854bee61b6e9cd331d523909f238fc7636290b96826e9cfa5faa00ab" dependencies = [ "futures-core", "futures-task", @@ -445,15 +445,15 @@ dependencies = [ [[package]] name = "futures-io" -version = "0.3.23" +version = "0.3.24" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "93a66fc6d035a26a3ae255a6d2bca35eda63ae4c5512bef54449113f7a1228e5" +checksum = "bbf4d2a7a308fd4578637c0b17c7e1c7ba127b8f6ba00b29f717e9655d85eb68" [[package]] name = "futures-macro" -version = "0.3.23" +version = "0.3.24" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0db9cce532b0eae2ccf2766ab246f114b56b9cf6d445e00c2549fbc100ca045d" +checksum = "42cd15d1c7456c04dbdf7e88bcd69760d74f3a798d6444e16974b505b0e62f17" dependencies = [ "proc-macro2", "quote", @@ -462,21 +462,21 @@ dependencies = [ [[package]] name = "futures-sink" -version = "0.3.23" +version = "0.3.24" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ca0bae1fe9752cf7fd9b0064c674ae63f97b37bc714d745cbde0afb7ec4e6765" +checksum = "21b20ba5a92e727ba30e72834706623d94ac93a725410b6a6b6fbc1b07f7ba56" [[package]] name = "futures-task" -version = "0.3.23" +version = "0.3.24" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "842fc63b931f4056a24d59de13fb1272134ce261816e063e634ad0c15cdc5306" +checksum = "a6508c467c73851293f390476d4491cf4d227dbabcd4170f3bb6044959b294f1" [[package]] name = "futures-util" -version = "0.3.23" +version = "0.3.24" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f0828a5471e340229c11c77ca80017937ce3c58cb788a17e5f1c2d5c485a9577" +checksum = "44fb6cb1be61cc1d2e43b262516aafcf63b241cffdb1d3fa115f91d9c7b09c90" dependencies = [ "futures-channel", "futures-core", @@ -571,9 +571,9 @@ dependencies = [ [[package]] name = "httparse" -version = "1.7.1" +version = "1.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "496ce29bb5a52785b44e0f7ca2847ae0bb839c9bd28f69acac9b99d461c0c04c" +checksum = "d897f394bad6a705d5f4104762e116a75639e470d80901eed05a860a95cb1904" [[package]] name = "httpdate" @@ -724,9 +724,9 @@ checksum = "8371e4e5341c3a96db127eb2465ac681ced4c433e01dd0e938adbef26ba93ba5" [[package]] name = "lock_api" -version = "0.4.7" +version = "0.4.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "327fa5b6a6940e4699ec49a9beae1ea4845c6bab9314e4f84ac68742139d8c53" +checksum = "9f80bf5aacaf25cbfc8210d1cfb718f2bf3b11c4c54e5afe36c236853a8ec390" dependencies = [ "autocfg", "scopeguard", @@ -834,9 +834,9 @@ dependencies = [ [[package]] name = "once_cell" -version = "1.13.1" +version = "1.14.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "074864da206b4973b84eb91683020dbefd6a8c3f0f38e054d93954e891935e4e" +checksum = "2f7254b99e31cad77da24b08ebf628882739a608578bb1bcdfc1f9c21260d7c0" [[package]] name = "openssl" @@ -1245,9 +1245,9 @@ checksum = "f67ad224767faa3c7d8b6d91985b78e70a1324408abcb1cfcc2be4c06bc06043" [[package]] name = "socket2" -version = "0.4.6" +version = "0.4.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "10c98bba371b9b22a71a9414e420f92ddeb2369239af08200816169d5e2dd7aa" +checksum = "02e2d2db9033d13a1567121ddd7a095ee144db4e1ca1b1bda3419bc0da294ebd" dependencies = [ "libc", "winapi", @@ -1317,18 +1317,18 @@ dependencies = [ [[package]] name = "thiserror" -version = "1.0.32" +version = "1.0.33" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f5f6586b7f764adc0231f4c79be7b920e766bb2f3e51b3661cdb263828f19994" +checksum = "3d0a539a918745651435ac7db7a18761589a94cd7e94cd56999f828bf73c8a57" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.32" +version = "1.0.33" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "12bafc5b54507e0149cdf1b145a5d80ab80a90bcd9275df43d4fff68460f6c21" +checksum = "c251e90f708e16c49a16f4917dc2131e75222b72edfa9cb7f7c58ae56aae0c09" dependencies = [ "proc-macro2", "quote", @@ -1372,9 +1372,9 @@ checksum = "cda74da7e1a664f795bb1f8a87ec406fb89a02522cf6e50620d016add6dbbf5c" [[package]] name = "tokio" -version = "1.20.1" +version = "1.21.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7a8325f63a7d4774dd041e363b2409ed1c5cbbd0f867795e661df066b2b0a581" +checksum = "89797afd69d206ccd11fb0ea560a44bbb87731d020670e79416d442919257d42" dependencies = [ "autocfg", "bytes", diff --git a/Cargo.toml b/Cargo.toml index 16e5a51..0da120b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -50,5 +50,5 @@ directories = "4.0.1" console = "0.15.1" [dependencies.tokio] -version = "1.20.1" +version = "1.21.0" features = ["rt", "rt-multi-thread", "io-std", "io-util", "process", "time", "macros", "tracing", "fs"] diff --git a/src/builder/makepkg.rs b/src/builder/makepkg.rs index e0ee16d..2bd4800 100644 --- a/src/builder/makepkg.rs +++ b/src/builder/makepkg.rs @@ -55,6 +55,7 @@ impl MakePkgBuilder { } /// Mark packages as non-explicitly installed + #[allow(clippy::wrong_self_convention)] pub fn as_deps(mut self, as_deps: bool) -> Self { self.as_deps = as_deps; diff --git a/src/builder/pacman.rs b/src/builder/pacman.rs index 4822c25..2e17ee0 100644 --- a/src/builder/pacman.rs +++ b/src/builder/pacman.rs @@ -81,6 +81,7 @@ pub struct PacmanQueryBuilder { #[derive(Debug)] enum PacmanQueryType { Foreign, + All, Info, } @@ -106,7 +107,11 @@ impl PacmanQueryBuilder { packages: Vec::new(), } } - /// Query for foreign packages + + pub fn all() -> Self { + Self::new(PacmanQueryType::All) + } + pub fn foreign() -> Self { Self::new(PacmanQueryType::Foreign) } @@ -135,12 +140,12 @@ impl PacmanQueryBuilder { self } - #[tracing::instrument(level = "debug")] + #[tracing::instrument(level = "trace")] pub async fn query(self) -> AppResult<()> { self.build_command().wait_success().await } - #[tracing::instrument(level = "debug")] + #[tracing::instrument(level = "trace")] pub async fn query_with_output(self) -> AppResult> { let output = self.build_command().wait_with_output().await?; let packages = output @@ -153,6 +158,7 @@ impl PacmanQueryBuilder { version: version.to_string(), }) .collect(); + tracing::debug!("Query result: {packages:?}"); Ok(packages) } @@ -163,6 +169,7 @@ impl PacmanQueryBuilder { command = match self.query_type { PacmanQueryType::Foreign => command.arg("-m"), PacmanQueryType::Info => command.arg("-i"), + PacmanQueryType::All => command, }; command = command.arg("--color"); diff --git a/src/internal/clean.rs b/src/internal/clean.rs index 558f278..465d996 100644 --- a/src/internal/clean.rs +++ b/src/internal/clean.rs @@ -4,9 +4,9 @@ pub fn clean(a: &[String]) -> Vec { let cleaned = a .iter() .map(|name| { - name.split_once("=") + name.split_once('=') .map(|n| n.0.to_string()) - .unwrap_or(name.to_string()) + .unwrap_or_else(|| name.to_string()) }) .collect(); diff --git a/src/internal/dependencies.rs b/src/internal/dependencies.rs index e767f82..2bdf884 100644 --- a/src/internal/dependencies.rs +++ b/src/internal/dependencies.rs @@ -3,7 +3,7 @@ use std::collections::HashSet; use aur_rpc::PackageInfo; use futures::future; -use crate::builder::pacman::PacmanSearchBuilder; +use crate::builder::pacman::{PacmanQueryBuilder, PacmanSearchBuilder}; use super::error::AppResult; use lazy_regex::regex; @@ -71,8 +71,10 @@ impl DependencyInformation { let mut packages_to_resolve: HashSet = package .make_depends .iter() - .filter_map(Self::map_dep_to_name) + .filter_map(|d| Self::map_dep_to_name(d)) .collect(); + + Self::filter_fulfilled_dependencies(&mut packages_to_resolve).await?; let mut already_searched = HashSet::new(); let mut dependencies = DependencyCollection::default(); @@ -91,6 +93,7 @@ impl DependencyInformation { .append(&mut not_found.into_iter().collect()); packages_to_resolve = Self::get_filtered_make_depends(&aur_packages, &already_searched); + Self::filter_fulfilled_dependencies(&mut packages_to_resolve).await?; dependencies.aur.append(&mut aur_packages); } @@ -103,8 +106,9 @@ impl DependencyInformation { let mut packages_to_resolve: HashSet = package .depends .iter() - .filter_map(Self::map_dep_to_name) + .filter_map(|d| Self::map_dep_to_name(d)) .collect(); + Self::filter_fulfilled_dependencies(&mut packages_to_resolve).await?; let mut already_searched = HashSet::new(); let mut dependencies = DependencyCollection::default(); @@ -123,6 +127,7 @@ impl DependencyInformation { .append(&mut not_found.into_iter().collect()); packages_to_resolve = Self::get_filtered_depends(&aur_packages, &already_searched); + Self::filter_fulfilled_dependencies(&mut packages_to_resolve).await?; dependencies.aur.append(&mut aur_packages); } @@ -143,28 +148,46 @@ impl DependencyInformation { } fn get_filtered_make_depends( - aur_packages: &Vec, + aur_packages: &[PackageInfo], searched: &HashSet, ) -> HashSet { aur_packages .iter() - .flat_map(|p| p.make_depends.iter().filter_map(Self::map_dep_to_name)) + .flat_map(|p| { + p.make_depends + .iter() + .filter_map(|d| Self::map_dep_to_name(d)) + }) .filter(|d| !searched.contains(d)) .collect() } fn get_filtered_depends( - aur_packages: &Vec, + aur_packages: &[PackageInfo], searched: &HashSet, ) -> HashSet { aur_packages .iter() - .flat_map(|p| p.depends.iter().filter_map(Self::map_dep_to_name)) + .flat_map(|p| p.depends.iter().filter_map(|d| Self::map_dep_to_name(d))) .filter(|d| !searched.contains(d)) .collect() } - fn map_dep_to_name(dep: &String) -> Option { + async fn filter_fulfilled_dependencies(deps: &mut HashSet) -> AppResult<()> { + let mut fulfilled = HashSet::new(); + + for dep in deps.iter() { + if get_dependency_fulfilled(dep.clone()).await? { + fulfilled.insert(dep.clone()); + } + } + + deps.retain(|pkg| !fulfilled.contains(pkg)); + + Ok(()) + } + + fn map_dep_to_name(dep: &str) -> Option { Dependency::try_from_str(dep).map(|d| d.name) } @@ -205,3 +228,14 @@ impl Dependency { }) } } + +#[tracing::instrument(level = "trace")] +async fn get_dependency_fulfilled(name: String) -> AppResult { + let not_found = PacmanQueryBuilder::all() + .package(name) + .query_with_output() + .await? + .is_empty(); + + Ok(!not_found) +} diff --git a/src/internal/initialise.rs b/src/internal/initialise.rs deleted file mode 100644 index db4305b..0000000 --- a/src/internal/initialise.rs +++ /dev/null @@ -1,112 +0,0 @@ -use std::env; -use std::path::Path; -use std::process::Command; - -use crate::{crash, internal::exit_code::AppExitCode}; - -pub fn init() { - let homedir = env::var("HOME").unwrap(); - - if !Path::new(&format!("{}/.local/share/ame", homedir)).exists() { - let r = std::fs::create_dir_all(format!("{}/.local/share/ame", homedir)); - match r { - Ok(_) => { - tracing::debug!("Created path: {}/.local/share/ame", homedir); - } - Err(e) => { - crash!( - AppExitCode::FailedCreatingPaths, - "Couldn't create path: {}/.local/share/ame: {}", - homedir, - e, - ); - } - } - } - - if !Path::new(&format!("{}/.cache/ame/", homedir)).exists() { - let r = std::fs::create_dir_all(format!("{}/.cache/ame", homedir)); - match r { - Ok(_) => { - tracing::debug!("Created path: {}/.cache/ame", homedir); - } - Err(e) => { - crash!( - AppExitCode::FailedCreatingPaths, - "Couldn't create path: {}/.cache/ame: {}", - homedir, - e, - ); - } - } - } else { - let r = std::fs::remove_dir_all(format!("{}/.cache/ame", homedir)); - match r { - Ok(_) => { - tracing::debug!("Removing cache: {}/.cache/ame", homedir); - } - Err(e) => { - crash!( - AppExitCode::FailedCreatingPaths, - "Couldn't remove path: {}/.cache/ame: {}", - homedir, - e, - ); - } - } - let r2 = std::fs::create_dir_all(format!("{}/.cache/ame", homedir)); - match r2 { - Ok(_) => { - tracing::debug!("Created path: {}/.cache/ame", homedir); - } - Err(e2) => { - crash!( - AppExitCode::FailedCreatingPaths, - "Couldn't create path: {}/.cache/ame: {}", - homedir, - e2, - ); - } - } - } - - let r = Command::new("chmod") - .arg("-R") - .arg("770") - .arg(format!("{}/.cache/ame", homedir)) - .status(); - match r { - Ok(_) => { - tracing::debug!("Set correct permissions for path: {}/.cache/ame", homedir); - } - Err(e) => { - crash!( - AppExitCode::FailedCreatingPaths, - "Couldn't set permissions for path: {}/.cache/ame: {}", - homedir, - e, - ); - } - }; - let r = Command::new("chmod") - .arg("-R") - .arg("770") - .arg(format!("{}/.local/share/ame", homedir)) - .status(); - match r { - Ok(_) => { - tracing::debug!( - "Set correct permissions for path: {}/.local/share/ame", - homedir - ); - } - Err(e) => { - crash!( - AppExitCode::FailedCreatingPaths, - "Couldn't set permissions for path: {}/.local/share/ame: {}", - homedir, - e, - ); - } - }; -} diff --git a/src/internal/mod.rs b/src/internal/mod.rs index 2e63968..81b1d0c 100644 --- a/src/internal/mod.rs +++ b/src/internal/mod.rs @@ -1,9 +1,6 @@ pub use clean::*; pub use clean::*; pub use detect::*; -pub use initialise::*; -pub use initialise::*; -pub use sort::*; pub use sort::*; pub use sudoloop::*; @@ -15,7 +12,6 @@ mod detect; pub mod error; pub mod exit_code; pub mod fs_utils; -mod initialise; pub mod rpc; mod sort; pub mod structs; diff --git a/src/internal/utils.rs b/src/internal/utils.rs index 83b0340..21a2282 100644 --- a/src/internal/utils.rs +++ b/src/internal/utils.rs @@ -10,6 +10,8 @@ use crate::logging::get_logger; use crate::logging::handler::PromptDefault; use lazy_static::lazy_static; +use super::error::{AppError, SilentUnwrap}; + #[macro_export] /// Macro for printing a message and destructively exiting macro_rules! crash { @@ -55,13 +57,11 @@ pub fn prompt_yn(question: String, prompt_default: PromptDefault) -> bool { } pub fn get_cache_dir() -> &'static Path { - let cache_dir = get_directories().cache_dir(); - - if !cache_dir.exists() { - fs::create_dir_all(cache_dir).unwrap(); + lazy_static! { + static ref CACHE_DIR: &'static Path = create_if_not_exist(get_directories().cache_dir()); } - cache_dir + *CACHE_DIR } fn get_directories() -> &'static ProjectDirs { @@ -72,6 +72,16 @@ fn get_directories() -> &'static ProjectDirs { &*DIRECTORIES } +fn create_if_not_exist(dir: &Path) -> &Path { + if !dir.exists() { + fs::create_dir_all(dir) + .map_err(AppError::from) + .silent_unwrap(AppExitCode::FailedCreatingPaths) + } + + dir +} + pub fn wrap_text>(s: S) -> Vec { wrap(s.as_ref(), get_wrap_options()) .into_iter() diff --git a/src/logging/handler.rs b/src/logging/handler.rs index 8e09795..8db545b 100644 --- a/src/logging/handler.rs +++ b/src/logging/handler.rs @@ -145,7 +145,7 @@ impl LogHandler { let mut output_type = self.output_type.write(); if let OutputType::MultiProgress(mp) = &*output_type { - Arc::new(mp.add(pb.clone())) + Arc::new(mp.add(pb)) } else { let pb = Arc::new(pb); *output_type = OutputType::Progress(pb.clone()); diff --git a/src/logging/output.rs b/src/logging/output.rs index c3910db..56e4d02 100644 --- a/src/logging/output.rs +++ b/src/logging/output.rs @@ -6,7 +6,7 @@ use crate::internal::dependencies::DependencyInformation; use super::get_logger; -pub fn print_dependency_list(dependencies: &Vec) -> bool { +pub fn print_dependency_list(dependencies: &[DependencyInformation]) -> bool { let (deps_repo, makedeps_repo, deps_aur, makedeps_aur) = dependencies .iter() .map(|d| { @@ -60,7 +60,7 @@ pub fn print_dependency_list(dependencies: &Vec) -> bool empty } -pub fn print_aur_package_list(packages: &Vec) { +pub fn print_aur_package_list(packages: &[PackageInfo]) { get_logger().print_list( packages.iter().map(|pkg| { format!( diff --git a/src/main.rs b/src/main.rs index 80366a9..51d629b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,7 +7,7 @@ use internal::error::SilentUnwrap; use crate::args::{InstallArgs, Operation, QueryArgs, RemoveArgs, SearchArgs}; use crate::internal::detect; use crate::internal::exit_code::AppExitCode; -use crate::internal::{init, sort, start_sudoloop, structs::Options}; +use crate::internal::{sort, start_sudoloop, structs::Options}; use clap_complete::{Generator, Shell}; use std::str::FromStr; @@ -35,8 +35,6 @@ async fn main() { asdeps: false, }; - init(); - if args.sudoloop { start_sudoloop().await; }