pacmandb-normalize : Repairs pacman database

Arch based Porteus community project

Moderator: M. Eerie

User avatar
M. Eerie
Moderator
Moderator
Posts: 713
Joined: 31 Aug 2017, 21:18
Distribution: Nemesis Xfce/MATE x64

pacmandb-normalize : Repairs pacman database

Post#1 by M. Eerie » 29 Mar 2025, 12:02

This is an updated method to repair the pacman database. Previously, in my nemaster script I was using a method based on the number of dependent packages. Although it works well, this method based on the description seems to be more robust.

NOTE: If you have AUR packages installed, the script may not accurately distinguish between explicit packages and dependencies. Running the script a second time is advisable to verify them. The script includes a commented section (disabled to improve performance); if you have AUR packages, please uncomment that section and run the script normally.

Code: Select all

#!/bin/bash
# ============================================
# pacmandb-normalize
# Tries to rebuild (a damaged) Pacman database
# marking packages as explicit or dependencies
# ============================================

# Get the list of all installed packages
readarray -t all_packages < <(pacman -Qq)

declare -A dependencies
explicit_packages=()

# Mark all packages as dependencies (clean base)
sudo pacman -D --asdeps $(pacman -Qqe) >/dev/null 2>&1

get_package_dependencies() {
  LC_ALL=en_us pacman -Qi "$1" | awk -F ': ' '/^Depends On/ {print $2}' | tr ' ' '\n' | grep -v "^None$"
}

echo "Identifying dependencies..."
for package in "${all_packages[@]}"; do
  while IFS= read -r dependency; do
    [[ -n "$dependency" ]] && dependencies["$dependency"]=1
  done < <(get_package_dependencies "$package")
done

echo "Marking explicit packages..."
for package in "${all_packages[@]}"; do
  if [[ -z "${dependencies[$package]}" ]]; then
    explicit_packages+=("$package")
    sudo pacman -D --asexplicit "$package" >/dev/null 2>&1
  fi
done

# Verification
#echo "Verifying classification..."
#for package in "${explicit_packages[@]}"; do
#  pacman -Qi "$package" | grep -q 'Installation Reason *: *Explicitly installed' || \
#    sudo pacman -D --asexplicit "$package" >/dev/null 2>&1
#done

#for package in "${all_packages[@]}"; do
#  [[ -n "${dependencies[$package]}" ]] && \
#  pacman -Qi "$package" | grep -q 'Installation Reason *: *Installed as a dependency' || \
#    sudo pacman -D --asdeps "$package" >/dev/null 2>&1
#done

# Final report
explicit_count=$(printf "%s\n" "${explicit_packages[@]}" | wc -l)
dependency_count=$(comm -12 <(printf "%s\n" "${all_packages[@]}" | sort) <(printf "%s\n" "${!dependencies[@]}" | sort) | wc -l)
total_count=${#all_packages[@]}

echo "Pacman database normalized."
echo "Explicit: $explicit_count"
echo "Dependencies: $dependency_count"
echo "Total packages: $total_count"

if orphan_packages=$(pacman -Qqdt >/dev/null 2>&1); [[ -n "$orphan_packages" ]]; then
  echo "Orphan packages: $orphan_packages"
fi
It goes without saying that you must run setup-pman first
> Does not compute_ 🖖

https://forum.porteus.org/viewtopic.php?p=94310#p94310
https://forum.porteus.org/viewtopic.php?p=102066#p102066
https://forum.porteus.org/viewtopic.php?p=102306#p102306
https://forum.porteus.org/viewtopic.php?p=72741#p72741

User avatar
M. Eerie
Moderator
Moderator
Posts: 713
Joined: 31 Aug 2017, 21:18
Distribution: Nemesis Xfce/MATE x64

pacmandb-normalize : Repairs pacman database

Post#2 by M. Eerie » 19 Apr 2025, 13:54

New code:

Code: Select all

#!/bin/bash
# ============================================
# pacmandb-normalize
# Tries to rebuild (a damaged) Pacman database
# marking packages as explicit or dependencies
# ============================================

set -euo pipefail

# Get all installed packages
all_packages=$(pacman -Qq)

# Mark every package as dependency
echo "$all_packages" | sudo xargs pacman -D --asdeps

# Mark as explicit those who are not required by any other
echo "$all_packages" | while read -r package; do
    if pacman -Qi "$package" | grep -q "Required By *: None"; then
        echo "$package"
    fi
done | sudo xargs pacman -D --asexplicit

# Final Report
explicit_count=$(pacman -Qqe | wc -l)
dependency_count=$(pacman -Qqd | wc -l)
total_count=$(pacman -Q | wc -l)

echo "Pacman database normalized."
echo "Explicit: $explicit_count"
echo "Dependencies: $dependency_count"
echo "Total packages: $total_count"

# Orphan packages (dependencies not required by any package)
orphan_packages=$(pacman -Qqdt 2>/dev/null || true)
if [[ -n "$orphan_packages" ]]; then
    echo "Orphan packages:"
    echo "$orphan_packages"
fi
> Does not compute_ 🖖

https://forum.porteus.org/viewtopic.php?p=94310#p94310
https://forum.porteus.org/viewtopic.php?p=102066#p102066
https://forum.porteus.org/viewtopic.php?p=102306#p102306
https://forum.porteus.org/viewtopic.php?p=72741#p72741

Post Reply