IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 17963


Ignore:
Timestamp:
Jun 6, 2008, 1:46:57 PM (18 years ago)
Author:
bills
Message:

Added 2 methods. create_temp_file and redirect_ouput
create_temp_file is used to manage creating tempfiles for filelists, etc
based on the basename() of the given workdir path. File goes to current
directory if $save_temps flag is passed along as second argument.

redirect_output redirects stderr and stdout to the given path, after
resolving the file and creating it's directory (if needed)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/PS-IPP-Config/lib/PS/IPP/Config.pm

    r17829 r17963  
    11# Copyright (c) 2006  Paul Price, Joshua Hoblitt
    22#
    3 # $Id: Config.pm,v 1.87 2008-05-28 20:28:46 jhoblitt Exp $
     3# $Id: Config.pm,v 1.88 2008-06-06 23:46:57 bills Exp $
    44
    55package PS::IPP::Config;
     
    1111
    1212use Carp qw( carp );
     13use File::Basename qw( basename dirname );
    1314# use File::Spec 3.19;
    1415use File::Spec 0.87;
     16use File::Temp qw( tempfile );
     17
    1518use PS::IPP::Metadata::Config 1.00;
    1619use Getopt::Long 2.33 qw( GetOptions :config gnu_getopt pass_through ); # Set pass_through so we don't kill @ARGV
     
    510513                                          exit($PS_EXIT_DATA_ERROR));
    511514    return 1;
     515}
     516
     517# create a (possibly) temporary file with name (basename $_[0])
     518# if $preserve create a file named basename$_[0] in current directory
     519
     520sub create_temp_file
     521{
     522    my $self     = shift;
     523    my $pathname = shift;
     524    my $preserve = shift;
     525
     526    die "pathname must be defined" unless ($pathname);
     527
     528    my $fileRef;
     529    my $fileName;
     530
     531    # we only use in the last part of the path
     532    my $base = basename($pathname);
     533
     534    if ($preserve) {
     535        # we want to keep the file just create it in the current directory
     536        $fileName = "./$base";
     537        open $fileRef, ">$fileName" or die "can't open $fileName for output";
     538    } else {
     539        #  we really want a tempfile, so put it in /tmp
     540        ($fileRef, $fileName) = tempfile("/tmp/$base.XXXX", UNLINK => 1);
     541    }
     542
     543    return ($fileRef, $fileName);
     544}
     545
     546# redirect stderr and stdout streams to a file
     547sub redirect_output
     548{
     549    my $self = shift;
     550    my $name = shift;
     551
     552    die "need name" unless $name;
     553
     554    my $filename = $self->file_resolve($name, "--touch");
     555
     556    die "cannot resolve $name" unless $filename;
     557
     558    # We need to make sure the directory for this file exists
     559    # If it's a nebulous file it has already been created so the directory must exist
     560    # we won't run mkdir there
     561    my $dir = dirname($filename);
     562    if (! -d $dir) {
     563        my $rc = system "mkdir -p $dir";
     564        die "failed to create directory for $filename" unless (!$rc);
     565    }
     566
     567    open STDOUT, ">>$filename" or die "failed to redirect stdout to $filename";
     568    open STDERR, ">>$filename" or die "failed to redirect stderr to $filename";
    512569}
    513570
Note: See TracChangeset for help on using the changeset viewer.