urpm/msg.pm

filter: Statementstmt 83.3% 10 / 12 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 87.5% 14 / 16 CC 1 SCAR 0.7
CC
1
Cov
83%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« urpm/cfg.pm ↑ Summary urpm/sys.pm »
1 package urpm::msg;
2  
3  
4 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 40 ▶no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 40 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 0 ▶use URPM;
Subroutine BEGIN: called CC=1 SCAR=2.6
8 use urpm::util 'append_to_file';
9  
10 =head1 NAME
11  
12 urpm::msg - routines to prompt messages from the urpm* tools
13  
14 =head1 SYNOPSIS
15  
16 =head1 DESCRIPTION
17  
18 =head1 METHODS
19  
20 =over
21  
22 =cut
23  
24 my $encoding;
25 BEGIN {
26 eval { require encoding; $encoding = encoding::_get_locale_encoding() };
27 eval "use open ':locale'" if $encoding && $encoding ne 'ANSI_X3.4-1968';
28 }
29  
30 our @ISA = 'Exporter';
31 our @EXPORT = qw(N N_ P translate bug_log message_input toMb formatXiB sys_log);
32  
33 #- I18N.
34 use Locale::gettext;
35 use POSIX ();
36 POSIX::setlocale(POSIX::LC_ALL(), "");
37 my @textdomains = qw(urpmi rpm-summary-main rpm-summary-contrib rpm-summary-devel);
38 foreach my $domain (@textdomains) {
39 Locale::gettext::bind_textdomain_codeset($domain, 'UTF-8');
40 }
41 URPM::bind_rpm_textdomain_codeset();
42  
43 our $no_translation;
44  
45 sub from_locale_encoding {
46 my ($s) = @_;
47 $encoding && eval {
48 require Encode;
49 Encode::decode($encoding, $s);
50 } || do {
51 require utf8;
52 utf8::decode($s);
53 $s;
54 } || $s;
55 }
56  
57  
58 =item translate($s, $o_plural, $o_nb)
59  
60 Actually translate a string using gettext.
61  
62 =cut
63  
64 sub translate {
65 my ($s, $o_plural, $o_nb) = @_;
66 my $res;
67 if ($no_translation) {
68 $s;
69 } elsif ($o_nb) {
70 foreach my $domain (@textdomains) {
71 eval { $res = Locale::gettext::dngettext($domain, $s || '', $o_plural, $o_nb) || $s };
72 return $res if $s ne $res;
73 }
74 return $s;
75 } else {
76 foreach my $domain (@textdomains) {
77 eval { $res = Locale::gettext::dgettext($domain, $s || '') || $s };
78 return $res if $s ne $res;
79 }
80 return $s;
81 }
82 }
83  
84 =item P($s_singular, $s_plural, $nb, @para)
85  
86 Like N(), translate a string but with different singular/plural strings according to $nb
87  
88 =cut
89  
90 sub P {
91 my ($s_singular, $s_plural, $nb, @para) = @_;
92 sprintf(translate($s_singular, $s_plural, $nb), @para);
93 }
94  
95 =item N($format, @params)
96  
97 Translate the $format string (a la printf using @params)
98  
99 =cut
100  
101 sub N {
102 my ($format, @params) = @_;
103 sprintf(translate($format), @params);
104 }
105  
106 =item N_($string)
107  
108 Unlike N(), it doesn't actually translate $string but serves to tag a string as translatable for extracting into pod catalog.
109  
110 The string will actually be translated later using translate().
111  
112 =cut
113  
114 sub N_ { $_[0] }
115  
116 my $noexpr = N("Nn");
117 my $yesexpr = N("Yy");
118  
119 eval {
120 require Sys::Syslog;
121 Sys::Syslog->import;
122 (my $tool = $0) =~ s!.*/!!;
123  
124 #- what we really want is "unix" (?)
125 #- we really don't want "console" which forks/exit and thus
126 # run callbacks registered through atexit() : x11, gtk+, rpm, ...
127 Sys::Syslog::setlogsock([ 'tcp', 'unix', 'stream' ]);
128  
129 openlog($tool, '', 'user');
130 END { defined &closelog and closelog() }
131 };
132  
133 =item sys_log(...)
134  
135 Log through syslog if Sys::Syslog is installed.
136  
137 =cut
138  
139 sub sys_log { defined &syslog and eval { syslog("info", @_) } }
140  
141 =item bug_log(...)
142  
143 Writes only to logfile, not to screen
144  
145 =cut
146  
147 sub bug_log {
148 append_to_file($::logfile, @_) if $::logfile;
149 }
150  
151 sub ask_yes_or_no {
152 my ($msg) = @_;
153 message_input($msg . N(" (y/N) "), boolean => 1) =~ /[$yesexpr]/;
154 }
155  
156 sub message_input {
157 my ($msg, %o_opts) = @_;
158 _message_input($msg, undef, %o_opts);
159 }
160 sub _message_input {
161 my ($msg, $o_default_input, %o_opts) = @_;
162 my $input;
163 while (1) {
164 print $msg;
165 if ($o_default_input) {
166 #- deprecated argument. don't you want to use $o_opts{default} instead?
167 $urpm::args::options{bug} and bug_log($o_default_input);
168 return $o_default_input;
169 }
170 $input = <STDIN>;
171 defined $input or return undef;
172 chomp $input;
173 $urpm::args::options{bug} and bug_log($input);
174 if ($o_opts{boolean}) {
175 $input =~ /^[$noexpr$yesexpr]?$/ and last;
176 } elsif ($o_opts{range}) {
177 $input eq "" and $input = $o_opts{default} || 1; #- defaults to first choice
178 (defined $o_opts{range_min} ? $o_opts{range_min} : 1) <= $input && $input <= $o_opts{range} and last;
179 } else {
180 last;
181 }
182 print N("Sorry, bad choice, try again\n");
183 }
184 return $input;
185 }
186  
187  
188 =item toMb($nb)
189  
190 Convert a number of bytes into Mb
191  
192 =cut
193  
194 sub toMb {
195 my $nb = $_[0] / 1024 / 1024;
196 int $nb + 0.5;
197 }
198  
199 my @format_line_field_sizes = (30, 12, 13, 7, 0);
200 my $format_line_format = ' ' . join(' ', map { '%-' . $_ . 's' } @format_line_field_sizes);
201  
202 sub format_line_selected_packages {
203 my ($urpm, $state, $pkgs) = @_;
204  
205 my (@pkgs, @lines, $prev_medium);
206 my $flush = sub {
207 push @lines, _format_line_selected_packages($state, $prev_medium, \@pkgs);
208 @pkgs = ();
209 };
210 foreach my $pkg (@$pkgs) {
211 my $medium = URPM::pkg2media($urpm->{media}, $pkg);
212 if ($prev_medium && $prev_medium ne $medium) {
213 $flush->();
214 }
215 push @pkgs, $pkg;
216 $prev_medium = $medium;
217 }
218 $flush->();
219  
220 (sprintf($format_line_format, N("Package"), N("Version"), N("Release"), N("Arch")),
221 @lines);
222 }
223 sub _format_line_selected_packages {
224 my ($state, $medium, $pkgs) = @_;
225  
226 my @l = map {
227 my @name_and_evr = $_->fullname;
228 if ($state->{selected}{$_->id}{recommended}) {
229 push @name_and_evr, N("(recommended)");
230 }
231 \@name_and_evr;
232 } sort { $a->name cmp $b->name } @$pkgs;
233  
234 my $i;
235 foreach my $max (@format_line_field_sizes) {
236 foreach (@l) {
237 if ($max && length($_->[$i]) > $max) {
238 $_->[$i] = substr($_->[$i], 0, $max-1) . '>';
239 }
240 }
241 $i++;
242 }
243  
244 ('(' . ($medium ? N("medium \"%s\"", $medium->{name}) : N("command line")) . ')',
245 map { sprintf($format_line_format, @$_) } @l);
246 }
247  
248  
249 =item formatXiB($number [, $o_newbase])
250  
251 Returns a nicely human size. eg:
252  
253 2097152 => "2MB"
254  
255 The optional parameter enables to provide the unit size (default is one).
256 eg for a 2000 512 sized sectors:
257  
258 formatXiB(2000, 512)
259  
260 Duplicated from L<common> (drakx/perl-install/common.pm)
261  
262 =cut
263  
264 sub formatXiB {
265 my ($newnb, $o_newbase) = @_;
266 my $newbase = $o_newbase || 1;
267 my ($nb, $base);
268 my $decr = sub {
269 ($nb, $base) = ($newnb, $newbase);
270 $base >= 1024 ? ($newbase = $base / 1024) : ($newnb = $nb / 1024);
271 };
272 my $suffix;
273 foreach (N("B"), N("KB"), N("MB"), N("GB"), N("TB")) {
274 $decr->();
275 if ($newnb < 1 && $newnb * $newbase < 1) {
276 $suffix = $_;
277 last;
278 }
279 }
280 my $v = $nb * $base;
281 my $s = $v < 10 && int(10 * $v - 10 * int($v));
282 int($v) . ($s ? ".$s" : '') . ($suffix || N("TB"));
283 }
284  
285 sub localtime2changelog { scalar(localtime($_[0])) =~ /(.*) \S+ (\d{4})$/ && "$1 $2" }
286  
287 1;
288  
289 =back
290  
291 =head1 COPYRIGHT
292  
293 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005 MandrakeSoft SA
294  
295 Copyright (C) 2005-2010 Mandriva SA
296  
297 Copyright (C) 2011-2020 Mageia
298  
299 =cut
« urpm/cfg.pm ↑ Summary urpm/sys.pm »