I left the original script in case there is anyone who might know how to change equery's piped output.
This one is ugly but it DOES work. There are some issues with the preg_replace patterns but it only affected a small number of packages. It's also significantly slower than the previous bash script since I decided to use php.
Again double check the output and pipe it through sort to get a new package.use file.
Script still beats doing it by hand, especially when you're dealing with a ton of packages and use flags that have always been set on the command line.
Code:
#!/usr/bin/php
<?php
set_time_limit(480);
// generate list of installed packages
exec('equery list > /tmp/pkglist.tmp');
//load the package list into an array
$packages = file('/tmp/pkglst.tmp');
// open the file where we will write our final output for package.use
$packageUse = fopen('/tmp/package.use','w+');
// create an array of clean package names and equery that package
foreach($packages as $indx=>$package)
{
//strip the version numbers leaving only a package name
$pat = "/-[r].*$/";
$package = preg_replace($pat,"",$package);
$pat = "/.[^-]*$/";
$package = trim(preg_replace($pat,"",$package));
// check for empty line, useless to run equery against nothing
if("$package" != "")
{
// get the flags from equery and output to a temporary file
exec("equery -N uses $package > /tmp/pkg.uses");
// load flags into an array for examination
$flags = file('/tmp/pkg.uses');
// size of the array - The first 6 lines of the output is useless
// and if the size is less than 6 lines then there are no use flags
$arySize = sizeof($flags);
if($arySize > 5)
{
// start reading from line 6
for($k=6; $k<=$arySize; $k++)
{
// format the line into something more useful
$fields = explode(' ',$flags[$k]);
// field 2 indicates the current use flags state (field 0 is the system wide setting for that flag)
$isSet = $fields[2];
// remove the plus symbol
$isSet = preg_replace("/\+/","",$isSet);
// get the use flag's name
$flagName = $fields[3];
// put it all back together again ie: -flag or flag
$pkgFlags .= " " . trim($isSet) . trim($flagName);
}
// put it back together ie: some-class/packageName flag1 -flag2 flag3
$outp = trim("$package $pkgFlags")."\n";
// output so we can watch what's happening
echo $outp;
//write it to the file
fwrite($packageUse,$outp);
unset($pkgFlags);
}
}
//remove the temporary file
unlink("/tmp/pkg.uses");
// proceed to next package in list
}
?>
at least it's something right?? Start the script, go eat a sandwich and return to examine your use list.