For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > April 2005 > keyword expansion script









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author keyword expansion script
CM Analyst

2005-04-27, 3:56 pm

I am struggling to modify an existing Perl script
that was originally written for a Unix version of
ClearCase (SCM tool).

The script is supposed provide "keyword expansion"
functionality for files that are checked into the SCM
system.

My task is to make the same script run on Windows and
I am not having too much luck.

The script compiles without errors but it is NOT
expanding and writing the keyword values to the file
when it is checked back into Clearcase.

Can anyone point me in right direction...

TIA.

Amad.


##############
use strict;
use warnings;

use Env;

##EJM begin added to fix warnings/errors
my $log_expand = 0;
my $fulldate = 0;
my $basename = 0;
my $newident = "\$Id: ...\$";
my $user = "amad";
##EJM End

my $copyright = "";

#if (-f "NO_EXPAND")
#{
# print "*** NOTICE *** - Not expanding
keywords...\n";
#exit 0;
#}

system("clearprompt proceed -mask proceed -prompt
\"Test to see if the script is firing...\"");

$log_expand=1;

#if(-f "NO_LOG_EXPAND" eq "No")
#{
$log_expand=0;
# print "*** NOTICE *** - Not expanding
\$Log\$keyword\n";
#}
my $c = "c:\\Program
Files\\Rational\\ClearCase\\bin\\clearto
ol.exe";

# create nice alias for ClearCase environment vars

my $opkind = $ENV{'CLEARCASE_OP_KIND'};
my $idstr = $ENV{'CLEARCASE_ID_STR'};
my $eltype = $ENV{'CLEARCASE_ELTYPE'};
my $file = $ENV{'CLEARCASE_XPN'};
my $comment = $ENV{'CLEARCASE_COMMENT'};
my $no_log = $ENV{'NO_LOG'};

#print "$file = $ENV{'CLEARCASE_XPN'}";
#print "$idstr = $ENV{'CLEARCASE_ID_STR'}";

# only process keywords for text_file
#
if ($eltype ne "text_file")
{
exit 0;
}

# make a standard date string
#
chop ($fulldate=`date '+%d-%h-%Y %H:%M'`);
chop ($copyright=`date '+%Y'`);
$copyright = "\$Copyright: (C) ".$copyright." My
Company, Inc. \$";


# remove all but the basename of $file
#

($basename = $file) = ~s@.*/@@;

# create an Id expansion based on the ClearCase
operation type
#
if ($opkind eq "checkin")
{
$newident = "\$Id: $basename\@\@$idstr \$";

exit 0;
}

#elsif ($opkind eq "checkout")
#{
# $newident = "\$Id: CHECKEDOUT $basename\@\@$idstr
\$";
#}

my $found_id = 0;
my $found_log = 0;

my $outfile = "$file.$$";
if (!open (IN, $file)) {

exit 0;
}
open (OUT, ">$outfile") || die "Cannot create out
file";

# read in the source file, expand keywords and write
the result
# back out
#

while (<IN> )
{
# expand the Id keyword
if ($found_id == 0 && /\$Id.*\$/)
{
s/\$Id.*\$/$newident/;
$found_id = 1;
}
print OUT $_;
}
# expand the Copyright keyword
#
if (/\$Copyright.*\$/) {
s/\$Copyright.*\$/$copyright/;
}
# for a checkin operation we look to expand
the Log keyword
#
if ($log_expand == 1) {
if (/\$Log\$/) {
if ($opkind eq "checkin") {
}
}
}

# remember all the stuff from the beginning of the
line to the Log keyword
# so that when we create a multiline expansion we can
prefix each of our
# lines with the same junk, presumably some language's
comment characters

(my $prefix) = /(^.*)\$Log\$/;

# close everthing up

close (IN);
close (OUT);

rename ($file, "$file.bak") || die "Cannot rename
$file to $file.bak";
rename ($outfile, $file) || die "Cannot rename files";
unlink ("$file.bak") || die "Cannot remove $file.bak";

if ($found_id == 0)
{
print "*** NOTICE - $file does not contain a \$Id\$
keyword\n";
}

if ($log_expand == 1 && $found_log == 0)

{
print "*** NOTICE - $file does not contain a
\$Log\$ keyword\n";

}

# tell ClearCase to continue

exit 0;



________________________________________
__________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
John W. Krahn

2005-04-27, 8:56 pm

CM Analyst wrote:
> I am struggling to modify an existing Perl script
> that was originally written for a Unix version of
> ClearCase (SCM tool).
>
> The script is supposed provide "keyword expansion"
> functionality for files that are checked into the SCM
> system.
>
> My task is to make the same script run on Windows and
> I am not having too much luck.
>
> The script compiles without errors but it is NOT
> expanding and writing the keyword values to the file
> when it is checked back into Clearcase.
>
> Can anyone point me in right direction...
>
> TIA.
>
> Amad.
>
>
> ##############
> use strict;
> use warnings;


Very good start. :-)


> use Env;


You are not using this module anywhere so why is it here?


