Scripting with Perforce

coordinatedcapableSoftware and s/w Development

Nov 4, 2013 (3 years and 7 months ago)


Scripting with Perforce

Prakash Ranade

The P4 Command
Line Client

P4 CLI offers access to all Perforce features. It can used in scripts
and is the ideal interface for automated processes such as backups
and nightly builds.

Custom automation of your development and administrative
processes can be accomplished by embedding the appropriate
commands in Perl, Python, or any other scripting language.

The P4 Command
Line Client

P4 CLI uses the same commands and syntax on more than 50
platforms, and automatically translates line ending conventions
between operating systems.

Generate custom reports from the Perforce metadata

The Perforce Perl Scripting

The Perforce Perl API allows you to write Perl scripts that directly
execute Perforce commands.

P4Perl is the product name for a Perl module that allows you to
execute Perforce commands without leaving Perl space and to get
the results in Perl Arrays and Hashes.


Perl Extensions


use P4;

# Initialisation

my $p4 = new P4;

>Init() or die( "Failed to connect to Perforce" );

# Running "p4 info" and getting the results in array form

my @info;

@info = $p4

# Submitting changes. Use "p4 change
o" to grab the change spec

# and "p4 submit
i" to do the submit.


# Commented out by default to make the example non

my $change = $p4

$change =~ s/<enter description here>/Some description/;

>SetInput( $change );

>Submit( "
i" ) );

NetApp environment

We are into development of storage and networking products.

Number of users: Approximately 800.

Number of development sites: 5

(main development center in Sunnyvale, California; remote US
based development sites in Pittsburgh, RTP, NANE, and Bangalore,

Perforce Proxy: Used in India, Pittsburgh offices

Status: In production; All development files are on our NetApp
storage systems (for main server only).

Scripting Advantages

Using scripting one can force the company specific rules.


Access control on depots

Branching strategy (cold, frozen, open)

Submit message check and integration with bug tracking system

Scripting at Bangalore site

P4P PROXY site

P4PORT defined based on site

Simple checks for P4CLIENT definitions

Enforce rule based on site.

How we did it!!!

/usr/local/bin/p4 wrapper perl script will manage
p4 interactions on heterogeneous systems using
platform dependant p4 binaries.

All Perforce users will use wrapper script.

Wrapper script does the argument processing,
Access control, branching checks and executes
p4 binary.

Checks at remote sites tight views

//depot/prod/XXX/YYY/… checks using tight views.

XXX is product

YYY is code

Define client views (and branch and label views) so users access only the files they
require. For example, the following "tight" client view is restricted to specific depot

//depot/main/svr/devA/... //ws/main/svr/devA/... //depot/main/dvr/lport/...
//ws/main/dvr/lport/... //depot/rel2.0/svr/devA/bin/... //ws/rel2.0/svr/devA/bin/...
//depot/qa/s6test/dvr/... //ws/qa/s6test/dvr/...

By contrast, the following unrestricted view is easier to set up but invites trouble when
depots are very large: //depot/... //ws/...

Client views, branch views, and label views are defined using the p4 client, p4
branch, and p4 label commands, respectively, by the users who created them.