package trig_utils; use strict; use warnings; use File::Basename; use File::Spec; use XML::Simple; use Data::Dumper; use FileHandle; # ########################################### mapPseudoIdToRealId sub mapPseudoIdToRealId { # mapPseudoIdToRealId # Input: Any accurev user id # Output: A real id when input ID is one of the administrative ids # # Why: For routines that must reference a real e-mail user id my $id = shift @_; my $realId = "wblair"; # Translate to this real ID my @pseudoIds = qw/builder ar6060/; # Known list of ID without a valid e-mail address my ($package, $filename, $line) = caller; foreach my $name (@pseudoIds) { if ($id eq $name) { print "$package, $filename, $line - Mapped pseudo-user ID \"$name\" to real ID \"$realId\"\n"; # Report action to the trigger log file $id = $realId; } } return($id); } # END - mapPseudoIdToRealId # ########################################### setEnvironment sub setEnvironment(){ # setEnvironment # Define OS portable paths # Modify as needed. We intentionally use this as the home directory for the server logging # to ensure all dependencies are located together to simplify backup/restore. $::AccuRevRoot = File::Spec->catfile("", "opt", "accurev", "ar6060"); $::AccuRevBin = File::Spec->catfile($::AccuRevRoot, "bin"); $::AccuRev = File::Spec->catfile($::AccuRevBin, "accurev"); # Accurev CLI $::trigDumpDir = File::Spec->catfile($::AccuRevRoot, "trigDumpDir"); # Dumpping ground for trigger parameter files $::adminGroup = "Admin"; $::buildGroup = "Builders"; } # END - setEnvironment # ########################################### validateLogin sub validateLogin { # validateLogin # my ($callerScript, $callerArg) = @_; my $loginStatus = `$::AccuRev secinfo`; chomp ($loginStatus); if ($loginStatus eq "notauth") { open TIO, ">$callerArg" or die "Can't create directory $callerArg"; print TIO "Server trigger cannot run because the server\n"; print TIO "admin account does not have a valid login token.\n"; close TIO; exit(1); } } # END - validateLogin # ########################################### show_hash sub show_hash(){ # show_hash # # Inputs: # msg - text string # phash = Reference to the hash to be displayed # Example: # Print the environment hash for the current process # &show_hash("ENV", \%ENV); my ($msg, $phash ) = @_; my @keys = sort keys %$phash; print "=== $msg (", 0 + @keys, ") ===\n"; foreach my $key (@keys) { my $val = $phash->{$key}; printf "%20.20s => '%s'\n", $key, $val; } } # END - show_hash # ########################################### dumpTriggerParamFile sub dumpTriggerParamFile() { # dumpTriggerParamFile # #### Save the trigger parameter file when flag file found else return. #### The saved trigger param file is handy when debugging triggers because #### they can be fed into the trigger script to simulate the server invocation. #### #### Also calls dumpper to write raw data into the trigger log file maintained #### by the server process. Use this when accurev support asks for trigger debug info. my $dumpFlag = File::Spec->catfile($::AccuRevRoot, "dumpParams.flag"); unless ( -e $dumpFlag ) {return 0;} $::debugOn = 1; my ($xmlinput_raw, $hook, $file) = @_; my $keepIt = File::Spec->catfile($::trigDumpDir, $hook . "-" . basename($file) . "-" . time); unless ( -d "$::trigDumpDir" ) { print "trig_utils::dumpTriggerParamFile is creating the trigger parameter dump directory\n"; print "\t$::trigDumpDir\n"; mkdir "$::trigDumpDir" or warn "Can't create directory \"$::trigDumpDir\" ($!)\n"; } open CACHE, ">$keepIt"; print CACHE $xmlinput_raw; close CACHE or die "Failed to close the private copy of the trigger parameter file: ($!)"; } # END - dumpTriggerParamFile # ########################################### dumpTriggerFlatFile sub dumpTriggerFlatFile() { # dumpTriggerFlatFile # #### Save the trigger FLAT parameter file when flag file found else return. #### The saved trigger param file is handy when debugging triggers because #### they can be fed into the trigger script to simulate the server invocation. #### #### Also calls dumpper to write raw data into the trigger log file maintained #### by the server process. Use this when accurev support asks for trigger debug info. my $dumpFlag = File::Spec->catfile($::AccuRevRoot, "dumpParams.flag"); unless ( -e $dumpFlag ) {return 0;} $::debugOn = 1; my ($hook, $file) = @_; my $keepIt = File::Spec->catfile($::trigDumpDir, $hook . "-" . basename($file) . "-" . time); unless ( -d "$::trigDumpDir" ) { print "trig_utils::dumpTriggerFlatFile is creating the trigger parameter dump directory\n"; print "\t$::trigDumpDir\n"; mkdir "$::trigDumpDir" or warn "Can't create directory \"$::trigDumpDir\" ($!)\n"; } open CACHE, ">$keepIt"; open(FLAT, "<", $file) or die "Can't open the trigger flat file \"$file\" for read access; ($!)"; while () { print CACHE $_; } close FLAT or die "Failed to close the trigger flat file \"$file\": ($!)"; close CACHE or die "Failed to close the private copy of the trigger parameter file: ($!)"; } # END - dumpTriggerFlatFile # ########################################### setHash sub setHash { # setHash # my ($listFile, $hashPointer) = @_; if (-e $listFile) { my $fh = new FileHandle; open($fh, "<", $listFile) or die "Can't open list file $listFile ($!)\n"; while (my $line = <$fh>) { chomp $line; next if ($line =~ /^\s*(#|$)/); $hashPointer->{"$line"} = 1; } # END - while close $fh; } # END - if listFile } # END - setHash # Mark end of module 1;