H
Hike Mike
I want to determine if there any of the following strings in a java
file that do not exist as part of a comment:
System.out.print
System.err.print
printStatckTrace
I'm totally new at Perl and I wrote the following script that get's
called from CVS when developers check in code (the script gets called
with a list of java files starting at $ARGV[1]. The idea is to not
allow any uncommented strings of the above type into the repository
unless they are inside comments.
It does not work for instances of a dis-allowed string that is on a
newline inside of a comment:
/* blah blah blah
System.out.println("foo");
*/
basically the problem is the following match code:
unless ($item =~ m/^((\/\/+)|(\/\*+)|(\*+))/) {
terminate($_, $stringToCheck);
thanks
------
#!/usr/bin/perl -w
use Socket;
use CGI qw
standard escape);
use strict;
######### That's all you have to do! ###########
# #
# #
# You shouldn't need to edit anything below #
# here! #
################################################
my $sArg = "";
shift @ARGV;
foreach( @ARGV )
{
#print "received ARGV: $_\n";
if ($_ =~ m/java/) {
#print "received java file: $_\n";
$sArg .= $_ . " ";
}
}
chop( $sArg );
if ($sArg) {
print "CVS commitinfo is checking .java files for disallowed code. If
your commit fails, check for un-commented 'System.out.
print', 'System.err.print', or 'printStackTrace()' in: $sArg \n";
}
my @rgFiles = split(" ", $sArg);
my $SystemOut = "System.out.print";
my $SystemErr = "System.err.print";
my $StackTrace = "printStackTrace";
my @badFiles;
my @rawData;
my $counter = 0;
my @instances;
my $item;
foreach( @rgFiles )
{
check($_, $SystemOut);
check($_, $SystemErr);
checkStackTrace($_, $StackTrace);
}
sub check {
my($file, $stringToCheck) = @_;
open(FILE, $file) || return ("could not open file to verify on
commit: $file\n");
@rawData=<FILE>;
@instances = grep(/$stringToCheck/, @rawData);
#print "file " . $_ . " has " . @instances . " elements.\n";
foreach $item (@instances) {
$item = trim($item);
unless ($item =~ m/^((\/\/+)|(\/\*+)|(\*+))/) {
terminate($_, $stringToCheck);
}
}
#if (@instances > 0) {
#terminate($_, $stringToCheck);
#}
close(FILE);
}
sub checkStackTrace {
my($file, $stringToCheck) = @_;
open(FILE, $file) || return ("could not open file to verify on
commit: $file\n");
@rawData=<FILE>;
@instances = grep(/$stringToCheck\({1}\){1}/, @rawData);
#print "file " . $_ . " has " . @instances . " elements.\n";
foreach $item (@instances) {
$item = trim($item);
unless ($item =~ m/^((\/\/+)|(\/\*+)|(\*+))/) {
terminate($_, $stringToCheck);
}
}
close(FILE);
}
sub terminate {
my($file, $badString) = @_;
die "Aborting CVS commit because file $file contains the un-commented
code string: $badString\n";
}
sub trim {
my @out = @_;
for (@out) {
s/^\s+//;
s/\s+//;
}
return wantarray ? @out : $out[0];
}
file that do not exist as part of a comment:
System.out.print
System.err.print
printStatckTrace
I'm totally new at Perl and I wrote the following script that get's
called from CVS when developers check in code (the script gets called
with a list of java files starting at $ARGV[1]. The idea is to not
allow any uncommented strings of the above type into the repository
unless they are inside comments.
It does not work for instances of a dis-allowed string that is on a
newline inside of a comment:
/* blah blah blah
System.out.println("foo");
*/
basically the problem is the following match code:
unless ($item =~ m/^((\/\/+)|(\/\*+)|(\*+))/) {
terminate($_, $stringToCheck);
thanks
------
#!/usr/bin/perl -w
use Socket;
use CGI qw
use strict;
######### That's all you have to do! ###########
# #
# #
# You shouldn't need to edit anything below #
# here! #
################################################
my $sArg = "";
shift @ARGV;
foreach( @ARGV )
{
#print "received ARGV: $_\n";
if ($_ =~ m/java/) {
#print "received java file: $_\n";
$sArg .= $_ . " ";
}
}
chop( $sArg );
if ($sArg) {
print "CVS commitinfo is checking .java files for disallowed code. If
your commit fails, check for un-commented 'System.out.
print', 'System.err.print', or 'printStackTrace()' in: $sArg \n";
}
my @rgFiles = split(" ", $sArg);
my $SystemOut = "System.out.print";
my $SystemErr = "System.err.print";
my $StackTrace = "printStackTrace";
my @badFiles;
my @rawData;
my $counter = 0;
my @instances;
my $item;
foreach( @rgFiles )
{
check($_, $SystemOut);
check($_, $SystemErr);
checkStackTrace($_, $StackTrace);
}
sub check {
my($file, $stringToCheck) = @_;
open(FILE, $file) || return ("could not open file to verify on
commit: $file\n");
@rawData=<FILE>;
@instances = grep(/$stringToCheck/, @rawData);
#print "file " . $_ . " has " . @instances . " elements.\n";
foreach $item (@instances) {
$item = trim($item);
unless ($item =~ m/^((\/\/+)|(\/\*+)|(\*+))/) {
terminate($_, $stringToCheck);
}
}
#if (@instances > 0) {
#terminate($_, $stringToCheck);
#}
close(FILE);
}
sub checkStackTrace {
my($file, $stringToCheck) = @_;
open(FILE, $file) || return ("could not open file to verify on
commit: $file\n");
@rawData=<FILE>;
@instances = grep(/$stringToCheck\({1}\){1}/, @rawData);
#print "file " . $_ . " has " . @instances . " elements.\n";
foreach $item (@instances) {
$item = trim($item);
unless ($item =~ m/^((\/\/+)|(\/\*+)|(\*+))/) {
terminate($_, $stringToCheck);
}
}
close(FILE);
}
sub terminate {
my($file, $badString) = @_;
die "Aborting CVS commit because file $file contains the un-commented
code string: $badString\n";
}
sub trim {
my @out = @_;
for (@out) {
s/^\s+//;
s/\s+//;
}
return wantarray ? @out : $out[0];
}