Skip to content

Commit

Permalink
Merge pull request #7 from aesiris/master
Browse files Browse the repository at this point in the history
avoid temp files
  • Loading branch information
graysky2 committed Dec 11, 2012
2 parents 7edb977 + f590ad6 commit 32c9a49
Showing 1 changed file with 30 additions and 45 deletions.
75 changes: 30 additions & 45 deletions profile-cleaner
Expand Up @@ -7,8 +7,6 @@ if [[ -z $(which parallel) ]]; then
exit 1 exit 1
fi fi


find /tmp -name 'work2do.*' -o -name 'accounting.*' -o -name "totalwork2do" 2>/dev/null | SHELL=/bin/bash parallel rm -f

echo -e "${RED}profile-cleaner v$VERS${NRM}" echo -e "${RED}profile-cleaner v$VERS${NRM}"
echo echo


Expand All @@ -21,30 +19,41 @@ XDG_CONFIG_HOME="${XDG_CONFIG_HOME:-$HOME/.config}"


do_clean() { do_clean() {
echo -en "${GRN} Cleaning${NRM} ${1##*/}" echo -en "${GRN} Cleaning${NRM} ${1##*/}"
bsize=$(ls -l "$1" | gawk {'print $5'}) bsize=$(du -b "$1" | cut -f 1)

sqlite3 "$1" vacuum sqlite3 "$1" vacuum
sqlite3 "$1" reindex sqlite3 "$1" reindex
asize=$(ls -l "$1" | gawk {'print $5'})
asize=$(du -b "$1" | cut -f 1)
dsize=$(echo "scale=2; ($bsize-$asize)/1048576" | bc) dsize=$(echo "scale=2; ($bsize-$asize)/1048576" | bc)
echo -e "$(tput cr)$(tput cuf 46) ${GRN}done${NRM} -${YLW}${dsize}${NRM} Mbytes" echo -e "$(tput cr)$(tput cuf 46) ${GRN}done${NRM} -${YLW}${dsize}${NRM} Mbytes"
echo "$name,$dsize" >> /tmp/accounting.$name }

do_clean_parallel () {
bsize=$(du -b -c "${toclean[@]}" | tail -n 1 | cut -f 1)

SHELL=/bin/bash parallel -k do_clean ::: "${toclean[@]}" 2>/dev/null

asize=$(du -b -c "${toclean[@]}" | tail -n 1 | cut -f 1)
cleanedsize=$(echo "scale=2; ($bsize-$asize)/1048576" | bc)
} }


find_dbs() { find_dbs() {
find "$profilepath" -maxdepth 2 -type f | while read i; do # fill toclean array by looking for SQLite files in each input directory
[[ -n $(file "$i" | grep SQLite) ]] && echo "${i}" >> /tmp/work2do.$name toclean=()
done while read i; do
toclean+=("${i}")
done < <( find -L "$@" -maxdepth 2 -type f -print0 2>/dev/null | xargs -0 file -e ascii | sed -n -e "s/:.*SQLite.*//p" )
} }


do_chromebased() { do_chromebased() {
[[ -h "$prepath" ]] && profilepath=$(readlink $prepath) || profilepath="$prepath" [[ -h "$prepath" ]] && profilepath=$(readlink $prepath) || profilepath="$prepath"
[[ ! -d "$profilepath" ]] && echo -e ${RED}"Error: no profile directory for $name found."${NRM} && exit 1 [[ ! -d "$profilepath" ]] && echo -e ${RED}"Error: no profile directory for $name found."${NRM} && exit 1
echo -e " ${YLW}Cleaning profile for $name${NRM}" echo -e " ${YLW}Cleaning profile for $name${NRM}"
find_dbs find_dbs "$profilepath"
cat /tmp/work2do.$name | SHELL=/bin/bash parallel -k do_clean 2>/dev/null do_clean_parallel
echo -e " ${BLD}Profile for $name reduced by ${YLW}$(grep $name /tmp/accounting.$name | gawk -F, '{ print $2 }' | paste -sd+ - | bc)${NRM} ${BLD}Mbytes.${NRM}" echo
[[ -f /tmp/accounting.$name ]] && rm -f /tmp/accounting.$name echo -e " ${BLD}Profile(s) for $name reduced by ${YLW}${cleanedsize}${NRM} ${BLD}Mbytes.${NRM}"
[[ -f /tmp/work2do.$name ]] && rm -f /tmp/work2do.$name
} }


