| 1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
=head1 NAME |
| 4 |
|
| 5 |
Debconf::AutoSelect - automatic FrontEnd selection library. |
| 6 |
|
| 7 |
=cut |
| 8 |
|
| 9 |
package Debconf::AutoSelect; |
| 10 |
use warnings; |
| 11 |
use strict; |
| 12 |
use Debconf::Gettext; |
| 13 |
use Debconf::ConfModule; |
| 14 |
use Debconf::Config; |
| 15 |
use Debconf::Log qw(:all); |
| 16 |
use base qw(Exporter); |
| 17 |
our @EXPORT_OK = qw(make_frontend make_confmodule); |
| 18 |
our %EXPORT_TAGS = (all => [@EXPORT_OK]); |
| 19 |
|
| 20 |
# Perl's distinction between the compilation phase and the execution phase |
| 21 |
# is extremely unhelpful. Glib::Object::Introspection is a dependency of |
| 22 |
# Gtk3 and has an INIT block, so trying to require it from anywhere inside a |
| 23 |
# string eval results in a "Too late to run INIT block" diagnostic. From |
| 24 |
# our point of view, there seems to be no workaround for this other than |
| 25 |
# requiring it unconditionally here and ignoring any errors. |
| 26 |
BEGIN { |
| 27 |
eval { |
| 28 |
require Glib::Object::Introspection; |
| 29 |
}; |
| 30 |
} |
| 31 |
|
| 32 |
=head1 DESCRIPTION |
| 33 |
|
| 34 |
This library makes it easy to create FrontEnd and ConfModule objects. It |
| 35 |
starts with the desired type of object, and tries to make it. If that fails, |
| 36 |
it progressively falls back to other types in the list. |
| 37 |
|
| 38 |
=cut |
| 39 |
|
| 40 |
my %fallback=( |
| 41 |
# preferred frontend # fall back to |
| 42 |
'Kde' => ['Qt', 'Dialog', 'Readline', 'Teletype'], |
| 43 |
'Qt' => ['Dialog', 'Readline', 'Teletype'], |
| 44 |
'Gnome' => ['Dialog', 'Readline', 'Teletype'], |
| 45 |
'Web' => ['Dialog', 'Readline', 'Teletype'], |
| 46 |
'Dialog' => ['Readline', 'Teletype'], |
| 47 |
'Gtk' => ['Dialog', 'Readline', 'Teletype'], |
| 48 |
'Readline' => ['Teletype', 'Dialog'], |
| 49 |
'Editor' => ['Readline', 'Teletype'], |
| 50 |
# Here to make upgrades clean for those who used to use the slang |
| 51 |
# frontend. |
| 52 |
'Slang' => ['Dialog', 'Readline', 'Teletype'], |
| 53 |
# And the Text frontend has become the Readline frontend. |
| 54 |
'Text' => ['Readline', 'Teletype', 'Dialog'], |
| 55 |
|
| 56 |
); |
| 57 |
|
| 58 |
my $frontend; |
| 59 |
my $type; |
| 60 |
|
| 61 |
=head1 METHODS |
| 62 |
|
| 63 |
=over 4 |
| 64 |
|
| 65 |
=item make_frontend |
| 66 |
|
| 67 |
Creates and returns a FrontEnd object. The type of FrontEnd used varies. It |
| 68 |
will try the preferred type first, and if that fails, fall back through |
| 69 |
other types, all the way to a Noninteractive frontend if all else fails. |
| 70 |
|
| 71 |
=cut |
| 72 |
|
| 73 |
sub make_frontend { |
| 74 |
my $script=shift; |
| 75 |
my $starttype; |
| 76 |
$starttype=ucfirst($type) if defined $type; |
| 77 |
if (! defined $starttype || ! length $starttype) { |
| 78 |
$starttype = Debconf::Config->frontend; |
| 79 |
if ($starttype =~ /^[A-Z]/) { |
| 80 |
warn "Please do not capitalize the first letter of the debconf frontend."; |
| 81 |
} |
| 82 |
$starttype=ucfirst($starttype); |
| 83 |
} |
| 84 |
|
| 85 |
my $showfallback=0; |
| 86 |
foreach my $trytype ($starttype, @{$fallback{$starttype}}, 'Noninteractive') { |
| 87 |
if (! $showfallback) { |
| 88 |
debug user => "trying frontend $trytype"; |
| 89 |
} |
| 90 |
else { |
| 91 |
warn(sprintf(gettext("falling back to frontend: %s"), $trytype)); |
| 92 |
} |
| 93 |
## no critic (BuiltinFunctions::ProhibitStringyEval) |
| 94 |
$frontend=eval qq{ |
| 95 |
use Debconf::FrontEnd::$trytype; |
| 96 |
Debconf::FrontEnd::$trytype->new(); |
| 97 |
}; |
| 98 |
## use critic |
| 99 |
if (defined $frontend) { |
| 100 |
$type = $trytype; |
| 101 |
return $frontend; |
| 102 |
} |
| 103 |
|
| 104 |
warn sprintf(gettext("unable to initialize frontend: %s"), $trytype); |
| 105 |
$@=~s/\n.*//s; |
| 106 |
warn "($@)"; |
| 107 |
$showfallback=1; |
| 108 |
} |
| 109 |
|
| 110 |
die sprintf(gettext("Unable to start a frontend: %s"), $@); |
| 111 |
} |
| 112 |
|
| 113 |
=item make_confmodule |
| 114 |
|
| 115 |
Pass the script (if any) the ConfModule will start up, (and optional |
| 116 |
arguments to pass to it) and this creates and returns a ConfModule. |
| 117 |
|
| 118 |
=cut |
| 119 |
|
| 120 |
sub make_confmodule { |
| 121 |
my $confmodule=Debconf::ConfModule->new(frontend => $frontend); |
| 122 |
|
| 123 |
$confmodule->startup(@_) if @_; |
| 124 |
|
| 125 |
return $confmodule; |
| 126 |
} |
| 127 |
|
| 128 |
=back |
| 129 |
|
| 130 |
=head1 AUTHOR |
| 131 |
|
| 132 |
Joey Hess <[email protected]> |
| 133 |
|
| 134 |
=cut |
| 135 |
|
| 136 |
1 |