/[gentoo-perl]/perl-cleaner/trunk/bin/perl-cleaner
Gentoo

Contents of /perl-cleaner/trunk/bin/perl-cleaner

Parent Directory Parent Directory | Revision Log Revision Log


Revision 93 - (show annotations) (download)
Fri Aug 18 22:05:54 2006 UTC (7 years, 11 months ago) by mcummings
File size: 13235 byte(s)
gah, forgot one line

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

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.20