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

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.20