Skip to content

Commit

Permalink
feat(check): check for old packages
Browse files Browse the repository at this point in the history
This only considers packages which were explicitly installed, and which
aren't depended on by anything. So these could be considered the most
"top level" packages. It's common to install things and then forget
about them, incurring a perpetual upgrade cost when doing -Syu.
  • Loading branch information
fosskers committed Jun 9, 2024
1 parent 3a1a66d commit 01c5dad
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
3 changes: 3 additions & 0 deletions rust/aura-pm/i18n/en-US/aura_pm.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@ check-cache-missing = Every installed official package has a tarball?
check-cache-missing-fix = Fix: You can download missing official tarballs with { $command }
check-cache-missing-for = Every installed AUR package has a tarball?
check-cache-missing-for-fix = Fix: View the missing packages with { $cmd } and reinstall them manually.
check-pkgs = Package Status
check-pkgs-old = All explicitly installed, non-dep packages are up to date?
check-pkgs-old-warn = { $pkg } was last updated { $days } ago.
# Configuration (conf)
conf-toml-err = Failed to serialize current config.
Expand Down
56 changes: 56 additions & 0 deletions rust/aura-pm/src/command/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::error::Nested;
use crate::localization::Localised;
use crate::utils::PathStr;
use crate::{aura, executable, green};
use alpm::PackageReason;
use colored::*;
use from_variants::FromVariants;
use i18n_embed::fluent::FluentLanguageLoader;
Expand All @@ -16,6 +17,7 @@ use std::collections::HashSet;
use std::ops::Not;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::time::{SystemTime, UNIX_EPOCH};

pub(crate) const GOOD: &str = "✓";
pub(crate) const WARN: &str = "!";
Expand Down Expand Up @@ -58,6 +60,7 @@ pub(crate) fn check(fll: &FluentLanguageLoader, env: &Env) -> Result<(), Error>
makepkg_config(fll);
snapshots(fll, &env.backups.snapshots, &caches);
cache(fll, &alpm, pool, &caches);
packages(fll, &alpm);
green!(fll, "common-done");

Ok(())
Expand Down Expand Up @@ -450,3 +453,56 @@ fn pacnew_work() -> Option<Vec<(PathBuf, u64)>> {

Some(bads)
}

fn packages(fll: &FluentLanguageLoader, alpm: &Alpm) {
aura!(fll, "check-pkgs");
old_packages(fll, alpm);
}

fn old_packages(fll: &FluentLanguageLoader, alpm: &Alpm) {
let now = SystemTime::now();

if let Ok(dur) = now.duration_since(UNIX_EPOCH) {
let sec = dur.as_secs();

let old: Vec<_> = alpm
.as_ref()
.localdb()
.pkgs()
.into_iter()
// Only consider packages that you explicitly installed...
.filter(|p| p.reason() == PackageReason::Explicit)
// ...and aren't required by anything.
.filter(|p| p.required_by().is_empty())
.filter_map(|p| {
p.install_date().and_then(|id| {
let diff = (sec - id as u64) / SECS_IN_DAY;
if diff > 365 {
Some((p, diff))
} else {
None
}
})
})
.collect();

let good = old.is_empty();
let symb = if good { GOOD.green() } else { WARN.yellow() };
println!(" [{}] {}", symb, fl!(fll, "check-pkgs-old"));

let len = old.len();
for (i, (p, diff)) in old.into_iter().enumerate() {
let pkg = p.name().cyan().to_string();

let day = if diff < 365 * 2 {
diff.to_string().yellow().to_string()
} else {
diff.to_string().red().to_string()
};

let msg = fl!(fll, "check-pkgs-old-warn", pkg = pkg, days = day);
let arrow = if i + 1 == len { "└─" } else { "├─" };
println!(" {} {}", arrow, msg);
}
}
}

0 comments on commit 01c5dad

Please sign in to comment.