[eggPlant Functional] FindUnusedImages

This FindUnusedImages script makes use of the FileDispenser example to step through all of the images in a suite. For each image, a second FileDispenser iterates over all of the scripts in the suite to see if any of them use that image.

If an image is found which doesn't seem to be in use by any of the scripts in the suite, the script will perform an action -- either deleting the script, or moving it into an "UnusedImages" directory within the suite. The script will optionally query you about each unused image that it finds, before taking action with that image.

Be aware that the search done by this script is fairly simple-minded, and also doesn't take into account the fact that some images may be used by scripts in other suites. If your images are used by scripts in several suites, you may want to modify this script to examine the scripts in those suites as well.

Note: You'll need version 1.5 (or later) of Eggplant, and have version 2 of the FileDispenser script in the same suite (or in a helper suite) to be able to successfully run this script. For your own protection, please make sure you make a backup of your suite, or at least your images directory, before running this script.

Comments

  • I have written a UNIX script to help me with the maintenance as well.

    We only have one license available, and I can't use Eggplant to help me do the usual mundane stuff.

    Anyways I tried to attach the file, but find that I don't see how to.

    Here's the code for the file. I used the bash shell. The usage for the file is at the top so everything should be explained.

    ** NOTE ** This is probably not the best way to do this. I was going to use PERL but that would've taken me too long to implement all this. I wrote this in about 3 hrs, so feel free to yell at me if you spot something wrong..
    
    #!/bin/bash
    
    usage() {
    cat <<EOF 
    
    PURPOSE:
       This program can do either:
    
       *   Create a report of used and unused images in a suite
          
           Use the -f option to specify the path of the Suite
    
       *   List scripts image(s) were used in (Specific for that suite)
    
           Images have to be listed in a file. The file should be passed in
           using the -i option
    
       Note *** All output is to stdout. You can re-direct the output to a file
    
    USAGE: 
       find_unused_imgs.sh -f <eggplant_suite_path> [-i <img_name_list>] \\
                              [-s] [-h | -help]
    
    OPTIONS:
       -f <eggplant_suite_path>    Path of Suite to check
       -s                          Toggle to show used files and number of occurances
       -i <img_name_list_file>     File containing list of images. This option will
                                   find and print scripts the images are used in.
                                   General report of image used will not be created .
    
    EXAMPLES:
    
       Report showing only unused images for Suite win_stuff.suite
          $   find_used_imgs.sh -f /usr/suites/win_stuff.suite
    
       Report showing used and unused imgs for Suite win_stuff.suite
          $   find_used_imgs.sh -f /usr/suites/win_stuff.suite -s
    
       Report showing usage information for a list of images specified in 
       file /tmp/fileList.txt
          $   find_used_imgs.sh -f /usr/suites/win_stuff_suite -i /tmp/fileList.txt
    
       The format of fileList.txt should list the images, one on each line.
       Do not add the extension of the file.
    
       fileList.txt
       img1
       img2
       img3
       img4
    
    EOF
    }
    
    SHOW_USED_IMGS=0
    SHOW_IMG_USE=0
    EGGPLANT_FOLDER=""
    IMG_NAME_FILE=""
    
    #
    #  Check for command line args
    #
    
    if [ $# -eq 0 ]; then
        usage
        exit
    fi
    
    #
    #  Do stuff depending on command line args
    #
    
    while [ $# -ne 0 ]; do
        case $1 in
    	-f) shift; EGGPLANT_FOLDER="${1}"
    	    ;;
    	-s) SHOW_USED_IMGS=1
    	    ;;
    	-i) shift; IMG_NAME_FILE="${1}"
    	    SHOW_IMG_USE=1
    	    ;;
    	-*) usage; exit
    	    ;;
        esac; shift
    done
    
    #
    #  Check if Eggplant suite passed in is a valid suite
    #
    
    if [ "${EGGPLANT_FOLDER}" = "" ]; then    
        usage
        echo 'ERROR: -f option requires a valid eggplant suite!'
        echo
        exit
    else
        if [ -d ${EGGPLANT_FOLDER} ];then
    	echo
    	echo "Folder to work with: ${EGGPLANT_FOLDER}"
    	echo
        else
            usage
    	echo "ERROR: folder ${EGGPLANT_FOLDER} cannot be found!"
            echo
    	exit
        fi
    fi
    
    #
    # If img find option has been set check if image exists in folder
    # and if so set program mode to image find mode
    #
    
    IMG_FOLDER="${EGGPLANT_FOLDER}/Images"
    SCRIPTS_FOLDER="${EGGPLANT_FOLDER}/Scripts"
    
    
    #
    # Branch depending on whether we want to find information about image
    # or create a report (-i option is not passed in for report)
    #
    
    if [ ${SHOW_IMG_USE} -eq 0 ]; then
        # A bit clumsy but I wasn't able to get the wildcard working right  
        # away, so used this to get the task done
        
        cd ${IMG_FOLDER}
    
    
        #
        # Create Report on used and unused images
        # 
        IMG_LIST=/tmp/imglist
        USED_IMGS=/tmp/usedimgs
        FIND_FILES=*.tiff
        
        if [ -f ${IMG_LIST} ]; then
    	rm -f ${IMG_LIST}
        fi
        
        if [ -f ${USED_IMGS} ]; then
    	rm -f ${USED_IMGS}
        fi
        
        ls -1 ${FIND_FILES} | cut -f1 -d'.' > ${IMG_LIST}
        
        cd ${SCRIPTS_FOLDER}
        
        COUNT=0
        TOTAL=0
        
        echo "Unused Files:"
        
        while read line; do
    	
    	NUM_OCC=`grep "${line}" * | wc -l | sed 's/ //g'`
    	
    	if [ ${NUM_OCC} -eq 0 ]; then
    	    echo "     ${line}"
    	    COUNT=`expr ${COUNT} + 1`
    	else
    	    echo "${line} is used ${NUM_OCC} time(s)" >> ${USED_IMGS}
    	fi    
    	TOTAL=`expr ${TOTAL} + 1`
    	
        done < ${IMG_LIST}
        
        
        if [ ${SHOW_USED_IMGS} -eq 1 ]; then
    	echo
    	echo "Used Images:"
    	while read line; do
    	    echo "     ${line}"
    	done < ${USED_IMGS}
        fi
        
        echo
        echo "Total Files: ${TOTAL}   Num Unused files: ${COUNT}    Used Files: `expr ${TOTAL} - ${COUNT}`"
        echo
    else
        #
        #  Check if imagelist has been specified and exists.
        #  If it does, then read file, get image names, find information
        #  and print report
        #
    
        if [ "${IMG_NAME_FILE}" = "" ]; then
    	usage
    	echo "ERROR: image list file name cannot be empty!"
            echo
    	exit
        fi
        if [ -f ${IMG_NAME_FILE} ]; then
    	echo "Image list file: ${IMG_NAME_FILE}"
    	echo
        else
    	usage
    	echo "ERROR: file ${IMG_NAME_FILE} cannot be found!"
            echo
    	exit	
        fi
    
        cd ${SCRIPTS_FOLDER}
        TEMP_IMG_SCRIPT_LIST=/tmp/scripts
        
        if [ -f ${TEMP_IMG_SCRIPT_LIST} ]; then
    	rm -f ${TEMP_IMG_SCRIPT_LIST}	
        fi
    
        while read line; do
    	echo "----------------------------------------------------------" >> ${TEMP_IMG_SCRIPT_LIST}
    	echo "Image Name : ${line}" >> ${TEMP_IMG_SCRIPT_LIST}	
    	echo "----------------------------------------------------------" >> ${TEMP_IMG_SCRIPT_LIST}
    	GREP_OUT=`grep "${line}" * | cut -f1 -d':' | sed 's/[ 	]+/\n/g' | sed '$!N; /^\(.*\)\n\1$/!P; D'`
    	
    	echo "${GREP_OUT}" >> ${TEMP_IMG_SCRIPT_LIST}
    	echo "" >> ${TEMP_IMG_SCRIPT_LIST}
    	echo "" >> ${TEMP_IMG_SCRIPT_LIST}
        done < ${IMG_NAME_FILE}
    
        cat ${TEMP_IMG_SCRIPT_LIST}
    fi
    
    
  • EggplantMattEggplantMatt ForumAdmin admin
    Thanks, Bharathh. We appreciate your taking the time to post useful utility scripts like this one. Your version has some nice features and I'm sure it will be useful to people who may be more comfortable with shell scripting than they currently are with SenseTalk.
  • Here one little improvement on the Unix script

    If the image files are in sub folders of Image directory,

    replace this line:
    ls -1 ${FIND_FILES} | cut -f1 -d'.' > ${IMG_LIST}

    with this line:
    find . -name "${FIND_FILES}" | grep -v .svn | cut -c 3- | cut -f1 -d'.' > ${IMG_LIST}

    ~Qie He - "Fried Eggplant with meat in the middle"
  • eggycarrieeggycarrie Member
    Update: there is now a feature in eggPlant Functional to Show Image Usage, available in the menu by right-clicking on one of your images in the image list in your Suite. This will mark images that are referenced somewhere in that same Suite green, and mark those that are not referenced as red. Note that because images inside an image collection are often not directly referenced in your scripts, they will show as red even when the image collection itself shows up as green/referenced.
Sign In or Register to comment.