summaryrefslogtreecommitdiffstats
path: root/urpme
blob: 3b7a916b04c1021d6c623c0a949e4c7705c76629 (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/usr/bin/perl

# Copyright (C) 1999,2002 MandrakeSoft <pixel@linux-mandrake.com>
#                                      <fpons@mandrakesoft.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.

#We only make good software ;-)
#use strict;

#use strict qw(subs vars refs);
use URPM;
use URPM::Resolve;
use urpm;

#- get I18N translation method.
import urpm _;

my ($auto, $matches, $maymatch, @l, @m, @toremove, %base);
my $askok = _("Is this OK?");
my $askrm = _("Remove them all?");
# Translator: Add here the keys which might be pressed in the "No"-case.
my $noexpr = _("Nn");
# Translator: Add here the keys which might be pressed in the "Yes"-case.
my $yesexpr = _("Yy");

local $_ = ' ' . join(' ', @ARGV) . ' ';

if ( / --?h/ || @ARGV == 0 ) {
    print STDERR _("usage: urpme [-a] [--auto] <packages...>\n");
    exit(0);
}

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

$urpm = new urpm;
$state = {};

#- open database to examine packages...
{
    my $db = URPM::DB::open('/', 0); #- open in read/write mode directly (so open it only once).

    @l = grep { !/^-/ } @ARGV;
    if (!$matches) {
	foreach (@l) {
	    my ($n, $found);

	    #- check if name-version-release may have been given.
	    if (($n) = /^(.*)-[^\-]*-[^\-]*\.[^\.\-]*$/) {
		$db->traverse_tag('name', [ $n ], sub {
				      my ($p) = @_;
				      $p->fullname eq $_ or return;
				      $urpm->resolve_closure_ask_remove($db, $state, $p);
				      push @m, join('-', ($p->fullname)[0..2]);
				      $found = 1;
				  });
		$found and next;
	    }

	    #- check if name-version-release may have been given.
	    if (($n) = /^(.*)-[^\-]*-[^\-]*$/) {
		$db->traverse_tag('name', [ $n ], sub {
				      my ($p) = @_;
				      join('-', ($p->fullname)[0..2]) eq $_ or return;
				      $urpm->resolve_closure_ask_remove($db, $state, $p);
				      push @m, join('-', ($p->fullname)[0..2]);
				      $found = 1;
				  });
		$found and next;
	    }

	    #- check if name-version may have been given.
	    if (($n) = /^(.*)-[^\-]*$/) {
		$db->traverse_tag('name', [ $n ], sub {
				      my ($p) = @_;
				      join('-', ($p->fullname)[0..1]) eq $_ or return;
				      $urpm->resolve_closure_ask_remove($db, $state, $p);
				      push @m, join('-', ($p->fullname)[0..2]);
				      $found = 1;
				  });
		$found and next;
	    }

	    #- check if only name may have been given.
	    $db->traverse_tag('name', [ $_ ], sub {
				  my ($p) = @_;
				  $p->name eq $_ or return;
				  $urpm->resolve_closure_ask_remove($db, $state, $p);
				  push @m, join('-', ($p->fullname)[0..2]);
				  $found = 1;
			      });
	    $found and next;

	    #- nothing has been found for the given name.
	    $maymatch .= ($maymatch && ", ") . $_;
	}
	if ($maymatch) {
	    my $msg = $maymatch =~ /, / ? _("unknown packages ") : _("unknown package ");
	    $msg =~ /\s$/ or $msg .= ' '; #- add trailing space to avoid fixing bad translator (too late now for 9.0).
	    $maymatch = "$msg$maymatch\n";
	}
	$maymatch && ($auto || @l > 1) and die $maymatch;
    }
    if ($matches || $maymatch) {
	my $match = join "|", map { quotemeta } @l;

	#- reset what has been already found.
	$state = {};
	@m = ();

	#- search for package that matches, and perform closure again.
	$db->traverse(sub {
			  my ($p) = @_;
			  $p->fullname =~ /$match/ or return;
			  $urpm->resolve_closure_ask_remove($db, $state, $p);
			  push @m, join('-', ($p->fullname)[0..2]);
		      });

	if ($maymatch) {
	    @m or die $maymatch;
	    my $msg = _("Using \"%s\" as a substring, I found", $match);
	    print STDOUT "$msg:\n@m\n$askrm" . _(" (y/N) ");
	    <STDIN> =~ /[$yesexpr]/ or exit 1;
	}
    }

    #- if nothing need to be removed.
    unless (%{$state->{ask_remove} || {}}) {
	print _("Nothing to remove.\n");
	exit(0);
    }

    my @base = qw(basesystem);
    my %basepackages;

    #- check if a package to be removed is a part of basesystem requires.
    while (defined($_ = shift @base)) {
	exists $basepackages{$_} and next;
	$db->traverse_tag(/^\// ? 'path' : 'whatprovides', [ $_ ], sub {
			      my ($p) = @_;
			      push @{$basepackages{$_} ||= []}, join '-', ($p->fullname)[0..2];
			      push @base, $p->requires_nosense;
			  });
    }

    foreach (values %basepackages) {
	my $n = @$_;
	foreach (@$_) {
	    $base{$_} = \$n;
	}
    }
}

my $base_str = '';
my @toremove = keys %{$state->{ask_remove}};
foreach (@toremove) {
    my $rn = $base{$_};
    if ($rn) {
	$$rn == 1 and $base_str .= _("removing package %s will break your system\n", $_);
	--$$rn;
    }
}
$base_str and die $base_str;

if (@toremove > @l && !$auto) {
    my $sum = 0;
    foreach (@toremove) {
	$sum += $state->{ask_remove}{$_}{size};
    }
    my $msg = _("To satisfy dependencies, the following packages are going to be removed (%d MB)", toMb($sum));
    print STDOUT "$msg:\n@toremove\n$askok" . _(" (Y/n) ");
    <STDIN> =~ /[$noexpr]/ and exit 0;
}

$urpm->install(\@toremove, {}, {});

sub toMb {
    my $nb = $_[0] / 1024 / 1024;
    int $nb + 0.5;
}