Create integration batch file for Perforce using PERL

Hello
We use Perforce and are constantly having to create batch files for branching reasons (branching = integration in Perforce).
The current process is fairly tedious and seems like a good candidate for some PERL functionality. So, let's see if I can 'splain it Lucy:

On a Free BSD UNIX box I create a text file with the following:
"p5 pkg 021638 > 021638.txt"
Where "021638" is our Change request number, although it's frequently referenced as "21638" so a check for the leading zero would be helpful.

The contents of 021638.txt contains (partial cut & paste):
//system/dev/DevMaintBranch/ECS/WebCode/bin/course/cust/terc/BioFormPreview.learn
//system/dev/DevMaintBranch/ECS/WebCode/bin/course/cust/terc/BioFormView.learn
//system/dev/DevMaintBranch/ECS/WebCode/bin/course/cust/terc/FormIncludes/Edit_ASLP.inc
//system/dev/DevMaintBranch/ECS/WebCode/bin/course/cust/terc/FormIncludes/Edit_ESTU.inc

Normally, I mail this file to myself and change the entries as follows:
p4 integ -b DevMaintBranch -s "//system/dev/DevMaintBranch/ECS/WebCode/bin/course/cust/terc/BioFormPreview.learn" //...
p4 integ -b DevMaintBranch -s "//system/dev/DevMaintBranch/ECS/WebCode/bin/course/cust/terc/BioFormView.learn" //...
p4 integ -b DevMaintBranch -s "//system/dev/DevMaintBranch/ECS/WebCode/bin/course/cust/terc/FormIncludes/Edit_ASLP.inc" //...
p4 integ -b DevMaintBranch -s "//system/dev/DevMaintBranch/ECS/WebCode/bin/course/cust/terc/FormIncludes/Edit_ESTU.inc" //...

So basically, I add
p4 integ -b DevMaintBranch -s "
to the beginning and
" //...
to the end of the line.
In "p4 integ -b DevMaintBranch -s ", the "DevMaintBranch" portion is derived from the path of the file (//system/dev/*DevMaintBranch*/ECS/...)

Seems easy enough for PERL...I would really appreciate any help you may be able to provide.  I'd like to simply call the perl script with the change number and end up with a batch file (for WinXP) that performs the Perforce integrations.

Thank you!
jwcorbettAsked:
Who is Participating?
 
FishMongerCommented:
>> $batfile = '$ARGV[0].bat'

There are 2 problems with that, 1) since you're using single quotes, you won't get the variable interpolation, 2) you're using the number prior to formating it with leading zeros.  So, instead do this:

$batfile = "$chgnum.bat";
0
 
FishMongerCommented:
This can be expanded and improved, but based on what I think you're wanting, this should do the job.


#!/usr/bin/perl -w

unless (@ARGV) { die "You did not enter the change number\n" }

if ($ARGV[0] !~ /\D/) {
   $file = sprintf("%0.6d.txt", $ARGV[0]);
}
else {
   die "The change number contains invalid charactures\n";
}


$batfile = 'p4integ.bat';

open IN, $file or die "could not open $file <$!>";
open OUT, ">$batfile" or die "could not open $batfile <$!>";

while (<IN>) {
   if (s!(//system/dev/)([^/]+)(.*)!p4 integ -b $2 -s "$1$2$3" //...!) {
      print OUT;
   }
}
0
 
jwcorbettAuthor Commented:
That's a good start, but where does the text file get its content created for PERL to parse?
Right now, I create the file content using "p5 pkg 021638 > 021638.txt"
Should I just use a 'system' command?
something like "system p5 pkg $ARGV > $file" after the "open IN"?
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
jwcorbettAuthor Commented:
Also, the open IN is failing to open the $file (021638.txt) stating no such file or directory...
0
 
FishMongerCommented:
I was making the assumption that the text file was already created, which is why the open IN failed.  See if these minor changes does what you want.

if ($ARGV[0] !~ /\D/) {
   $chgnum = sprintf("%0.6d", $ARGV[0]);
   $file = "$chgnum.txt";
}
else {
   die "The change number contains invalid charactures\n";
}

system "p5 pkg $chgnum > $file";
$batfile = 'p4integ.bat';
0
 
jwcorbettAuthor Commented:
I've already worked something like that out, but I'd like to figure out how to name the batch file after the change number....so instead of "$batfile = 'p4integ.bat'" I'd like to have it called 021638.bat is 021638 is the change number.
I don't have my PERL book with me, but using $batfile = '$ARGV[0].bat' creates a file called "$ARGV[0].bat" instead of 021638.bat....
I'll try using your $chgnum example....
0
 
jwcorbettAuthor Commented:
Fishmonger,
You can mong my fish anytime!
-J
0
 
FishMongerCommented:
Glad I was able to help. :)
0
 
jwcorbettAuthor Commented:
FishMonger!
Is there a way to terminate the lines that would be more suited towards a Windows batch file?  Perhaps a \n ?
It's all working, just the line endings are still UNIX...
0
 
FishMongerCommented:
There are several (at leat 3) ways to do the conversion.  1) use a regular expression to substitute the line endings.  2) use chomp at the begining of the while loop to remove the line ending, then add it back in the print statement.  3) after the while loop, use the unix2dos utility to convert the line endings.

1)
while (<IN>) {
   if (s!(//system/dev/)([^/]+)(.*)!p4 integ -b $2 -s "$1$2$3" //...!) {
      s/\n$/\r\n/;
      print OUT;
   }

2)
while (<IN>) {
   chomp;
   if (s!(//system/dev/)([^/]+)(.*)!p4 integ -b $2 -s "$1$2$3" //...!) {
      print OUT "$_\r\n";
   }

3)
system "unix2dos $batfile $batfile";
0
 
jwcorbettAuthor Commented:
FishMonger,
You are too good.
I chose option 3, and it works like a champ (after I installed UNIX2DOS ;-)
Thanks again!
-J
0
 
kanduraCommented:
yet another way to do line endings:

{
  local $/ = "\r\n";
  print OUT "$_$/";
}

(which is one of the reasons I always use $/ to print a linefeed.)
0
 
jwcorbettAuthor Commented:
Nice kandura, I'll keep that in mind.

FishMonger,
One more thing, could you explain your expression:
if (s!(//system/dev/)([^/]+)(.*)!p4 integ -b $2 -s "$1$2$3" //...!)

I'm still trying to get a handle on regular expressions and I'm a bit confused on this one.
Also, is there a way to go back through the "while (<IN>)" loop?
I'd like to write to the file, doing something along the same lines as the "p4 integ", but use "p4 resolve" and put the results in the batch file after the integ portion.

Thanks for all your help so far, you've no idea how much you've helped out!
-J
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.