LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware
User Name
Password
Slackware This Forum is for the discussion of Slackware Linux.

Notices


Reply
  Search this Thread
Old 05-13-2012, 11:43 PM   #1
bosth
Member
 
Registered: Apr 2011
Location: British Columbia, Canada
Posts: 304

Rep: Reputation: 127Reputation: 127
Checking for unused SlackBuilds


I wrote a little script to check for unneeded SlackBuilds on my system and thought I'd share it here. It works by checking each installed package with a _SBo tag to see if it is a dependency for any other installed SlackBuild package.

By default, the script just prints the list of the 'unneeded' packages to stdout. The commandline options are: -i (interactive) prompts for package deletion during the search; -p prompts for deletion after the search; -v runs in verbose mode, where you can see dependencies.

The script works by checking the README files for dependency info, so it is slow and can get tripped up by spelling mistakes or optional dependencies. It also can't tell the difference between packages which are dependencies only and packages which are both dependencies and stand-alone programs. It's not perfect, it works well enough.

You need to have sbopkg installed for this all to work. Obviously, package deletion only works if you run as root.

Code:
#!/bin/bash

# modified version of that found in removepkg
package_name() 
{
    STRING=`basename $1`
    # If we don't do this, commands run later will take the '-' to be an option
    # and will destroy the package database.  Packages should not contain spaces
    # in them.  Normally this type of problem results from a command line typo.
    if [ "$(echo $STRING | cut -b 1)" = "-" ]; then
      STRING="malformed-package-name-detected"
    fi
    # Check for old style package name with one segment:
    if [ "$(echo $STRING | cut -f 1 -d -)" = "$(echo $STRING | cut -f 2 -d -)" ]; then
      echo $STRING
    else # has more than one dash delimited segment
      # Count number of segments:
      INDEX=1
      while [ ! "$(echo $STRING | cut -f $INDEX -d -)" = "" ]; do
	INDEX=$(expr $INDEX + 1)
      done
      INDEX=$(expr $INDEX - 1) # don't include the null value
      # If we don't have four segments, return the old-style (or out of spec) package name:
      if [ "$INDEX" = "2" -o "$INDEX" = "3" ]; then
	echo $STRING
      else # we have four or more segments, so we'll consider this a new-style name:
	NAME=$(expr $INDEX - 3)
	NAME="$(echo $STRING | cut -f 1-$NAME -d -)"
	echo $NAME
      fi
    fi
}

remove_pkg()
{
    if [ -n "$REMOVE_ALL" ]
    then
	INPUT="Y"
    else
	echo -en "\033[0m: remove? (y)es/(n)o/(a)ll/(q)uit "
	read -n 1 INPUT
	INPUT=$(echo $INPUT | tr '[:lower:]' '[:upper:]')
    fi

    if [ "$INPUT" = "Q" ]
    then
	echo
	exit 0
    fi
    if [ "$INPUT" = "A" ]
    then
	REMOVE_ALL=true
    fi
    if [ "$INPUT" = "I" ]
    then
	unset INTERACTIVE
    fi
    if [ "$INPUT" = "Y" ]
    then
	/sbin/removepkg $1
    fi
}

set -e
source /etc/sbopkg/sbopkg.conf

LOGPATH=/var/log/packages
SBOPATH=$REPO_ROOT/SBo/$REPO_BRANCH
FLAGGED=""

for PARAM in "$@"
do
    if [ "$PARAM" = "-v" ]; then
	VERBOSE=true
    fi
    if [ "$PARAM" = "-p" ]; then
	PROMPT=true
    fi
    if [ "$PARAM" = "-i" ]; then
	INTERACTIVE=true
	unset PROMPT
    fi
    if [ "$PARAM" = "-h" ]; then
	echo -e "-i interactive (prompt for deletion after each package)\n-p prompt for deletion before exit\n-v verbose"
	exit 0
    fi
done

for FILENAME in $LOGPATH/*SBo
do
  PKGNAM="$(package_name $FILENAME)"
  if [ -n "$VERBOSE" ]; then
      echo -ne "\033[0m$PKGNAM ... "
  fi
  HAS_DEPENDENCY=0
  HAS_INSTALLED_DEPENDENCY=0
  for MATCH in $(find $SBOPATH -name README -exec grep -wil $PKGNAM '{}' + )
  do
      DEPENDENCY=$(basename $(dirname $MATCH) )
      if [ "$PKGNAM" != "$DEPENDENCY" ]; then
	  HAS_DEPENDENCY=1 
	  ls $LOGPATH/*SBo | grep -q $LOGPATH/$DEPENDENCY && HAS_INSTALLED_DEPENDENCY=1
	  if [ "$?" -eq 0 ]; then
	      if [ -n "$VERBOSE" ]; then
		  echo -en " \033[1;32m$DEPENDENCY "
	      fi
	  else
	      if [ -n "$VERBOSE" ]; then
		  echo -en " \033[0m$DEPENDENCY "
	      fi
	  fi
      fi
  done

  if [ "$HAS_DEPENDENCY" -eq 1 ]; then
      if [ "$HAS_INSTALLED_DEPENDENCY" -eq 0 ]; then
	  if [ -n "$VERBOSE" ]; then
	      echo -en "\033[1;31m ... not needed"
	  else
	      echo -en "\033[0m$PKGNAM"
	  fi

	  if [ -n "$INTERACTIVE" ]; then
	      if [ $(whoami) = "root" ]; then
		  remove_pkg $PKGNAM    
	      fi
	  fi

	  if [ -z "$VERBOSE" ]; then
	      echo
	  fi

	  FLAGGED="$FLAGGED $PKGNAM"
      fi
  fi

  if [ -n "$VERBOSE" ]; then
      echo
  fi
done

if [ -n "$VERBOSE" ]; then
    FLAGGED_COUNT=$(echo $FLAGGED | wc -w)
    echo -e "\n\033[0mNumber of packages that can be removed: $FLAGGED_COUNT\n"
fi

if [ -z "$INTERACTIVE" -a -n "$PROMPT" ]; then
    if [ $(whoami) = "root" ]; then
	for PKGNAM in $FLAGGED
	do
	    echo -en "\033[0m$PKGNAM"
	    remove_pkg $PKGNAM
	    echo
	done
    else
	echo "Run as root to remove unneeded packages"
    fi
fi

exit 0

Last edited by bosth; 05-14-2012 at 11:10 PM.
 
  


Reply

Tags
slackbuilds


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
/etc/init.d/cups status => checking for cupsd: unused. WHY? JZL240I-U Linux - Software 5 12-17-2008 03:25 AM
Unused Modules pete1234 Linux - General 3 07-16-2007 12:42 AM
making use of unused partitions adityavpratap Slackware 2 03-18-2007 07:49 PM
Use SlackBuilds.org or my own hosting to offer up SlackBuilds? hollywoodb Slackware 6 11-30-2006 08:56 PM
amavisd unused ?? Wynand1 SUSE / openSUSE 0 09-22-2005 07:50 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware

All times are GMT -5. The time now is 03:24 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration