diff options
author | Pascal Rigaux <pixel@mandriva.com> | 2006-11-13 17:07:07 +0000 |
---|---|---|
committer | Pascal Rigaux <pixel@mandriva.com> | 2006-11-13 17:07:07 +0000 |
commit | 30cab8dbf055736db8adcf5c2106ace053b8c8b8 (patch) | |
tree | 852b2d8808d9b0ffae5394b8fde34b609212efb8 | |
parent | 12ba4482a5cae621dbcd031ce960978fd1a25c9f (diff) | |
download | urpmi-30cab8dbf055736db8adcf5c2106ace053b8c8b8.tar urpmi-30cab8dbf055736db8adcf5c2106ace053b8c8b8.tar.gz urpmi-30cab8dbf055736db8adcf5c2106ace053b8c8b8.tar.bz2 urpmi-30cab8dbf055736db8adcf5c2106ace053b8c8b8.tar.xz urpmi-30cab8dbf055736db8adcf5c2106ace053b8c8b8.zip |
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)
-rw-r--r-- | urpm.pm | 61 |
1 files changed, 31 insertions, 30 deletions
@@ -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 { |