| 1 |
# Copyright (C) 2010 Modestas Vainius <[email protected]> |
| 2 |
# |
| 3 |
# This program is free software: you can redistribute it and/or modify |
| 4 |
# it under the terms of the GNU General Public License as published by |
| 5 |
# the Free Software Foundation, either version 3 of the License, or |
| 6 |
# (at your option) any later version. |
| 7 |
# |
| 8 |
# This program is distributed in the hope that it will be useful, |
| 9 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 |
# GNU General Public License for more details. |
| 12 |
# |
| 13 |
# You should have received a copy of the GNU General Public License |
| 14 |
# along with this program. If not, see <http://www.gnu.org/licenses/> |
| 15 |
|
| 16 |
package Debian::PkgKde; |
| 17 |
|
| 18 |
use File::Spec; |
| 19 |
use Cwd qw(realpath); |
| 20 |
|
| 21 |
use base qw(Exporter); |
| 22 |
our @EXPORT = qw(get_program_name |
| 23 |
printmsg info warning errormsg error syserr usageerr); |
| 24 |
our @EXPORT_OK = qw(find_exe_in_path); |
| 25 |
|
| 26 |
sub find_exe_in_path { |
| 27 |
my ($exe, @exclude) = @_; |
| 28 |
my @realexclude; |
| 29 |
|
| 30 |
# Canonicalize files to exclude |
| 31 |
foreach my $exc (@exclude) { |
| 32 |
if (my $realexc = realpath($exc)) { |
| 33 |
push @realexclude, $realexc; |
| 34 |
} |
| 35 |
} |
| 36 |
if (File::Spec->file_name_is_absolute($exe)) { |
| 37 |
return $exe; |
| 38 |
} elsif ($ENV{PATH}) { |
| 39 |
foreach my $dir (split /:/, $ENV{PATH}) { |
| 40 |
my $path = realpath(File::Spec->catfile($dir, $exe)); |
| 41 |
if (-x $path && ! grep({ $path eq $_ } @realexclude)) { |
| 42 |
return $path; |
| 43 |
} |
| 44 |
} |
| 45 |
} |
| 46 |
return undef; |
| 47 |
} |
| 48 |
|
| 49 |
{ |
| 50 |
my $progname; |
| 51 |
sub get_program_name { |
| 52 |
unless (defined $progname) { |
| 53 |
$progname = ($0 =~ m,/([^/]+)$,) ? $1 : $0; |
| 54 |
} |
| 55 |
return $progname; |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
sub format_message { |
| 60 |
my $type = shift; |
| 61 |
my $format = shift; |
| 62 |
|
| 63 |
my $msg = sprintf($format, @_); |
| 64 |
return ((defined $type) ? |
| 65 |
get_program_name() . ": $type: " : "") . "$msg\n"; |
| 66 |
} |
| 67 |
|
| 68 |
sub printmsg { |
| 69 |
print STDERR format_message(undef, @_); |
| 70 |
} |
| 71 |
|
| 72 |
sub info { |
| 73 |
print STDERR format_message("info", @_); |
| 74 |
} |
| 75 |
|
| 76 |
sub warning { |
| 77 |
warn format_message("warning", @_); |
| 78 |
} |
| 79 |
|
| 80 |
sub syserr { |
| 81 |
my $msg = shift; |
| 82 |
die format_message("error", "$msg: $!", @_); |
| 83 |
} |
| 84 |
|
| 85 |
sub errormsg { |
| 86 |
print STDERR format_message("error", @_); |
| 87 |
} |
| 88 |
|
| 89 |
sub error { |
| 90 |
die format_message("error", @_); |
| 91 |
} |
| 92 |
|
| 93 |
sub usageerr { |
| 94 |
die format_message("usage", @_); |
| 95 |
} |
| 96 |
|
| 97 |
1; |