# $Id: //depot/prod/DOT/dev/test/lib/NACL/APISet/Host/CLI/Linux/Redhat/rpm.pm#1 $
#
# Copyright (c) 2013 NetApp, Inc.
# All rights reserved.
#
## @summary Routines to parse Linux rpm command
##
## @description
## This is a library to parse the raw output of Linux rpm command.
## The function name will be the same as command name with underscores being
## used instead of spaces.
## For Example, the parser function for "rpm" will be rpm()
##
## @synopsis
## use NACL::APISet::Host::CLI::Linux::Redhat::rpm;
##
## rpm(%args);
##
## @author Veerabhadraswamy.P@netapp.com, dl-nacl-dev@netapp.com
##
## @status public
package NACL::APISet::Host::CLI::Linux::Redhat::rpm;
use strict;
use NACL::APISet::Response::CLI::ParserUtils;
# Include exceptions which will be thrown
use NACL::APISet::Exceptions::InvalidParamException;
use NACL::APISet::Exceptions::MissingRequiredParamException;
## @name ::rpm
##
## @summary Parses the raw output from the "rpm" command
##
## @description
## This method can be used for parsing the raw output obtained after calling
## the method from rpm with any of its options as described in the
## corresponding cdef file 'rpm.cdefs'
## @synopsis
## rpm(%args);
## INPUTS TO METHOD CALL 'rpm' ON APISET OBJECT
##
## OPTION | PROGRAMMATIC NAME | BOOLEAN OPTION |
## '-d' | 'documents' | '1' |
## '-f' | 'file' | '0' |
## '--whatprovides' | 'whatprovides' | '1' |
## '-a' | 'all' | '1' |
## '-s' | 'state' | '1' |
## '-g' | 'group' | '0' |
## '-R' | 'requires' | '1' |
## '-c' | 'configuration' | '1' |
## '-l' | 'list' | '1' |
## '-i' | 'info' | '1' |
## '--whatrequires' | 'whatrequires' | '1' |
## '-q' | 'query' | '1' |
## '--last' | 'last' | '1' |
## No switch for this key | 'package' | '0' |
## No switch for this key | 'capability' | '0' |
## '--provides' | 'provides' | '1' |
##
## @arg %args Required:
## response_object => The response object that was constructed (containing the
## raw output, command line constructed and execution status)
## @return
## Returns an array reference which contains the parsed output.
## @examples
## Command = rpm -q -i perl-Pod-Escapes-1.04-119.el6_1.1.x86_64
##
## -------- Raw Output ---------
## Name : perl-Pod-Escapes Relocations: (not relocatable)
## Version : 1.04 Vendor: Red Hat, Inc.
## Release : 119.el6_1.1 Build Date: Tuesday 04,October,2011 11:09:28 AM EDT
## Install Date: Thursday 04,October,2012 03:43:16 AM EDT Build Host: hs20-bc2-5.build.redhat.com
## Group : Development/Libraries Source RPM: perl-5.10.1-119.el6_1.1.src.rpm
## Size : 21092 License: GPL+ or Artistic
## Signature : RSA/8, Tuesday 01,November,2011 06:08:40 AM EDT, Key ID 199e2f91fd431d51
## Packager : Red Hat, Inc.
## URL : http://www.perl.org/
## Summary : Perl module for resolving POD escape sequences
## Description :
## This module provides things that are useful in decoding Pod E<...>
## sequences. Presumably, it should be used only by Pod parsers and/or
## formatters.
##
## -------- Parsed Output ------
## $VAR1 = [
## {
## 'build_host' => 'hs20-bc2-5.build.redhat.com',
## 'key_id' => '199e2f91fd431d51',
## 'build_date' => 'Tuesday 04,October,2011 11:09:28 AM EDT',
## 'install_date' => 'Thursday 04,October,2012 03:43:16 AM EDT',
## 'size' => '21092',
## 'group' => 'Development/Libraries',
## 'url' => 'http://www.perl.org/',
## 'signature_date' => 'Tuesday 01,November,2011 06:08:40 AM EDT',
## 'version' => '1.04',
## 'name' => 'perl-Pod-Escapes',
## 'release' => '119.el6_1.1',
## 'packager' => 'Red Hat, Inc. ',
## 'license' => 'GPL+ or Artistic',
## 'relocations' => 'not relocatable',
## 'signature' => 'RSA/8',
## 'source_rpm' => 'perl-5.10.1-119.el6_1.1.src.rpm',
## 'vendor' => 'Red Hat, Inc.'
## }
## ];
##
## Command = rpm -q --whatprovides webserver
##
## -------- Raw Output ---------
## httpd-2.2.15-15.el6.x86_64
##
## -------- Parsed Output ------
## $VAR1 = [
## {
## 'provided_by_rpm' => [
## 'httpd-2.2.15-15.el6.x86_64'
## ]
## }
## ];
##
sub rpm {
my %args = @_;
my $resp = $args{response_object};
my $output = $resp->get_processed_output();
_check_error( raw_output => $output, response_object => $resp );
my ( @result_array, %hash );
my $cmd = $resp->get_command();
my @lines = get_lines( raw_output => $output, response_object => $resp );
my @values;
my $result;
my $str;
my $line;
if ( $cmd =~ /^rpm -q -i/ ) {
foreach $line (@lines) {
if ( $line =~ /^Name\s+:\s+(.*\w)\s.*:\s\((.*)\)/ ) {
$result_array[0]->{'name'} = $1;
$result_array[0]->{'relocations'} = $2;
} elsif ( $line =~ /^Version\s+:\s+(.*\w)\s+Vendor:\s+(.*)/ ) {
$result_array[0]->{'version'} = $1;
$result_array[0]->{'vendor'} = $2;
} elsif ( $line =~ /^Release\s+:\s+(.*\w)\s+Build Date:\s+(.*)/ )
{
$result_array[0]->{'release'} = $1;
$result_array[0]->{'build_date'} = $2;
} elsif (
$line =~ /^Install Date:\s+(.*\w)\s+Build Host:\s+(.*)/ )
{
$result_array[0]->{'install_date'} = $1;
$result_array[0]->{'build_host'} = $2;
} elsif ( $line =~ /^Group\s+:\s+(.*\w)\s+Source RPM:\s+(.*)/ ) {
$result_array[0]->{'group'} = $1;
$result_array[0]->{'source_rpm'} = $2;
} elsif ( $line =~ /^Size\s+:\s+(\d+)\s+License:\s+(.*)/ ) {
$result_array[0]->{'size'} = $1;
$result_array[0]->{'license'} = $2;
} elsif (
$line =~ /Signature\s+:\s+(.*\w),\s(.*),\sKey ID\s+(.*)/ )
{
$result_array[0]->{'signature'} = $1;
$result_array[0]->{'signature_date'} = $2;
$result_array[0]->{'key_id'} = $3;
} elsif ( $line =~ /Packager\s+:\s+(.*)/ ) {
$result_array[0]->{'packager'} = $1;
} elsif ( $line =~ /URL\s+:\s+(.*)/ ) {
$result_array[0]->{'url'} = $1;
}
}
} elsif ( $cmd =~ /^rpm -q -a --last$/ ) {
my $count = 0;
foreach $line (@lines) {
if ( $line =~ /(\w.*\d)\s+([SMTWF].*)/ ) {
$result_array[$count]->{'package'} = $1;
$result_array[$count]->{'installed_date'} = $2;
$count++;
}
}
} elsif ( $cmd =~ /^rpm -q -g/ ) {
$result = _rpm_sub(
'lines' => \@lines,
key => 'packages_in_groups'
);
return $result;
} elsif ( $cmd =~ /^rpm -q -a/ ) {
$result = _rpm_sub(
'lines' => \@lines,
key => 'all_package'
);
return $result;
} elsif ( $cmd =~ /^rpm -q -l/ ) {
$result = _rpm_sub(
'lines' => \@lines,
key => 'files_in_package'
);
return $result;
} elsif ( $cmd =~ /^rpm -q -f/ ) {
$result_array[0]->{'package_owns_files'} = $lines[0];
} elsif ( $cmd =~ /^rpm -q -d/ ) {
$result = _rpm_sub(
'lines' => \@lines,
key => 'docs_in_package'
);
return $result;
} elsif ( $cmd =~ /^rpm -q -c/ ) {
$result = _rpm_sub(
'lines' => \@lines,
key => 'configuration_files'
);
return $result;
} elsif ( $cmd =~ /^rpm -q -s/ ) {
my $count = 0;
foreach $line (@lines) {
@values = split( '\s+', $line );
$result_array[$count]->{'state'} = $values[0];
$result_array[$count]->{'files'} = $values[1];
$count++;
}
} elsif ( $cmd =~ /^rpm -q -R/ ) {
$result = _rpm_sub(
'lines' => \@lines,
key => 'required_packages'
);
return $result;
} elsif ( $cmd =~ /^rpm -q --provides/ ) {
$result = _rpm_sub(
'lines' => \@lines,
key => 'package_provides'
);
return $result;
} elsif ( $cmd =~ /^rpm -q --whatprovides/ ) {
$result = _rpm_sub(
'lines' => \@lines,
key => 'provided_by_rpm'
);
return $result;
} elsif ( $cmd =~ /^rpm -q --whatrequires/ ) {
$result = _rpm_sub(
'lines' => \@lines,
key => 'dependencies'
);
return $result;
}
return \@result_array;
}
sub _rpm_sub {
my %args = @_;
my @lines = @{ $args{'lines'} };
my ( @result, %hash );
my $line;
foreach $line (@lines) {
push( @{ $result[0]->{ $args{key} } }, $line );
}
return \@result;
}
sub _check_error {
my %args = @_;
my $raw_output = $args{raw_output};
my $resp = $args{response_object};
my $error_message;
my $error = undef;
if ( $raw_output =~ /Usage:\s+rpm.*/i | $raw_output =~ /rpm:\s--hash.*/i )
{
$error = "InvalidParam";
$error_message = $raw_output;
} elsif ( $raw_output =~ /rpm:\sno arguments .*/i ) {
$error = "MissingRequiredParam";
$error_message = $raw_output;
}
if ($error) {
$error = 'NACL::APISet::Exceptions::' . $error . 'Exception';
$error->throw( text => $error_message, response_object => $resp );
}
} ## end sub _check_error
1;