# $Id: Foundation.pm 26421 2007-05-04 19:26:50Z moweis $ # Copyright @ 2002 - 2010 The Institute for Genomic Research (TIGR). # All rights reserved. # # This software is provided "AS IS". TIGR makes no warranties, express or # implied, including no representation or warranty with respect to the # performance of the software and derivatives or their safety, # effectiveness, or commercial viability. TIGR does not warrant the # merchantability or fitness of the software and derivatives for any # particular purpose, or that they may be exploited without infringing the # copyrights, patent rights or property rights of others. # # This software program may not be sold, leased, transferred, exported or # otherwise disclaimed to anyone, in whole or in part, without the prior # written consent of TIGR. package TIGR::Foundation; { =head1 NAME TIGR::Foundation - TIGR Foundation object =head1 SYNOPSIS use TIGR::Foundation; my $obj_instance = new TIGR::Foundation; =head1 DESCRIPTION This module defines a structure for Perl programs to utilize logging, version reporting, and dependency checking in a simple way. =cut BEGIN { require 5.006_00; # error if using Perl < v5.6.0 } use strict; use Cwd; use Cwd 'chdir'; use Cwd 'abs_path'; use File::Basename; use Getopt::Long; use IO::Handle; use POSIX qw(strftime); use Sys::Hostname; use IO::File; use Term::ReadKey; # For getPassword require Exporter; our @ISA; our @EXPORT; @ISA = ('Exporter'); @EXPORT = qw( isReadableFile isWritableFile isExecutableFile isCreatableFile isReadableDir isWritableDir isCreatableDir isCreatablePath getISODate getSybaseDate getMySQLDate getFilelabelDate getLogfileDate ); ## internal variables and identifiers our $REVISION = (qw$Revision: 26421 $)[-1]; our $VERSION = '1.45'; our $VERSION_STRING = "$VERSION (Build $REVISION)"; our @DEPEND = (); # there are no dependencies ## prototypes # Functional Class : general sub new(); sub getProgramInfo($); sub runCommand($); sub runCommandWithInput($$); # Functional Class : depend sub printDependInfo(); sub printDependInfoAndExit(); sub addDependInfo(@); # Functional Class : version sub getVersionInfo(); sub printVersionInfo(); sub printVersionInfoAndExit(); sub setVersionInfo($); sub setVersionHandler($); # Functional Class : help sub printHelpInfo(); sub printHelpInfoAndExit(); sub setHelpInfo($); # Functional Class : usage sub printUsageInfo(); sub printUsageInfoAndExit(); sub setUsageInfo($); # Functional Class : files sub isReadableFile($); sub isExecutableFile($); sub isWritableFile($); sub isCreatableFile($); sub isReadableDir($); sub isWritableDir($); sub isCreatableDir($); sub isCreatablePath($); # Functional Class : date sub getISODate(;@); sub getSybaseDate(;@); sub getMySQLDate(;@); sub getFilelabelDate(;@); sub getLogfileDate(;@); # Functional Class : logging sub setDebugLevel($;$); sub getDebugLevel(); sub setLogFile($;$); sub getLogFile(); sub getErrorFile(); sub printDependInfo(); sub invalidateLogFILES(); sub cleanLogFILES(); sub closeLogERROR(); sub closeLogMSG(); sub openLogERROR(); sub openLogMSG(); sub logAppend($;$); sub debugPush(); sub debugPop(); sub logLocal($$); sub logError($;$); sub bail($;$); # Functional Class : security sub getPassword($;$); sub getDBLoginParams($$); sub generatePasswordFile($$$$$); # Functional Class : modified methods sub TIGR_GetOptions(@); ## Implementation # We automatically try to clean up the invocation. This is done in a begin # block to allow this to happen as quickly as possible. In practice, this # will cause the process string to be cleaned as soon as the # 'use TIGR::Foundation' line is processed. BEGIN { # Define the subroutine so that it is available later if someone might # possibly want to run it a second time. sub cleanInvocation() { # # Get a clean version of the invocation # my $argstring = ""; my $skip_arg = 0; ARG: foreach my $arg (@ARGV) { # If we have requested to skip arguments, decrement the counter and # keep looping. if ($skip_arg) { $skip_arg--; } # Ignore the following options which take sensitive arguments elsif ( ($arg eq '-P') or # TIGR's standard password option ($arg eq '--password') # A generic and obvious password ) { # Take the next argument as well $skip_arg = 1; } # If the argument contains a space, quote it to allow it to be # easily copied and pasted. elsif ($arg =~ / /) { $argstring .= "'$arg' "; } # The argument is normal, append it. else { $argstring .= "$arg "; } } my $invocation = "$0 $argstring"; $0 = $invocation; } use vars qw( $SCRIPT ); # First harvest the script name for later $SCRIPT = basename($0); # Now clean the result # cleanInvocation(); } # Functional Class : general =over =item $obj_instance = new TIGR::Foundation; This function creates a new instance of the TIGR::Foundation object. A reference pointing to the object is returned on success. Otherwise, this method returns undefined. =cut sub new() { my $self = {}; my $pkg = shift; # create the object bless $self, $pkg; # Get the program name. my $pname = $SCRIPT; if ( (defined ($pname) ) && ($pname =~ /^(.*)$/) ) { $pname = $1; $self->{program_name} = $pname ; } if ($self->{program_name} =~ /^-$/) { # check if '-' is the input $self->{program_name} = "STDIN"; } # Get the invocation. my $pcommand = join (' ', @ARGV); if ( defined $pcommand ) { $pcommand =~ /^(.*)$/; $pcommand = $1; } else { $pcommand = ""; } $self->{invocation} = $pcommand ; # The following variables are to contain information specified by # the 'host' program; there are methods of setting and retrieving each. @{$self->{depend_info}} = (); $self->{version_handler} = undef; $self->{version_info} = undef; $self->{help_info} = undef; $self->{usage_info} = undef; # These are used for logging. $self->{debug_level} = -1; # debug is negative, no logging @{$self->{debug_store}} = (); # the backup debug level stack @{$self->{debug_queue}} = (); # queue used by MSG routine @{$self->{error_queue}} = (); # queue used by ERROR routine $self->{max_debug_queue_size} = 100; # maximum size for queue before # log entries are expired @{$self->{log_files}} = # these log files are consulted ("$self->{program_name}.log", # on file write error and are "/tmp/$self->{program_name}.$$.log"); # modified by setLogFile $self->{msg_file_open_flag} = 0; # flag to check logLocal file $self->{error_file_open_flag} = 0; # flag to check logError file $self->{msg_file_used} = 0; # flag to indicate if log file $self->{error_file_used} = 0; # has been written to $self->{msg_append_flag} = 0; # by default logs are truncated $self->{error_append_flag} = 0; # by default logs are truncated $self->{log_append_setting} = 0; # (truncate == 0) $self->{static_log_file} = undef; # user defined log file # These monitor program execution time. $self->{start_time} = time; # program start time $self->{finish_time} = undef; # program stop time # Set a user name and a host name. $self->{'host_name'} = hostname(); if ( ! defined ( $self->{'host_name'} ) ) { $self->{'host_name'} = "NOHOSTNAME"; } else { $self->{'host_name'} =~ s/^(\.*)$/$1/; # Taint-check it. } # A __WARN__ handler is needed to keep this sane. my $tmp_warn_handler = $SIG{__WARN__} || "DEFAULT"; $SIG{__WARN__} = sub {}; my @info_arr = getpwuid($<); $self->{'user_name'} = $info_arr[0]; $self->{'home_dir'} = $info_arr[7]; $SIG{__WARN__} = $tmp_warn_handler; if ( ! defined ( $self->{'user_name'} ) ) { $self->{'user_name'} = "NOUSERNAME"; } else { $self->{'user_name'} =~ s/^(\.*)$/$1/g;# Taint check. } if ( ! defined ( $self->{'home_dir'} ) ) { $self->{'home_dir'} = "/"; } else { $self->{'home_dir'} =~ s/^(\.*)$/$1/g; # Taint check. } my $cmd_line = $self->{'invocation'}; $cmd_line =~ s/-P\s+\w+/-P ****/ if $cmd_line; $self->logLocal("START: " . $self->{'program_name'} ." ". $cmd_line, 0); $self->logLocal("Username: " . $self->{'user_name'}, 0); $self->logLocal("Hostname: " . $self->{'host_name'}, 0); return $self; } =item $value = $obj_instance->getProgramInfo($field_type); This function returns field values for specified field types describing attributes of the program. The C<$field_type> parameter must be a listed attribute: C, C, C, C. The C field specifies the bare name of the executable. The C field specifies the command line arguments passed to the executable. The C value returns the environment path to the working directory. The C value specifies the absolute path to the working directory. If C is found to be inconsistent, then that value will return the C value. If an invalid C<$field_type> is passed, the function returns undefined. =cut sub getProgramInfo($) { my $self = shift; my $field_type = shift; my $return_value = undef; if (defined $field_type) { $field_type =~ /^name$/ && do { $return_value = $self->{program_name}; }; $field_type =~ /^invocation$/ && do { $return_value = $self->{invocation}; }; $field_type =~ /^env_path$/ && do { my $return_value = ""; if ( (defined $ENV{'PWD'}) && (abs_path($ENV{'PWD'}) eq abs_path(".") ) && ($ENV{'PWD'} =~ /^(.*)$/) ) { $ENV{'PWD'} = $1; $return_value = $ENV{'PWD'}; } else { my $tmp_val = abs_path("."); if ( (defined ($tmp_val) ) && ($tmp_val =~ /^(.*)$/) ) { $tmp_val = $1; $return_value = $tmp_val; } } return $return_value; }; $field_type =~ /^abs_path$/ && do { my $tmp_val = abs_path("."); if ( (defined ($tmp_val) ) && ($tmp_val =~ /^(.*)$/) ) { $tmp_val = $1; $return_value = $tmp_val; } }; } return $return_value; } =item $exit_code = $obj_instance->runCommand($command_str); This function passes the argument C<$command_str> to /bin/sh for processing. The return value is the exit code of the C<$command_str>. If the exit code is not defined, then either the signal or core dump value of the execution is returned, whichever is applicable. Perl variables C<$?> and C<$!> are set accordingly. If C<$command_str> is not defined, this function returns undefined. Log messages are recorded at log level 4 to indicate the type of exit status and the corresponding code. A failure to start the program (invalid program) results in return code -1. =cut sub runCommand($) { my $self = shift; my $command_str = shift; my $exit_code = undef; my $signal_num = undef; my $dumped_core = undef; my $return_value = undef; my $current_dir = $self->getProgramInfo("abs_path"); # Return if the command string is not set. if ( ! defined ( $command_str ) ) { return undef; } # Substitute out the tilde and dot in the directory paths. if ( defined ($ENV{PATH}) ) { ( $ENV{PATH} ) = $ENV{PATH} =~ /^(.*)$/; my @paths = split /:/, $ENV{PATH}; for (my $i = 0; $i <= $#paths; $i++) { $paths[$i] =~ s/^~\/?$/$self->{'home_dir'}/g; $paths[$i] =~ s/^\.\/?$/$current_dir/g; } $ENV{PATH} = join(":", @paths); } $command_str =~ s/^(.*)$/$1/g; # Taint checking. # Run the command and parse the results. system($command_str); my $return_str = $?; $exit_code = $? >> 8; $signal_num = $? & 127; $dumped_core = $? & 128; if ( $return_str == -1 ) { # Check for invalid program. $self->logLocal("Invalid execution of \'$command_str\'.", 4); $return_value = -1; } elsif ( $dumped_core != 0 ) { $self->logLocal("\'$command_str\' dumped core.", 4); $return_value = $dumped_core; } elsif ( $signal_num != 0 ) { $self->logLocal("\'$command_str\' signalled \'$signal_num\'.", 4); $return_value = $signal_num; } else { $self->logLocal("\'$command_str\' exited \'$exit_code\'.", 4); $return_value = $exit_code; } return $return_value; } =item $exit_code = $obj_instance->runCommandWithInput($command_str, $input) This function runs a command with input on STDIN. It is similar to the runCommand() routine. It takes two arguments, the command string and the input to pipe in. The return value is the exit code of the command string. Perl variables $? and $! are set accordingly. If the command string or input value are not defined, this function returns undefined. =cut sub runCommandWithInput($$) { my $self = shift; my $command_str = shift; # command string to exec my $input_val = shift; # input value to pass my $exit_code = undef; # exit value $self->logLocal("Now processing in \'runCommandWithInput()\'.", 2); # Check definition of command_str. if ( ! defined ( $command_str ) ) { $self->logError("Command not supplied to \'runCommandWithInput\'!", 3); return undef; } # Check definition of input_val. if ( ! defined ( $input_val ) ) { $self->logError("Input not supplied to \'runCommandWithInput\'!", 3); return undef; } # Try executing the command. if ( ! open (RCMDINP, "| $command_str") ) { $self->logError("Cannot open command pipe \'$command_str\'.", 3); return undef; } # Pipe the input to the command. if ( ! print RCMDINP $input_val ) { $self->logError("Cannot pipe input to command \'$command_str\'.", 3); return undef; } # Close the pipe. if ( ! close (RCMDINP) ) { $self->logError("Cannot close command pipe \'$command_str\'.", 3); return undef; } # Grab exit status. $exit_code = $?; $exit_code >>= 8; $self->logLocal("Command \'$command_str\' exited \'$exit_code\'.", 2); $self->logLocal("Now leaving \'runCommandWithInput\'.", 2); return $exit_code; } # Functional Class : depend =item $obj_instance->printDependInfo(); The C function prints the dependency list created by C. One item is printed per line. =cut sub printDependInfo() { my $self = shift; foreach my $dependent (@{$self->{depend_info}}) { print STDERR $dependent, "\n"; } } =item $obj_instance->printDependInfoAndExit(); The C function prints the dependency list created by C. One item is printed per line. The function exits with exit code 0. =cut sub printDependInfoAndExit() { my $self = shift; $self->printDependInfo(); exit 0; } =item $obj_instance->addDependInfo(@depend_list); The C function adds C<@depend_list> information to the dependency list. If C<@depend_list> is empty, the internal dependency list is emptied. Contents of C<@depend_list> are not checked for validity (eg. they can be composed entirely of white space or multiple files per record). The first undefined record in C<@depend_list> halts reading in of dependency information. =cut sub addDependInfo(@) { my $self = shift; my $num_elts = 0; while (my $data_elt = shift @_) { push (@{$self->{depend_info}}, $data_elt); $num_elts++; } if ($num_elts == 0) { @{$self->{depend_info}} = (); } } # Functional Class : version =item $version_string = $obj_instance->getVersionInfo(); The C function returns the version information set by the C function. =cut sub getVersionInfo() { my $self = shift; return $self->{version_info}; } =item $obj_instance->printVersionInfo(); The C function calls the version handler, if set. If not, it prints the version information set by the C function. If there is no defined version information, a message is returned notifying the user. =cut sub printVersionInfo() { my $self = shift; if ( defined $self->{'version_handler'} ) { $self->{'version_handler'}->(); } elsif (defined $self->getVersionInfo() ) { print STDERR $self->getProgramInfo('name'), " ", $self->getVersionInfo(), "\n"; } else { print STDERR $self->getProgramInfo('name'), " has no defined version information\n"; } } =item $obj_instance->printVersionInfoAndExit(); The C function calls the version handler, if set. Otherwise, it prints prints version info set by the C function. If there is no defined version information, a message is printed notifying the user. This function calls exit with exit code 0. =cut sub printVersionInfoAndExit() { my $self = shift; $self->printVersionInfo(); exit 0; } =item $obj_instance->setVersionInfo($version_string); The C function sets the version information to be reported by C. If C<$version_string> is empty, invalid, or undefined, the stored version information will be undefined. =cut sub setVersionInfo($) { my $self = shift; my $v_info = shift; if ( defined ( $v_info ) && ( $v_info =~ /\S/ ) ) { $self->{version_info} = $v_info; } else { $self->{version_info} = undef; } } =item $obj_instance->setVersionHandler($function_ref); The C method establishes a callback function for handling the reporting of version information to the user. If a handler is set, then any information passed in via C is not reported. To remove the handler, call this method without any arguments. If a handler is not a proper code reference, this method returns undefined and does not set a handler. This method returns 1 on success. =cut sub setVersionHandler($) { my $self = shift; my $v_handler = shift; if ( defined ( $v_handler ) && ( (ref $v_handler) eq "CODE" ) ) { $self->{version_handler} = $v_handler; } elsif ( ! defined ( $v_handler ) ) { $self->{version_handler} = undef; } else { # Bad input. return undef; } return 1; } # Functional Class : help =item $obj_instance->printHelpInfo(); The C function prints the help information passed by the C function. =cut sub printHelpInfo() { my $self = shift; if (defined $self->{help_info}) { print STDERR $self->{help_info}; } else { print STDERR "No help information defined.\n"; } } =item $obj_instance->printHelpInfoAndExit(); The C function prints the help info passed by the C function. This function exits with exit code 0. =cut sub printHelpInfoAndExit() { my $self = shift; $self->printHelpInfo(); exit 0; } =item $obj_instance->setHelpInfo($help_string); The C function sets the help information via C<$help_string>. If C<$help_string> is undefined, invalid, or empty, the help information is undefined. =cut sub setHelpInfo($) { my $self = shift; my $help_string = shift; if ( ( defined $help_string ) && ( $help_string =~ /\S/ ) ) { chomp $help_string; $self->{help_info} = $help_string . "\n"; } else { $self->{help_info} = undef; } } # Functional Class : usage =item $obj_instance->printUsageInfo(); The C function prints the usage information reported by the C function. If no usage information is defined, but help information is defined, help information will be printed. =cut sub printUsageInfo() { my $self = shift; if ( defined $self->{usage_info} ) { print STDERR $self->{usage_info}; } elsif ( defined $self->{help_info} ) { print STDERR $self->{help_info}; } else { print STDERR "No usage information defined.\n"; } } =item $obj_instance->printUsageInfoAndExit(); The C function prints the usage information the reported by the C function and exits with status 1. =cut sub printUsageInfoAndExit() { my $self = shift; $self->printUsageInfo(); exit 1; } =item $obj_instance->setUsageInfo($usage_string); The C function sets the usage information via C<$usage_string>. If C<$usage_string> is undefined, invalid, or empty, the usage information is undefined. =cut sub setUsageInfo($) { my $self = shift; my $usage_string = shift; if ( ( defined $usage_string ) && ( $usage_string =~ /\S/ ) ) { chomp($usage_string); $self->{usage_info} = $usage_string . "\n"; } else { $self->{usage_info} = undef; } } # Functional Class : files =item $valid = isReadableFile($file_name); This function accepts a single scalar parameter containing a file name. If the file corresponding to the file name is a readable plain file or symbolic link, this function returns 1. Otherwise, the function returns 0. If the file name passed is undefined, this function returns 0 as well. =cut sub isReadableFile($) { my $self = shift; my $file = shift; if ( ! defined ( $file ) ) { # class, not instance, invocation $file = $self; } if (defined ($file) && # was a file name passed? ( (-f $file) || (-l $file) ) && # is the file a file or sym. link? (-r $file) # is the file readable? ) { return 1; } else { return 0; } } =item $valid = isExecutableFile($file_name); This function accepts a single scalar parameter containing a file name. If the file corresponding to the file name is an executable plain file or symbolic link, this function returns 1. Otherwise, the function returns 0. If the file name passed is undefined, this function returns 0 as well. =cut sub isExecutableFile($) { my $self = shift; my $file = shift; if ( ! defined ( $file ) ) { # class invocation, not instance $file = $self; } if (defined ($file) && # was a file name passed? ( (-f $file) || (-l $file) ) && # is the file a file or sym. link? (-x $file) # is the file executable? ) { return 1; } else { return 0; } } =item $valid = isWritableFile($file_name); This function accepts a single scalar parameter containing a file name. If the file corresponding to the file name is a writable plain file or symbolic link, this function returns 1. Otherwise, the function returns 0. If the file name passed is undefined, this function returns 0 as well. =cut sub isWritableFile($) { my $self = shift; my $file = shift; if ( ! defined ( $file ) ) { # class, not instance, invocation $file = $self; } if (defined ($file) && # was a file name passed? ( (-f $file) || (-l $file) ) && # is the file a file or sym. link? (-w $file) # is the file writable? ) { return 1; } else { return 0; } } =item $valid = isCreatableFile($file_name); This function accepts a single scalar parameter containing a file name. If the file corresponding to the file name is creatable this function returns 1. The function checks if the location of the file is writable by the effective user id (EUID). If the file location does not exist or the location is not writable, the function returns 0. If the file name passed is undefined, this function returns 0 as well. Note that files with suffix F are not supported under UNIX platforms, and will return 0. =cut sub isCreatableFile($) { my $self = shift; my $file = shift; if ( ! defined ( $file ) ) { $file = $self; } my $return_code = 0; if ( (defined ($file) ) && (! -e $file) && ($file !~ /\/$/) ) { my $dirname = dirname($file); # check the writability of the directory $return_code = isWritableDir($dirname); } else { # the file exists, it's not creatable $return_code = 0; } return $return_code; } =item $valid = isReadableDir($directory_name); This function accepts a single scalar parameter containing a directory name. If the name corresponding to the directory is a readable, searchable directory entry, this function returns 1. Otherwise, the function returns 0. If the name passed is undefined, this function returns 0 as well. =cut sub isReadableDir($) { my $self = shift; my $file = shift; if ( ! defined ( $file ) ) { # class invocation $file = $self; } if (defined ($file) && # was a name passed? (-d $file) && # is the name a directory? (-r $file) && # is the directory readable? (-x $file) # is the directory searchable? ) { return 1; } else { return 0; } } =item $valid = isWritableDir($directory_name); This function accepts a single scalar parameter containing a directory name. If the name corresponding to the directory is a writable, searchable directory entry, this function returns 1. Otherwise, the function returns 0. If the name passed is undefined, this function returns 0 as well. =cut sub isWritableDir($) { my $self = shift; my $file = shift; if ( ! defined ( $file ) ) { # class invocation $file = $self; } if (defined ($file) && # was a name passed? (-d $file) && # is the name a directory? (-w $file) && # is the directory writable? (-x $file) # is the directory searchable? ) { return 1; } else { return 0; } } =item $valid = isCreatableDir($directory_name); This function accepts a single scalar parameter containing a directory name. If the name corresponding to the directory is creatable this function returns 1. The function checks if the immediate parent of the directory is writable by the effective user id (EUID). If the parent directory does not exist or the tree is not writable, the function returns 0. If the directory name passed is undefined, this function returns 0 as well. =cut sub isCreatableDir($) { my $self = shift; my $dir = shift; if ( ! defined ( $dir ) ) { $dir = $self; } my $return_code = 0; if (defined ($dir) ) { $dir =~ s/\/$//g; $return_code = isCreatableFile($dir); } return $return_code; } =item $valid = isCreatablePath($path_name); This function accepts a single scalar parameter containing a path name. If the C<$path_name> is creatable this function returns 1. The function checks if the directory hierarchy of the path is creatable or writable by the effective user id (EUID). This function calls itself recursively until an existing directory node is found. If that node is writable, ie. the path can be created in it, then this function returns 1. Otherwise, the function returns 0. This function also returns zero if the C<$path_name> supplied is disconnected from a reachable directory tree on the file system. If the path already exists, this function returns 0. The C<$path_name> may imply either a path to a file or a directory. Path names may be relative or absolute paths. Any unresolvable relative paths will return 0 as well. This includes paths with F<..> back references to nonexistent directories. This function is recursive whereas C and C are not. =cut sub isCreatablePath($) { my $self = shift; my $pathname = shift; if ( ! defined ( $pathname ) ) { # class invocation $pathname = shift; } my $return_code = 0; if (defined $pathname) { # strip trailing '/' $pathname =~ s/(.+)\/$/$1/g; my $filename = basename($pathname); my $dirname = dirname($pathname); if ( (! -e $pathname) && ($dirname ne $pathname) && ($filename ne "..") ) { if (-e $dirname) { $return_code = isWritableDir($dirname); } else { $return_code = isCreatablePath($dirname); } } else { $return_code = 0; } } return $return_code; } # Functional Class : date =item $date_string = getISODate($tm); This function returns the ISO 8601 datetime as a string given a time structure as returned by the C