Created
August 18, 2010 16:27
-
-
Save curtisharvey/535323 to your computer and use it in GitHub Desktop.
ports_admin - simple bash script to help manage macports
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
if [[ $(id -u) != 0 ]]; then | |
echo "Script requires root priviliges" | |
echo "Run with: sudo $(basename $0)" | |
exit 1 | |
fi | |
ask () { | |
echo -ne "===> $@ [y/n] " | |
read ans | |
case "$ans" in | |
y*|Y*) return 0;; | |
*) return 1;; | |
esac | |
} | |
AUTO=0 | |
if [[ $1 == 'auto' ]] || ask 'Perform all admin steps:\n* update MacPorts\n* update ports\n* remove inactive/orphaned ports\nautomatically?'; then | |
AUTO=1 | |
fi | |
if (($AUTO)) || ask 'Update MacPorts and sync ports tree?'; then | |
echo '---> updating MacPorts' | |
port selfupdate | |
fi | |
if (($AUTO)) || ask 'Check for outdated ports?'; then | |
echo '---> checking for outdated ports' | |
echo | |
outdated_ports=$(port list outdated) | |
if [[ "x$outdated_ports" == "x" ]]; then | |
echo '---> all ports current' | |
else | |
echo -e "$outdated_ports\n" | |
if (($AUTO)) || ask 'Upgrade ALL outdated ports?'; then | |
echo '---> upgrading all outdated ports' | |
port upgrade --enforce-variants outdated | |
else | |
outdated_ports=$(echo "$outdated_ports" | awk '{print $1}') | |
for p in $outdated_ports; do | |
ask "Upgrade port $p?" && port upgrade "$p" | |
done | |
fi | |
fi | |
fi | |
if (($AUTO)) || ask 'Remove all inactive ports?'; then | |
echo '---> removing all inactive ports' | |
port -fu uninstall | |
fi | |
if (($AUTO)) || ask 'Check for orphaned ports?'; then | |
[[ ! -d ~/.macports ]] && mkdir ~/.macports | |
[[ ! -f ~/.macports/ports_admin_orphan_exceptions ]] && touch ~/.macports/ports_admin_orphan_exceptions | |
exceptions=~/.macports/ports_admin_orphan_exceptions | |
tmp_exceptions='' | |
confirm () { | |
local p=$1 | |
echo -n "===> port $p appears to be orphaned, remove? [y/n/i/e/?] " | |
read ans | |
case "$ans" in | |
y) return 0;; | |
n) echo "---> adding temporary exception for $p"; tmp_exceptions="$p\n$tmp_exceptions"; return 1;; | |
e) echo "---> adding exception for $p"; echo "$p" >> $exceptions; return 1;; | |
i) port info $p && confirm $p;; | |
*) echo 'y=Yes, n=No, i=Info, e=add Exception' && confirm $p;; | |
esac | |
} | |
remove_orphaned () { | |
local check_again=0 | |
for p in $(port list installed | awk '{print $1}'); do | |
if [[ | |
$(grep "^$p$" "$exceptions") == '' && | |
$(echo -e "$tmp_exceptions" | grep "^$p$") == '' && | |
$(port dependents $p | grep -v 'no dependents') == '' | |
]]; then | |
confirm $p && port uninstall $p && check_again=1 | |
fi | |
done | |
if (($check_again)); then | |
echo "---> checking if removed ports created any new orphans" | |
remove_orphaned | |
fi | |
} | |
echo '---> searching for orphaned ports' | |
remove_orphaned | |
echo '---> all ophaned ports removed or skipped' | |
fi | |
echo '---> DONE!' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment