From 9c47e5da4f1908643d698d7bfacbeb90c5dd10de Mon Sep 17 00:00:00 2001 From: Axel Beckert Date: Thu, 11 Jul 2013 00:39:05 +0200 Subject: [PATCH] Use File::Which instead of findBinary or `which` --- TODO.markdown | 3 --- bin/xen-create-image | 12 +++++------ bin/xt-guess-suite-and-mirror | 7 +++---- debian/changelog | 3 ++- lib/Xen/Tools/Common.pm | 39 ++++------------------------------- 5 files changed, 15 insertions(+), 49 deletions(-) diff --git a/TODO.markdown b/TODO.markdown index d1b8345..29876cd 100644 --- a/TODO.markdown +++ b/TODO.markdown @@ -174,9 +174,6 @@ Bugs to fix and features to add for 5.0 distributions the same. Currently Debian is a special case and Ubuntu half a special case. -* Replace findBinary() and backticked calls to `which` (e.g. in - xt-guess-suite-and-mirror) with File::Which) - * Remove from the (unused) Xen::Tools what's already in the used Xen::Tools::Common. diff --git a/bin/xen-create-image b/bin/xen-create-image index 5d77c59..c5787f1 100755 --- a/bin/xen-create-image +++ b/bin/xen-create-image @@ -1151,7 +1151,7 @@ E_O_ERROR foreach my $bin (@required) { - if ( !defined( findBinary($bin) ) ) + if ( !defined( which($bin) ) ) { logprint( "The script '$bin' was not found.\nAborting\n\n" ); $CONFIG{'FAIL'} = 1; @@ -2274,7 +2274,7 @@ sub checkBinariesPresent foreach my $file (@required) { - if ( !defined( findBinary($file) ) ) + if ( !defined( which($file) ) ) { logprint("The following binary is required to run this tool\n"); logprint("\t$file\n"); @@ -2290,7 +2290,7 @@ sub checkBinariesPresent { # loopback image - if ( !defined( findBinary("dd") ) ) + if ( !defined( which("dd") ) ) { logprint("The following binary is required to run this tool\n"); logprint("\tdd\n"); @@ -2311,7 +2311,7 @@ sub checkBinariesPresent foreach my $file (@evms) { - if ( !defined( findBinary($file) ) ) + if ( !defined( which($file) ) ) { logprint("The following binary is required to run this tool\n"); logprint("\t$file\n"); @@ -2331,7 +2331,7 @@ sub checkBinariesPresent foreach my $file (@lvm) { - if ( !defined( findBinary($file) ) ) + if ( !defined( which($file) ) ) { logprint("The following binary is required to run this tool\n"); logprint("\t$file\n"); @@ -3305,7 +3305,7 @@ sub createFilesystem # my ($binary, $args) = split(/ /, $command, 2); - if ( !defined( findBinary($binary) ) ) + if ( !defined( which($binary) ) ) { logprint( "The binary '$binary' required to create the filesystem $fs is missing\n" diff --git a/bin/xt-guess-suite-and-mirror b/bin/xt-guess-suite-and-mirror index ab7e34e..d167c0d 100755 --- a/bin/xt-guess-suite-and-mirror +++ b/bin/xt-guess-suite-and-mirror @@ -69,6 +69,7 @@ my @sources_list_files = ( '/etc/apt/sources.list', use File::Slurp; use Getopt::Long; use Pod::Usage; +use File::Which; use strict; @@ -148,10 +149,8 @@ all_sources_list_files: foreach my $sources_list_file (@sources_list_files) { die "Couldn't find a useful entry in the sources.list files of the Dom0. Tried:\n ". join("\n ", @sources_list_files)."\n" unless $found; -my $lsb_release = `which lsb_release`; -chomp($lsb_release); - -if (!$found and $lsb_release and -x $lsb_release) { +my $lsb_release = which('lsb_release'); +if (!$found and defined($lsb_release) and -x $lsb_release) { my $vendor = `$lsb_release -s -i`; if ($vendor eq 'Debian' or $vendor eq 'Ubuntu') { diff --git a/debian/changelog b/debian/changelog index 479e3c4..6edf960 100644 --- a/debian/changelog +++ b/debian/changelog @@ -27,7 +27,8 @@ xen-tools (4.4~dev-1) UNRELEASED; urgency=low but not debootstrap (Thanks Elmar Heeb!) - Makefile accepts DESTDIR=… - Move examples from debian/examples to examples. - - Uses File::Which, added (build-) dependency on libfile-which-perl + - Uses File::Which exclusively (no more findBinary, no more `which`), + added (build-) dependency on libfile-which-perl - Knows about module-init-tools → kmod transition in wheezy/raring. - Adds default mount options for ext4, identical to ext2/ext3. - Ships /etc/initramfs-tools/conf.d/xen-tools for generating Dom0 diff --git a/lib/Xen/Tools/Common.pm b/lib/Xen/Tools/Common.pm index cc282f6..391d0c8 100644 --- a/lib/Xen/Tools/Common.pm +++ b/lib/Xen/Tools/Common.pm @@ -20,9 +20,10 @@ use Exporter 'import'; use vars qw(@EXPORT_OK @EXPORT); use English; +use File::Which; @EXPORT = qw(readConfigurationFile xenRunning runCommand setupAdminUsers - findXenToolstack findBinary + findXenToolstack logprint_with_config logonly_with_config fail_with_config); =head1 FUNCTIONS @@ -139,38 +140,6 @@ sub xenRunning ($$) return ($running); } -=head2 findBinary - -=begin doc - - Find the location of the specified binary on the curent user's PATH. - - Return undef if the named binary isn't found. - -=end doc - -=cut - -sub findBinary -{ - my ($bin) = (@_); - - # strip any path which might be present. - $bin = $2 if ( $bin =~ /(.*)[\/\\](.*)/ ); - - foreach my $entry ( split( /:/, $ENV{ 'PATH' } ) ) - { - - # guess of location. - my $guess = $entry . "/" . $bin; - - # return it if it exists and is executable - return $guess if ( -e $guess && -x $guess ); - } - - return undef; -} - =head2 findXenToolstack =begin doc @@ -192,12 +161,12 @@ sub findXenToolstack return $toolstack if $toolstack; } - my $xm = findBinary('xm'); + my $xm = which('xm'); if ($xm and system("$xm list >/dev/null 2>/dev/null") == 0) { return $xm; } - my $xl = findBinary('xl'); + my $xl = which('xl'); if ($xl and system("$xl list >/dev/null 2>/dev/null") == 0) { return $xl; }