Perforce Training, 2010.1
1
Scripting Perforce
Introduction
• IntroductionsIntroductions
• Class Schedule
• About the Exercises
Perforce Training, 2010.1
2
Intended course audience
• Experienced Perforce users who need to• Experienced Perforce users who need to write scripts
• Configuration management, build, and release engineers, and others
Prerequisite experience
P f d d i i t t• Perforce user and administrator
• Writing scripts using Perl
Perforce Training, 2010.1
3
Objectives
D l i t t f t k• Develop scripts to perform common tasks
• Follow best practices
Uses of scripts
• Web browser interface to Perforce
• Plug-in for an IDE
• Back-up and recovery routines
• Wrappers for Perforce commands
• Customized diff and merge toolsCusto ed d a d e ge too s
• Conversion from other SCM tools
Perforce Training, 2010.1
4
Script examples in this course
• Automating processesAutomating processes
• Generating reports
• Sending email
• Process support
• Auditing metadata
Course Contents
• Preliminary Decisions• Perforce Commands for Scripting• Command Line Program (p4) • Application Program Interface (API)• Daemons • T i• Triggers• Wrappers and Administrative Scripts
Perforce Training, 2010.1
5
Preliminary Decisions
• Setting Environment Variables• Setting Environment Variables
• User Authentication
• Client Program Interface
Preliminary Decisions
Setting Environment Variables
Perforce Training, 2010.1
6
Use global options
• Set environment variables in scriptSet environment variables in scriptp4 –p serverport –u username info
• Advantage: Overrides all other settings
• Disadvantage: Edit each script to change
Use configuration files
• Set P4CONFIG to a file namep4 set P4CONFIG=config.txt
• Place config.txt files in script folders
• List environment variables in config.txtP4PORT=lemon:1909P4CLIENT=script_user_reporterP4USER=script_user
Perforce Training, 2010.1
7
P4CONFIG example
C:\scripts
Environment Variable:Environment Variable:P4CONFIG=config.txtP4CONFIG=config.txt
P4PORT=krypton:1666
P4PORT=xenon:1666P4CLIENT=report-writerP4USER=reporterconfig.txtconfig.txt
\Krypton
config.txtconfig.txt
P4PORT=krypton:1666P4CLIENT=scriptuser-prodP4USER=scriptuser
\Xenon
Using P4CONFIG
• Advantages• Configures many scripts• Active in specific directories
• Disadvantage• If P4CONFIG unset script environment mayIf P4CONFIG unset, script environment may
change
Perforce Training, 2010.1
8
Use environment variables
• Set environment variables (Windows)p4 set P4PORT=p4demo.com:1666
• List environment variablesp4 setP4CLIENT=bruno ws (set)C b u o_ s (set)P4PORT=p4demo.com:1666 (set)P4USER=bruno (set)
Using environment variables
• Advantage: Easy to setg y
• Disadvantages
• May be accidentally changed
• Depends upon shell being usedDepends upon shell being used
Perforce Training, 2010.1
9
Configuration order of precedence
• Command-line flags
• P4CONFIG files
• Environment variable settings
• Registry variable settings (on Windows)
• Default value for environment variables
Preliminary Decisions
User Authentication
Perforce Training, 2010.1
10
Use login
• Works for all server security levelsWorks for all server security levels
• Usage:p4 loginEnter password:U i t l d iUser script_user logged in.
Use a group to extend session
Group: scriptsonly
p4 group scriptsonly
MaxResults: 100000
Maxscanrows: 500000
MaxLockTime: 30000
Timeout: unlimited
Subgroups:
OOwners: bruno
Users:
script_user
Perforce Training, 2010.1
11
• Security levels 0-2
Use a password
p4 -P Script_pass users
• Security level 3p4 login -pEnter password: 9CC3EACDEFCC8C7020C134D6D333F46Fp4 -P 9CC3EACDEFCC8C7020C134D6D333F46F users
Preliminary Decisions
Client Program Interface
Perforce Training, 2010.1
12
Comparing client program interfaces
Command Line Program Application Program Interfaceg pp g
p4 Perforce C/C++ API
One connection per command
User controls connection
Returns lines of text Tagged data available
Some language support Language derivatives availableJava-native API (P4Java)Objective-C API (P4ObjC)
Tagging output: Command line and API
• Command line returns lines of text
• Format output by using -ztagp4 -ztag clients... client bruno_ws... Update 1104271684... Access 1104340062... etc.... etc.
• Perforce API supports tagged data output
Perforce Training, 2010.1
13
Command line program: bypass an editor
• Redirect to standard outputRedirect to standard outputp4 change -o
• Read from standard inputp4 submit -ip4 submit i
Command line program: Using Python
• Create Python marshalled dictionary objectp4 -G job -o
Perforce Training, 2010.1
14
#!/usr/local/bin/python#Example script named readmarshal.py
import marshal, systrue = 1
Sample Python Script and p4 -G Output
true = 1try:
while true:vars = marshal.load(sys.stdin)print vars
except EOFError:'' #note that these are two single-quotes
4 G j | d h lp4 -G user -o raj | readmarshal.py{'Email': '[email protected]', 'Update': '2005/12/06 11:16:30', 'Reviews1': '//depot/dev/main/...', 'Reviews0': '//depot/www/...', 'FullName': 'Raj Bai', 'User': 'raj', 'code': 'stat', 'Access': '2006/07/06 15:16:30'}
Command line program: Using Ruby
• Create Ruby marshalled dictionary objectp4 -R job –o
Perforce Training, 2010.1
15
# Example script named readruby.rb
Sample Ruby script and p4 -R output
f = IO.popen("p4 -R user -o " + ARGV[0])user_info = Marshal.load(f)p user_info
readruby.rb bruno{"code"=>"stat", "User"=>"bruno", "Email"=>"[email protected]", "FullName"=>"Bruno Batswan"}
Perforce’s derived APIs
P4P l• P4Perl
• P4Python
• P4Ruby
Perforce Training, 2010.1
16
Scripting environment for the exercises
• Use P4CONFIG to Set Environment Variables
• User Authenticationp4 login
• Client Program Interfaceg• p4 and API (using P4Perl) examples
Scripting Perforce
ExerciseSetting the Perforce Environment
Perforce Training, 2010.1
17
Scripting Perforce
Perforce Commands for Scripting
Perforce Commands for Scripting
• General• General
• Review Daemons
• Data Mining
Perforce Training, 2010.1
18
Perforce Commands
General
Capture errors, warnings and messages
p4 -s opened -c defaulterror: File(s) not opened on this client.exit: 0
Perforce Training, 2010.1
19
Submit without invoking an editor
p4 submit d "Fixed results of sim scans "p4 submit -d Fixed results of sim scans.Submitting change 1825.Locking 2 files ...edit //Sim/Prod/MAIN/src/sim.c#36edit //Sim/Prod/MAIN/src/scan.c#2Change 1825 submittedChange 1825 submitted.
Script efficiently
• Limit number of lines of data returnedLimit number of lines of data returned
• Narrow scope of Perforce commands
• Refer to known workspace spec
• Access small groups of depot filesg p p
Perforce Training, 2010.1
20
List a single changelist
U i li kUsing a client workspace namep4 changes -m1 –s submitted –c raj-spruceChange 421 on 2001/01/07 by raj@raj-spruce'Update sim to work reasonably on'
List several changelists
Using a depot pathUsing a depot pathp4 changes –m10 //depot/www/...Change 1769 on 2006/12/11 by quinn@quinn-azalea
'Copy jamgraph example to web'Change 1763 on 2006/11/23 by hera@hera-amphora
'Jamgraph 1.0 is live.'
etc...etc.
Perforce Training, 2010.1
21
Perforce Commands
Review Daemons
Use a counter
• Set personal counterSet personal counterp4 counter notifyd 746 Counter notifyd set.
• Poll a counterp4 counter notifyd746
Perforce Training, 2010.1
22
List all counter values
p4 countersp4 counterschange = 750job = 4journal = 15notifyd = 746
... etc.
Review daemon tools
• View changelists past counter valuep4 review -t notifydp yChange 747 bruno [email protected](Bruno Batswan)Change 749 brad [email protected](Brad Manners)Change 750 sam [email protected](Sam Maxwell)
• View subscribers for email notificationp4 reviews -c 747bruno <[email protected]> (Bruno Batswan)
laura <[email protected]> (Laura Germain)
Perforce Training, 2010.1
23
Change your user profile
p4 user
User: earl
Email: [email protected]: 2007/08/09 13:45:59Access: 2008/03/07 16:45:05FullName: Earl AshbyReviews:
//Sim/Prod/MAIN/...Add “Reviews:” field if running review daemon
//depot/www/...g
List recently edited/created jobs
p4 jobs -e "date>=2007/06/21"job002127 on 2007/11/19 by earl *open* ‘Fix parse error’job002097 on 2007/10/21 by raj *closed* ‘Exit off’job001931 on 2007/10/16 by gail *punted* ‘Web style’job001762 on 2007/07/08 by raj *closed* ‘Scan func’
Perforce Training, 2010.1
24
List fixed jobs
p4 fixes //Sim/Prod/MAIN/src/p4 fixes //Sim/Prod/MAIN/src/...job000001 fixed by change 55 on 1999/01/19 by earl@earl-devjob000002 fixed by change 114 on 1999/02/28 by earl@earl-devjob000003 fixed by change 114 on 1999/02/28 by earl@earl-devjob000004 fixed by change 148 on 1999/07/19 by earl@earl-devjob000004 fixed by change 132 on 1999/04/30 by earl@earl-dev
etc... etc.
Perforce Commands
Data Mining
Perforce Training, 2010.1
25
Report depot data
• List directoriesp4 dirs //depot/*p p//depot/Jam//depot/Jamgraph
...etc.
• List filesp4 files //depot/Jam/MAIN/src/...//depot/Jam/MAIN/src/Build.com#9 - edit change 839//depot/Jam/MAIN/src/Build.mpw#3 - edit change 839//depot/Jam/MAIN/src/command.c#12 - edit change 839
...etc.
Gather file data as tagged output
p4 fstat execcmd.hdepotFile //depot/Jam/MAIN/src/execcmd h... depotFile //depot/Jam/MAIN/src/execcmd.h
... clientFile c:\raj\Jam\MAIN\src\execcmd.h
... isMapped
... headAction edit
... headType text
... headTime 1106848927
... headRev 2
... headChange 30
... headModTime 1106847314
... haveRev 2
Perforce Training, 2010.1
26
Report on a single file
p4 annotate ac jam cp4 annotate -ac jam.c
//depot/Jam/MAIN/src/jam.c#37 - edit change 93 (text)
1-93: /*1-1: * Copyright 1993 Christopher Seiwald.30-93: * Copyright 1993, 1995 Christopher Seiwald.
... etc.
Search for patterns in filesp4 grep -a -n -F -e "/* debug */" jam.c//depot/Jam/MAIN/src/jam.c#14:114: 1, /* debug *///depot/Jam/MAIN/src/jam.c#13:114: 1, /* debug *///depot/Jam/MAIN/src/jam.c#12:114: 1, /* debug *///depot/Jam/MAIN/src/jam.c#11:106: 1, /* debug *///depot/Jam/MAIN/src/jam.c#10:106: 1, /* debug *///depot/Jam/MAIN/src/jam.c#9:96: 1, /* debug *///depot/Jam/MAIN/src/jam.c#8:96: 1, /* debug *///depot/Jam/MAIN/src/jam.c#6:93: 1, /* debug *///depot/Jam/MAIN/src/jam.c#5:93: 1, /* debug *///depot/Jam/MAIN/src/jam.c#4:93: 1, /* debug *///depot/Jam/MAIN/src/jam.c#3:93: 1, /* debug *///depot/Jam/MAIN/src/jam.c#2:91: 1, /* debug *///depot/Jam/MAIN/src/jam.c#1:86: 1, /* debug */
Perforce Training, 2010.1
27
Report Perforce metadata
Li t ll li t k• List all client workspace specsp4 clients
• List client workspace specs by ownerp4 clients –u brunop
Use -n flag to report status
p4 integrate -n -b SimMAINtoFLATp4 integrate n b SimMAINtoFLAT
//Sim/Prod/FLAT/src/scan.c#3 sync/integrate from //Sim/Prod/MAIN/src/scan.c#12 //Sim/Prod/FLAT/src/scan.h#2 sync/integrate from //Sim/Prod/MAIN/src/scan.h#14
...etc.
Perforce Training, 2010.1
28
Test p4 commands before writing script
• Learn data output format• Learn data output format
• Experiment with flags
• Test if API returns tagged data
• Narrow scope of commandsNarrow scope of commands
Scripting Perforce
Command Line Program (p4)
Perforce Training, 2010.1
29
Module objectives
• Use p4 client program in a Perl Scriptg
• Use P4CONFIG
• Capture error messages
• Submit files without invoking editor
Ch k f f l b it ( i )• Check for successful submit (exercise)
• Report results to standard output
Specifications for a submit script
• Script name is submit.pl
• Create a command string
• Check for opened files in default changelist
• Exit if no files opened
• Submit files
• Check for successful submit (exercise)
• Print submitted changelist
Perforce Training, 2010.1
30
Construct a p4 command string
$p4c = "script_ws";
$p4 = "C:\\p4programs\\p4";
$p4 = "$p4 -c $p4c";
Exit if no opened files in default changelist
@chk = `$p4 -s opened -c default`;@ $p p ;if ($chk[0] =~ /error: File\(s\) not opened on this client./){
print "No files in default changelist to submit.";exit;
}}
Perforce Training, 2010.1
31
Submit files
$desc = "An automated submit from $p4c ";$desc = An automated submit from $p4c. ;
`$p4 submit -d "$desc"`;
Print submitted changelist
$chg = `$p4 changes -m1 -s submitted -c $p4c`;
$chg =~ /(^Change) ([0-9]+) (.+$)/;
print `$p4 describe -s $2`;
Perforce Training, 2010.1
32
Scripting Perforce
ExerciseA Submit Script
Scripting Perforce
Application Program Interface (API)
Perforce Training, 2010.1
33
Module objectives
• Write a generic P4Perl scriptWrite a generic P4Perl script
• Run a command
• Check for errors
P d t• Process data
• Python and Ruby examples
Specifications for a simple P4Perl script
• Script name is p4template.plI iti li P4 i t• Initialize P4 instance
• Select server level compatibility• Connect• Run a command (p4 info)• Check for errors• Process data (print data)• Disconnect
Perforce Training, 2010.1
34
Initialize P4 instance
P4use P4;
$p4c = “script_ws";
$p4 = new P4;
$p4 SetClient( $p4c );
Set server level compatibility
$p4 SetApiLevel( 67 );$p4 SetApiLevel( 67 );
Perforce Knowledge Base Articles: “Perforce Protocol Levels” and “Perforce Server Levels”
Perforce Training, 2010.1
35
Set script name
$p4 SetProg( “Template” );$p4 SetProg( Template );
Connect
$ 4 C t() di ("C t t t ")$p4 Connect() or die("Cannot connect to server.");
Perforce Training, 2010.1
36
Select output mode
• Tagged mode (on by default)Normally returns hash reference$p4 Tagged(1);
• Standard mode
Normally returns array of stringsNormally returns array of strings$p4 Tagged(0);
Run a command
• General syntaxGeneral syntax$p4 Run("p4command", "flags", "filespecs");
• Example
@res = $p4 Run("info");@res $p4 Run( info );
Perforce Training, 2010.1
37
Check for errors
if ($p4 ErrorCount())($p ()){
@err = $p4 Errors();foreach $str(@err){
print "Error message: $str \n"; }}
Check for warnings
} elsif ($p4 WarningCount()){{
@wrn = $p4 Warnings();foreach $str(@wrn){
print “Warning message: $str \n”;}
}elseelse {
# Other data integrity checks}
Perforce Training, 2010.1
38
Process tagged data
if ($p4 IsTagged()) {{
foreach $str(@res){
%hash = %{$str};while (($key, $value) = each(%hash)) {
print "$key => $value\n"; p $ y $ ;}
}}
Process standard data
else
{
foreach $str (@res)
{
print ($str, "\n");
}}
}
Perforce Training, 2010.1
39
Disconnect
$p4 Disconnect();
Scripting Perforce
Demop4template.pl
Perforce Training, 2010.1
40
Python p4template.py
import sys, typesfrom P4 import P4, P4Exceptionp4 = P4()def init():
p4.api_level = 65p4.exception_level = 2p4.prog = "Python Template"p4.client = "script_ws"try:try:
p4.connect()except P4Exception:
print "Cannot connect to the server."sys.exit(1)
Python function getdata
def getdata(cl):try:
res = p4.run(cl)return res
except P4Exception:for e in p4.errors:
print "Errors: " + e4 di t()p4.disconnect()
sys.exit(1)for w in p4.warnings:
print "Warnings: " + w
Perforce Training, 2010.1
41
Python function printdata
def printdata(r):p ( )if not (isinstance(r, types.NoneType)):
for i in range(len(r)):if (p4.tagged):
dict = r[i]for key in dict.keys():print "%s :: %s" % (key,dict[key])
else:print r[i]
Python MAIN programdef runstub(c):
data = getdata(c)printdata(data)
# MAIN PROGRAM init()p4.tagged = Falsecmd = "info"runstub(cmd)
i t " \ \ "print " \n \n"p4.tagged = Truerunstub(cmd)
p4.disconnect()
Perforce Training, 2010.1
42
Ruby p4template.rbdef init
beginrequire "P4"require P4$p4 = P4.new() p4.api_level = 65$p4.exception_level = P4::RAISE_ALL$p4.prog = “Ruby Template"$p4.client = "script_ws"$p4.connectrescue P4Exception
print "Cannot connect to the server."exit
endend
Ruby method getdata
def getdata(cl)beginres = $p4.run(cl)return resrescue P4Exception
$p4.errors.each { |e| puts( e ) }if not $p4.errors.empty?
$p4.disconnectexit
end $p4.warnings.each { |w| puts( w ) }
endend
Perforce Training, 2010.1
43
Ruby method printdata
def printdata(r)( )if not r.empty?
if $p4.tagged?h = r[0]h.each { |k,v| puts "#{k} => #{v}"}
elseputs rputs r
endend
end
Ruby MAIN programdef runstub(c)
data = getdata(c)printdata(data)p ( )
end
# MAIN PROGRAM init$p4.tagged = falsecmd = "info"
t b( d)runstub(cmd)print " \n \n"$p4.tagged = truerunstub(cmd)
$p4.disconnect
Perforce Training, 2010.1
44
Specifications for a release notes script
• Input• Depot path• Depot path • Revision range• Number of changelists to process
• Output• List of changelist numbers• List of changelist numbers• Description for each changelist (exercise)• List of jobs fixed (exercise)
Get a list of changelists
# Script name is relnotes pl# Script name is relnotes.pl
# Initialize P4Perl, connect, then:
@cmd = ("changes", "-s", "submitted", "//Sim/Prod/REL2.1/src/... ");
@chg = $p4 Run(@cmd);
Perforce Training, 2010.1
45
Process data
foreach $str (@chg)foreach $str (@chg){
%hash = %{$str};
print ($hash{"change"}, "\n");
}
Scripting Perforce
ExerciseA Release Notes Script
Perforce Training, 2010.1
46
Scripting Perforce
Daemons
Daemons learning objectives
• What is a review daemon?
• How users subscribe to get email
• Write a review daemon script
• Use Perforce counters
• Check for Perforce job updates
Perforce Training, 2010.1
47
Specifications for a review daemon
• Input• Changelists to review
• Output• Email changelist descriptions to subscribed users
• Update review daemon counter
• Option• Email edited jobs to subscribed users (exercise)
Main program
# Script name is p4review pl# Script name is p4review.pl
# Initialize P4Perl, connect then:
checkcounter();
while(1)
{
poll_server();
sleep($sleeptime);
}
Perforce Training, 2010.1
48
Initialize review counter
sub checkcounter{
$p4 Connect() or die ("Cannot connect to Perforce server."); $p4 Tagged(0);@data = $p4 Run("counter", "review");$rc = @data[0];if ($rc == 0){
$p4 Run ("counter", "review", "0");p ( , , );}$p4 Tagged(1);$p4 Disconnect();
}
Poll the server
sub poll_server {
$p4 Connect() or die ("Cannot connect to Perforce server.");$p () ( );@data = $p4 Run ("review", "-t", "review");if ($#data > -1) {
foreach $str (@data){
%hash = %{$str}; $chg = $hash{change};
i ($ h )reviewers($chg);}updatecounter($chg);
}$p4 Disconnect();
}
Perforce Training, 2010.1
49
Check for reviewerssub reviewers {
($chg) = @ ;($chg) = @_;@cmd = ("reviews", "-c", $chg);@data = $p4 Run(@cmd);if ($data[0] =~m/^HASH/)
{@c1 = ("describe", "-s", $chg); @ ( , , $ g);@des = $p4 Run(@c1);sendoutmail(@des, @data);
} }
Get the changelist data
sub sendoutmail {
(@users) = @_;$cdes = shift(@users);
foreach $str (@cdes)
{
%change = %{$str};% g %{$ };
$chgno = $change{change};
$desc = $change{desc};
}
Perforce Training, 2010.1
50
Send out mail
foreach $str (@users)
{{
%user = %{$str};
print "To: $user{email} \n";
print "From: daemon\@p4demo.com \n";
print "Subject: Changelist $chgno submitted. \n";
print "Description: $desc\n";
}
}
Update review counter
sub updatecounter{
($chg) = @_;$p4 Run ("counter", "review", $chg);
}
Perforce Training, 2010.1
51
Adding updates to jobs reporting
Ti L luse Time::Local;
# create a jobreview counter# time() returns local Unix time.
$p4 Run("counter" "jobreview" time());$p4 Run( counter , jobreview , time());
Check for newly modified jobssub checkforjobs{
$p4 Tagged(0);$p4 Tagged(0);@data = $p4 Run ("counter", "jobreview"); $p4 Tagged(1);$tchk = @data[0]; @jobs = $p4 Run("jobs", "-e", "date>=$tchk");foreach $str (@jobs){{
@users = jobreviewers();sendjobemail($str, @users);
} updatejobcounter();
}
Perforce Training, 2010.1
52
Check for job reviewers
sub jobreviewers
{
@data = $p4 Run ("reviews", "//depot/jobs");
return @data;
}}
Additions to the review daemon script
• Add jobs to email
• Update jobreview counter
Perforce Training, 2010.1
53
Scripting Perforce
ExerciseA Review Daemon
Scripting Perforce
Triggersgg
Perforce Training, 2010.1
54
Triggers learning objectives
• Supported trigger types in PerforceSupported trigger types in Perforce
• Best practices when writing triggers
• Implementing triggers
• Install a “submit policy” trigger
• Install a “workspace check” trigger
Supported trigger types
• Password authenticationPassword authentication
• Changelist processing
• Form manipulation or validation
• Job fix adds or deletesJob fix adds or deletes
Perforce Training, 2010.1
55
Use triggers to enforce policy
• Authenticate passwords against an LDAP server
• RELNOTES file always submitted with *.c files
• Submit to “rel1” codeline fixes at least one job
• Add “reviewed by:” when changelist created
• Evaluate edited forms for correctness
• Begin a build after submit to “dev” codeline
• Prevent users from deleting branch specs
Script efficiently
• Follow best practices in scriptingFollow best practices in scripting
• Check Perforce release notes for optimizations
Ti i t’ ti• Time your script’s execution
Perforce Training, 2010.1
56
Avoid common errors
• Writing data to the server
• Recursive triggers
• Attempting workspace operations
Implementing triggers
• T t i t l ll• Test script locally
• Install on test server
• Deploy on production server
Perforce Training, 2010.1
57
Enforcing a submit policy
• Trigger name is checksubmit plTrigger name is checksubmit.pl
• Run as a change-submit trigger
• Enforce a codeline policy: edits of *.c files always include a RELNOTES file updatealways include a RELNOTES file update.
Enforcing a workspace configuration policy
• Trigger name is checkworkspace.plgg p p
• Run as a form-save trigger
• Enforce a workspace creation policy: no client workspaces can be saved with a p“wide open” view e.g.,//Sim/... //my_client/...
Perforce Training, 2010.1
58
Scripting Perforce
ExerciseImplementing Triggers
Scripting Perforce
Wrappers and Administrative Scriptingpp p g
Perforce Training, 2010.1
59
Module objectives
• Modify Perforce form using P4Perl
• List unwanted user created configurations
Examples of build wrappers
• Create temporary client specCreate temporary client spec
• Sync to changelist or label
• Create label spec and tag files
• Email users build results
• Automate submit of objects
Perforce Training, 2010.1
60
Specifications for temporary client spec
Edit li t• Edit a client spec
• Modify options
• Modify view
• Save client spec• Save client spec
Get the client spec form
# Script name is p4client.pl
# Initialize P4Perl, connect, then:
$cl = $p4 GetClient();
@data = $p4 FetchClient($cl);@data $p4 FetchClient($cl);
Perforce Training, 2010.1
61
Modify the client spec form
foreach $str(@data) {
% %{$ }%hash = %{$str};
$opt = "noallwrite noclobber nocompress locked nomodtime rmdir";
$hash{Options} = $opt;
@wv = @{$hash{View}};
@wv = ("//Sim/Prod/MAIN/src/ //$cl/src/ "@wv = ( //Sim/Prod/MAIN/src/... //$cl/src/... ,
"//depot/www/... //$cl/www/...");
$hash{View} = \@wv;
}
Save the client spec form
$p4 SaveClient( \%hash);
$p4 Disconnect();
Perforce Training, 2010.1
62
Scripting Perforce
ExerciseCreating a Temporary Client Spec
Sample wrapper scripts
• Add custom tools to P4VAdd custom tools to P4V
• Launch third-party tools.
• Run command line commands
• Get latest files based on have list
• Automate routine integrations
Perforce Training, 2010.1
63
Sample administrative scripts
• Enforce branching policyg p y
• Find files that need merging
• Back up database
• Delete obsolete client workspaces
• Delete obsolete labelsDelete obsolete labels
• Ensure client specs used from single host
Specifications for a client spec abuse script
• List client specs with blank Host field
• List users who own too many client specs
Perforce Training, 2010.1
64
Main program
# Script name is clientspecabuse.pl# Script name is clientspecabuse.pl
init();
checkforhosts();
workspacesperuser();
printdata();
Initialize
sub initsub init
{
# Initialize P4Perl, connect, then:
$workspacelimit = 2;
}}
Perforce Training, 2010.1
65
Check for Host abuse
sub checkforhosts {
@cls = $p4 Run("clients");@cls = $p4 Run( clients );foreach $str (@cls){
%hash = %{$str};$h = $hash{"Host"};$w = $hash{"client"};if ($h eq "") ($ q ){
push (@hlist, "No Host for $w \n");}
} }
Workspaces per user
sub workspacesperuser
{
@u = $p4 Run("users");
foreach $str (@u)
{
%hash = %{$str};%hash = %{$str};
$un = $hash{"User"};
Perforce Training, 2010.1
66
Workspaces per user continued...
@c = $p4 Run("clients", "-u", $un);if ($#c > $workspacelimit) {
push (@clist, "User $un owns $#c workspaces. \n");}
}}}
Print results
sub printdata {
print "List of HOST abusers: \n\n"; foreach $str (@hlist) {
print $str;}print “Over $workspacelimit workspaces \n\n";foreach $str (@clist)foreach $str (@clist) {
print $str; }
}
Perforce Training, 2010.1
67
Scripting Perforce
ExerciseDetecting Client Workspace Abuse
Recap of exercises
• Setting the Perforce environment
• Using p4: submitting files
• Using P4Perl: creating release notes
• Codeline policy enforcement: triggers
• Creating a temporary client specg p y p
• Detecting client workspace abuse
Perforce Training, 2010.1
68
The End
Please fill out class evaluations.
All Perforce manuals and technical notes are available on our web page, www.perforce.com.
Report problems and get technical help from [email protected].
Share tips and ideas with other users on [email protected]