--- sisyphus-0.9.1/bin/sisyphus_cleanup_dups.cleanupdups 2005-07-01 18:59:29 +0400 +++ sisyphus-0.9.1/bin/sisyphus_cleanup_dups 2005-09-06 17:43:42 +0400 @@ -21,53 +21,148 @@ # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. # . /etc/sisyphus/config . /etc/sisyphus/functions +TEST= component=classic force_yes= dereference=1 show_help() { cat <&2 exit 1 } +process_dups() +{ + local rep arch + rep="$1" && shift || Fatal "first argument required" + arch="$1" && shift || Fatal "second argument required" + + mkdir -p -m700 -- "$WORKDIR/$rep" + echo -n "Processing $rep: " + find "$PREFIX/$rep" -name "*\.$arch\.rpm" -execdir rpmrdups \{\} \+ > "$WORKDIR/$rep/dups" && + echo "done" || Fatal "failed" +} + +sources_index() { + echo -n "Generating src-bin list: " + + local arch paths + for arch in $ARCHITECTURES; do + paths="$paths $PREFIX/$NEW_FILE_PREFIX/$arch/RPMS" + done + + [ -n "$paths" ] || Fatal "No binary paths found" + + find $paths -mindepth 1 -maxdepth 1 -type f -name '*.rpm' \ + -execdir rpmquery -p --qf '%{SOURCERPM}\t%{ARCH}\n' -- \{\} \+ | + sort -u > "$WORKDIR/src-bin" && + echo "done" || { echo "failed"; return 1; } +} + +correct_dups() { + local rep="$1" && shift || Fatal "first argument required" + local n f + >"$WORKDIR/$rep/needremove" + cut -d\ -f2- -- "$WORKDIR/$rep/dups" | tr \ \\n | + while read f; do + [ -e "$f" ] || continue + n="$(grep -c -m2 --mmap -F "${f##*/}" "$WORKDIR/src-bin" ||:)" + [ "$n" -gt 1 ] || { printf '%s\n' "$f" >> "$WORKDIR/$rep/needremove"; continue; } + printf %s\\n "Package retained: ${f##*/}" + done +} + +confirm_cleanup() { + local REPLY + while :; do + echo "Really purge files listed above? (yes/no)" + read + if [ "$REPLY" = no ]; then + echo Cancelled! + return 1 + fi + if [ "$REPLY" = yes ]; then + break + fi + done + return 0 +} + +repos_cleanup() { + local type="$1" && shift || Fatal "first argument required" + [ "$#" -gt 0 ] || return 1 + local rep list_cmd + + for rep in $*; do + [ -s "$WORKDIR/$rep/dups" ] || continue + + echo "Duplicated files found in \"$rep\" repository:" + cat -- "$WORKDIR/$rep/dups" + echo + + if [ "$type" = "bin" ]; then + cut -d\ -f2- -- "$WORKDIR/$rep/dups" > "$WORKDIR/$rep/needremove" + elif [ "$type" = "src" ]; then + correct_dups "$rep" + fi + + cd "$PREFIX/$rep" + if [ -z "$force_yes" ]; then + cat -- "$WORKDIR/$rep/needremove" | + xargs -r ls -Llt -- + confirm_cleanup || continue + fi + + if [ -n "$dereference" ]; then + cat -- "$WORKDIR/$rep/needremove" | + xargs -r realpath | + xargs -r $TEST rm -v -- + else + cat -- "$WORKDIR/$rep/needremove" | + xargs -r $TEST rm -v -- + fi + done +} + WORKDIR= exit_handler() { local rc=$? trap - EXIT [ -z "$WORKDIR" ] || rm -rf -- "$WORKDIR" exit $rc } -TEMP=`getopt -n $PROG -o a:,c:,f,h,p:,P -l architectures:,apt-config:,component:,force-yes,help,no-dereference,prefix: -- "$@"` || show_usage +TEMP=`getopt -n $PROG -o a:,c:,f,h,p:,P,d -l architectures:,apt-config:,component:,force-yes,help,no-dereference,prefix:,dry-run -- "$@"` || show_usage eval set -- "$TEMP" while :; do case "$1" in -a|--architectures) shift ARCHITECTURES="$1" ;; -c|--component) shift @@ -75,16 +170,18 @@ while :; do ;; -p|--prefix) shift PREFIX="$(readlink -ev "$1")" ;; -f|--force-yes) force_yes=1 ;; -P|--no-dereference) dereference= ;; + -d|--dry-run) TEST=echo + ;; -h|--help) show_help ;; --) shift; break ;; *) Fatal "unrecognized option: $1" ;; esac shift @@ -93,65 +190,21 @@ done [ -n "$ARCHITECTURES" ] || Fatal 'Invalid ARCHITECTURES.' [ -n "$component" ] || Fatal 'Invalid component.' trap exit_handler HUP INT QUIT TERM EXIT WORKDIR="$(mktemp -dt "$PROG.XXXXXXXXXX")" echo 'Making dups in repositories...' -process_dups() -{ - local rep arch - rep="$1" && shift - arch="$1" && shift - - mkdir -p -m700 -- "$WORKDIR/$rep" - cd "$PREFIX/$rep" - echo -n "Processing $rep: " - >"$WORKDIR/$rep/dups" || Fatal "$rep: failed" - for n in `ls -1 |grep ".$arch.rpm\$" |cut -c1 |LC_COLLATE=C sort -u`; do - printf %s "$n" - rpmrdups "$n"*."$arch".rpm >>"$WORKDIR/$rep/dups" || - Fatal "$rep/$n: failed" - done - echo -} - -reps= +reps_bin= +reps_src= for arch in $ARCHITECTURES; do - reps="$reps $arch/RPMS.$component $arch/SRPMS.$component" - process_dups "$arch/RPMS.$component" "$arch" - process_dups "$arch/SRPMS.$component" "src" -done + reps_bin="$reps_bin $arch/RPMS.$component" + reps_src="$reps_src $arch/SRPMS.$component" -for rep in $reps; do - if [ -s "$WORKDIR/$rep/dups" ]; then - echo "Duplicated files found in \"$rep\" repository:" - cat -- "$WORKDIR/$rep/dups" - echo - - cd "$PREFIX/$rep" - if [ -z "$force_yes" ]; then - cut -d\ -f2- -- "$WORKDIR/$rep/dups" | - xargs -r ls -Llt -- - while :; do - echo "Really purge files listed above? (yes/no)" - read - if [ "$REPLY" = no ]; then - echo Cancelled! - continue 2; - fi - if [ "$REPLY" = yes ]; then - break; - fi - done - fi - if [ -n "$dereference" ]; then - cut -d\ -f2- -- "$WORKDIR/$rep/dups" | - xargs -r realpath | - xargs -r rm -v -- - else - cut -d\ -f2- -- "$WORKDIR/$rep/dups" | - xargs -r rm -v -- - fi - fi + process_dups "$arch/RPMS.$component" "$arch" + process_dups "$arch/SRPMS.$component" "src" done + +repos_cleanup bin $reps_bin +sources_index +repos_cleanup src $reps_src