Files
packie/src/graph.rs
T
2026-10-11 13:47:02 +00:00

225 lines
6.7 KiB
Rust

use crate::{
package::{PkgIdent, PkgSpec},
repo::RepoPackage,
};
use itertools::Itertools;
use rust_i18n::t;
impl super::Packie {
pub fn calculate_installation(
&mut self,
pkgs: &[PkgSpec],
options: &CalculateInstallationOptions,
) -> Result<Vec<InstallationNode>, GraphError> {
let mut all = Vec::with_capacity(pkgs.len() * 4);
for pkg in pkgs {
let mut current = self._calculate_installation(pkg, options)?;
all.append(&mut current);
}
let manual_install: Vec<(PkgIdent, PkgSpec)> = all
.iter()
.filter(|x| !x.auto_installed)
.map(|x| {
(
x.repo_pkg.pkg_manifest.pkg_ident(),
x.install_pkgspec.clone(),
)
})
.collect();
let mut all: Vec<_> = all
.into_iter()
.unique_by(|x| x.repo_pkg.pkg_manifest.pkg_ident())
.collect();
for pkg in &mut all {
let manual = manual_install
.iter()
.find(|(pkgid, _)| pkgid == &pkg.repo_pkg.pkg_manifest.pkg_ident());
if let Some((_, install_pkgspec)) = manual {
pkg.auto_installed = false;
pkg.install_pkgspec = install_pkgspec.clone();
}
}
Ok(all)
}
fn _calculate_installation(
&mut self,
desired: &PkgSpec,
options: &CalculateInstallationOptions,
) -> Result<Vec<InstallationNode>, GraphError> {
let prior_arch = self.profile.host_arch.clone();
let mut query = self.query_repo();
let mut depgraph = Vec::new();
let mut stack = vec![desired.clone()];
loop {
let Some(pkgspec) = stack.pop() else {
break;
};
let mut found = query.search_package(&pkgspec);
if found.is_empty() {
return Err(GraphError::NotFound(pkgspec.to_string()));
}
let prior_pkg = prior_pkg(
found.iter().map(|x| x.pkg_manifest.pkg_ident()),
&prior_arch,
);
let prior_pkg = found
.iter()
.enumerate()
.find(|(_, p)| p.pkg_manifest.pkg_ident() == prior_pkg)
.unwrap()
.0;
let repo_pkg = found.remove(prior_pkg);
let already_installed = !self
.search_installation(&pkgspec)
.unwrap_or_default()
.is_empty();
if already_installed && !(&pkgspec == desired && options.no_existence_check) {
continue;
}
depgraph.push(InstallationNode {
repo_pkg: repo_pkg.clone(),
auto_installed: &pkgspec != desired,
install_pkgspec: pkgspec,
});
for dep in &repo_pkg.pkg_manifest.dependencies {
stack.push(dep.clone());
}
if !options.skip_recommendations {
for recommended in &repo_pkg.pkg_manifest.recommendations {
stack.push(recommended.clone());
}
}
}
depgraph.reverse();
Ok(depgraph)
}
pub fn calculate_upgradation(
&mut self,
pkgs: Vec<PkgSpec>,
) -> Result<UpgradationSummary, GraphError> {
let mut installations = Vec::new();
if pkgs.is_empty() {
self.for_each_installation(|x| Ok::<_, rusqlite::Error>(installations.push(x)))
.unwrap();
} else {
for pkg in pkgs {
let mut searched = self.search_installation(&pkg).unwrap_or_default();
installations.append(&mut searched);
}
}
let mut removes = Vec::new();
let mut installs = Vec::new();
for pkg in installations {
let mut summary = self._calculate_upgradation(&pkg.pkg_manifest.pkg_ident())?;
removes.append(&mut summary.removes);
installs.append(&mut summary.installs);
}
let removes = removes.into_iter().unique().collect();
let installs = installs
.into_iter()
.unique_by(|x| x.repo_pkg.pkg_manifest.pkg_ident())
.collect();
Ok(UpgradationSummary { removes, installs })
}
fn _calculate_upgradation(&mut self, pkg: &PkgIdent) -> Result<UpgradationSummary, GraphError> {
let installed = self
.local_db
.select_installation_by_ident(pkg)
.map_err(|_| GraphError::NotFound(pkg.to_string()))?;
let depgraph = self._calculate_installation(
&installed.install_pkgspec,
&CalculateInstallationOptions {
skip_recommendations: false,
no_existence_check: true,
},
);
let Ok(depgraph) = depgraph else {
return Ok(UpgradationSummary::empty());
};
if depgraph.last().map(|x| x.repo_pkg.pkg_manifest.pkg_ident()) == Some(pkg.clone()) {
return Ok(UpgradationSummary::empty());
}
Ok(UpgradationSummary {
removes: vec![pkg.clone()],
installs: depgraph,
})
}
}
#[derive(Debug, thiserror::Error)]
pub enum GraphError {
#[error("{}", t!("graph.GraphError.NotFound", item = .0))]
NotFound(String),
}
#[derive(Debug, Default, Clone)]
pub struct CalculateInstallationOptions {
/// True if we don't need to collect recommendations.
skip_recommendations: bool,
/// True if we don't need to check if the package is already installed. This is used for checking for updates.
no_existence_check: bool,
}
impl CalculateInstallationOptions {
pub fn new() -> Self {
Self::default()
}
}
#[derive(Debug, Clone)]
pub struct InstallationNode {
pub repo_pkg: RepoPackage,
pub install_pkgspec: PkgSpec,
pub auto_installed: bool,
}
#[derive(Debug, Clone)]
pub struct UpgradationSummary {
pub removes: Vec<PkgIdent>,
pub installs: Vec<InstallationNode>,
}
impl UpgradationSummary {
pub fn empty() -> Self {
Self {
removes: vec![],
installs: vec![],
}
}
pub fn is_empty(&self) -> bool {
self.removes.is_empty() && self.installs.is_empty()
}
}
/// Gets prior package that is installed by default in package candidates.
fn prior_pkg(all: impl Iterator<Item = PkgIdent>, prior_arch: &str) -> PkgIdent {
let mut all = all.collect::<Vec<_>>();
all.sort_by(|x, y| x.version.cmp(&y.version));
all.reverse();
for i in &all {
if i.arch == prior_arch {
return i.clone();
}
}
all.remove(0)
}