From 30cab8dbf055736db8adcf5c2106ace053b8c8b8 Mon Sep 17 00:00:00 2001 From: Pascal Rigaux Date: Mon, 13 Nov 2006 17:07:07 +0000 Subject: create select_media_by_name which return the list of matching media. the goal is to stop using $medium->{modified} to mean selected (eg: ->remove_selected_media used in urpmi.removemedia) --- urpm.pm | 61 +++++++++++++++++++++++++++++++------------------------------ 1 file changed, 31 insertions(+), 30 deletions(-) diff --git a/urpm.pm b/urpm.pm index ec29eaac..94c68a23 100644 --- a/urpm.pm +++ b/urpm.pm @@ -868,44 +868,45 @@ sub select_media { my $urpm = shift; my $options = {}; if (ref $_[0]) { $options = shift } - my %media; @media{@_} = (); - - foreach (@{$urpm->{media}}) { - if (exists($media{$_->{name}})) { - $media{$_->{name}} = 1; #- keep in mind this one has been selected. - #- select medium by setting the modified flag, do not check ignore. - $_->{modified} = 1; - } + foreach (select_media_by_name($urpm, [ @_ ], $options)) { + #- select medium by setting the modified flag, do not check ignore. + $_->{modified} = 1; } +} + +sub select_media_by_name { + my ($urpm, $names, $options) = @_; + + my %wanted = map { $_ => 1 } @$names; + + #- first the exact matches + my @l = grep { delete $wanted{$_->{name}} } @{$urpm->{media}}; #- check if some arguments don't correspond to the medium name. #- in such case, try to find the unique medium (or list candidate #- media found). - foreach (keys %media) { - unless ($media{$_}) { - my $q = quotemeta; - my (@found, @foundi); - my $regex = $options->{strict_match} ? qr/^$q$/ : qr/$q/; - my $regexi = $options->{strict_match} ? qr/^$q$/i : qr/$q/i; - foreach my $medium (@{$urpm->{media}}) { - $medium->{name} =~ $regex and push @found, $medium; - $medium->{name} =~ $regexi and push @foundi, $medium; - } - if (@found == 1) { - $found[0]{modified} = 1; - } elsif (@foundi == 1) { - $foundi[0]{modified} = 1; - } elsif (@found == 0 && @foundi == 0) { - $urpm->{error}(N("trying to select nonexistent medium \"%s\"", $_)); - } else { #- several elements in found and/or foundi lists. - $urpm->{log}(N("selecting multiple media: %s", join(", ", map { qq("$_->{name}") } (@found ? @found : @foundi)))); - #- changed behaviour to select all occurences by default. - foreach (@found ? @found : @foundi) { - $_->{modified} = 1; - } + foreach (keys %wanted) { + my $q = quotemeta; + my (@found, @foundi); + my $regex = $options->{strict_match} ? qr/^$q$/ : qr/$q/; + my $regexi = $options->{strict_match} ? qr/^$q$/i : qr/$q/i; + foreach my $medium (@{$urpm->{media}}) { + $medium->{name} =~ $regex and push @found, $medium; + $medium->{name} =~ $regexi and push @foundi, $medium; + } + @found = @foundi if !@found; + + if (@found == 0) { + $urpm->{error}(N("trying to select nonexistent medium \"%s\"", $_)); + } else { + if (@found > 1) { + $urpm->{log}(N("selecting multiple media: %s", join(", ", map { qq("$_->{name}") } @found))); } + #- changed behaviour to select all occurences by default. + push @l, @found; } } + @l; } sub remove_selected_media { -- cgit v1.2.1