| |||||||
Chapter 11 Scripting Perforce: Daemons & Triggers | |||||||
User-written scripts can greatly enhance Perforce's functionality. There are three primary methods of scripting Perforce:
Wrappers are scripts that call Perforce commands. Daemons run at predetermined times, looking for changes to the Perforce metadata. When a daemon determines that the state of the depot has changed in some useful way, it runs other commands. For example, a daemon might look for newly submitted changelists and send email to users who have previously updated the files that were submitted in those changelists. Perforce provides a number of tools that make daemon-writing easier. Pre-submit triggers are scripts that Perforce has been told to run whenever particular files are submitted. A trigger script returns a value to Perforce that determines whether or not the submit should be performed. For example, you might write a script that watches for a particular executable file to be submitted; when this file is submitted, the trigger script might tell the submit to fail if the release notes file has not been updated within the same changelist. This chapter assumes that you know how to write scripts. Since Perforce provides nothing in particular to help you write wrappers, wrappers are not discussed here.
| |||||||
Using Triggers | |||||||
A pre-submit trigger is a script called by Perforce when files that you've specified have been submitted. If the script returns a value of 0, the submit continues. If it returns any other value, the submit fails. Triggers might be used for any of the following:
Each line in the trigger table has three fields:
The script must be specified in a way that allows the p4d server account to find the file. You can place the directory of the script in the p4d server's path, or specify the full path name of the script within the trigger table.
| |||||||
|
The development group wants to make sure that whenever a .exe file is submitted to the
depot that the release notes for the program are submitted at the same time. Ed is assigned
to write a trigger script that does this. He writes a script that takes a changelist number as
its only argument, does a p4 opened on the changelist, parses the results to find the files
included in the changelist, and ensures that for every executable file that's been
submitted, a RELNOTES file in the same directory has been submitted. If this is true, the
script exits 0; otherwise it exits 1. He then adds this trigger script to the trigger table:
Whenever an .exe file is submitted, this trigger is run. If Ed's script fails, it returns a non-0 value to Perforce, and the submit is aborted. The triggers are run in the order entered in the triggers table. If multiple filepatterns are meant to trigger the same script, you should create multiple triggers with the same name, the same script, and different filepatterns. Some trigger scripts will need to know which files are included in the changelist. Since p4d can only pass 1K of data to a trigger script, the file list can't be passed via the trigger. Use p4 opened -ac changelist# in your trigger scripts to get the list of files for the changelist number provided as an argument. The actual contents of the files are not accessible from within the trigger script, since the files are not stored in the depot until the submit completes. Before p4 submit calls the trigger script, it creates the changelist, assigns it a number, and locks the files in the changelist. After the trigger script is run, p4 submit may run subsequent trigger scripts that cause the submit to fail. For this reason, trigger scripts should not take any actions that assume the submit will succeed. Trigger scripts are meant primarily for changelist validation; if you need to take particular actions based on the success of a submit, use a daemon.
| ||||||
Using Daemons | |||||||
Daemons are scripts that are called by the server OS at preset times. Daemons that utilize Perforce usually work by examining the server metadata as often as needed and taking action as often as necessary. At Perforce Software, we use three daemons:
Perforce's Review DaemonThe change review daemon and the jobs daemon described above are included in a single script that can be downloaded from http://www.perforce.com/perforce/loadsupp.html . This script runs under Python, which can be retrieved from http://www.python.org/. Before running the script, please be sure to read and follow the configuration instructions included the script itself.The change review portion of the daemon looks at the files included in each newly-submittted changelist and emails those users who have `subscribed' to any of the files included in the changelist, letting those users know that the file(s) they're interested in have changed. Users subscribe to files by calling p4 user and entering filepatterns on the resulting form:
An interested user should enter her email address in the Email: field, and enter any number of filepatterns corresponding to the files she's interested in into the Reviews: field. The change review portion of the change daemon implements the following scheme:
| |||||||
|
Daemon CommandsCertain Perforce commands will be used almost exclusively by review daemons. These commands are:
| ||||||
Creating Other Daemons | |||||||
You can use p4review.py as a starting point to create your own daemons, changing it as needed. As an example, another daemon might upload Perforce job information into an external bug tracking system after changelist submission. It would use the p4 review command with a new review counter to list new changelists, and use p4 fixes to get the list of jobs fixed by the newly submitted changelists. This information might then be fed to the external system, notifying it that certain jobs have been completed.
If you do write a daemon of your own, and would like to share it with other users, you can submit it to the Perforce Public Depot.
| |||||||
| |||||||
Please send comments and questions about this manual to
[email protected]. Copyright 1997, 1999 Perforce Software. All rights reserved. Last updated: September 15, 1999. Manual version 99.1.ug.4. |