diff options
author | Oliver Tappe | 2007-07-01 22:28:50 +0200 |
---|---|---|
committer | Oliver Tappe | 2007-07-01 22:28:50 +0200 |
commit | 6974fa8b0419bbd0711f79c8b78e07a9543810dd (patch) | |
tree | 25141f0f4d20ca8fdb1c845edf5b9ce4b24a6e98 /lib/OpenSLX/Utils.pm | |
parent | Tried to add Ubuntu 7.04 to the list of cloneable systems. (diff) | |
download | core-6974fa8b0419bbd0711f79c8b78e07a9543810dd.tar.gz core-6974fa8b0419bbd0711f79c8b78e07a9543810dd.tar.xz core-6974fa8b0419bbd0711f79c8b78e07a9543810dd.zip |
* activated 'use warnings' to all modules and adjusted all occurences of
'use of uninitialized values', a couple of which might still show up
* adjusted all code with respect to passing perlcritic level 4 and 5
git-svn-id: http://svn.openslx.org/svn/openslx/trunk@1207 95ad53e4-c205-0410-b2fa-d234c58c8868
Diffstat (limited to 'lib/OpenSLX/Utils.pm')
-rw-r--r-- | lib/OpenSLX/Utils.pm | 116 |
1 files changed, 87 insertions, 29 deletions
diff --git a/lib/OpenSLX/Utils.pm b/lib/OpenSLX/Utils.pm index 6dbd0e7c..4d11e702 100644 --- a/lib/OpenSLX/Utils.pm +++ b/lib/OpenSLX/Utils.pm @@ -18,10 +18,10 @@ use vars qw(@ISA @EXPORT $VERSION); use Exporter; $VERSION = 1.01; -@ISA = qw(Exporter); +@ISA = qw(Exporter); @EXPORT = qw( - ©File &fakeFile &linkFile &slurpFile &followLink + copyFile fakeFile linkFile slurpFile spitFile followLink unshiftHereDoc ); ################################################################################ @@ -34,73 +34,131 @@ use OpenSLX::Basics; sub copyFile { - my $fileName = shift; - my $targetDir = shift; + my $fileName = shift || croak 'need to pass in a fileName!'; + my $targetDir = shift || croak 'need to pass in target dir!'; my $targetFileName = shift || ''; - system("mkdir -p $targetDir") unless -d $targetDir; + system("mkdir -p $targetDir") unless -d $targetDir; my $target = "$targetDir/$targetFileName"; vlog(2, _tr("copying '%s' to '%s'", $fileName, $target)); if (system("cp -p $fileName $target")) { - die _tr("unable to copy file '%s' to dir '%s' (%s)", - $fileName, $target, $!); + croak( + _tr( + "unable to copy file '%s' to dir '%s' (%s)", + $fileName, $target, $! + ) + ); } + return; } sub fakeFile { - my $fullPath = shift; + my $fullPath = shift || croak 'need to pass in full path!'; my $targetDir = dirname($fullPath); - system("mkdir", "-p", $targetDir) unless -d $targetDir; + system("mkdir", "-p", $targetDir) unless -d $targetDir; if (system("touch", $fullPath)) { - die _tr("unable to create file '%s' (%s)", - $fullPath, $!); + croak(_tr("unable to create file '%s' (%s)", $fullPath, $!)); } + return; } sub linkFile { - my $linkTarget = shift; - my $linkName = shift; + my $linkTarget = shift || croak 'need to pass in link target!'; + my $linkName = shift || croak 'need to pass in link name!'; my $targetDir = dirname($linkName); - system("mkdir -p $targetDir") unless -d $targetDir; + system("mkdir -p $targetDir") unless -d $targetDir; if (system("ln -sfn $linkTarget $linkName")) { - die _tr("unable to create link '%s' to '%s' (%s)", - $linkName, $linkTarget, $!); + croak( + _tr( + "unable to create link '%s' to '%s' (%s)", + $linkName, $linkTarget, $! + ) + ); } + return; +} + +sub checkFlags +{ + 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; } sub slurpFile { - my $file = shift; - my $mayNotExist = shift; + my $fileName = shift || confess 'need to pass in fileName!'; + my $flags = shift || {}; + + checkFlags($flags, ['failIfMissing']); + my $failIfMissing + = exists $flags->{failIfMissing} ? $flags->{failIfMissing} : 1; - if (!open(F, "< $file") && !$mayNotExist) { - die _tr("could not open file '%s' for reading! (%s)", $file, $!); + local $/; + my $fh; + if (!open($fh, '<', $fileName)) { + return '' unless $failIfMissing; + croak _tr("could not open file '%s' for reading! (%s)", $fileName, $!); } - local $/ = undef; - my $text = <F>; - close(F); - return $text; + my $content = <$fh>; + close($fh) + or croak _tr("unable to close file '%s' (%s)\n", $fileName, $!); + return $content; +} + +sub spitFile +{ + my $fileName = shift || croak 'need to pass in a fileName!'; + my $content = shift; + + my $fh; + open($fh, '>', $fileName) + or croak _tr("unable to create file '%s' (%s)\n", $fileName, $!); + print $fh $content + or croak _tr("unable to print to file '%s' (%s)\n", $fileName, $!); + close($fh) + or croak _tr("unable to close file '%s' (%s)\n", $fileName, $!); + return; } sub followLink { - my $path = shift; + my $path = shift || croak 'need to pass in a path!'; my $prefixedPath = shift || ''; - + my $target; while (-l "$path") { $target = readlink "$path"; if (substr($target, 1, 1) eq '/') { $path = "$prefixedPath/$target"; - } else { - $path = $prefixedPath.dirname($path).'/'.$target; + } + else { + $path = $prefixedPath . dirname($path) . '/' . $target; } } return $path; } -1;
\ No newline at end of file +sub unshiftHereDoc +{ + my $content = shift; + return $content unless $content =~ m{^(\s+)}; + my $shift = length($1); + return + join "\n", + map { substr($_, $shift); } + split m{\n}, $content; +} + +1; |