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

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.20