LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Slackware (https://www.linuxquestions.org/questions/slackware-14/)
-   -   Rudimentary dependency checker :) (https://www.linuxquestions.org/questions/slackware-14/rudimentary-dependency-checker-196283/)

Tinkster 06-22-2004 03:02 AM

Rudimentary dependency checker :)
 
Hi Guys ...

Feel free to comment/enhance. It's an ultra-primitive
prototype ... :)

Code:

#!/bin/bash
# dep_check.sh - author Tink
# Inspired by Cameron Kerr's testldddeps,
# it lacks his brevity and elegance but may
# be more readable :)

IFS='
'

# Some preliminary error-checking
if [ $# != "1" ]; then
    echo "Usage: dep_check.sh <target directory>"
    exit 1
fi

if [ ! -d $1 ]; then
    echo $1 " does not exist or is not a directory!"
    exit 2
fi


# Building the list of files ...
files=`find $1 -type f -perm +111 -xdev `
echo "Checking perms!" > /tmp/ldd.cache
for i in $files ; do
    miss_libs=`ldd $i 2>/dev/null | grep "not found"`
    if [ $? == "0" ]; then
        echo $i
        for j in $miss_libs; do
            echo "  " $j
            found=`grep "$j" /var/log/packages/*`
            missing=`grep "$j" /var/log/removed_packages/*`
            if [ ! $found = "" ]; then
                echo $j " from  installed package " $found " is missing!"
            fi
            if [ ! $missing = "" ]; then
                echo $j " was part of removed package " $missing
            fi
        done
        echo ""
    fi
done


Cheers,
Tink

thegeekster 08-16-2004 03:08 PM

Thanx, Tink.........

Just what I'm looking for to use in my Slackware PkgBuild setup I'm working on...........I've already got ideas to expand on this script for a system-wide dep check....... :)

thegeekster 08-18-2004 12:19 AM

Okay, here's another little script for checking dependencies........

I decided not to use 'ldd' and used 'objdump' instead.........The reason for this is 'ldd' not only returns direct dependencies, but also returns dependencies of dependencies (quite a mouthful, eh), or "indirect" dependencies............The rationale behind this is you only need to worry about dependencies that are called directly by the executable (and not indirectly through one of it's dependencies)...........A proper search will turn up any missing files.........(plus I use 'objdump' for a bit of error-checking)
Code:

#!/bin/sh
#*******************************************************************************
# Name: finddep
#
# Look for binary dependencies by supplying the name of a file or a directory.
# All output goes to the screen.
#
# Some files may return the error message: "File format not recognized". All
# this means is it is not a binary executable or shared object (lib file). It's
# probably a script or other file type.
#
# TIP: When searching a whole directory, you can limit the output to the screen
#      by grep'ing for the word "MISSING", like so:
#
#        finddep <directory> | grep 'MISSING'
#
#      Now, only missing dependencies will be listed on the screen.
#*******************************************************************************

# Check for command line args:
[ $# -ne 1 ] && echo "
  Usage: $0 NAME  (where NAME can be a file or directory)
" && return 1

find_dep() {
  ldd $1 | grep "`$OBJDUMP | grep 'NEEDED' | cut -d' ' -f9`" \
    | sed 's/not\ found/MISSING/' | cut -d ' ' -f1,3  | while read dep
  do
    echo "$1  -->  $dep"
  done
}

# If a file is supplied on the command line:
[ -f "$1" ] && {
  OBJDUMP="objdump -p $1"
  # Use 'objdump' for error-checking:
  [ "`$OBJDUMP 2>/dev/null`" ] && {
    # Look for dependencies:
    [ "`$OBJDUMP | grep 'NEEDED'`" ] && {
      echo "FILE --> DEPENDENCY LOCATION"
      find_dep "$1"
      # Use 'column' for a bit of formatting:
      } | column -t && echo || echo -e "$1: NO DEPENDENCIES\n"  # Didn't find "NEEDED"
  } || {
    # Using the error message from 'objdump':
    $OBJDUMP 2>&1 | cut -d' ' -f2- && echo
  }
}

# Otherwise, if a directory is supplied on the command line;
[ -d "$1" ] && printf "FILE \t    -->  DEPENDENCY\t LOCATION\n" && {
  # Ignore subdirectories when parsing files in a directory:
  find "$1" -type f -prune | while read d
  do {
    OBJDUMP="objdump -p $d"
    # Use 'objdump' for error-checking:
    [ "`$OBJDUMP 2>/dev/null`" ] && {
      # Look for dependencies:
      [ "`$OBJDUMP | grep 'NEEDED'`" ] && {
        find_dep "$d"
        # Use 'column' for a bit of formatting:
        } | column -t && echo || echo -e "$d: NO DEPENDENCIES\n"  # Didn't find "NEEDED"
    } || {
      # Using the error message from 'objdump':
      $OBJDUMP 2>&1 | cut -d' ' -f2- && echo
    }
  } done
}

The output is also formatted a bit for easier reading, and when searching in directories, subdirectories will be ignored, This is so you can create a list for a single directory...........If you want a recursive search, just place the script in a 'find' command, such as:

find <path> -type d -exec finddep {} \; | grep 'MISSING'

This will search out any missing dependencies............

I plan on expanding this to create a database of dependencies in /var/log/ for Slackware, one for each package installed, much like the "packages" and "scripts" directories in /var/log....... :)


=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
EDIT: Made changes in the find_dep() function to use 'ldd' instead of 'objdump' for searching dependencies.........

thegeekster 08-18-2004 03:15 PM

Okay, stop the presses.....................It seems the 'whereis' command I use for finding the lib dependencies has a serious limitation................it does not know where all the lib directories are. like for KDE or X11 (apparently doesn't use the /etc/ld.so.cache database).......

So the 'whereis' command needs a little help.......or I'll need to use 'ldd' for searching locations for dependencies.......

Anyway, my script needed more work...........so I revised the find_dep() function to use 'ldd' while still retaining 'objdump' for error-checking (it has a more informative error message which I can use).......

The changes are already made in the script above...............

:)


All times are GMT -5. The time now is 02:40 PM.