#!/usr/local/bin/perl
#----------------------------------------------------------------------#
# Copyright 1999 Cimatron Ltd. #
# #
# Perl name : p4trig.pl #
# Programmers : Eli Ofek & Yael Stern #
# #
# Description:
# This script is a trigger engine for Changelists approvements.
# This script uses the file autorize.dat as permissions DB.
# When changelists is approved a <chnagesnum>.ok file is created.
# When changelists submitted their file is deleted.
# Invocation: #
# See Help sub Below... #
# #
# #
# #
#----------------------------------------------------------------------#
#----------------------------------------------------------------------#
# Change defenitions here:
#-----------------------------------------------------------------
# Internal Server path:
# Path to permissions file:
my $p4PermFileLoc='d:\Perm\authorize.dat';
# Path to approved changelist files:
my $p4ApprovedLoc='d:\NSCMDB\approved\\';
# E-Mail settings:
my $ServerName="Ntserv1"; # Define Mail server to use here.
my $Domain="\@cimatron.co.il"; # Define the domain to send to.
my $MessageBodyFile="body.txt";
# Create body of message to put in mail.
my @MessageBody="";
#push (@MessageBody,"\n Hi,\n\n");
#push (@MessageBody,"This is an automatic message from NSCM Application\n");
#push (@MessageBody,"to let you know that your changelist was approved by your supervisor.\n");
#push (@MessageBody,"You can read all the details in the subject field of this E-Mail.\n\n\n");
# Path to permissions file:
my $PermFileLoc='\\\\ntserv5\Perm\authorize.dat';
# Path to permissions file:
my $Perm2FileLoc='\\\\ntserv5\Perm\authorize2.dat';
# Path to approved changelist files:
my $ApprovedLoc='\\\\ntserv5\NSCMDB\approved\\';
# Extension for Approved changes files:
my $Exten='ok';
#
#
# Set general parameters: #
#----------------------------------------------------------------------#
# General Variables: #
#----------------------------------------------------------------------#
$TempFile="Tmp.$$";
my $Geresh='"';
#################### S U B S ###########################################
sub Help
{
print "\tUsage:\n\n\n";
print "\t\t\t p4trig.exe -confirm <changenumbers> : To Approve changelists\n\n";
print "\t\t\t p4trig.exe -disconfirm <changenumbers> : To DisApprove changelists\n\n";
print "\t\t\t p4trig.exe -confirmsubmit <changenum> : To Submit a changelist\n\n";
print "\t\t\t p4trig.exe -equal <changenum><port><client><demouser> : To check if all the files in changelist are from the same branch\n\n";
print "\t\t\t p4trig.exe -reserved<changenum><user><port><client><demouser> : To check that non of the files are reserved\n\n";
print "\t\t\t p4trig.exe -nsgroup <user name><port><client><demouser> :To make sure that the user is not from No_Submit group.\n\n";
print "\n\n\n";
}
sub SendMail($,$,$) # This sub sends mail to user.
{ # Input: recepient login, sender login, subject message
# Get parameters:
my $recepient=shift;
my $sender=shift;
my $subject=shift;
$recepient=join('',$recepient,$Domain); # Make email address.
$subject=join('','"',$subject,'"');
my $Temp="";
###### Make Message Body ######
if (open(BODY,"+>$MessageBodyFile")!=1)
{ die "\nError!\nCan't create body of message in file: $MessageBodyFile\n\n";}
print BODY "@MessageBody";
close(BODY);
######## Send Messages ##########
$Temp=`blat.exe $MessageBodyFile -t $recepient -s $subject -server $ServerName -f $sender`;
die "Error !\n Can't run:blat.exe $MessageBodyFile -t $recepient -s $subject -server $ServerName -f $sender\n Failed in sub SendMail.\n" if ($?!=0);
}
sub Approve($) # This sub approves a chnagelist by checking if current user
{ # is permitted to approve, and if yes, create an approvement file and adds mark to description (ABY: <login>).
# Main Return exit(0) on success, and exit(1) on failure.
# Input: Changelist number to be approved.
my $ChangeProp="";
my $Temp="";
my $Change=shift;
my $Supervisor="";
my $User="";
my $Permissions="";
my $Error=0;
my $ApprovedBeforeBy="";
# Get Supervisor name:
$Temp=`p4 user -o`;
die "Error !\n Can't run:p4 user -o\n Failed in sub Approve.\n" if ($?!=0);
$Temp=~/\nUser:\t(\w*)\n/; # Get User field.
$Supervisor="$1";
# Get User name:
$ChangeProp=`p4 change -o $Change`;
#die "Error !\n Can't run:p4 change -o $Change\n Failed in sub Approve.\n" if ($?!=0);
if ($?!=0)
{
print "Error Changelist $Change - Ignored.\n";
return;
}
$ChangeProp=~/\nUser:\t(\w*)\n/; # Get User field.
$User="$1";
if (($ChangeProp=~/[Aa][Bb][Yy]:(\s*)(\w*)[\W]/) and (-e "$ApprovedLoc$Change.$Exten")) # Find if already approved.
{
$ApprovedBeforeBy=$2;
print "Notice: Changelist $Change already approved By $ApprovedBeforeBy - Ignored \n";
$Error=1; # Mark that an Error accured.
return; # Quit sub.
}
# Read permissions from Database:
$Permissions=`type $PermFileLoc`;
die "Error !\n Can't run:type $PermFileLoc\n Failed in sub Approve.\n" if ($?!=0);
# Check if permitted to approve:
if ($Permissions=~/\n\t$User\W*$Supervisor\W*\n/)
{ # If permitted:
if (not($ChangeProp=~s/\nFiles:/\n\t< ABY: $Supervisor >\nFiles:/))
{ $ChangeProp="$ChangeProp\n\t< ABY: $Supervisor >\n";} ; # Add mark to change description.
# Create file for input.
open(TMP,">$TempFile") || die "Error !\n Can't open file $TempFile\n Failed in sub Approve.\n";
print TMP $ChangeProp;
close(TMP);
# Updtae Change properties.
$Temp=`p4 change -i < $TempFile`;
die "Error !\n Can't run:p4 change -i < $TempFile\n Failed in sub Approve.\n" if ($?!=0);
unlink "$TempFile";
$Temp=`echo OK > $ApprovedLoc$Change.$Exten`; # Create approvement file.
die "Error !\n Can't run:echo OK > $ApprovedLoc$Change.$Exten\n Failed in sub Approve.\n" if ($?!=0);
&SendMail($User,$Supervisor,"NSCM Message: Changelist $Change was approved for submit by $Supervisor"); # Send Email notification to user.
print "\n Changelist $Change Approved for submit !\n";
}
else # If not permitted:
{
# Notify of an Error:
print "\nNotice: User $Supervisor is not permitted to approve Changelist $Change of user $User ! - Ignored.\n";
$Error=1; # Mark that an Error accured.
}
}
sub DisApprove($) # This sub disapproves a chnagelist by checking if current user
{ # is permitted to disapprove, and if yes, create delete approvement file and description mark.
# Main Return exit(0) on success, and exit(1) on failure.
# Input: Changelist number to be disapproved.
my $ChangeProp="";
my $Temp="";
my $Change=shift;
my $Supervisor="";
my $User="";
my $Permissions="";
my $Error=0;
my $ApprovedBeforeBy="";
# Get Supervisor name:
$Temp=`p4 user -o`;
die "Error !\n Can't run:p4 user -o\n Failed in sub DisApprove.\n" if ($?!=0);
$Temp=~/\nUser:\t(\w*)\n/; # Get User field.
$Supervisor="$1";
# Get User name:
$ChangeProp=`p4 change -o $Change`;
#die "Error !\n Can't run:p4 change -o $Change\n Failed in sub Approve.\n" if ($?!=0);
if ($?!=0)
{
print "Error Changelist $Change - Ignored.\n";
return;
}
$ChangeProp=~/\nUser:\t(\w*)\n/; # Get User field.
$User="$1";
if (not (-e "$ApprovedLoc$Change.$Exten")) # Find if Not approved yet.
{
print "Notice: Changelist $Change was NOT approved yet - Ignored \n";
$Error=1; # Mark that an Error accured.
return; # Quit sub.
}
# Read permissions from Database:
$Permissions=`type $PermFileLoc`;
die "Error !\n Can't run:type $PermFileLoc\n Failed in sub DisApprove.\n" if ($?!=0);
# Check if permitted to disapprove:
if ($Permissions=~/\n\t$User\W*$Supervisor\W*\n/)
{ # If permitted:
$ChangeProp=~s/< [Aa][Bb][Yy]:(\s*)(\w*) >//; # Remove mark from change description.
# Create file for input.
open(TMP,">$TempFile") || die "Error !\n Can't open file $TempFile\n Failed in sub DisApprove.\n";
print TMP $ChangeProp;
close(TMP);
# Updtae Change properties.
$Temp=`p4 change -i < $TempFile`;
die "Error !\n Can't run:p4 change -i < $TempFile\n Failed in sub DisApprove.\n" if ($?!=0);
unlink "$TempFile";
unlink "$ApprovedLoc$Change.$Exten"; # Remove approvement file.
&SendMail($User,$Supervisor,"NSCM Message: Changelist $Change was Disapproved for submit by $Supervisor"); # Send Email notification to user.
print "\n Changelist $Change DisApproved for submit !\n";
}
else # If not permitted:
{
# Notify of an Error:
print "\nNotice: User $Supervisor is not permitted to disapprove Changelist $Change of user $User ! - Ignored.\n";
$Error=1; # Mark that an Error accured.
}
}
sub Submit($) # This sub submits a chnagelist by checking if there is an approvement file.
{ # If yes return exit(0) else Notify of an Error and returns exit(1).
# Input: Changelist number to be submitted.
my $Change=shift;
# Check if Aprovement file exist:
#if (-e "$ApprovedLoc$Change.$Exten") # If Approved:
# {
# unlink "$ApprovedLoc$Change.$Exten"; # Erase Approvemant file.
# print "\nChangelist $Change may be submitted !\n\n";
# exit(0); # Return status 0 (OK).
# }
if (-e "$p4ApprovedLoc$Change.$Exten") # If Approved:
{
unlink "$p4ApprovedLoc$Change.$Exten"; # Erase Approvemant file.
print "\nChangelist $Change may be submitted !\n\n";
exit(0); # Return status 0 (OK).
}
else
{
print "\nChangelist $Change was not approved yet !\n\n";
exit(1); # Return status 1 (Falilure).
}
}
sub EqualBranch($,$,$,$) # This sub submits a chnagelist by checking that all the files in the changelist are from the same branch.
{ # If yes return exit(0) else Notify of an Error and returns exit(1).
# Input: Changelist number to be submitted,port,client,user.
my $Change=shift;
my $ServerPort = shift;
my $ClientName = shift;
my $UserName = shift;
my $fconf="";
my $conf="";
my $PROJ="";
my @Buffer="";
my $Sign="/";
my $crtr="";
my @Files=`p4 -p $ServerPort -c $ClientName -u $UserName opened -c $Change `;
#my @Files=`p4 opened -a -c $Change 2> $TempFile & type $TempFile`;
if ($Files[0]=~/file(s) not opened on this client./i)
{print("\nchangelist is not found!!!.\nFailed in sub EqualBranch.");}
$Files[0]=~/^\/\/(\w*)\//;#find the project name of the first file.
$PROJ=$1;
push(@OutPut,"dev\n"); # dev always exists.
push(@OutPut,"add-in\n"); # it is not included in p4 branches.
@Buffer=`p4 -p $ServerPort -c $ClientName -u $UserName branches`;
if ($?!=0) {print("\nCannot create configuration list from p4 branches.\nFailed in sub EqualBranch.");}
@TempArr=grep(/Branch $PROJ/i,@Buffer);
foreach $TempArr (@TempArr) #put in OutPut all the existing branches.
{
$TempArr=~/Branch $PROJ(\S*)(\s)/i;
$TempVar=$1;
$TempVar=~s|_|$Sign|g;
$TempVar=substr($TempVar,1);
push(@OutPut,"$TempVar\n");
}
foreach $OutPut (@OutPut) # find the branch of the first file.
{
chomp($OutPut);
$crtr="//$PROJ/$OutPut/";
if($Files[0]=~/^$crtr/i)
{
$conf=$OutPut;
}
}
if ($conf eq "")
{
print "\n\nError! Unknown branch!!!\n\n";
exit(1);
}
foreach $Files (@Files) #make sure that all the other files are from the same branch as the first file.
{
foreach $OutPut (@OutPut)
{
chomp($OutPut);
$crtr="//$PROJ/$OutPut/";
if($Files =~/^$crtr/i)
{
$fconf=$OutPut;
}
}
if ($fconf ne $conf )
{
print "\n\nError!! all the files must be from the same branch!!!";
exit (1);
}
$fconf="";
}
exit(0);
}
sub NoSubmitGroup()#Prevent submittal from users which are members of the No_Submit group.
{
my $user=shift;
my $ServerPort=shift;
my $Client=shift;
my $DemoUser=shift;
my @group=`p4 -p $ServerPort -c $Client -u $DemoUser group -o no_submit`;
if ($?!=0) {die "can't run 'p4 group -o no_submit' in sub NoSubmitGroup.";}
@group=grep (/^\t/,@group);
foreach $group (@group)
{
$group=~s/^\t//;
chomp $group;
if ($group eq $user)
{
print "\n\nnot allowed to submit(Group No_Submit)!!!\n\n";
exit (1);
}
}
exit(0);
}
sub ReservedFiles($,$,$,$)# Prevernt Submittal of reserved files
{
my $Change=shift;
my $user=shift;
my $ServerPort = shift;
my $ClientName = shift;
my $UserName = shift;
my @Files=`p4 -p $ServerPort -c $ClientName -u $UserName opened -c $Change `;
if ($Files[0]=~/file(s) not opened on this client./i)
{print("\nchangelist is not found!!!.\nFailed in sub EqualBranch.");}
#Get the file with the premissions.
@Permissions=`type $Perm2FileLoc`;
die "Error !\n Can't run:type $Perm2FileLoc\n Failed in sub Approve.\n" if ($?!=0);
#leave only the relevant lines.
@Permissions=grep (/^\t\S/,@Permissions);
#Make sure that the reserved files are not in the changelist and if they are check if this user is allowed to edit this files.
foreach $Permissions (@Permissions)
{
$file=$Permissions;
if(not($file=~s/^\t(\S+)\.\.\.(\S+)\s/$1\.\*$2/))
{
$file=~s/^\t(\S+)\s/$1/;
}
@samefile=grep (/$file/i,@Files);
if (@samefile ne "")
{
if(not($Permissions=~/\s+$user\s/))
{
print "\nFile $file is reserved, and you are not allowed to submit him!!!\n\n";
exit (1);
}
}
}
exit(0);
}
##################### M A I N ###########################################
if ($ARGV[0] eq '-h') # Display help screen if requested.
{
&Help();
exit(0);
}
if ($#ARGV<1) # Display help screen if wrong Arguments.
{
print "\nError !\n Missing Arguments, read the help and try again !\n\n";
&Help();
exit(1);
}
if ($ARGV[0] eq '-confirm')
{
shift(@ARGV); # delete option.
foreach $ARGV (@ARGV) # foreach number given try to approve it.
{
if ($ARGV+0 == 0) # If argument is not a number:
{
print "\nError !\n Argument $ARGV must be a number ! - Ignored !\n\n";
}
else
{
&Approve($ARGV); # Try to approve changelist.
}
}
if ($Error) # If an Error accured:
{
print "\nEncountered some Errors.\n";
print "Contact Configuration Managers For more info.\n\n";
exit(1);
}
else
{ exit(0); } # If no Errors.
}
elsif ($ARGV[0] eq '-disconfirm')
{
shift(@ARGV); # delete option.
foreach $ARGV (@ARGV) # foreac number given try to approve it.
{
if ($ARGV+0 == 0) # If argument is not a number:
{
print "\nError !\n Argument $ARGV must be a number ! - Ignored !\n\n";
}
else
{
&DisApprove($ARGV); # Try to approve changelist.
}
}
if ($Error) # If an Error accured:
{
print "\nEncountered some Errors.\n";
print "Contact Configuration Managers For more info.\n\n";
exit(1);
}
else
{ exit(0); } # If no Errors.
}
elsif ($ARGV[0] eq '-confirmsubmit')
{
if ($ARGV[1]+0 == 0) # If second argument is not a number:
{
print "\nError !\n Second Argument must be a number !, read the help and try again !\n\n";
&Help();
exit(1);
}
&Submit($ARGV[1]); # Try to approve changelist.
}
elsif ($ARGV[0] eq '-equal')
{
if ($ARGV[1]+0 == 0) # If second argument is not a number:
{
print "\nError !\n Second Argument must be a number !, read the help and try again !\n\n";
&Help();
exit(1);
}
&EqualBranch($ARGV[1],$ARGV[2],$ARGV[3],$ARGV[4]); # Check that all files are from the same branch.
}
elsif ($ARGV[0] eq '-reserved')
{
if ($ARGV[1]+0 == 0) # If second argument is not a number:
{
print "\nError !\n Second Argument must be a number !, read the help and try again !\n\n";
&Help();
exit(1);
}
&ReservedFiles($ARGV[1],$ARGV[2],$ARGV[3],$ARGV[4]); # Check that all files are from the same branch.
}
elsif ($ARGV[0] eq '-ns')
{
&NoSubmitGroup($ARGV[1],$ARGV[2],$ARGV[3],$ARGV[4]);
}
else # Invalid option - Error
{
print "\nError !\n Invalid First Argument, read the help and try again !\n\n";
&Help();
exit(1);
}