#!/opt/bin/perl # $Id: //guest/kelly_setzer/perforce/utils/review/p4review.pl#2 $ # vi:set nowrap,set ts=8 ## ## Copyright (C) 2000, Kelly Setzer ## This program is free software under the terms of the GNU General Public ## License. Copyright and license info are at the end of the file. ## ## In order to use this script, you need to change a few of the configuration ## settings to match your site. They are detailed, point-by-point below. ## ## This script was inspired by p4review.py, a perforce review daemon written. ## It attempts to be functionally similar to ease transition to or from ## the python version. Many of the configuration variables are the same, ## and it uses roughly the same logic for parsing through changes and jobs. ## ## Section 1: Setup ## 1) This script requires the perl Mail::Mailer package. You will need ## to install that first. Try running 'perl -MCPAN -e shell' as root. ## At the cpan> prompt, type 'install Mail::Mailer'. ## ## 2) $administrator ## Set this variable (at the beginning of the script below) to a valid ## email address that reaches your perforce administrator or some other ## person of authority. Additionally, perforce job reviews will come from ## this email address so that any replies will go to a reasonable place. ## ## Some email systems will not allow setting the from address or require ## additional configuration. For sendmail, you need to add a member to ## the T class. For example, if you are running this script as the user ## 'perforce', Add 'Tperforce' to your sendmail.cf. There should already ## be several members, like 'Troot' and 'Tdaemon'. You will need to ## restart sendmail by sending it a HUP signal. ## ## For qmail, you need to to alter the environment as described below: ## ## How do I set up user masquerading? I'd like my own From lines ## to show "The Boss" rather than god@heaven.af.mil. ## Answer: Add MAILHOST=af.mil, MAILUSER=boss, and MAILNAME='The ## Boss' to your environment. To override From lines supplied by ## your MUA, add QMAILINJECT=f to your environment. ## ## (from http://cr.yp.to/qmail/faq/appearance.html#user-masquerading ) ## ## 3) $p4 ## Set this to the full path of the perforce client program. Every ## call to perforce is done with this variable so that relocating ## the perforce client program is relatively easy. ## ## 4) $mailhost ## Set this to the hostname or IP address of your SMTP mail server. ## If you have a mail server running on the same server as this script, ## you can probably set it to 'localhost'. ## ## 5) $jobpath ## This is a bogus (well, it could be real) depot path that is used to ## identify users that are "subscribed" to the job reviews. To set this ## up, follow these steps. ## ## 1 - Set $jobpath to a nonexistent toplevel directory under your depot. ## //depot/jobs where 'depot' is your main depot would probably work. ## 2 - Edit your perforce user profile by typing 'p4 user'. At the ## bottom, you will need to add a 'Reviews:' section (which is ## documented in the user profile). You may already have a Reviews: ## section. You need to make sure that At least one of your review ## paths is the same as $jobpath or is a superset of $jobpath. ## ## 6) P4PORT, P4CLIENT, P4PASSWD ## These are environment variables that identify the perforce server and ## port, client and password. If any of these are already set when this ## script runs, they will override the settings below. ## ## Section 2: How do I? ## 1) How do I set this up to run periodically? ## On unix systems, use cron. Windows NT systems have something similar. ## If you use cron, you can use an entry like: ## ## 0 * * * * /web/iw/main/sbin/p4review > /dev/null 2>&1 & ## ## This would run p4review once an hour. ## ## 2) ## use strict; use Mail::Mailer; ## CONFIGURATION VARIABLES ## my $notify_changes = "not yet implemented"; my $notify_jobs = "not yet implemented"; my $bcc_admin = "not yet implemented"; my $send_to_author = "not yet implemented"; my $reply_to_admin = "not yet implemented"; my $administrator = 'hostmaster@yoursite.com'; my $p4 = '/opt/bin/p4'; my $mailhost = 'mailhost'; my $repeat = true; my $sleeptime = 60; my $limit_emails = "not yet implemented"; my $jobpath = '//depot/jobs'; $ENV{'P4PORT'} ||= 'perforce:1666'; $ENV{'P4USER'} ||= 'user'; $ENV{'P4CLIENT'} ||= 'client'; $ENV{'P4PASSWD'} ||= 'password'; ## INTERNAL VARIABLES ## my $re_p4review = 'Change\s+(\d+)\s+(\S+)\s+(<.*>)\s+\((.*)\)'; my $re_p4reviews = '(\S+)\s+<(.*)>\s+\((.*)\)'; if($repeat) { exec ("$0 &"); } LOOP: ## Iterate through each change and mail it to the proper reviewers foreach (p4command("review -t review")) { /$re_p4review/ or next; my $change = $1; my $desc = p4command("describe -s $change"); my @reviews = p4reviews("-c $change"); mailit("$4 $3","Perforce Change Review (#$change)",$desc,@reviews); p4command("counter review $change"); } ## Iterate through each open, unseen job and mail it to the reviewers my $lastjob = sprintf("job%06d",p4command("counter jobreview")); my @reviews = p4reviews($jobpath); foreach (p4command("jobs -e \"status=open Job>$lastjob\"")) { my ($jobnum) = split; my $jobdesc = p4command("job -o $jobnum"); mailit($administrator,"Perforce Job Review ($jobnum)",$jobdesc,@reviews); p4command("counter jobreview " . int(substr($jobnum,3))); } if($repeat) { sleep($sleeptime); goto LOOP; } sub mailit { my($from,$subject,$body,@to) = @_; my $mailer = Mail::Mailer->new(); $mailer->open({From=>$from,To=>\@to,Subject=>$subject}); print $mailer $body; $mailer->close() or bail_out("$subject"); } sub bail_out { die $_[0] . "\n"; } sub p4command { chomp(my @l = `$p4 $_[0]`); if (wantarray()) { return(@l); } elsif (defined wantarray()) { return(join("\n",@l)); } else { return 0; } } sub p4reviews { return (map { /$re_p4reviews/; "$2" } p4command("reviews " . shift)); } ## ## Copyright (C) 2000, Kelly Setzer ## ## Ingram Entertainment ## Two Ingram Blvd ## Box 833b ## Antioch, TN 37089 ## ## This program is free software; you can redistribute it and/or ## modify it under the terms of the GNU General Public License ## as published by the Free Software Foundation; either version 2 ## of the License, or (at your option) any later version. ## ## This program is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with this program; if not, write to the Free Software ## Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. ##