summaryrefslogtreecommitdiff
blob: 63a67f43f7d85484e5f23504511b71a376bf5018 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#!/bin/bash

# First and foremost - make sure we have a perl to work with...
PERL=`which perl`
if [ "${PERL}x" == "x" ]; then
   echo "NO PERL INSTALLED!! (at least not in your path)"
   exit
fi
eval `perl '-V:version'`
PERL_VERSION=${version}


TMPDIR="/tmp"
PKGDIR="/var/db/pkg"
DATESTAMP=`date +"%Y%m%d%H%M%S"`
LOG="${TMPDIR}/perl-cleaner.log.$DATESTAMP"

ASK=""

if [ ! -z $2 ]; then
   ASK=Y
fi

# Set up our temporary files

function cleanrun() {
for FILE in modules.list ebuilds.preinstall ebuilds.ordered ebuilds.reinstall ; do

if [ -f $TMPDIR/$FILE ]; then
   cp /dev/null $TMPDIR/$FILE
else
   touch $TMPDIR/$FILE
fi

done
}

function postclean {
for FILE in modules.list ebuilds.preinstall ebuilds.ordered ebuilds.reinstall; do

if [ -f $TMPDIR/$FILE ]; then
   rm -f $TMPDIR/$FILE
fi

done

echo ""
echo "For a complete log, please read ${TMPDIR}/perl-cleaner.log.DATESTAMP"
echo ""
}

# This is to clean out the old .ph files generated in our last perl install
function ph_clean() {
echo "`date` : Beginning a clean up of .ph files"
echo "`date` : Beginning a clean up of .ph files">>$LOG

INC=`perl -e 'foreach $line (@INC) { next if $line eq "."; next if $line =~ m/'${PERL_VERSION}'/; print "$line\n" }'`

echo "Locating ph files for removal"
for DIR in $INC; do
   if [ -d $DIR ]; then
      for file in `find $DIR -name "*.ph" -type f`; do
         if [ ! `echo "$file"|grep $PERL_VERSION` ]; then
         echo "`date` : Removing old ph file: $file"
         echo "`date` : Removing old ph file: $file">>$LOG
         rm $file
         fi
      done
   fi
done
}

# Generate ph files; this is useful if we've upgraded packages with headers so that perl knows the new info
function ph_update() {
   echo "`date` : Updating ph files"
   echo "`date` : Updating ph files">>$LOG
		cd /usr/include; h2ph * sys/* arpa/* netinet/* bits/* security/* asm/* gnu/* linux/*>>$LOG
		cd /usr/include/linux; h2ph * >>$LOG
}

# Build a list of modules installed under older perls - only valid if the module was an ebuild :)
function module_list() {
# Reset INC - INC is dynamically generated, and if we removed any ph 
# files - and they were the only thing left in a dir - then there's 
# no sense in revisiting that dir
echo "`date` : Building list of modules for reinstall"
echo "`date` : Building list of modules for reinstall">>$LOG
INC=`perl -e 'foreach $line (@INC) { next if $line eq "."; next if $line =~ m/'${PERL_VERSION}'/; print "$line\n" }'`
echo "Locating modules for reinstall"
for DIR in $INC; do
   if [ -d $DIR ]; then
      for file in `find $DIR -iname "*.pm" -type f|grep -v "${PERL_VERSION}"`; do
         grep -l $file $PKGDIR/*/*/CONTENTS >>$TMPDIR/modules.list
      done
   fi
done
}

# The meat of it - rebuilding the ebuilds
# ALL emerges are oneshots - we don't want to mess with the world file
# We first attempt to emerge the specific module that was installed last time
# If that fails, we attempt to install a newer version

