diff options
-rw-r--r-- | lib/MDV/Distribconf/Build.pm | 115 | ||||
-rw-r--r-- | t/05build.t | 52 |
2 files changed, 158 insertions, 9 deletions
diff --git a/lib/MDV/Distribconf/Build.pm b/lib/MDV/Distribconf/Build.pm index dd71ff6..756ddb8 100644 --- a/lib/MDV/Distribconf/Build.pm +++ b/lib/MDV/Distribconf/Build.pm @@ -13,6 +13,7 @@ MDV::Distribconf::Build - Subclass to MDV::Distribconf to build configuration use strict; use warnings; use MDV::Distribconf; +use File::Path; use base qw(MDV::Distribconf MDV::Distribconf::Checks); our $VERSION = (qq$Revision$ =~ /(\d+)/)[0]; @@ -53,8 +54,9 @@ sub init { } foreach my $dir (map { $self->getfullpath(undef, $_) } qw(mediadir infodir)) { if (!-d $dir) { - if (!mkdir($dir)) { - warn "Cannot create $dir: $!\n"; + eval { mkpath($dir) }; + if ($@) { + warn "Cannot create $dir: $@\n"; return 0; } } @@ -79,16 +81,18 @@ Return 1 on success, 0 otherwise sub create_media { my ($self, $media) = @_; + $self->setvalue($media, undef, undef); foreach my $dir (map { $self->getfullmediapath($media, $_) } qw(path infodir)) { if (!-d $dir) { - if (!mkdir($dir)) { - warn "Cannot create $dir: $!\n"; + eval { mkpath($dir) }; + if ($@) { + warn "Cannot create $dir: $@\n"; + $self->delvalue($media, undef); return 0; } } } - $self->setvalue($media, undef, undef); 1; } @@ -104,16 +108,71 @@ no defined parameters. sub setvalue { my ($distrib, $media, $var, $val) = @_; $media ||= 'media_info'; + $distrib->{cfg}->AddSection($media); if ($var) { + if ($media && !$distrib->mediaexists($media)) { + $distrib->setvalue($media); + } $var =~ /^(?:media|info)dir\z/ and do { $distrib->{$var} = $val; - return; + return 1; }; - $distrib->{cfg}->newval($media, $var, $val) - or warn "Can't set value [$var=$val] for $media\n"; + if ($val) { + $distrib->{cfg}->newval($media, $var, $val) + or warn "Can't set value [$var=$val] for $media\n"; + } else { + $distrib->{cfg}->delval($media, $var); + } + } + $distrib->_post_setvalue($media, $var, $val) if ($media); +} + +sub _post_setvalue { + my ($distrib, $cmedia, $cvar, $cval) = @_; + if ($cvar) { + my $vsettings = MDV::Distribconf::MediaCFG::_value_info($cvar); + if ($vsettings->{cross}) { + my %pointed_media = map { $_ => 1 } split(/\s/, $cval); + foreach my $media ($distrib->listmedia()) { + my %ml = map { $_ => 1 } + split(/\s/, $distrib->getvalue($media, $vsettings->{cross})); + + if (exists($pointed_media{$media})) { + exists($ml{$cmedia}) and next; + $ml{$cmedia} = 1; + } else { + exists($ml{$cmedia}) or next; + delete($ml{$cmedia}); + } + $distrib->setvalue( + $media, + $vsettings->{cross}, + join(" ", keys %ml), + ); + } + } } else { - $distrib->{cfg}->AddSection($media); + foreach my $media ($distrib->listmedia()) { + foreach my $val ($distrib->{cfg}->Parameters($media)) { + my $vsettings = MDV::Distribconf::MediaCFG::_value_info($val); + if ($vsettings->{cross}) { + if (grep { $_ eq $cmedia } + split(/\s/, $distrib->getvalue($media, $val))) { + my %ml = map { $_ => 1 } + split(/\s/, $distrib->getvalue($cmedia, $vsettings->{cross})); + exists($ml{$media}) and next; + $ml{$media} = 1; + $distrib->setvalue( + $cmedia, + $vsettings->{cross}, + join(" ", keys %ml), + ); + } + } + } + } } + 1; } =item $distrib->delvalue($media, $var) @@ -130,6 +189,44 @@ sub delvalue { } else { $distrib->{cfg}->DeleteSection($media); } + $distrib->_post_delvalue($media, $var); +} + +sub _post_delvalue { + my ($distrib, $cmedia, $cvar) = @_; + foreach my $media ($distrib->listmedia()) { + if ($cvar) { + my $vsettings = MDV::Distribconf::MediaCFG::_value_info($cvar); + if ($vsettings->{cross}) { + if($distrib->getvalue($media, $vsettings->{cross})) { + my %ml = map { $_ => 1 } split(/\s/, $distrib->getvalue($media, $vsettings->{cross})); + exists($ml{$cmedia}) or next; + delete($ml{$cmedia}); + + $distrib->setvalue( + $media, + $vsettings->{cross}, + join(" ", keys %ml) + ); + } + } + } else { + foreach my $val ($distrib->{cfg}->Parameters($media)) { + my $vsettings = MDV::Distribconf::MediaCFG::_value_info($val); + if ($vsettings->{ismedialist} && $distrib->getvalue($media, $val)) { + my %ml = map { $_ => 1 } split(/\s/, $distrib->getvalue($media, $val)); + exists($ml{$cmedia}) or next; + delete($ml{$cmedia}); + $distrib->setvalue( + $media, + $val, + join(" ", keys %ml) + ); + } + } + } + } + 1; } =item $distrib->write_hdlists($hdlists) diff --git a/t/05build.t b/t/05build.t new file mode 100644 index 0000000..1def21d --- /dev/null +++ b/t/05build.t @@ -0,0 +1,52 @@ +#!/usr/bin/perl + +# $Id: 01-compile.t 38877 2006-07-12 12:16:51Z nanardon $ + +use strict; +use warnings; + +use Test::More tests => 28; +use File::Temp qw(tempdir); + +use_ok('MDV::Distribconf::Build'); + +my $dir = tempdir(); + +sub END { + if (-d $dir) { + system(qw(rm -fr --), $dir); + } +} + +my $mdb = MDV::Distribconf::Build->new($dir); +isa_ok($mdb, 'MDV::Distribconf::Build'); +$mdb->settree(); +ok($mdb->init(), "Can create a distrib"); +ok(-d $mdb->getfullpath(undef, 'infodir'), 'info directory has been created'); +ok($mdb->create_media('media_a'), 'can create a media'); +ok($mdb->mediaexists('media_a'), 'media_a exists'); +ok(-d $mdb->getfullpath('media_a', 'path'), 'media directory has been created'); +ok(-d $mdb->getfullpath('media_a', 'infodir'), 'media info directory has been created'); +ok($mdb->create_media('media_b'), 'can create a second media'); +ok($mdb->mediaexists('media_b'), 'media_b exists'); + +ok($mdb->setvalue('media_a', 'srpms', 'media_b'), 'set a new value'); +is($mdb->getvalue('media_a', 'srpms'), 'media_b', 'set value works'); +is($mdb->getvalue('media_b', 'rpms'), 'media_a', 'set peer value works'); +ok($mdb->setvalue('media_a', 'srpms', ''), 'set a new value'); +is($mdb->getvalue('media_a', 'srpms'), '', 'reset value works'); +is($mdb->getvalue('media_b', 'rpms'), '', 'reset peer value works'); + +ok($mdb->setvalue('media_a', 'srpms', 'media_c'), 'set a new value'); +ok(!$mdb->mediaexists('media_c'), 'peer media does not exists'); +ok($mdb->setvalue('media_c'), 'lazy media creation'); +is($mdb->getvalue('media_c', 'rpms'), 'media_a', 'set peer value works'); +ok($mdb->create_media('media_c'), 'can create a third media'); +ok(-d $mdb->getfullpath('media_c', 'path'), 'media directory has been created'); +ok(-d $mdb->getfullpath('media_c', 'infodir'), 'media info directory has been created'); + +ok($mdb->delvalue('media_c', 'rpms'), 'deleting a value works'); +is($mdb->getvalue('media_a', 'srpms'), '', 'delete peer value works'); +ok($mdb->setvalue('media_a', 'srpms', 'media_c'), 'set a new value'); +ok($mdb->delvalue('media_c'), 'deleting a media works'); +is($mdb->getvalue('media_a', 'srpms'), '', 'delete peer value works'); |