| 1 |
#!/bin/bash -xv |
| 2 |
|
| 3 |
|
| 4 |
# sort of a changelog if you want to call it that... |
| 5 |
# version 1.2 - swtaylor gave some good pointers on making the tmp files, as well as reminding me of grep's -f functionality :) |
| 6 |
# version 1.1 - Mr. Bones gave a lot of good input on cleaning up the script |
| 7 |
# Version 1 - stuff |
| 8 |
|
| 9 |
# First and foremost - make sure we have a perl to work with... |
| 10 |
PERL=$(which perl) |
| 11 |
if [ "${PERL}x" == "x" ]; then |
| 12 |
echo "NO PERL INSTALLED!! (at least not in your path)" |
| 13 |
exit |
| 14 |
fi |
| 15 |
eval $(perl '-V:version') |
| 16 |
PERL_VERSION=${version} |
| 17 |
|
| 18 |
source /sbin/functions.sh || { |
| 19 |
echo "$0: Could not source /sbin/functions.sh!" |
| 20 |
exit 1 |
| 21 |
} |
| 22 |
|
| 23 |
TMPDIR=${TMPDIR:-/tmp} |
| 24 |
|
| 25 |
PKGDIR=$(/usr/bin/portageq vdb_path) |
| 26 |
DATESTAMP=$(date +"%Y%m%d%H%M%S") |
| 27 |
LOG=$(mktemp ${TMPDIR}/perl-cleaner.log.$DATESTAMP.XXXXXXXXXX) |
| 28 |
PAGER=${PAGER:-more} |
| 29 |
|
| 30 |
# Set up our temporary files |
| 31 |
MODULES_LIST=$(mktemp ${TMPDIR}/modules.list.XXXXXXXXXX) |
| 32 |
EBUILDS_PREINSTALL=$(mktemp ${TMPDIR}/ebuilds.preinstall.XXXXXXXXXX) |
| 33 |
EBUILDS_ORDERED=$(mktemp ${TMPDIR}/ebuilds.ordered.XXXXXXXXXX) |
| 34 |
EBUILDS_REINSTALL=$(mktemp ${TMPDIR}/ebuilds.reinstall.XXXXXXXXXX) |
| 35 |
|
| 36 |
function postclean { |
| 37 |
for FILE in ${MODULES_LIST} ${EBUILDS_PREINSTALL} ${EBUILDS_ORDERED} ${EBUILDS_REINSTALL}; do |
| 38 |
|
| 39 |
if [ -f $FILE ]; then |
| 40 |
rm -f $FILE |
| 41 |
fi |
| 42 |
|
| 43 |
done |
| 44 |
|
| 45 |
if [ -s $LOG ]; then |
| 46 |
echo |
| 47 |
echo "For a complete log, please read $LOG" |
| 48 |
echo |
| 49 |
else |
| 50 |
if [ -f $LOG ]; then |
| 51 |
rm -f $LOG |
| 52 |
fi |
| 53 |
fi |
| 54 |
} |
| 55 |
|
| 56 |
# This is to clean out the old .ph files generated in our last perl install |
| 57 |
function ph_clean() { |
| 58 |
echo "$(date) : Beginning a clean up of .ph files" | tee -a $LOG |
| 59 |
|
| 60 |
INC=$(perl -e 'for $line (@INC) { next if $line eq "."; next if $line =~ m/'${PERL_VERSION}'/; print "$line\n" }') |
| 61 |
|
| 62 |
echo "Locating ph files for removal" |
| 63 |
for file in $(find $INC -name "*.ph" -type f 2>/dev/null); do |
| 64 |
if [ ! $(echo "$file"|grep $PERL_VERSION) ]; then |
| 65 |
echo "$(date) : Removing old ph file: $file" | tee -a $LOG |
| 66 |
rm $file |
| 67 |
fi |
| 68 |
done |
| 69 |
|
| 70 |
# Silently remove those dirs that we just emptied |
| 71 |
find $INC -depth -type d 2>/dev/null | xargs -r rmdir 2>/dev/null |
| 72 |
} |
| 73 |
|
| 74 |
# Generate ph files; this is useful if we've upgraded packages with headers so that perl knows the new info |
| 75 |
function ph_update() { |
| 76 |
echo "$(date) : Updating ph files" | tee -a $LOG |
| 77 |
cd /usr/include; h2ph * sys/* arpa/* netinet/* bits/* security/* asm/* gnu/* linux/* | tee -a $LOG |
| 78 |
cd /usr/include/linux; h2ph * | tee -a $LOG |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
# Build a list of modules installed under older perls - only valid if the module was an ebuild :) |
| 83 |
function module_list() { |
| 84 |
echo "$(date) : Building list of modules for reinstall" | tee -a $LOG |
| 85 |
echo "Locating modules for reinstall" |
| 86 |
for checkfile in `find $PKGDIR -maxdepth 3 -mindepth 3 -name "CONTENTS" |xargs grep -l /usr/$(get_libdir)/perl5 `; do |
| 87 |
if [ "`grep -l "${PERL_VERSION}" $checkfile`x" = "x" ]; then |
| 88 |
echo "$checkfile" >> ${MODULES_LIST} |
| 89 |
fi; |
| 90 |
done |
| 91 |
} |
| 92 |
|
| 93 |
function alternate_module_list() { |
| 94 |
# Takes longer to run, but identifes modules not associated with |
| 95 |
# an ebuild. |
| 96 |
# |
| 97 |
# Reset INC - INC is dynamically generated, and if we removed any ph |
| 98 |
# files - and they were the only thing left in a dir - then there's |
| 99 |
# no sense in revisiting that dir |
| 100 |
echo "$(date) : Building list of modules for reinstall" | tee -a $LOG |
| 101 |
INC=$(perl -e 'for $line (@INC) { next if $line eq "."; next if $line =~ m/'${PERL_VERSION}'/; print "$line\n" }') |
| 102 |
INSTALLED_EBUILDS=$(find $PKGDIR -name CONTENTS) |
| 103 |
echo "Locating modules for reinstall" |
| 104 |
for file in $(find $INC -iname "*.pm" \! -type f 2>/dev/null | grep -v "${PERL_VERSION}"| sort -u); do |
| 105 |
PKG=$(for ebuild in $INSTALLED_EBUILDS; do if grep -l $file $ebuild; then break; fi; done) |
| 106 |
if [ -z "$PKG" ] ; then |
| 107 |
echo "Warning: $file is not owned by a currently installed ebuild" |
| 108 |
else |
| 109 |
echo "$PKG" >>${MODULES_LIST} |
| 110 |
fi |
| 111 |
done |
| 112 |
} |
| 113 |
|
| 114 |
# The meat of it - rebuilding the ebuilds |
| 115 |
# ALL emerges are oneshots - we don't want to mess with the world file |
| 116 |
# We first attempt to emerge the specific module that was installed last time |
| 117 |
# If that fails, we attempt to install a newer version |
| 118 |
|
| 119 |
function ebuild_rebuild() { |
| 120 |
|
| 121 |
echo "$(date) : Rebuilding modules: Building list of ebuilds" | tee -a $LOG |
| 122 |
if [ -s ${MODULES_LIST} ]; then |
| 123 |
for line in $(sort -u ${MODULES_LIST}); do |
| 124 |
echo "$line"|sed -e 's|.*pkg/||' -e 's|/CONTENTS||'|grep -v "dev-lang/perl" >>${EBUILDS_PREINSTALL} |
| 125 |
done |
| 126 |
fi |
| 127 |
|
| 128 |
# If they asked for interactive, let them see what will be reinstalled |
| 129 |
if [ -s ${EBUILDS_PREINSTALL} ]; then |
| 130 |
|
| 131 |
if [ ! -z $ASK ]; then |
| 132 |
echo "Press Enter to see the list of ebuilds we'll be evaluating" |
| 133 |
read key |
| 134 |
$PAGER ${EBUILDS_PREINSTALL} |
| 135 |
printf "Continue? (Y/N) " |
| 136 |
read ANSWER |
| 137 |
if [ $(echo "${ANSWER}" | egrep -e "^n|N" ) ]; then |
| 138 |
echo "$(date) : USER ABORTED REBUILD">>$LOG |
| 139 |
exit |
| 140 |
fi |
| 141 |
fi |
| 142 |
|
| 143 |
for EBUILD in $(cat ${EBUILDS_PREINSTALL} ); do |
| 144 |
if emerge --oneshot -p "=$EBUILD"|egrep -q ".*ebuilds.*satisfy"; then |
| 145 |
if emerge --oneshot -p ">=$EBUILD"|egrep -q ".*ebuilds.*satisfy"; then |
| 146 |
echo "$(date) : There are no unmasked ebuilds to satisfy $EBUILD. Skipping" | tee -a $LOG |
| 147 |
sleep 2 |
| 148 |
else |
| 149 |
if [ ! -z $ASK ]; then |
| 150 |
printf "${EBUILD} isn't available, but a new version is. Install? (Y/N) " |
| 151 |
read ANSWER |
| 152 |
if [ $(echo "${ANSWER}" | egrep -e "^y|Y" ) ]; then |
| 153 |
echo ">=$EBUILD" >> ${EBUILDS_ORDERED} |
| 154 |
echo "$(date) : User chose to install >=${EBUILD}">>$LOG |
| 155 |
fi |
| 156 |
else |
| 157 |
echo ">=$EBUILD" >>${EBUILDS_ORDERED} |
| 158 |
fi |
| 159 |
fi |
| 160 |
else |
| 161 |
echo "=$EBUILD">>${EBUILDS_ORDERED} |
| 162 |
fi |
| 163 |
done |
| 164 |
|
| 165 |
if [ -s ${EBUILDS_ORDERED} ]; then |
| 166 |
if [ ! -z $ASK ]; then |
| 167 |
echo "Press Enter to see the final list of ebuilds to install" |
| 168 |
read key |
| 169 |
$PAGER ${EBUILDS_ORDERED} |
| 170 |
printf "Continue? (Y/N) " |
| 171 |
read ANSWER |
| 172 |
if [ $(echo "${ANSWER}" | egrep -e "^n|N" ) ]; then |
| 173 |
echo "$(date) : USER ABORTED REBUILD">>$LOG |
| 174 |
exit |
| 175 |
fi |
| 176 |
fi |
| 177 |
|
| 178 |
# Cut down to one line so portage can handle ordering these appropriately |
| 179 |
emerge -p --oneshot $(cat ${EBUILDS_ORDERED} ) | grep ebuild | sed -e 's:\([^ ]\+\):=\1:g' -e 's:.*\] \([^ ]*\) .*:\1:'>>${EBUILDS_REINSTALL} |
| 180 |
|
| 181 |
echo "Reinstalling ebuilds" |
| 182 |
echo "$(date) : Ebuilds to reinstall: ">>$LOG |
| 183 |
cat ${EBUILDS_REINSTALL}>>$LOG |
| 184 |
echo >>$LOG |
| 185 |
|
| 186 |
# Now that we have them in the right order, emerge them one at a time |
| 187 |
# This is to avoid problems if one doesn't emerge correctly |
| 188 |
|
| 189 |
for EBUILD in $(cat ${EBUILDS_REINSTALL}); do |
| 190 |
emerge --oneshot ${EMERGE_OPTIONS} "$EBUILD" |
| 191 |
done |
| 192 |
else |
| 193 |
echo |
| 194 |
echo "Nothing to reinstall!" |
| 195 |
echo |
| 196 |
fi |
| 197 |
else |
| 198 |
echo |
| 199 |
echo "Nothing to reinstall!" |
| 200 |
echo |
| 201 |
fi |
| 202 |
|
| 203 |
} |
| 204 |
|
| 205 |
# Locate .so's and binaries linked against libperl.so |
| 206 |
# The coup is in ! -newer libperl.so - cut out anything that was obviously installed |
| 207 |
# after our last install of libperl, which should cut out the false positives. |
| 208 |
|
| 209 |
function libperl_list() { |
| 210 |
echo "$(date) : Locating ebuilds linked against libperl" | tee -a $LOG |
| 211 |
for i in $(find $(egrep -v "^#" /etc/ld.so.conf) -type f -name '*.so*' ! -newer /usr/$(get_libdir)/libperl.so 2>/dev/null) \ |
| 212 |
$(find $(echo $PATH | sed 's/:/ /g') -type f -perm +0111 ! -newer /usr/$(get_libdir)/libperl.so 2>/dev/null) ; |
| 213 |
do |
| 214 |
if [ -f ${i} ]; then |
| 215 |
ldd ${i} 2>&1 | grep "libperl" - >/dev/null && grep -l " $i " $PKGDIR/*/*/CONTENTS>>${MODULES_LIST}; |
| 216 |
fi |
| 217 |
done |
| 218 |
|
| 219 |
} |
| 220 |
|
| 221 |
# Assuming a successful module run, look to see whats left over |
| 222 |
function leftovers() { |
| 223 |
echo "$(date) : Finding left over modules" | tee -a $LOG |
| 224 |
|
| 225 |
echo "$(date) : The following files remain. These were either installed by hand" | tee -a $LOG |
| 226 |
echo "$(date) : or edited. This script cannot deal with them." | tee -a $LOG |
| 227 |
echo | tee -a $LOG |
| 228 |
|
| 229 |
|
| 230 |
INC=$(perl -e 'for $line (@INC) { next if $line eq "."; next if $line =~ m/'${PERL_VERSION}'/; print "$line\n" }') |
| 231 |
for file in $(find $INC -type f 2>/dev/null |grep -v "${PERL_VERSION}" ) ; do |
| 232 |
echo "$(date) : ${file}" | tee -a $LOG |
| 233 |
done |
| 234 |
} |
| 235 |
|
| 236 |
function usage() { |
| 237 |
echo "Usage: $0 [options] [ask]" |
| 238 |
printf "\tmodules - rebuild perl modules for old installs of perl\n" |
| 239 |
printf "\tallmodules - rebuild perl modules for any install of perl\n" |
| 240 |
printf "\tlibperl - rebuild anything linked against libperl\n" |
| 241 |
printf "\tph-clean - clean out old ph files from a previous perl\n" |
| 242 |
printf "\tphupdate - update existing ph files, useful after an upgrade to system parts like the kernel\n" |
| 243 |
printf "\tphall - clean out old ph files and run phupdate\n" |
| 244 |
printf "\tall - rebuild modules, libperl linkages, clean ph files, and rebuild them\n" |
| 245 |
printf "\treallyall - rebuild modules for any install of perl, libperl linkages, clean ph files, and rebuild them\n" |
| 246 |
printf "\n" |
| 247 |
printf "\task - ask for confirmation on each emerge" |
| 248 |
printf "\n\n" |
| 249 |
exit 0 |
| 250 |
} |
| 251 |
|
| 252 |
if [ -z "$1" ]; then |
| 253 |
usage |
| 254 |
fi |
| 255 |
|
| 256 |
EMERGE_OPTIONS="" |
| 257 |
ASK="" |
| 258 |
MODULES=false |
| 259 |
LIBPERL=false |
| 260 |
PHCLEAN=false |
| 261 |
PHUPDATE=false |
| 262 |
FORCE=false |
| 263 |
LEFTOVERS=false |
| 264 |
while [ ! -z "$1" ] ; do |
| 265 |
case "$1" in |
| 266 |
help | --help | -h ) |
| 267 |
usage |
| 268 |
;; |
| 269 |
leftovers ) |
| 270 |
LEFTOVERS=true |
| 271 |
shift |
| 272 |
;; |
| 273 |
modules ) |
| 274 |
MODULES=true |
| 275 |
# LEFTOVERS=true |
| 276 |
shift |
| 277 |
;; |
| 278 |
allmodules ) |
| 279 |
MODULES=true |
| 280 |
FORCE=true |
| 281 |
shift |
| 282 |
;; |
| 283 |
libperl ) |
| 284 |
LIBPERL=true |
| 285 |
shift |
| 286 |
;; |
| 287 |
ph-clean ) |
| 288 |
PHCLEAN=true |
| 289 |
shift |
| 290 |
;; |
| 291 |
phupdate ) |
| 292 |
PHUPDATE=true |
| 293 |
shift |
| 294 |
;; |
| 295 |
phall ) |
| 296 |
PHCLEAN=true |
| 297 |
PHUPDATE=true |
| 298 |
shift |
| 299 |
;; |
| 300 |
all ) |
| 301 |
MODULES=true |
| 302 |
LIBPERL=true |
| 303 |
PHCLEAN=true |
| 304 |
PHUPDATE=true |
| 305 |
LEFTOVERS=true |
| 306 |
shift |
| 307 |
;; |
| 308 |
reallyall ) |
| 309 |
MODULES=true |
| 310 |
LIBPERL=true |
| 311 |
PHCLEAN=true |
| 312 |
PHUPDATE=true |
| 313 |
FORCE=true |
| 314 |
shift |
| 315 |
;; |
| 316 |
ask ) |
| 317 |
ASK="true" |
| 318 |
EMERGE_OPTIONS="${EMERGE_OPTIONS} --ask" |
| 319 |
shift |
| 320 |
;; |
| 321 |
force ) |
| 322 |
FORCE=true |
| 323 |
shift |
| 324 |
;; |
| 325 |
* ) |
| 326 |
EMERGE_OPTIONS="${EMERGE_OPTIONS} $1" |
| 327 |
shift |
| 328 |
;; |
| 329 |
esac |
| 330 |
done |
| 331 |
|
| 332 |
$FORCE && PERL_VERSION="0.0.0" |
| 333 |
$PHCLEAN && ph_clean |
| 334 |
$PHUPDATE && ph_update |
| 335 |
$MODULES && module_list |
| 336 |
$LIBPERL && libperl_list |
| 337 |
($MODULES || $LIBPERL) && ebuild_rebuild |
| 338 |
$LEFTOVERS && leftovers |
| 339 |
|
| 340 |
#postclean |
| 341 |
|
| 342 |
exit |