Binary.pl #1

  • //
  • guest/
  • jeff_bowles/
  • perforce-triggers/
  • Binary.pl
  • View
  • Commits
  • Open Download .zip Download (3 KB)
#
# Example trigger to enforce a rule "files of suffix .x need to be checked
# in as type Z". (For example, ".gif" files must always be "binary" and ".sh"
# files should always be "text".)
#
# This script will do the following:
#	1. Return "success" (exit code 0) if the change has
#	   no applicable files in the changelist;
#	2. Otherwise, it checks that the files you've given are
#	   being submitted with the other component in that pair, and
#	   if not, reports an error.
#
# Unix usage:
#	perl /whatever/Pairs.pl %changelist% %serverport% %client%
# NT usage:
#       c:/perl/bin/perl  c:/whatever/Pairs.pl %changelist% %serverport% %client%
# (Note that the name of this script might need to be an "8.3" filename,
# depending on the version of Perl you're running.)
#
#
# Example 'triggers' section:
# (Note: since this is applicable to only filenames with certain suffixes, you
# might want to restrict the trigger to run when those files are submitted.
# So, if this is looking at .cpp/.h/.txt/.html lines, you might want to have
# it run only on those files.)
#    Triggers:
#	exampleB  //.../*.gif  "perl whatever/Binary.pl %changelist% %serverport% %client%"
#	exampleB  //.../*.bmp  "perl whatever/Binary.pl %changelist% %serverport% %client%"
#	exampleB  //.../*.sh   "perl whatever/Binary.pl %changelist% %serverport% %client%"
#
# Tested on Platforms:   FreeBSD, NT (as program, not service).
#
# Known bugs:
#	1. Perforce triggers have problems [at the moment] running when the
#	   Perforce server is running as an NT server.
#
# 
#

$ChangeNum = $ARGV[0];
$ServerPort = $ARGV[1];
$ClientName = $ARGV[2];
$p4 = "p4 -p $ServerPort -c $ClientName";

$nerrs = 0;
$MaxErrs = 10;
$OptimizeErrorOutput = "yes";

#-------- enter the suffixes for the pairs, here -----------------------------------
$filetype{"sh"} = ".*text";	# x.sh is text
	$errmsg{"sh"} = "filetype of .sh files should be text/xtext/ktext";
$filetype{"gif"} = ".*binary";	# x.gif is binary
	$errmsg{"gif"} = "filetype of .gif files should be binary";
#-----------------------------------------------------------------------------------

die "Changelist $ChangeNum (1st arg) needs to be numeric!\n" unless ($ChangeNum =~ /^\d+$/);
die "\%serverport\% (2nd arg) wasn't specified.\n" if ($ServerPort eq "");
die "\%clientname\% (3rd arg) wasn't specified.\n" if ($ClientName eq "");

@OpenedList = `$p4 opened`;	# cannot use 'p4 describe -s changenum', so we
				# get a list of open files this way...
#
# Algorithm used:
#	Pry out the suffix/type for each file being submitted, and pass it to "CheckType".
#	It'll pass back "-1" for each bad file type.
#
chomp(@OpenedList);
foreach (@OpenedList) {
	my($tmp);
	next unless /^(\/\/.*)#1 - .*\s$ChangeNum\s\((\S+)\)\s.*/;
	if (CheckType($1, $2, %filetype) < 0) {
		$nerrs++;
	}
	if ($nerrs >= $MaxErrs) {   
		print "*** Only first $MaxErrs errors shown\n";
		last;
	}
}

if ($nerrs > 0) {
	print "Errors found, submission refused\n";	# Note, this prints to STDOUT. See above.
	exit(1);
}
exit(0);

sub CheckType {
	my($fname, $filetype, %filetypes) = @_;
	my($suffix) = $1
		if ($fname =~ /^.*\.([^\.]+)$/);
	if ($1 ne "" && defined($filetypes{$suffix}) && $filetype !~ /^$filetypes{$suffix}/ ) {
		print "$fname: $errmsg{$suffix}.\n";
		return (-1);
	}
	return 0;
}
# Change User Description Committed
#3 105 Jeff Bowles deleting original names, in order to match the 'utils' area.
#2 91 Jeff Bowles Making sure that all output goes to <stdout> not <stderr>
#1 81 Jeff Bowles adding some trigger examples....