I have been working on a script to clean old source files from the distfiles directory.
Running the following script should leave just the files which are in the world file, in the distfiles directory.
Other files are moved into distfiles_backup. This will include source for files which were emerged with the inject option.
Old versions of files which still exist in the distfiles directory are deleted from the distfiles_backup directory.
The script works by parsing the output of 'emerge -ef world'. So it would be a good idea to run 'emerge -ef world before this script, just to make sure that all the packages needed are still available, and that emerge is working o.k.
The script uses /tmp to store its temporary files, so this directory must already exist (doesn't it exist on all Gentoo systems ?)
Oh, and of course you need to be root to run the script.
Only tested with Portage 2.0.48-r5.
O.K. heres the script, just save it as something like /usr/bin/source_clean.sh
Code: Select all
#!/usr/bin/env bash
read_file="packages.txt"
write_file1="edit1.txt"
write_file2="edit2.txt"
write_dir="/tmp"
source_dir="/usr/portage/distfiles"
backup_dir="/usr/portage/distfiles_backup"
# Create package list
echo Creating package list
emerge -ef world > ${write_dir}/${read_file}
# Remove un-needed text from list
cat ${write_dir}/${read_file} |
while read filename
do
case ${filename} in
">>> md5 src_uri"*)
echo ${filename##*' '} >> ${write_dir}/${write_file1};;
esac
done
# Remove duplicate lines
sort -u ${write_dir}/${write_file1} > ${write_dir}/${write_file2}
# Move packages to backup file
mkdir -p ${backup_dir}
mv -f ${source_dir}/* ${backup_dir}
# Remove cvs-src directory if it exists
if [ -d ${backup_dir}/cvs-src ]
then
rm -rf ${backup_dir}/cvs-src
fi
# Move required packages back to source directory
cat ${write_dir}/${write_file2} |
while read filename
do
mv ${backup_dir}/${filename} ${source_dir}/
done
# Remove old versions from backup directory
sort -u ${write_dir}/${write_file2} > ${write_dir}/${write_file1}
rm ${write_dir}/${write_file1}
cat ${write_dir}/${write_file2} |
while read filename
do
filename=${filename%-*}"*"
if [ -f ${backup_dir}/${filename} ]
then
rm ${backup_dir}/${filename}
fi
done
# Remove temporary files
rm -f ${write_dir}/${write_file1}
rm -f ${write_dir}/${write_file2}
rm -f ${write_dir}/${read_file}
echo Distfiles directory now cleaned
Ian





