summaryrefslogtreecommitdiffstats
path: root/urpme
blob: 091c806f9719c60127ccb3ec475b3425525695e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/perl

# Copyright (C) 1999 MandrakeSoft (pixel@linux-mandrake.com)
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

local $_ = join '', @ARGV;

/-h/ || @ARGV == 0 and die "usage: rpme [-a] [--auto] <packages...>\n";

$matches = /-a/;
$auto = /-auto/;

my $DIR = "/var/lib/urpmi";

@l = grep { !/^-/ } @ARGV;
if (!$matches) {
    @m = map { chop; $_ } `rpm -q @l 2>&1`;
    if ($?) {
	$maymatch = "unknown package(s) " . join(", ", map { /package (\S+) is not installed/ ? $1 : () } @m) . "\n";
	$auto || @l > 1 and die $maymatch;
    }
}
if ($matches || $maymatch) {
    my $match = join "|", @l;
    @m = grep { /$match/ } map { chop; $_ } `rpm -qa`;

    if ($maymatch) {
	@m or die $maymatch;
	print "Using $match as a substring, I found:\n@m\nRemove them all? (y/N) ";
	<STDIN> =~ /y/i or exit 1;
    }
}

load_provides();
my %toremove; @toremove{$_, @{$provides{$_} || []}} = () foreach @m;
my $changed = 1; while ($changed) { $changed = 0;
    local *F;
    open F, "rpm -e --test " . join(" ", keys %toremove) . " 2>&1 |";
    foreach (<F>) {
    	if (/package (\S+) is not installed/) {
    	    delete $toremove{$1};
    	} elsif (/is needed by (\S+)/ && ! exists $toremove{$1}) {
    	    $toremove{$1} = 1;
    	    $changed = 1;
    	}
    }
}
my @toremove = keys %toremove or die "nothing to remove\n";
if (@toremove > @l && !$auto) {
    my $sum = 0; map { $sum += $_ } `rpm -q --queryformat "%{SIZE}\n" @toremove`;
    print "To satisfy dependencies, the following packages are going to be removed";
    printf " (%d MB)", toMb($sum);
    print ":\n@toremove\nIs it ok? (Y/n) ";
    <STDIN> =~ /n/i and exit 0;
}
system("rpm", "-e", @toremove);


sub load_provides {
    local *F;
    open F, "$DIR/depslist" or return;
    foreach (<F>) {
	my ($p, $size, @l) = split;
	$size{$p} = $size;
	push @{$provides{$_}}, $p foreach @l;
    }
}
sub toMb {
    my $nb = $_[0] / 1024 / 1024;
    int $nb + 0.5;
}