Skip to content

Instantly share code, notes, and snippets.

@shim0mura
Last active December 14, 2015 20:08
Show Gist options
  • Save shim0mura/5141609 to your computer and use it in GitHub Desktop.
Save shim0mura/5141609 to your computer and use it in GitHub Desktop.
バージョン違いで重複したrpmパッケージを削除するスクリプト バージョンが新しいものを残す。バージョン同じならビルド日時が新しいものを残す
#!/bin/sh
OLD_PACKAGES=removed/
IS_UPDATE=1
replace_package(){
mv $1 $ORIG_PACKAGES
mv $2 $OLD_PACKAGES
IS_UPDATE=0
}
add_package(){
echo "mv $1 to origpackages"
mv $1 $ORIG_PACKAGES
IS_UPDATE=0
}
if [ $# -ne 2 ]; then
echo "Usage: sh duplicated_cleaner.sh updates_directory original_package_directory"
exit
else
UPDATES=${1%/*}
ORIG_PACKAGES=${2%/*}
fi
if [ ! -d $UPDATES -o ! -d $ORIG_PACKAGES ]; then
echo "can't find directory."
exit
fi
for update_file in $UPDATES/*; do
update_info=(`rpm -qip $update_file | awk 'NR<=3 {print (NR == 3) ? $3"\n"$6$7 : $3}' | tr '\n' ' '`)
arch=`echo $update_file | sed -e "s/^.*\(x86_64\|i686\|noarch\).rpm$/\1/"`
orig_files=`find $ORIG_PACKAGES/ -name "*${update_info[0]}*$arch*"`
if [ -z "$orig_files" ]; then
add_package $update_file
continue
fi
for orig_file in $orig_files; do
orig_file_info=(`rpm -qip $orig_file | awk 'NR<=3 {print (NR == 3) ? $3"\n"$6$7 : $3}' | tr '\n' ' '`)
if [ ${update_info[0]} = ${orig_file_info[0]} ]; then
update_version=(`echo ${update_info[1]} | sed -e "s/\./ /g"`)
orig_version=(`echo ${orig_file_info[1]} | sed -e "s/\./ /g"`)
if [ $update_version = $orig_version ]; then
update_build_time=(`echo ${update_info[3]} | sed -e "s/[^0-9]//g"`)
orig_build_time=(`echo ${orig_file_info[3]} | sed -e "s/[^0-9]//g"`)
if [ $update_build_time -gt $orig_build_time ]; then
replace_package $update_file $orig_file
else
IS_UPDATE=0
rm $update_file
fi
break
fi
if [ ${#update_version[*]} -gt ${#orig_version[*]} ]; then
replace_package $update_file $orig_file
break
elif [ ${#update_version[*]} -lt ${#orig_version[*]} ]; then
break
fi
for ((i=0; i<${#update_version[@]}; i++)); do
if [ ${update_version[$i]} -eq ${orig_version[$i]} ]; then
continue
elif [ ${update_version[$i]} -gt ${orig_version[$i]} ]; then
replace_package $update_file $orig_file
break
elif [ ${update_version[$i]} -lt ${orig_version[$i]} ]; then
break
fi
done
break
fi
done
if [ $IS_UPDATE == 1 ]; then
add_package $update_file
fi
IS_UPDATE=1
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment