[p4] Writing triggers with p4.net ?
Jeff A. Bowles
jab at pobox.com
Wed Jul 1 08:01:14 PDT 2009
Read up on triggers using simple examples, first. You're making a
couple of mistakes: (1) Don't try to change a file to be checked-out
or labeled or anything like that, as part of the trigger script. (2)
What is that triple-slash in those pathnames?
In general, think of any trigger (there is one trigger subtype that is
an exception to this, but "checking consistency during submits" is not
one of those) as having NO side-effects except the return-code and
error-string. The most a trigger should do, to assure that it doesn't
confuse the server or you, is return that something is allowed or not
allowed. (What if a second trigger runs on the same files for the same
submit, and fails? Shouldn't the end-result be the same as if they
had never been run, since the submit is failed? That is one of the
two main reasons for "no side effects.")
A trigger to check that files exist, or are being submitted together,
etc, should not need to connect to Perforce except -- perhaps -- to
run "p4 describe -s 12312312" or "p4 files @=12312312" for a specific
changelist.
You return success/fail with a zero/non-zero status; error messages
in the output are copied to the user.
Do yourself a favor. Write the following 2-3 simple triggers, first:
- "files whose name match //main.br/WebSite/....bak will hit a
trigger that immediately exits with a fail-status." (This is a simple
one, not useful for a real environment, but it will show you that
triggers are really a two-step mechanism-- the entry in the triggers
table that matches the filenames you want to run the trigger against
(and when/why), and the source code. The source code for this one
will be, likely, 1-3 lines.
- "If you did not include the word 'fixed' in the change description
for files checked into //main.br/WebSite/..., it'll fail." (Do it as a
separate trigger, to be able to easily compare. The source code for
this will include a single connection to Perforce and a single command
that it then parses the output of.)
- "If you checked in x.java but not x.class, the trigger fails; if you
did, it succeeds. This goes both ways - x.class requires
x.java." (This is the same as the previous one, but you need the file-
list and need to match up filenames to subordinate files. This is
where the Perl/Python/Ruby dictionary-lookup hash arrays are so handy,
since you can just make an entry for each filename in the changelist
and then use that dictionary-lookup hash array as a way to QUICKLY
answer the question, "is this filename on the list?" as you rescan the
list making sure the matches are included in the change.
The above three are fairly easy, and will lead you to understand the
steps a bit more.
Lastly: if you have more than a small number of developer work/files
in those Perforce databases of yours, let's say enough that your check-
point tales more than 5-10 minutes, try to avoid adding trigger-table
and protect-table entries on the production server until you've slept
with the docs for a while. There's a particularly expensive way you
could accidently configure a trigger-table or a protect-table to eat
your performance, and you'll want to avoid that on larger servers.
-Jeff Bowles
(ps. I'm referring to "double-wild cards" in the protect-table and
"different, multiple triggers with the SAME expensive prologue, run
against the same changelist for the same event." There are
workarounds for both cases.)
On Jul 1, 2009, at 7:30 AM, Naim Akbar wrote:
> Thanks for your reply..
>
> I have tried using P4.net. I have also created a trigger on the file
> I am intrested in. If I add some exception handlers in the code,
> then the file submits ok. If I leave out exception handling then I
> get the message " Submition failed ", 'Trig validation failed: No
> error message". Plese see my trigger and the code below. I have also
> added a file writer class, but nothing gets written which makes it
> seem this bit of the code is not hit.
>
> What is wrong with the way I am doing it.?
> Why is there no file output...Is this part of code being hit?
> What do I have to return to indicate pass or fail ?
> How do I raise an error message?
>
>
> Here is my trigger
>
> Trig change-submit "///Misc Tools/PerforceClassLibrary/
> PerforceClassLibrary/PerforceClassLibrary/Properties/
> AssemblyInfo.cs" "c:\debug\ConsoleApplication1.exe %changelist%"
>
> Here is my main class
>
> class Program
> {
> static void Main(string[] args)
> {
>
> FileInfo t = new FileInfo("C:\Trigger.txt");
> StreamWriter Tex = t.CreateText();
> Tex.WriteLine(args[0]);
> Tex.WriteLine("Collin has launced another article");
> Tex.WriteLine("csharpfriends is the new url for c-sharp");
> Tex.Write(Tex.NewLine);
> Tex.Close();
>
> Perforce f;
> f = new Perforce("Naim", "MyCom");
> f.CreateConnection();
> string[] files = new string[2];
> files[0] = "///main.br/WebSite/WebUI/MINT Version.txt";
> files[1] = "///main.br/WebSite/WebUI/WebProfile.cs";
> int number = f.OpenFileForEdit(files, "This is a change to the
> following files"); //proven method that opens these two files for edit
>
> }
> }
>
> Thanks,
> Naeem
>
>
> > Date: Wed, 1 Jul 2009 07:31:43 -0400
> > From: steve at vance.com
> > To: naim_akbar at hotmail.com
> > CC: jab at pobox.com; perforce-user at perforce.com
> > Subject: Re: [p4] Writing triggers with p4.net ?
> >
> > Sure you can use .NET. You can use any language that can be run
> like an
> > executable. There is a COM API which I think can be called
> from .NET.
> > You can get that on the Perforce web site.
> >
> > As for examples, take a look at the Perforce Public Depot. Jeff
> Bowles
> > has excellent examples in //guest/jeff_bowles/... and many other
> people
> > have examples, probably some in .NET. There have been conference
> papers
> > on how to write triggers and scripts (see the presentations by
> Jeff and
> > I from 2005). Perforce and the PCTs also have a scripting class
> for a
> > more individual bit of guidance.
> >
> > Steve
> >
> > Naim Akbar wrote:
> > > So you cant use P4.NET to right triggers?
> > >
> > >
> > >
> > > If I was to use Perl, then where would I start. Ome sample code
> would be useful.
> > >
> > >
> > >
> > > Many thanks,
> > >
> > >
> > >
> > >
> > >
> > > Date: Tue, 30 Jun 2009 15:15:36 -0700
> > > Subject: Re: [p4] Writing triggers with p4.net ?
> > > From: jab at pobox.com
> > > To: naim_akbar at hotmail.com
> > > CC: perforce-user at perforce.com
> > >
> > > I'd use Perl, if I was at the level of asking where to start.
> > >
> > >
> > > I like Python / Ruby better, but you can quickly and easily do it.
> > >
> > >
> > > Step #1: write the trigger table entry, to fire ONLY on submits
> of files that match certain patterns. That will do the "scan" to see
> if the submit affects any part of a list of files.
> > >
> > >
> > > Step #2: Start with a trivial script that just writes its
> arguments to a debug-log somewhere and returns a SUCCESS error code.
> (See the docs or "p4 help triggers" for that.)
> > >
> > >
> > > Step #3: Change the script to look for subordinate files, using
> whatever rules you like. ("any checkin of x.java must also include
> x.reference.logs" is a good, trivial example).
> > >
> > >
> > > Step #4: Document this. Make sure the trigger(s) are backed up
> as part of your backups.
> > >
> > >
> > > Step #5: Try the ugly testing process: "what if x.reference.logs
> is submitted, as above, but not the java file?"
> > >
> > >
> > > I would not do it in a compiled language like Java or C or C++,
> since the number of steps / overhead to iterate over the programming
> is higher and because it might make other assumptions about the
> environment. (Java: classpath; C or C++: DLLs. Perl: weird CPAN
> libraries. And so on) So, mind your dependencies.
> > >
> > >
> > > -Jeff Bowles
> > > ---
> > > Jeff Bowles - jeff at vizim.com
> > >
> > >
> > >
> > > On Tue, Jun 30, 2009 at 2:17 PM, Naim Akbar <naim_akbar at hotmail.com
> > wrote:
> > >
> > >
> > > Hi there,
> > >
> > >
> > >
> > > I was just wandering whether it is possible to right perforce
> triggers with P4.Net. If so, how would I call this assembly from
> within the triggers table. If not what other scripting langauge do I
> use...
> > >
> > >
> > >
> > > My goal is: On a perforce submit of a file, I want to check if
> this file is in a list of particular files. If it is then I would
> like to submit a few other files. If not, then do nothing and carry
> on.
> > >
> > > Many thanks,
> > >
> > >
> > >
> > >
> > >
> > > _________________________________________________________________
> > > Get the best of MSN on your mobile
> > > http://clk.atdmt.com/UKM/go/147991039/direct/01/
> > > _______________________________________________
> > > perforce-user mailing list - perforce-user at perforce.com
> > > http://maillist.perforce.com/mailman/listinfo/perforce-user
> > >
> > >
> > > _________________________________________________________________
> > > Share your photos with Windows Live Photos – Free.
> > > http://clk.atdmt.com/UKM/go/134665338/direct/01/
> > > _______________________________________________
> > > perforce-user mailing list - perforce-user at perforce.com
> > > http://maillist.perforce.com/mailman/listinfo/perforce-user
> > >
> > >
> >
> > --
> > Stephen Vance
> > www.vance.com
> >
>
> Beyond Hotmail — see what else you can do with Windows Live. Find
> out more.
More information about the perforce-user
mailing list