Hello,
Is there a standard programmatic way to manipulate yum configuration files, particularly the .repo files?
I want to add things like "priority=..." per repo, or "check_obsoletes=1" to the priorities plugin config.
I can cook specific search/append using perl or sed but was wondering whether there is a more elegant way.
I found Perl's Conf::INI module but it expects comments beginning with ";", not "#".
Thanks,
--Amos
On Thu, Jan 08, 2009 at 11:11:53AM +1100, Amos Shapira wrote:
Hello,
Is there a standard programmatic way to manipulate yum configuration files, particularly the .repo files?
I want to add things like "priority=..." per repo, or "check_obsoletes=1" to the priorities plugin config.
I can cook specific search/append using perl or sed but was wondering whether there is a more elegant way.
I found Perl's Conf::INI module but it expects comments beginning with ";", not "#".
Not that I'm aware of, but sounds like an interesting idea. You also might ask on the yum or yum development mailing list.
Let us know what you find out!
Ray
Amos Shapira wrote:
Is there a standard programmatic way to manipulate yum configuration files, particularly the .repo files?
Puppet has a yum module, which is quite capable and what I use.
2009/1/8 Karanbir Singh kbsingh@centos.org:
Amos Shapira wrote:
Is there a standard programmatic way to manipulate yum configuration files, particularly the .repo files?
Puppet has a yum module, which is quite capable and what I use.
Thanks to both of you. We don't use Puppet for all our hosts and adding it for those which don't need it so far is a bit of an overkill.
Actually I'd rather try to reduce our dependence on puppet (and possibly replace it with "rollout", http://code.google.com/p/rollout/, or gradually improve our own home-grown scripts).
Thanks,
--Amos
On Wed, Jan 7, 2009 at 19:11, Amos Shapira amos.shapira@gmail.com wrote:
I found Perl's Conf::INI module but it expects comments beginning with ";", not "#".
Why don't you use Python's ConfigParser? That's what yum itself actually uses (AFAIK). http://docs.python.org/library/configparser.html
With that module, you can read a file, modify the objects, and then write it to a new file.
HTH, Filipe
2009/1/8 Filipe Brandenburger filbranden@gmail.com:
On Wed, Jan 7, 2009 at 19:11, Amos Shapira amos.shapira@gmail.com wrote:
I found Perl's Conf::INI module but it expects comments beginning with ";", not "#".
Why don't you use Python's ConfigParser? That's what yum itself actually uses (AFAIK). http://docs.python.org/library/configparser.html
With that module, you can read a file, modify the objects, and then write it to a new file.
That's exactly (well, 99%) what I was hoping to find. I guessed that there are tools already around used by Yum but wouldn't know where to start looking. I looked for a more yum-specific code. I'll try to wrap the Python thing with something I can call from Perl.
Cheers,
--Amos
On Thu, 8 Jan 2009, Amos Shapira wrote:
I found Perl's Conf::INI module but it expects comments beginning with ";", not "#".
and | sed -e 's@^#@;@g' cannot cure that bad habit on generated files or an input stream?
[herrold@centos-5 ~]$ cat - << END | sed -e 's@^#@;@g'
one two#two $three#three #four;four #five#five ;six#six END
one two#two ;three ;four;four ;five#five ;six#six [herrold@centos-5 ~]$
-- Russ herrold
2009/1/8 R P Herrold herrold@centos.org:
On Thu, 8 Jan 2009, Amos Shapira wrote:
I found Perl's Conf::INI module but it expects comments beginning with ";", not "#".
and | sed -e 's@^#@;@g' cannot cure that bad habit on generated files or an input stream?
Possibly, but then I'll get pushed back to scripting things around.
When I saw Filipe's pointer to configparser I already finished coding something rough in perl which detects [section]'s in input files, adds the "priority=nnn" for each section and moves the old version to a backup with "~". The code should be generalised to do the other stuff I'm interested in (e.g. configure yum-priorities, or the trigger for all of this - yum-updatesd). It assumes that a section and also end with the first empty line. Maybe this is wrong but it keeps my current files "looking right".
It's small enough that I pasted it below for your amusement.
I also found a python script based on configparser which can do this from command line (http://robinbowes.com/article.php/20081026162228424) but it loses comments.
Cheers,
--Amos
Here is my script:
#!/usr/bin/perl
use strict; use warnings; use IO::File; use Readonly;
Readonly::Hash our %PRIORITIES => ( 'base' => '1', 'updates' => '1', 'addons' => '1', 'extras' => '1', 'centosplus' => '2', 'c5-testing' => '13', 'rpmforce' => '10', );
sub configure_priorities { my $orig_fh = new IO::File; my $new_fh = new IO::File;
for my $repo_file (glob '/etc/yum.repos.d/*.repo') { $orig_fh->open($repo_file, 'r') or die "!!! configure_priorities: failed to open "$repo_file""; rename($repo_file, $repo_file.'~') or die "!!! configure_priorities: failed to rename "$repo_file" to "$repo_file~": $!"; $new_fh->open($repo_file, 'w') or die "!!! configure_priorities: failed to create new "$repo_file": $!";
my $reponame = undef; while (my $line = $orig_fh->getline) { if ($line =~ /^[(.+)])/) { defined $reponame and defined $PRIORITIES{$reponame} and $new_fh->print("priority=", $PRIORITIES{$reponame}, "\n"); $reponame = $1; # will become undef if current $line is empty } $new_fh->print($line); } # if no empty lines after last repo defined $reponame and defined $PRIORITIES{$reponame} and $new_fh->print("priority=", $PRIORITIES{$reponame}, "\n"); $new_fh->close or die "!!! configure_priorities: Failed to close new version of "$repo_file": $!"; } $orig_fh->close or warn "close failed: $!"; }
configure_priorities;
exit 0;