#!/usr/local/bin/perl -w # # USAGE: changeid.pl %user% %formfile%" # Restricts new job creation to P4AUTHUSER # changes "new" jobids to be the value in # DTG_DTISSUE # # To add the trigger to the Peforce server, add the following line to the # trigger table: # jobId form-in job "/changeid.pl %user% %formfile%" # # Todd O'Connor - toconnor@pillardata.com # # Based on a Ruby version provided by Perforce. # use Carp; use strict; use File::Basename; use POSIX qw(strftime); my $scriptName = "changeid.pl"; my $USER = $ARGV[0]; # from %user% in trigger table my $formfile = $ARGV[1]; # from %formfile% in trigger table my $jobValue = ""; my $dtgValue = ""; my %value = (); my $P4AUTHUSER = "p4dtg"; # user that is allowed to create new # jobs and edit ro fields chomp($USER); chomp($formfile); if ( "$USER" eq "" || "$formfile" eq "" ) { print "$scriptName: Error: Missing argument\n"; exit 1; } fix_P4DTG_Job_Format("$formfile"); # read the form %value = read_form("$formfile"); $jobValue = $value{Job}; $dtgValue = $value{DTG_DTISSUE}; chomp($jobValue); chomp($dtgValue); if ("$jobValue" eq "new") { if ( "$USER" ne "$P4AUTHUSER" ) { print "$scriptName: Error: You are not authorized to create new jobs.\n"; exit 1; } else { if (defined($value{DTG_DTISSUE})) { open(F,"$formfile"); my @form = ; @form = map( (/^Job:/ ? "Job: $dtgValue\n" : $_ ) , @form); open(F,">$formfile"); # now overwrite the form print F @form; close(F); } else { print "$scriptName: Error: could not determine DTG_DTISSUE value.\n"; exit 1; } } } exit 0; # read a Perforce style form and return it as a hash sub read_form { my ($file) = shift; my (%hash,$current_keyword); open(F,"<$file") or croak("can't open $file: $!"); while() { s/\s*#.*$//; # kill comments and any whitespace preceding the comment if(/^$/) { # empty line or line with just a comment undef($current_keyword); } elsif(substr($_,0,1) eq "\t") { croak("unrecognized line") if (!defined($current_keyword)); s/^\t//; $hash{$current_keyword} .= $_; } elsif(/(.*?):\s*(.*)/) { # keyword is everything up to the *first* colon $hash{$current_keyword=$1} = $2; } } close(F); return %hash; } # A hack to fix the P4DTG job form output which does not # match the Perforce CLI job form output # P4DTG adds \t and \n # # Job: # new # # should be: # # Job: new # sub fix_P4DTG_Job_Format { my ($file) = shift; my(@rebuild); open(F,"<$file") or croak("can't read $file: $!"); while () { if ($_ =~ /^Job:[\s]+$/) { push(@rebuild, "Job: "); } else { push(@rebuild, "$_"); } } close(F); open(F,">$file") or croak("can't write $file: $!"); print F @rebuild; close(F); }