function ebuild_rebuild() {

echo "`date` : Rebuilding modules: Building list of ebuilds"
echo "`date` : Rebuilding modules: Building list of ebuilds">>$LOG
if [ -s $TMPDIR/modules.list ]; then
   for line in `cat $TMPDIR/modules.list|sort -u`; do
      echo "$line"|sed -e 's|.*pkg/||' | sed -e 's|/CONTENTS||'|grep -v "dev-lang/perl" >>$TMPDIR/ebuilds.preinstall
   done
fi


# If they asked for interactive, let them see what will be reinstalled
if [ -s $TMPDIR/ebuild.preinstall ]; then
  
  if [ ! -z $ASK ]; then
     echo "Press Enter to see the list of ebuilds we'll be avaluating"
     read key
     if [ ! -z $PAGER ]; then
        $PAGER $TMPDIR/ebuilds.preinstall
     else
        more $TMPDIR/ebuilds.preinstall
     fi
     printf "Continue? (Y/N) "
     read ANSWER
     if [ `echo "${ANSWER}" | egrep -e "^n|N" ` ]; then
        echo "`date` : USER ABORTED REBUILD">>$LOG
        exit
     fi
  fi
  
  for EBUILD in `cat $TMPDIR/ebuilds.preinstall`; do
  if emerge --oneshot -p "=$EBUILD"|egrep -q ".*ebuilds.*satisfy"; then
     if emerge --oneshot -p ">=$EBUILD"|egrep -q ".*ebuilds.*satisfy"; then
        echo "`date` : There are no unmasked ebuilds to satisfy $EBUILD. Skipping"
        echo "`date` : There are no unmasked ebuilds to satisfy $EBUILD. Skipping">>$LOG
        sleep 2
     else
        if [ ! -z $ASK ]; then
        printf "${EBUILD} isn't available, but a new version is. Install? (Y/N) "
        read ANSWER
           if [ `echo "${ANSWER}" | egrep -e "^y|Y" ` ]; then
              echo ">=$EBUILD" >> $TMPDIR/ebuilds.ordered
	      echo "`date` : User chose to install >=${EBUILD}">>$LOG
           fi
        else
           echo ">=$EBUILD" >>$TMPDIR/ebuilds.ordered
        fi
     fi
  else
     echo "=$EBUILD">>$TMPDIR/ebuilds.ordered

  fi
  done

 if [ -s $TMPDIR/ebuilds.ordered ]; then
  if [ ! -z $ASK ]; then
     echo "Press Enter to see the final list of ebuilds to install"
     read key
     if [ ! -z $PAGER ]; then
        $PAGER $TMPDIR/ebuilds.ordered
     else
        more $TMPDIR/ebuilds.ordered
     fi
     printf "Continue? (Y/N) "
     read ANSWER
     if [ `echo "${ANSWER}" | egrep -e "^n|N" ` ]; then
        echo "`date` : USER ABORTED REBUILD">>$LOG
        exit
     fi
  fi

# Cut down to one line so portage can handle ordering these appropriately
  emerge -p --oneshot `cat $TMPDIR/ebuilds.ordered` | grep ebuild | sed 's:\([^ ]\+\):=\1:g' | sed 's:.*\] \([^ ]*\) .*:\1:'>>$TMPDIR/ebuilds.reinstall

  echo "Reinstalling ebuilds"
  echo "`date` : Ebuilds to reinstall: ">>$LOG
  cat $TMPDIR/ebuilds.reinstall>>$LOG
  echo "" >>$LOG

# Now that we have them in the right order, emerge them one at a time
# This is to avoid problems if one doesn't emerge correctly

  for EBUILD in `cat $TMPDIR/ebuilds.reinstall`; do
    if [ ! -z $ASK ]; then
     emerge --oneshot --ask "$EBUILD"
    else
     emerge --oneshot "$EBUILD"
    fi
  done
 else
	echo ""
	echo "Nothing to reinstall!"
	echo ""
 fi
else
	echo ""
	echo "Nothing to reinstall!"
	echo ""
fi

}

# Locate .so's and binaries linked against libperl.so
# The coup is in ! -newer libperl.so - cut out anything that was obviously installed
# after our last install of libperl, which should cut out the false positives.

function libperl_list() {
echo "`date` : Locating ebuilds linked against libperl"
echo "`date` : Locating ebuilds linked against libperl">>$LOG
for i in $(find $(egrep -v ^# /etc/ld.so.conf) -type f -name '*.so*' ! -newer /usr/lib/libperl.so ) \
      $(find $(echo $PATH | sed 's/:/ /g') -type f -perm +0111 ! -newer /usr/lib/libperl.so ) ;
do
  echo "Checking ${i}" #MPC
  if [ -f ${i} ]; then
     ldd ${i} 2>&1 | grep "libperl" - >/dev/null && grep -l $i $PKGDIR/*/*/CONTENTS>>$TMPDIR/modules.list;
  fi
done 

}

# Assuming a successful module run, look to see whats left over
function leftovers() {
echo "`date` : Finding left over modules"
echo "`date` : Finding left over modules">>$LOG

echo "`date` : The following files remain. These were either installed by hand"
echo "`date` : or edited. This script cannot deal with them."
echo ""

echo "`date` : The following files remain. These were either installed by hand">>$LOG
echo "`date` : or edited. This script cannot deal with them.">>$LOG
echo "">>$LOG

INC=`perl -e 'foreach $line (@INC) { next if $line eq "."; next if $line =~ m/'${PERL_VERSION}'/; print "$line\n" }'`
for DIR in $INC; do
   if [ -d $DIR ]; then
      for file in `find $DIR -type f |grep -v  "${PERL_VERSION}" ` ; do
         echo "`date` : ${file}">>$LOG
      done
   fi
done
}

cleanrun

case "$1" in
   leftovers)
      leftovers
      ;;
   modules)
      module_list
      ebuild_rebuild
      leftovers
      ;;
   libperl)
      libperl_list
      ebuild_rebuild
      ;;
   ph-clean)
      ph_clean
      ;;
   phupdate)
      ph_update
      ;;
   phall)
      ph_clean
      ph_update
      ;;
   all)
      ph_clean
      ph_update
      module_list
      libperl_list
      ebuild_rebuild
      leftovers
      ;;
   *)
   echo "Usage: $0 [options] [ask]"
   printf "\tmodules - rebuild perl modules\n"
   printf "\tlibperl - rebuild anything linked against libperl\n"
   printf "\tph-clean - clean out old ph files from a previous perl\n"
   printf "\tphupdate - update existing ph files, useful after an upgrade to system parts like the kernel\n"
   printf "\tphall - clean out old ph files and run phupdate\n"
   printf "\tall - rebuild modules, libperl linkages, clean ph files, and rebuild them\n"
   printf "\n"
   printf "\task - ask for confirmation on each emerge"
   printf "\n\n"
   
      ;;
esac

postclean

exit