> ##EJM begin added to fix warnings/errors
> my $log_expand = 0;
> my $fulldate = 0;
> my $basename = 0;
> my $newident = "\$Id: ...\$";
> my $user = "amad";


You do not use $user anywhere, are you sure you need it?


> ##EJM End
>
> my $copyright = "";
>
> #if (-f "NO_EXPAND")
> #{
> # print "*** NOTICE *** - Not expanding
> keywords...\n";
> #exit 0;
> #}
>
> system("clearprompt proceed -mask proceed -prompt
> \"Test to see if the script is firing...\"");
>
> $log_expand=1;
>
> #if(-f "NO_LOG_EXPAND" eq "No")


-f returns true or false, it will never be equal to the string "No".


> #{
> $log_expand=0;
> # print "*** NOTICE *** - Not expanding
> \$Log\$keyword\n";
> #}
> my $c = "c:\\Program
> Files\\Rational\\ClearCase\\bin\\clearto
ol.exe";


You do not use $c anywhere, are you sure you need it?


> # create nice alias for ClearCase environment vars
>
> my $opkind = $ENV{'CLEARCASE_OP_KIND'};
> my $idstr = $ENV{'CLEARCASE_ID_STR'};
> my $eltype = $ENV{'CLEARCASE_ELTYPE'};
> my $file = $ENV{'CLEARCASE_XPN'};
> my $comment = $ENV{'CLEARCASE_COMMENT'};
> my $no_log = $ENV{'NO_LOG'};


You do not use $comment or $no_log anywhere, are you sure you need them?


> #print "$file = $ENV{'CLEARCASE_XPN'}";
> #print "$idstr = $ENV{'CLEARCASE_ID_STR'}";
>
> # only process keywords for text_file
> #
> if ($eltype ne "text_file")
> {
> exit 0;
> }
>
> # make a standard date string
> #
> chop ($fulldate=`date '+%d-%h-%Y %H:%M'`);


You do not use $fulldate anywhere, are you sure you need it?


> chop ($copyright=`date '+%Y'`);
> $copyright = "\$Copyright: (C) ".$copyright." My
> Company, Inc. \$";


There is no need to call an external program to get the date and AFAIK those
won't work on Windows anyway:

use POSIX 'strftime';

my $fulldate = strftime '%d-%h-%Y %H:%M', localtime;
my $copyright = strftime '$Copyright: (C) %Y My Company, Inc. $', localtime;


> # remove all but the basename of $file
> #
>
> ($basename = $file) = ~s@.*/@@;


use File::Basename;

my $basename = basename( $file );


> # create an Id expansion based on the ClearCase
> operation type
> #
> if ($opkind eq "checkin")
> {
> $newident = "\$Id: $basename\@\@$idstr \$";
>
> exit 0;
> }
>
> #elsif ($opkind eq "checkout")
> #{
> # $newident = "\$Id: CHECKEDOUT $basename\@\@$idstr
> \$";
> #}
>
> my $found_id = 0;
> my $found_log = 0;
>
> my $outfile = "$file.$$";
> if (!open (IN, $file)) {
>
> exit 0;
> }
> open (OUT, ">$outfile") || die "Cannot create out
> file";
>
> # read in the source file, expand keywords and write
> the result
> # back out
> #
>
> while (<IN> )
> {
> # expand the Id keyword
> if ($found_id == 0 && /\$Id.*\$/)
> {
> s/\$Id.*\$/$newident/;
> $found_id = 1;
> }


There is no need to use the same regular expression twice:

if ($found_id == 0 && s/\$Id.*\$/$newident/)
{
$found_id = 1;
}


> print OUT $_;
> }
> # expand the Copyright keyword
> #
> if (/\$Copyright.*\$/) {
> s/\$Copyright.*\$/$copyright/;
> }


The while loop has ended so the only thing in $_ is the last line from $file
and even if you changed that it is not printed out anywhere. And again, there
is no need to test the match before doing the substitution, just do the
substitution.


> # for a checkin operation we look to expand
> the Log keyword
> #
> if ($log_expand == 1) {
> if (/\$Log\$/) {
> if ($opkind eq "checkin") {
> }
> }
> }


This block of code is not doing anything?


> # remember all the stuff from the beginning of the
> line to the Log keyword
> # so that when we create a multiline expansion we can
> prefix each of our
> # lines with the same junk, presumably some language's
> comment characters
>
> (my $prefix) = /(^.*)\$Log\$/;


You do not use $prefix anywhere, are you sure you need it?


> # close everthing up
>
> close (IN);
> close (OUT);
>
> rename ($file, "$file.bak") || die "Cannot rename
> $file to $file.bak";
> rename ($outfile, $file) || die "Cannot rename files";
> unlink ("$file.bak") || die "Cannot remove $file.bak";
>
> if ($found_id == 0)
> {
> print "*** NOTICE - $file does not contain a \$Id\$
> keyword\n";
> }
>
> if ($log_expand == 1 && $found_log == 0)
>
> {
> print "*** NOTICE - $file does not contain a
> \$Log\$ keyword\n";
>
> }
>
> # tell ClearCase to continue
>
> exit 0;



John
--
use Perl;
program
fulfillment
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com