[CentOS] Scriptable way to edit yum .repo files?

Thu Jan 8 03:57:00 UTC 2009
Amos Shapira <amos.shapira at gmail.com>

2009/1/8 R P Herrold <herrold at 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



Here is my script:


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
    # if no empty lines after last repo
    defined $reponame and
    defined $PRIORITIES{$reponame} and
      $new_fh->print("priority=", $PRIORITIES{$reponame}, "\n");
      or die "!!! configure_priorities: Failed to close new version of
\"$repo_file\": $!";
    or warn "close failed: $!";


exit 0;