From cedf72c7559e96d07fefdf55dc007aeb6bca1ca0 Mon Sep 17 00:00:00 2001 From: Oliver Tappe Date: Wed, 25 Jul 2007 19:30:02 +0000 Subject: * reworked OpenSLX::OSSetup::Distro structure to incorporate a distro specific hierarchy level in addition to the distro & version specific files. The latter will be converted to config-files soon. * moved string2Array() and chrootInto() from OpenSLX::OSSetup::Engine to OpenSLX::Utils to make them available to other callers * started work on ubuntu & debian installation (not done yet) * cleaned interface of OpenSLX::OSSetup::Engine (separated public from private methods) git-svn-id: http://svn.openslx.org/svn/openslx/trunk@1275 95ad53e4-c205-0410-b2fa-d234c58c8868 --- lib/OpenSLX/Basics.pm | 127 +++++++++++++++++++++++++++++++++++++++++++------- lib/OpenSLX/Utils.pm | 73 ++++++++++++++++++++++++++++- 2 files changed, 182 insertions(+), 18 deletions(-) (limited to 'lib') diff --git a/lib/OpenSLX/Basics.pm b/lib/OpenSLX/Basics.pm index 45a37986..e1ca36fb 100644 --- a/lib/OpenSLX/Basics.pm +++ b/lib/OpenSLX/Basics.pm @@ -28,7 +28,7 @@ $VERSION = 1.01; &warn &die &croak &carp &confess &cluck &callInSubprocess &executeInSubprocess &slxsystem &vlog - &checkFlags + &checkParams &instantiateClass &addCleanupFunction &removeCleanupFunction ); @@ -469,19 +469,111 @@ sub _doThrowOrWarn return; } -# ------------------------------------------------------------------------------ -sub checkFlags +=item checkParams() + +Utility function that can be used by any method that accepts param-hashes +to check if the given parameters actually match the expectations. + +Each individual parameter has a specification that describes the expectation +that the calling function has towards this param. The following specifications +are supported: + +* '!' - the parameter is required +* '?' - the parameter is optional +* 'm{regex}' - the parameter must match the given regex +* '!class=...' - the parameter is required and must be an object of the given class +* '?class=...' - if the parameter has been given, it must be an object of the given class + +The function will confess for any unknown, missing, or non-matching param. + +If accepted as useful, this function could be moved to a utility module of +the framework in order to be available to all other OTRS-modules. + +=cut + +sub checkParams { - my $flags = shift || confess 'need to pass in flags-hashref!'; - my $knownFlags = shift || confess 'need to pass in knownFlags-arrayref!'; - - my %known; - @known{@$knownFlags} = (); - foreach my $flag (keys %$flags) { - next if exists $known{$flag}; - cluck("flag '$flag' not known!"); - } - return; + my $params = shift or confess('need to pass in params-hashref!'); + my $paramsSpec = shift or confess('need to pass in params-spec-hashref!'); + + # print a warning for any unknown parameters that have been given: + my @unknownParams + = grep { !exists $paramsSpec->{$_}; } + keys %$params; + if (@unknownParams) { + my $unknownParamsStr = join ',', @unknownParams; + confess("Enocuntered unknown params: '$unknownParamsStr'!\n"); + } + + # check if all required params have been specified: + foreach my $param (keys %$paramsSpec) { + my $spec = $paramsSpec->{$param}; + if (ref($spec) eq 'HASH') { + # Handle nested specs by recursion: + my $subParams = $params->{$param}; + if (!defined $subParams) { + confess("Required param '$param' is missing!"); + } + checkParams($subParams, $spec); + } + elsif (ref($spec) eq 'ARRAY') { + # Handle nested spec arrays by looped recursion: + my $subParams = $params->{$param}; + if (!defined $subParams) { + confess("Required param '$param' is missing!"); + } + elsif (ref($subParams) ne 'ARRAY') { + confess("Value for param '$param' must be an array-ref!"); + } + foreach my $subParam (@$subParams) { + checkParams($subParam, $spec->[0]); + } + } + elsif ($spec eq '!') { + # required parameter: + if (!exists $params->{$param}) { + confess("Required param '$param' is missing!"); + } + } + elsif ($spec =~ m{^\!class=(.+)$}i) { + my $class = $1; + # required parameter ... + if (!exists $params->{$param}) { + confess("Required param '$param' is missing!"); + } + # ... of specific class + if (!$params->{$param}->isa($class)) { + confess("Param '$param' is not a '$class', but that is required!"); + } + } + elsif ($spec eq '?') { + # optional parameter - nothing to do + } + elsif ($spec =~ m{^\?class=(.+)$}i) { + my $class = $1; + # optional parameter ... + if (exists $params->{$param}) { + # ... has been given, so it must match specific class + if (!$params->{$param}->isa($class)) { + confess("Param '$param' is not a '$class', but that is required!"); + } + } + } + elsif ($spec =~ m{^m{(.+)}$}) { + # try to match given regex: + my $regex = $1; + my $value = $params->{$param}; + if ($value !~ m{$regex}) { + confess("Required param '$param' isn't matching regex '$regex' (given value was '$value')!"); + } + } + else { + # complain about unknown spec: + confess("Unknown param-spec '$spec' encountered!"); + } + } + + return scalar 1; } # ------------------------------------------------------------------------------ @@ -490,7 +582,10 @@ sub instantiateClass my $class = shift; my $flags = shift || {}; - checkFlags($flags, ['pathToClass', 'version']); + checkParams($flags, { + 'pathToClass' => '?', + 'version' => '?' + }); my $pathToClass = $flags->{pathToClass}; my $requestedVersion = $flags->{version}; @@ -499,10 +594,10 @@ sub instantiateClass $moduleName .= '.pm'; unless (eval { require $moduleName } ) { if ($! == 2) { - die _tr("Module <%s> not found!\n", $moduleName); + die _tr("Module '%s' not found!\n", $moduleName); } else { - die _tr("Unable to load module <%s> (%s)\n", $moduleName, $@); + die _tr("Unable to load module '%s' (%s)\n", $moduleName, $@); } } if (defined $requestedVersion) { diff --git a/lib/OpenSLX/Utils.pm b/lib/OpenSLX/Utils.pm index d23eb6e8..fdb1192c 100644 --- a/lib/OpenSLX/Utils.pm +++ b/lib/OpenSLX/Utils.pm @@ -23,7 +23,13 @@ $VERSION = 1.01; @ISA = qw(Exporter); @EXPORT = qw( - copyFile fakeFile linkFile slurpFile spitFile followLink unshiftHereDoc + copyFile fakeFile linkFile + copyBinaryWithRequiredLibs + slurpFile spitFile + followLink + unshiftHereDoc + string2Array + chrootInto ); ################################################################################ @@ -88,7 +94,7 @@ sub slurpFile my $fileName = shift || confess 'need to pass in fileName!'; my $flags = shift || {}; - checkFlags($flags, ['failIfMissing']); + checkParams($flags, { 'failIfMissing' => '?' }); my $failIfMissing = exists $flags->{failIfMissing} ? $flags->{failIfMissing} : 1; @@ -137,6 +143,39 @@ sub followLink return $path; } +sub copyBinaryWithRequiredLibs { + my $params = shift; + + checkParams($params, { + 'binary' => '!', # file to copy + 'targetFolder' => '!', # where file shall be copied to + 'libTargetFolder' => '!', # base target folder for libs + 'targetName' => '?', # name of binary in target folder + }); + copyFile($params->{binary}, $params->{targetFolder}, $params->{targetName}); + + # determine all required libraries and copy those, too: + vlog(1, _tr("calling slxldd for $params->{binary}")); + my $slxlddCmd = "slxldd $params->{binary}"; + vlog(2, "executing: $slxlddCmd"); + my $requiredLibsStr = qx{$slxlddCmd}; + if ($?) { + die _tr( + "slxldd couldn't determine the libs required by '%s'! (%s)", + $params->{binary}, $? + ); + } + chomp $requiredLibsStr; + vlog(2, "slxldd results:\n$requiredLibsStr"); + + foreach my $lib (split "\n", $requiredLibsStr) { + vlog(3, "copying lib '$lib'"); + my $libDir = dirname($lib); + copyFile($lib, "$params->{libTargetFolder}$libDir"); + } + return $requiredLibsStr; +} + sub unshiftHereDoc { my $content = shift; @@ -148,4 +187,34 @@ sub unshiftHereDoc split m{\n}, $content; } +sub string2Array +{ + my $string = shift || ''; + + my @lines = split m[\n], $string; + for my $line (@lines) { + # remove leading and trailing whitespace: + $line =~ s{^\s*(.*?)\s*$}{$1}; + } + + # drop empty lines and comments: + return grep { length($_) > 0 && $_ !~ m[^\s*#]; } @lines; +} + +sub chrootInto +{ + my $osDir = shift; + + vlog(2, "chrooting into $osDir..."); + chdir $osDir + or die _tr("unable to chdir into '%s' (%s)\n", $osDir, $!); + + # ...do chroot + chroot "." + or die _tr("unable to chroot into '%s' (%s)\n", $osDir, $!); + + $ENV{PATH} = "/bin:/sbin:/usr/bin:/usr/sbin"; + return; +} + 1; -- cgit v1.2.3-55-g7522