From 0c8f651988b155d12deb3aea26c2f206648c61e5 Mon Sep 17 00:00:00 2001 From: Rafael Garcia-Suarez Date: Wed, 14 Apr 2004 13:47:02 +0000 Subject: Factorize the message/prompt/I18N code of urpm* in a separate module --- rpm-find-leaves | 1 + urpm.pm | 16 +------- urpm/args.pm | 14 ++++--- urpm/msg.pm | 109 +++++++++++++++++++++++++++++++++++++++++++++++++++++ urpme | 30 +-------------- urpmf | 1 + urpmi | 110 ++++++++++-------------------------------------------- urpmi.addmedia | 1 + urpmi.removemedia | 2 +- urpmi.update | 1 + urpmq | 1 + 11 files changed, 148 insertions(+), 138 deletions(-) create mode 100644 urpm/msg.pm diff --git a/rpm-find-leaves b/rpm-find-leaves index 46741906..894003b9 100644 --- a/rpm-find-leaves +++ b/rpm-find-leaves @@ -1,5 +1,6 @@ #!/usr/bin/perl use urpm; +use urpm::msg; sub main { diff --git a/urpm.pm b/urpm.pm index 35f6a1e7..33ac145f 100644 --- a/urpm.pm +++ b/urpm.pm @@ -5,27 +5,15 @@ package urpm; use strict; use vars qw($VERSION @ISA @EXPORT); use MDK::Common; +use urpm::msg; $VERSION = '4.4'; -@ISA = qw(Exporter URPM); -@EXPORT = qw(N); +@ISA = qw(URPM); use URPM; use URPM::Resolve; use POSIX; -#- I18N. -eval { - require Locale::gettext; - setlocale(LC_ALL, ""); - Locale::gettext::textdomain("urpmi"); -}; - -sub N { - my ($format, @params) = @_; - sprintf(eval { Locale::gettext::gettext($format || '') } || $format, @params); -} - #- tool functions. sub localtime2changelog { scalar(localtime($_[0])) =~ /(.*) \S+ (\d{4})$/ && "$1 $2" }; diff --git a/urpm/args.pm b/urpm/args.pm index 15d246fb..6dc6fb45 100644 --- a/urpm/args.pm +++ b/urpm/args.pm @@ -18,6 +18,9 @@ Getopt::Long::Configure(@configuration); # global urpm object to be passed by the main program my $urpm; +# stores the values of the command-line options +our %options; + # options specifications for Getopt::Long my %options_spec = ( @@ -35,7 +38,9 @@ my %options_spec = ( 'excludemedia|exclude-media=s' => \$::excludemedia, 'sortmedia|sort-media=s' => \$::sortmedia, 'synthesis=s' => \$::synthesis, - auto => \$urpm->{options}{auto}, + auto => sub { + $urpm->{options}{auto} = $::auto = 1; + }, 'allow-medium-change' => \$::allow_medium_change, 'auto-select' => \$::auto_select, 'no-remove|no-uninstall' => \$::no_remove, @@ -75,12 +80,12 @@ my %options_spec = ( $value =~ /(.+):(.+)/ or die N("bad proxy declaration on command line\n"); @{$urpm->{proxy}}{qw(user pwd)} = ($1, $2); }, - 'bug=s' => \$::bug, + 'bug=s' => \$options{bug}, 'env=s' => \$::env, - X => \$::X, + X => \$options{X}, WID => \$::WID, 'best-output' => sub { - $::X ||= $ENV{DISPLAY} && system('/usr/X11R6/bin/xtest', '') == 0 + $options{X} ||= $ENV{DISPLAY} && system('/usr/X11R6/bin/xtest', '') == 0 }, 'verify-rpm!' => \$urpm->{options}{'verify-rpm'}, 'test!' => \$::test, @@ -238,7 +243,6 @@ my %options_spec = ( ); # common options setup -# TODO <> for arguments foreach my $k ("help|h", "no-locales", "test!", "force", "root=s", "use-distrib=s", "parallel=s") diff --git a/urpm/msg.pm b/urpm/msg.pm new file mode 100644 index 00000000..bf3272e2 --- /dev/null +++ b/urpm/msg.pm @@ -0,0 +1,109 @@ +package urpm::msg; + +use strict; +use Exporter; +our @ISA = 'Exporter'; +our @EXPORT = qw(N log_it to_utf8 message_input gmessage message); + +my $noexpr = N("Nn"); +my $yesexpr = N("Yy"); + +#- I18N. +eval { + require Locale::gettext; + use POSIX qw(LC_ALL); + setlocale(LC_ALL, ""); + Locale::gettext::textdomain("urpmi"); +}; + +sub N { + my ($format, @params) = @_; + sprintf(eval { Locale::gettext::gettext($format || '') } || $format, @params); +} + +sub log_it { + #- if invoked as a simple user, nothing should be logged. + if (our $log) { + open my $fh, ">>$log" or die "can't output to log file: $!\n"; + print $fh @_; + close $fh; + } +} + +sub to_utf8 { Locale::gettext::iconv($_[0], undef, "UTF-8") } + +sub gmessage { + my ($msg, %params) = @_; + my $ok = to_utf8($params{ok} || N("Ok")); + my $cancel = to_utf8($params{cancel} || N("Cancel")); + $ok =~ s/,/\\,/g; $cancel =~ s/,/\\,/g; + my $buttons = $params{ok_only} ? "$ok:0" : "$ok:0,$cancel:2"; + foreach (@{$params{add_buttons}}) { + s/,/\\,/g; + $buttons .= ",$_"; + } + $msg = to_utf8($msg); + `gmessage -default "$ok" -buttons "$buttons" "$msg"`; +} + +sub message_input { + my ($msg, $default_input, %opts) = @_; + my $input; + if ($urpm::args::options{X} && !$default_input) { + #- if a default input is given, the user doesn't have to choose (and being asked). + gmessage($msg, ok_only => 1); + $urpm::args::options{bug} and log_it($msg); + } else { + while (1) { + if ($urpm::args::options{bug}) { + print STDOUT $msg; + } else { + print SAVEOUT $msg; + } + if ($default_input) { + $urpm::args::options{bug} and log_it($default_input); + return $default_input; + } + $input = ; + defined $input or return undef; + $urpm::args::options{bug} and log_it($input); + if ($opts{boolean}) { + $input =~ /^[$noexpr$yesexpr]*$/ and last; + } elsif ($opts{range}) { + 1 <= $input && $input <= $opts{range} and last; + } else { + last; + } + message(N("Sorry, bad choice, try again\n")); + } + } + return $input; +} + +sub message { + my ($msg, $no_X) = @_; + if ($urpm::args::options{X} && !$no_X && !$::auto) { + gmessage($msg, ok_only => 1); + $urpm::args::options{bug} and log_it($msg); + } else { + if ($urpm::args::options{bug}) { + print STDOUT "$msg\n"; + } else { + print SAVEOUT "$msg\n"; + } + } +} + +1; + +__END__ + +=head1 NAME + +urpm::msg - routines to prompt messages from the urpm* tools + +=head1 SYNOPSIS + +=head1 DESCRIPTION + +=cut diff --git a/urpme b/urpme index 3e1a1070..1d5686d1 100644 --- a/urpme +++ b/urpme @@ -23,8 +23,9 @@ use strict; use urpm; use urpm::args; +use urpm::msg; -our ($root, $test, $parallel, $auto, $matches, $verbose, $maymatch, $usedistrib, $force, $bug, @l); +our ($root, $test, $parallel, $auto, $matches, $verbose, $maymatch, $usedistrib, $force, @l); my $askok = N("Is this OK?"); # Translator: Add here the keys which might be pressed in the "No"-case. my $noexpr = N("Nn"); @@ -112,30 +113,3 @@ sub toMb { my $nb = $_[0] / 1024 / 1024; int $nb + 0.5; } - -sub message_input { - my ($msg, $default_input, %options) = @_; - my $input; - my $noexpr = N("Nn"); - my $yesexpr = N("Yy"); - - while (1) { - print STDOUT $msg; - if ($default_input) { - $bug and log_it($input); - return $default_input; - } - $input = ; - defined $input or return undef; - $bug and log_it($input); - if ($options{boolean}) { - $input =~ /^[$noexpr$yesexpr]*$/ and last; - } elsif ($options{range}) { - 1 <= $input && $input <= $options{range} and last; - } else { - last; - } - print STDOUT N("Sorry, bad choice, try again\n")."\n"; - } - return $input; -} diff --git a/urpmf b/urpmf index 4bf687dc..71b5713b 100755 --- a/urpmf +++ b/urpmf @@ -21,6 +21,7 @@ use strict; use urpm; use urpm::args; +use urpm::msg; sub usage { print STDERR N("urpmf version %s diff --git a/urpmi b/urpmi index c7905b40..d6c77de8 100755 --- a/urpmi +++ b/urpmi @@ -21,6 +21,7 @@ use strict; use urpm; use urpm::args; +use urpm::msg; use MDK::Common; #- contains informations to parse installed system. @@ -43,9 +44,7 @@ my $split_level = 20; my $split_length = 1; our $force = 0; our $parallel = ''; -our $bug = ''; our $env = ''; -our $X = 0; our $WID = 0; our $test = 0; our $skip = ''; @@ -137,20 +136,6 @@ usage: exit(0); } -sub to_utf8 { Locale::gettext::iconv($_[0], undef, "UTF-8") } -sub gmessage { - my ($msg, %params) = @_; - my $ok = to_utf8($params{ok} || N("Ok")); - my $cancel = to_utf8($params{cancel} || N("Cancel")); - $ok =~ s/,/\\,/g; $cancel =~ s/,/\\,/g; - my $buttons = $params{ok_only} ? "$ok:0" : "$ok:0,$cancel:2"; - foreach (@{$params{add_buttons}}) { - $_ =~ s/,/\\,/g; - $buttons .= ",$_"; - } - $msg = to_utf8($msg); - `gmessage -default "$ok" -buttons "$buttons" "$msg"`; -} sub save_file { my ($orig) = @_; my $msg = N("Choose location to save file"); @@ -206,7 +191,7 @@ if ($install_src) { @names = (); } -if ($X && @src_files == 1 && @files == 0 && @names == 0 && @src_names == 0 && !$force) { +if ($urpm::args::options{X} && @src_files == 1 && @files == 0 && @names == 0 && @src_names == 0 && !$force) { gmessage( N("You have selected a source package: @@ -227,7 +212,7 @@ What would you like to do?", $src_files[0]), } } -if ($X && @files == 1 && @names == 0 && $verbose >= 0) { +if ($urpm::args::options{X} && @files == 1 && @names == 0 && $verbose >= 0) { gmessage( N("You are about to install the following software package on your computer: @@ -240,8 +225,11 @@ You may prefer to just save it. What is your choice?", $files[0]), } #- prepare bug report. +my $bug = $urpm::args::options{bug}; if ($bug) { - mkdir $bug or $urpm->{fatal}(8, (-d $bug ? N("Directory [%s] already exists, please use another directory for bug report or delete it") : N("Unable to create directory [%s] for bug report"), $bug)); + mkdir $bug or $urpm->{fatal}(8, (-d $bug + ? N("Directory [%s] already exists, please use another directory for bug report or delete it") + : N("Unable to create directory [%s] for bug report"), $bug)); #- copy all synthesis file used, along with configuration of urpmi system("cp", "-af", $urpm->{skiplist}, $urpm->{instlist}, $bug); #- allow log file. @@ -395,7 +383,7 @@ sub ask_choice { my $msg = (defined $from ? N("One of the following packages is needed to install %s:", $from) : N("One of the following packages is needed:")); - if ($X) { + if ($urpm::args::options{X}) { `gchooser "$msg" @l`; $n = $? >> 8 || die; } else { @@ -409,7 +397,8 @@ sub ask_choice { $choices->[$n - 1]; }; -if ($parallel && $X) { #- do it early, we'll have ui outputs while resolving deps +if ($parallel && $urpm::args::options{X}) { + #- do it early, we'll have ui outputs while resolving deps require gurpm; gurpm::init(to_utf8(N("Package installation...")), to_utf8(N("Initializing..."))); $urpm->{ui} = { msg => sub { gurpm::label(to_utf8($_[0])) }, progress => sub { gurpm::progress($_[0]) } }; @@ -436,7 +425,7 @@ if (@ask_unselect) { if ($urpm->{options}{auto}) { message($msg, 'noX'); } else { - if ($X) { + if ($urpm::args::options{X}) { gmessage("$msg\n".N("do you agree ?")); $? and exit 0; } else { @@ -462,7 +451,7 @@ have to be removed for others to be upgraded:\n%s\n", $list); if ($urpm->{options}{auto}) { message($msg, 'noX'); } else { - if ($X) { + if ($urpm::args::options{X}) { gmessage("$msg\n".N("do you agree ?")); $? and exit 0; } else { @@ -504,11 +493,11 @@ if ($env) { if (@root_only) { print STDERR N("You need to be root to install the following dependencies:\n%s\n", join ' ', @root_only); exit 1; -} elsif (!$urpm->{options}{auto} && ($ask_user || $X) && @to_install) { +} elsif (!$urpm->{options}{auto} && ($ask_user || $urpm::args::options{X}) && @to_install) { my $msg = N("To satisfy dependencies, the following packages are going to be installed (%d MB)", toMb($sum)); my $msg2 = N("Is this OK?"); my $p = join "\n", @to_install; - if ($X) { + if ($urpm::args::options{X}) { gmessage("$msg:\n$p\n\n$msg2"); $? and exit 0; } else { @@ -526,7 +515,7 @@ unless ($local_sources || $list) { $urpm->{fatal}(3, N("unable to get source packages, aborting")); } -if ($X && !$parallel) { #- for $parallel, already done +if ($urpm::args::options{X} && !$parallel) { #- for $parallel, already done require gurpm; gurpm::init(to_utf8(N("Package installation...")), to_utf8(N("Initializing..."))); } @@ -540,7 +529,7 @@ $urpm->copy_packages_of_removable_media($list, \%sources, ask_for_medium => (!$urpm->{options}{auto} || $allow_medium_change) && sub { my $msg = N("Please insert the medium named \"%s\" on device [%s]", @_); my $msg2 = N("Press Enter when ready..."); - if ($X) { + if ($urpm::args::options{X}) { $msg =~ s/"/\\"/g; gmessage($msg); !$?; @@ -577,7 +566,7 @@ foreach my $set (@{$state->{transaction} || []}) { force_local => 1, callback => sub { my ($mode, $file, $percent, $total, $eta, $speed) = @_; - if ($X) { + if ($urpm::args::options{X}) { if ($mode eq 'start') { $file =~ s|/*\s*$||; $file =~ s|.*/||; gurpm::label(to_utf8(N("Downloading package `%s'...", $file))); @@ -621,7 +610,7 @@ foreach my $set (@{$state->{transaction} || []}) { message("$msg:\n$p\n", 'noX'); exit 1; } else { - if ($X) { + if ($urpm::args::options{X}) { gmessage("$msg:\n$p\n\n$msg2"); $? and exit 1; } else { @@ -675,7 +664,7 @@ foreach my $set (@{$state->{transaction} || []}) { $urpm->{log}("starting installing packages"); my $progress_nb; my $total_nb = grep { m|^/| } values %transaction_sources_install, values %transaction_sources; - my $callback_inst = $X && sub { + my $callback_inst = $urpm::args::options{X} && sub { my ($urpm, $type, $id, $subtype, $amount, $total) = @_; my $pkg = defined $id && $urpm->{depslist}[$id]; @@ -752,7 +741,7 @@ foreach my $set (@{$state->{transaction} || []}) { } } } -$X and gurpm::end(); +$urpm::args::options{X} and gurpm::end(); #- keep a track of error code. my $exit_code = 0; @@ -814,65 +803,6 @@ sub toMb { int $nb + 0.5; } -sub log_it { - #- if invoked as a simple user, nothing should be logged. - if ($log) { - local *LOG; - open LOG, ">>$log" or die "can't output to log file\n"; - print LOG @_; - } -} - -#- message functions. -sub message { - my ($msg, $noX) = @_; - if ($X && !$noX && !$urpm->{options}{auto}) { - gmessage($msg, ok_only => 1); - $bug and log_it($msg); - } else { - if ($bug) { - print STDOUT "$msg\n"; - } else { - print SAVEOUT "$msg\n"; - } - } -} -sub message_input { - my ($msg, $default_input, %options) = @_; - my $input; - - if ($X && !$default_input) { #- if a default input is given, the user doesn't have to choose (and being asked). - gmessage($msg, ok_only => 1); - $bug and log_it($msg); - } else { - my $noexpr = N("Nn"); - my $yesexpr = N("Yy"); - while (1) { - if ($bug) { - print STDOUT $msg; - } else { - print SAVEOUT $msg; - } - if ($default_input) { - $bug and log_it($input); - return $default_input; - } - $input = ; - defined $input or return undef; - $bug and log_it($input); - if ($options{boolean}) { - $input =~ /^[$noexpr$yesexpr]*$/ and last; - } elsif ($options{range}) { - 1 <= $input && $input <= $options{range} and last; - } else { - last; - } - message(N("Sorry, bad choice, try again\n")); - } - } - return $input; -} - sub untaint { my @r; foreach (@_) { diff --git a/urpmi.addmedia b/urpmi.addmedia index 793316b0..803dca8c 100755 --- a/urpmi.addmedia +++ b/urpmi.addmedia @@ -23,6 +23,7 @@ use strict; use urpm; use urpm::args; +use urpm::msg; # Default mirror list our $mirrors = 'http://www.linux-mandrake.com/mirrorsfull.list'; diff --git a/urpmi.removemedia b/urpmi.removemedia index 38b6b8fe..f556e158 100755 --- a/urpmi.removemedia +++ b/urpmi.removemedia @@ -20,9 +20,9 @@ #- this program is based upon old urpmi.addmedia -#use strict qw(subs vars refs); use strict; use urpm; +use urpm::msg; sub main { my (@toremoves, %options); diff --git a/urpmi.update b/urpmi.update index a4dcbd7c..2a591329 100755 --- a/urpmi.update +++ b/urpmi.update @@ -21,6 +21,7 @@ use strict; use urpm; use urpm::args; +use urpm::msg; sub usage { warn N("usage: urpmi.update [options] ... diff --git a/urpmq b/urpmq index e5d62ff3..b22bf447 100755 --- a/urpmq +++ b/urpmq @@ -23,6 +23,7 @@ use strict; use urpm; use urpm::args; +use urpm::msg; use MDK::Common; #- default options. -- cgit v1.2.1