do_xulbased() { do_xulbased() {
Expand All @@ -62,13 +71,6 @@ do_xulbased() {


[[ ! -f $profilepath/profiles.ini ]] && echo -e ${RED}"Error: cannot locate $profilepath/profiles.ini to determine names of profiles for $name."${NRM} && exit 1 [[ ! -f $profilepath/profiles.ini ]] && echo -e ${RED}"Error: cannot locate $profilepath/profiles.ini to determine names of profiles for $name."${NRM} && exit 1


#
# read each profile into a temp file for processing
#

profiles_extracted=/tmp/$name.tmplist
grep '[P,p]'ath $profilepath/profiles.ini | sed -e 's/[P,p]ath=//' -e 's/\r//' >$profiles_extracted

# #
# build an array correcting for rel and abs paths therein # build an array correcting for rel and abs paths therein
# while read will read line-by-line and will tolerate spaces whereas a for loop will not # while read will read line-by-line and will tolerate spaces whereas a for loop will not
Expand All @@ -82,40 +84,23 @@ do_xulbased() {
finalArr[index]="$profilepath/$line" finalArr[index]="$profilepath/$line"
fi fi
index=$index+1 index=$index+1
done <$profiles_extracted done < <(grep '[P,p]'ath "$profilepath/profiles.ini" | sed -e 's/[P,p]ath=//' -e 's/\r//' )

[[ -f $profiles_extracted ]] && rm -f $profiles_extracted


#
# identify which files are sqlite dbs and add them to the work2do file for parallel processing
#
echo -e " ${YLW}Cleaning profile for $name${NRM}" echo -e " ${YLW}Cleaning profile for $name${NRM}"
for i in "${finalArr[@]}"; do find_dbs "${finalArr[@]}"
find "$i" -maxdepth 2 -type f | while read line; do do_clean_parallel
[[ -n $(file "$line" | grep SQLite) ]] && echo "$line" >> /tmp/work2do.$name echo
done echo -e " ${BLD}Profile(s) for $name reduced by ${YLW}${cleanedsize}${NRM} ${BLD}Mbytes.${NRM}"
done

#
# process the work
#

cat /tmp/work2do.$name | SHELL=/bin/bash parallel -k do_clean 2>/dev/null
echo
echo -e " ${BLD}Profile(s) for $name reduced by ${YLW}$(grep $name /tmp/accounting.$name | gawk -F, '{ print $2 }' | paste -sd+ - | bc)${NRM} ${BLD}Mbytes.${NRM}"
[[ -f /tmp/accounting.$name ]] && rm -f /tmp/accounting.$name
[[ -f /tmp/work2do.$name ]] && rm -f /tmp/work2do.$name
} }


do_dbbased() { do_dbbased() {
[[ -h "$prepath" ]] && profilepath=$(readlink $prepath) || profilepath="$prepath" [[ -h "$prepath" ]] && profilepath=$(readlink $prepath) || profilepath="$prepath"
[[ ! -d "$profilepath" ]] && echo -e ${RED}"Error: no profile directory for $name found."${NRM} && exit 1 [[ ! -d "$profilepath" ]] && echo -e ${RED}"Error: no profile directory for $name found."${NRM} && exit 1
echo -e " ${YLW}Cleaning profile for $name${NRM}" echo -e " ${YLW}Cleaning profile for $name${NRM}"
find ${profilepath} -maxdepth 2 -type f -name '*.db' | SHELL=/bin/bash parallel do_clean 2>/dev/null find_dbs "${profilepath}"
do_clean_parallel
echo echo
echo -e " ${BLD}Profile for $name reduced by ${YLW}$(grep $name /tmp/accounting.$name | gawk -F, '{ print $2 }' | paste -sd+ - | bc)${NRM} ${BLD}Mbytes.${NRM}" echo -e " ${BLD}Profile(s) for $name reduced by ${YLW}${cleanedsize}${NRM} ${BLD}Mbytes.${NRM}"
[[ -f /tmp/accounting.$name ]] && rm -f /tmp/accounting.$name
[[ -f /tmp/work2do.$name ]] && rm -f /tmp/work2do.$name
} }


export -f do_clean export -f do_clean
Expand Down

0 comments on commit 32c9a49

Please sign in to comment.