Created
September 13, 2012 14:15
-
-
Save youknowriad/3714593 to your computer and use it in GitHub Desktop.
RpmQuery : A quick class to monitor installed Rpms with rpm -q
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Rpm | |
{ | |
const FILED_SEPARATOR = ';;;'; | |
const ROW_SEPARATOR = '|||'; | |
public function listPackages() | |
{ | |
$parameters = array('name', 'version', 'packager'); | |
$command = sprintf('rpm -q -a --queryformat "%s"', $this->getQueryFormatForParameters($parameters)); | |
$process = new process($command); | |
$process->run(); | |
$rpms = array(); | |
$rows = explode(self::ROW_SEPARATOR, $process->getOutput()); | |
foreach ($rows as $row) { | |
$parts = explode(self::FILED_SEPARATOR, $row); | |
if (count($parts) == count($parameters)) { | |
$rpms[] = $this->getRpmWithParameters($parts, $parameters); | |
} | |
} | |
return $rpms; | |
} | |
public function package($package_name) | |
{ | |
$parameters = array('name', 'size', 'version', 'packager', 'release', 'installtime'); | |
$command = sprintf('rpm -q %s --queryformat "%s"', $package_name, $this->getQueryFormatForParameters($parameters, false)); | |
$process = new process($command); | |
$process->run(); | |
$row = $process->getOutput(); | |
$parts = explode(self::FILED_SEPARATOR, $row); | |
if (count($parts) == count($parameters)) { | |
return $this->getRpmWithParameters($parts, $parameters); | |
} | |
return null; | |
} | |
private function getQueryFormatForParameters($parameters, $row_separator = true) | |
{ | |
$strs = array(); | |
foreach ($parameters as $parameter) { | |
$strs[] = '%{'.$parameter.'}'; | |
} | |
return implode(self::FILED_SEPARATOR, $strs).($row_separator ? self::ROW_SEPARATOR : ''); | |
} | |
private function getRpmWithParameters($parts, $parameters) | |
{ | |
$rpm = array(); | |
$key = 0; | |
foreach ($parameters as $parameter) { | |
$rpm[$parameter] = trim($parts[$key]); | |
$key++; | |
} | |
return $rpm; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$rpm = new Rpm(); | |
$packages = $rpm->listPacakges(); | |
$package = $rpm->package('php'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment