Solved

Create integration batch file for Perforce using PERL

Posted on 2004-09-01
13
356 Views
Last Modified: 2010-05-18
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!
0
Comment
Question by:jwcorbett
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
13 Comments
 
LVL 28

Expert Comment

by:FishMonger
ID: 11956243
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
 

Author Comment

by:jwcorbett
ID: 11956501
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
 

Author Comment

by:jwcorbett
ID: 11956522
Also, the open IN is failing to open the $file (021638.txt) stating no such file or directory...
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 28

Expert Comment

by:FishMonger
ID: 11957491
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
 

Author Comment

by:jwcorbett
ID: 11957516
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
 
LVL 28

Accepted Solution

by:
FishMonger earned 500 total points
ID: 11957589
>> $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
 

Author Comment

by:jwcorbett
ID: 11957592
Fishmonger,
You can mong my fish anytime!
-J
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 11957636
Glad I was able to help. :)
0
 

Author Comment

by:jwcorbett
ID: 11957907
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
 
LVL 28

Expert Comment

by:FishMonger
ID: 11958253
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
 

Author Comment

by:jwcorbett
ID: 11958274
FishMonger,
You are too good.
I chose option 3, and it works like a champ (after I installed UNIX2DOS ;-)
Thanks again!
-J
0
 
LVL 18

Expert Comment

by:kandura
ID: 11959343
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
 

Author Comment

by:jwcorbett
ID: 11969020
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Syslog text file into database or into .CSV 19 177
Perl script to parse log and delete the file 17 179
Perl, group, sort, count question 6 149
compress files in RAR using perl 13 89
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question