Solved

Create integration batch file for Perforce using PERL

Posted on 2004-09-01
13
351 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
  • 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
 
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

705 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now