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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
|
#!/usr/bin/perl -U
load_po();
$ENV{PATH} = "/sbin:/usr/sbin:/bin:/usr/bin:/usr/X11R6/bin";
$< = $>;
log_it(scalar localtime, " urpmi called with @ARGV\n");
if (@ARGV and $ARGV[0] eq '--auto') {
$auto = 1;
shift @ARGV;
}
if (@ARGV and $ARGV[0] eq '--X') {
$X = 1;
shift @ARGV;
}
while (@ARGV and $ARGV[0] eq '--comment') {
shift @ARGV;
shift @ARGV;
}
@ARGV or die "usage: urpmi [--auto] package_name [package_names...]\n";
$dir = "/var/lib/urpmi";
$dir2 = "/etc/urpmi";
$depsfile = "$dir/depslist";
$rpmlistfiles = "$dir/list.*";
open SAVEOUT, ">&STDOUT";
open SAVEERR, ">&STDERR";
open STDOUT, "|tee -a /var/log/urpmi.log" or die;
open STDERR, "|tee -a /var/log/urpmi.log" or die;
select STDERR; $| = 1; # make unbuffered
select STDOUT; $| = 1; # make unbuffered
open F, $depsfile or die "missing dependencies file ($depsfile)";
foreach (<F>) {
my ($pack, $size, $deps) = /(\S+)\s+(\S+)\s+(.*)/ or die("urpmi: bad format file $depsfile\n");
$size{$pack} = $size;
$deps{$pack} = [ split ' ', $deps ];
foreach $v (@ARGV) {
$_ = quotemeta $v;
$pack =~ /^$_-[^-]+-[^-]+$/ and $exact{$v} = $pack;
$pack =~ /^$_-[^-]+$/ and $exact{$v} = $pack;
$pack =~ /^$_$/ and $exact{$v} = $pack;
$pack =~ /$_/ and push @{$found{$v}}, $pack;
$pack =~ /$_/i and push @{$foundi{$v}}, $pack;
}
}
close F or die();
my $ok = 1;
foreach (@ARGV) {
my $pack = $exact{$_};
unless ($pack) {
my $l = $found{$_}; $l or $l = $foundi{$_};
if (@$l == 0) { warn(_("no package named %s\n", $_)); $ok = 0; }
elsif (@$l > 1) { warn(_("The following packages contain %s: %s\n", $_, join(' ', @$l))); $ok = 0; }
$pack = $$l[0];
}
push @packages, $pack;
}
$ok or exit 1;
@installed = `/bin/rpm -qa`; $? and die(_("rpm database query failed\n"));
foreach (@installed) {
my ($name, $v, $r) = /(.*)-([^-]+)-([^-.]+)/;
exists $installed{$name} && le_version([$v, $r], $installed{$name}) or $installed{$name} = [$v, $r];
}
@installed{map { chop; $_ } @installed} = ();
my %to_install; closure_deps(\%to_install, @packages); @to_install = keys %to_install;
if (!$auto and difference2(\@to_install, \@packages)) {
my $sum = 0; map { $sum += $size{$_} } @to_install;
my $msg = _("To satisfy dependencies, the following packages are going to be installed (%d MB)", toMb($sum));
my $msg2 = _("Is it ok?");
if ($X) {
my $p = join "\n", @to_install;
my $ok = _("Ok");
my $cancel = _("Cancel");
`gmessage -default $ok -buttons "$ok:0,$cancel:2" "$msg:\n$p\n\n$msg2"`;
$? and exit 0;
} else {
print SAVEOUT "$msg:\n@to_install\n$msg2 (y/n) ";
<STDIN> =~ /y/i or exit 0;
}
}
$to_install = join '|', map { quotemeta($_) } @to_install;
foreach $file (glob($rpmlistfiles)) {
open F, $file or die("urpmi: error opening $file\n");
$file =~ s|.*/list\.(.*?)|$1|;
foreach (<F>) { chop; m|/($to_install)| and $long{$1} = $_, $name{$1} = $file; }
close F;
}
foreach $l (@to_install) {
local $_ = $long{$l} or die("urpmi: package $l is not available\n");
if (s|removable_(\w+)_(\d*):/||) {
my $n = "$2-$name{$l}";
$removables{$n}->{name} = $name{$l};
$removables{$n}->{device} = $1;
push @{$removables{$n}->{list}}, $_;
} else {
if (s|^file:/||) {
if (m|^(/mnt/.*?)/|) {
-e $_ || $try2mount{$1} or $try2mount{$1} = 1, `mount $1 2>/dev/null`;
}
}
push @to_install_long, $_;
}
}
install(@to_install_long) unless $removables{0} || $removables{1};
foreach (sort keys %removables) {
my $f = @{$removables{$_}->{list}}[0];
my $dev = $removables{$_}->{device};
`mount /dev/$dev 2>/dev/null`;
unless (-e $f) {
`umount /dev/$dev 2>/dev/null ; eject $dev 2>/dev/null`;
message(_("Please insert the %s named %s", $removables{$_}->{device}, $removables{$_}->{name}));
unless ($X) {
print SAVEOUT _("Press enter when it's done...");
<STDIN>;
}
`mount /dev/$dev 2>/dev/null`;
unless (-e $f) {
message(_("Sorry can't find file %s, exiting", $f));
exit 1;
}
}
install(@{$removables{$_}->{list}});
}
install(@to_install_long) if $removables{0} || $removables{1};
$something_was_installed or message(_("everything already installed"));
sub install {
@_ or return;
$something_was_installed = 1;
print SAVEOUT _("installing %s\n", join(' ', @_));
log_it(scalar localtime, " @_\n");
$X ? `grpmi @_` : `rpm -U @_`;
$? and message(_("Installation failed")), exit 1;
}
# uses @deps and @installed
sub closure_deps {
my $to_install = shift;
foreach (@_) {
exists $to_install->{$_} and next;
my ($name, $v, $r) = /(.*)-([^-]+)-([^-.]+)/;
exists $installed{$name} && le_version([$v, $r], $installed{$name}) and next;
$to_install->{$_} = undef;
my @d = @{$deps{$_}} or return $_;
up: foreach (@d) {
my %l;
foreach (split '\|') {
exists $to_install->{$_} and next up;
my ($name, $v, $r) = /(.*)-([^-]+)-([^-.]+)/;
if (exists $installed{$name}) {
le_version([$v, $r], $installed{$name}) and next up;
%l = ($name => [$v, $r]);
last;
}
$l{$name} = [$v, $r] unless $l{$name} && le_version([$v, $r], $l{$name});
}
my ($l) = my @l = map { "$_-$l{$_}[0]-$l{$_}[1]" } keys %l;
if (@l > 1 && !$auto) {
$msg = _("One of the following packages is needed:");
my $n;
if ($X) {
`gchooser "$msg" @l`;
$n = $? >> 8 || die;
} else {
print SAVEOUT "$msg\n";
my $i = 0; foreach (@l) { print SAVEOUT " ", ++$i, "- $_\n"; }
while (1) {
print SAVEOUT _("What is your choice? (1-%d) ", $i);
$n = <STDIN>;
1 <= $n && $n <= $i and last;
print SAVEOUT _("Sorry, bad choice, try again\n");
}
}
$l = $l[$n - 1];
}
closure_deps($to_install, $l);
}
}
}
#- compare a version string.
sub version_compare {
my ($a, $b) = @_;
local $_;
while ($a && $b) {
my ($sb, $sa) = map { $1 if $a =~ /^\W*\d/ ? s/^\W*0*(\d+)// : s/^\W*(\D+)// } ($b, $a);
$_ = length($sa) cmp length($sb) || $sa cmp $sb and return $_;
}
}
sub le_version {
my $cmp = version_compare($_[0][0], $_[1][0]);
return $cmp < 0 || $cmp == 0 && version_compare($_[0][1], $_[1][1]) < 0;
}
sub toMb {
my $nb = $_[0] / 1024 / 1024;
int $nb + 0.5;
}
sub message { $X ? `gmessage -default Ok -buttons Ok "$_[0]"` : print SAVEOUT "$_[0]\n"; }
sub member($@) { my $e = shift; foreach (@_) { $e eq $_ and return 1 } 0 }
sub uniq { my %l; @l{@_} = (); keys %l }
sub difference2(\@\@) { my %l; @l{@{$_[1]}} = (); grep { !exists $l{$_} } @{$_[0]} }
sub _ { my $s = shift @_; sprintf translate($s), @_ }
sub translate { $I18N{$_[0]} || $_[0]; }
sub log_it {
local *LOG;
open LOG, ">>/var/log/urpmi.log" or die;
print LOG @_;
}
sub untaint {
my @r = ();
foreach (@_) {
/(.*)/;
push @r, $1;
}
@r == 1 ? $r[0] : @r
}
sub load_po {
my ($from, $to, $state, $fuzzy);
my $lang = $ENV{LANGUAGE} || $ENV{LC_ALL} || $ENV{LC_MESSAGES} || $ENV{LANG};
my $f; -e ($f = "/usr/share/locale/$_/LC_MESSAGES/urpmi.po") and last foreach split ':', $lang;
local *F; open F, $f or return;
foreach (<F>) {
/^msgstr/ and $state = 1;
/^msgid/ && !$fuzzy and $state = 2;
if (/^(#|$)/ && $state != 3) {
$state = 3;
s/\\n/\n/g foreach $from, $to;
$I18N{$from} = $to if $from;
$from = $to = '';
}
$to .= (/"(.*)"/)[0] if $state == 1;
$from .= (/"(.*)"/)[0] if $state == 2;
$fuzzy = /^#, fuzzy/;
}
}
|