#native_company# #native_desc#
#native_cta#

Check RPMS

By Grant Walters
on March 20, 2002

Version: 1.1

Type: Full Script

Category: Other

License: GNU General Public License

Description: Will display version of RPM’s in current directory and in rpm database together. (requires Support functions snippet/code and site.cfg)

#!/cmi/bin/php -q
<?
/*
 $Id: check_rpms,v 1.1 2002/03/16 21:17:19 grant Exp $
 $Name:  $

 Script to check which of the RPMs in the current directory
 haved been installed

*/

$file_list = array();
$file_list_count = 0;
$rpm_package = array();
$rpm_package_count = 0;

function scr($text="",$termination=0) {
  switch ($termination) {
  case "1":
    echo $text;
    break;
  default:
    echo $text."n";
    break;
  }
}

function bit($text="",$termination=1)  {
  scr($text."... ",$termination);
  }

function chk($text="",$termination=1) {
  scr("CHK:".date("His").": $text ... ",$termination);
  }

function err($text="")  {
  //get "ERR"
  scr("ERR:".date("His").": $text");
  scr("nFinished Processing");
  //echo -e "n${RET}"
  exit;
  }

scr("Checking Current RPM Status");
scr(str_repeat("=",80));
$curdir=$PWD;
chk("Current Directory $curdir");
if (is_dir($curdir)) {
  scr("Present");
} else {
  scr("Missing?");
  err("Can't Open Directory");
}

$command = '/bin/rpm -qa --qf "%{NAME};%{VERSION}-%{RELEASE}.%{ARCH}n"';
//$command = '/bin/rpm -qa --qf "%{NAME}n"';

chk("Opening Current Directory");
if ($dir=opendir($PWD)) {
  scr("Handle: $dir");
  chk("RPM: In Current Directory");
  while ($file = chop(readdir($dir))) {
    $item = ereg(".*.rpm$",$file);
    if ($item) { 
      $file = ereg_replace(".rpm$","",$file);
      $package = ereg_replace("-[0-9]+[0-9]*..*$","",$file);
      $version = str_replace("$package-","",$file);
      $file_list[] = array(0=>$package, 1=>$version);
      $file_list_count++;
    }
  }
  scr("Count = $file_list_count");
  if ($file_list_count) {
    asort($file_list);
    chk("RPM: In RPM Database");
    $rpm = popen($command,"r");
    if ($rpm) {
      $rpm_package_count=0;
      while (!feof($rpm)) {
        $item = chop(fgets($rpm,512));
        if ("$item" <> "" ) {
          $parts = explode(";",$item);
          $rpm_package[$parts[0]] = $parts[1];
          $rpm_package_count++;
        }
      }
      asort($rpm_package);
      scr("Count = $rpm_package_count");
      pclose($rpm);
    } else {
      err("Can't Obtain List from pipe");
    }
    scr(str_repeat("=",80));
    scr(sprintf("%-30s%-25s%-25s","Package Name","Database Version","Disk Version"));
    scr(str_repeat("=",80));
    while ( list ($key,$file) = each ($file_list)) {
      //scr("    ".date("His"),1);
      if ($rpm_package[$file[0]]) {
        scr(sprintf("%-29s %-24s %-24s",$file[0],$rpm_package[$file[0]],$file[1]));
      } else {
        scr(sprintf("%-29s %-24s %-24s",$file[0],"Absent",$file[1]));
      }
      $file_list_count++;
    }
    scr(str_repeat("=",80));
    chk("RPMS: Checked");
    scr("Count = $file_list_count");
/*
    // This will print out the array
    while ( list ( $key,$val ) = each ($rpm_package)) {
      scr("KEY:$key:VAL:$val");
    }
*/
  }
  scr("Processing Complete",0);
} else {
  scr("Failed!");
  err("Can't Open Directory");
}

/*
 $Log: check_rpms,v $
 Revision 1.1  2002/03/16 21:17:19  grant
 Removed php3 extension

 Revision 1.1  2002/03/14 08:57:18  grant
 Completed initial program

*/
?>