Scripting with Perforce · Scripting with Perforce Using the Perl and Ruby interfaces (c) ... fs =...
Transcript of Scripting with Perforce · Scripting with Perforce Using the Perl and Ruby interfaces (c) ... fs =...
![Page 1: Scripting with Perforce · Scripting with Perforce Using the Perl and Ruby interfaces (c) ... fs = p4.run_fstat( “file.c” ) head = fs[ “headRev” ] (c) 2003 Perforce Software,](https://reader031.fdocuments.in/reader031/viewer/2022021704/5b1e82027f8b9af1328b8e54/html5/thumbnails/1.jpg)
(c) 2003 Perforce Software, Inc.
Scripting with Perforce
Using the Perl and Ruby interfaces
![Page 2: Scripting with Perforce · Scripting with Perforce Using the Perl and Ruby interfaces (c) ... fs = p4.run_fstat( “file.c” ) head = fs[ “headRev” ] (c) 2003 Perforce Software,](https://reader031.fdocuments.in/reader031/viewer/2022021704/5b1e82027f8b9af1328b8e54/html5/thumbnails/2.jpg)
(c) 2003 Perforce Software, Inc.
Introduction
ØWhat are P4Perl and P4Ruby• Perl & Ruby bindings for the Perforce C++
API• Object-Oriented• Interface designed to suit the language
![Page 3: Scripting with Perforce · Scripting with Perforce Using the Perl and Ruby interfaces (c) ... fs = p4.run_fstat( “file.c” ) head = fs[ “headRev” ] (c) 2003 Perforce Software,](https://reader031.fdocuments.in/reader031/viewer/2022021704/5b1e82027f8b9af1328b8e54/html5/thumbnails/3.jpg)
(c) 2003 Perforce Software, Inc.
Why Bother?ØReasons to be cheerful…
• Data returned as native objects: arrays and hashes
• Support for both tagged mode and non-tagged mode
• Smart form processing• Run many commands over a single
connection.• Exception based error handling (P4Ruby)
![Page 4: Scripting with Perforce · Scripting with Perforce Using the Perl and Ruby interfaces (c) ... fs = p4.run_fstat( “file.c” ) head = fs[ “headRev” ] (c) 2003 Perforce Software,](https://reader031.fdocuments.in/reader031/viewer/2022021704/5b1e82027f8b9af1328b8e54/html5/thumbnails/4.jpg)
(c) 2003 Perforce Software, Inc.
Getting Started
ØBefore running commands• Load the module• Create an instance of the P4 class• Set options• Connect
![Page 5: Scripting with Perforce · Scripting with Perforce Using the Perl and Ruby interfaces (c) ... fs = p4.run_fstat( “file.c” ) head = fs[ “headRev” ] (c) 2003 Perforce Software,](https://reader031.fdocuments.in/reader031/viewer/2022021704/5b1e82027f8b9af1328b8e54/html5/thumbnails/5.jpg)
(c) 2003 Perforce Software, Inc.
Getting Started (Examples)ØPerl
use P4;my $p4 = new P4;$p4->Init() or die( “Can’t connect to Perforce” );
ØRubyrequire “P4”p4 = P4.newp4.connect
![Page 6: Scripting with Perforce · Scripting with Perforce Using the Perl and Ruby interfaces (c) ... fs = p4.run_fstat( “file.c” ) head = fs[ “headRev” ] (c) 2003 Perforce Software,](https://reader031.fdocuments.in/reader031/viewer/2022021704/5b1e82027f8b9af1328b8e54/html5/thumbnails/6.jpg)
(c) 2003 Perforce Software, Inc.
Simple Usage
ØSimple things are simple:• Running “p4 sync”
p4->Run( “sync”); (Perl)p4.run( “sync” ) (Ruby)
![Page 7: Scripting with Perforce · Scripting with Perforce Using the Perl and Ruby interfaces (c) ... fs = p4.run_fstat( “file.c” ) head = fs[ “headRev” ] (c) 2003 Perforce Software,](https://reader031.fdocuments.in/reader031/viewer/2022021704/5b1e82027f8b9af1328b8e54/html5/thumbnails/7.jpg)
(c) 2003 Perforce Software, Inc.
Command ShorthandØBoth P4Perl and P4Ruby have a
shorthand syntax for executing Perforce commandsØThey differ slightly due to the desire to
make the interfaces feel natural to each languageØMakes P4Perl and P4Ruby less
dependent on server version
![Page 8: Scripting with Perforce · Scripting with Perforce Using the Perl and Ruby interfaces (c) ... fs = p4.run_fstat( “file.c” ) head = fs[ “headRev” ] (c) 2003 Perforce Software,](https://reader031.fdocuments.in/reader031/viewer/2022021704/5b1e82027f8b9af1328b8e54/html5/thumbnails/8.jpg)
(c) 2003 Perforce Software, Inc.
Command Shorthand (Perl)ØAll unknown methods are assumed to be
Perforce commands• $p4->NewCommand();• Runs “p4 newcommand”• Implemented using Perl’s AUTOLOADER
ØFetch* and Save* commands are special• $p4->FetchXXX is equivalent to $p4->XXX( “-o” )• $p4->SaveXXX is equivalent to “$p4->XXX( ”-I” )
![Page 9: Scripting with Perforce · Scripting with Perforce Using the Perl and Ruby interfaces (c) ... fs = p4.run_fstat( “file.c” ) head = fs[ “headRev” ] (c) 2003 Perforce Software,](https://reader031.fdocuments.in/reader031/viewer/2022021704/5b1e82027f8b9af1328b8e54/html5/thumbnails/9.jpg)
(c) 2003 Perforce Software, Inc.
Command Shorthand (Ruby)ØAll unknown methods starting with “run_” are
assumed to be Perforce commands• p4.run_newcommand• Runs “p4 newcommand”• Implemented by “P4#method_missing”
Ø fetch_* and save_* are special• fetch_xxx is equivalent to p4.run_xxx( “-o”).shift• save_xxx is equivalent to p4.run_xxx( “-i” ).shift
![Page 10: Scripting with Perforce · Scripting with Perforce Using the Perl and Ruby interfaces (c) ... fs = p4.run_fstat( “file.c” ) head = fs[ “headRev” ] (c) 2003 Perforce Software,](https://reader031.fdocuments.in/reader031/viewer/2022021704/5b1e82027f8b9af1328b8e54/html5/thumbnails/10.jpg)
(c) 2003 Perforce Software, Inc.
Error Handling IntroductionØNot all errors are errors
• Some are warnings• API users can test the severity of errors directly• P4Perl and P4Ruby distinguish between errors
and warnings
ØCommands may partially succeed/fail• May succeed with some files but not with others• Requires careful handling
![Page 11: Scripting with Perforce · Scripting with Perforce Using the Perl and Ruby interfaces (c) ... fs = p4.run_fstat( “file.c” ) head = fs[ “headRev” ] (c) 2003 Perforce Software,](https://reader031.fdocuments.in/reader031/viewer/2022021704/5b1e82027f8b9af1328b8e54/html5/thumbnails/11.jpg)
(c) 2003 Perforce Software, Inc.
Error HandlingØPerl
• Requires explicit call to check for errors• Use P4::ErrorCount() to see how many errors
occurred.• Use P4::Errors() to get errors as an array
ØRuby• Exceptions raised on errors and (optionally)
warnings• Use P4#errors() to get errors as an array
![Page 12: Scripting with Perforce · Scripting with Perforce Using the Perl and Ruby interfaces (c) ... fs = p4.run_fstat( “file.c” ) head = fs[ “headRev” ] (c) 2003 Perforce Software,](https://reader031.fdocuments.in/reader031/viewer/2022021704/5b1e82027f8b9af1328b8e54/html5/thumbnails/12.jpg)
(c) 2003 Perforce Software, Inc.
Error Handling (Perl)
ØExample
$p4->Sync();if ( $p4->ErrorCount() ) {foreach my $e ( $p4->Errors() ) {
print( STDERR, $e, “\n” );}
}
![Page 13: Scripting with Perforce · Scripting with Perforce Using the Perl and Ruby interfaces (c) ... fs = p4.run_fstat( “file.c” ) head = fs[ “headRev” ] (c) 2003 Perforce Software,](https://reader031.fdocuments.in/reader031/viewer/2022021704/5b1e82027f8b9af1328b8e54/html5/thumbnails/13.jpg)
(c) 2003 Perforce Software, Inc.
Error Handling (Ruby)ØException Levels
• 0 = no exceptions raised at all• 1 = no exceptions on warnings• 2 = exceptions on both warnings and errors
(default)ØExceptions raised at command
completion• Meaning that at least one error occurred
![Page 14: Scripting with Perforce · Scripting with Perforce Using the Perl and Ruby interfaces (c) ... fs = p4.run_fstat( “file.c” ) head = fs[ “headRev” ] (c) 2003 Perforce Software,](https://reader031.fdocuments.in/reader031/viewer/2022021704/5b1e82027f8b9af1328b8e54/html5/thumbnails/14.jpg)
(c) 2003 Perforce Software, Inc.
Error Handling (Ruby)ØUsing Exception Level 1
p4.exception_level = 1begin
p4.run_syncp4.run_edit( “index.html” )…
rescue P4Exceptionp4.errors.each { |e| $stderr.puts( e ) }raise
end
![Page 15: Scripting with Perforce · Scripting with Perforce Using the Perl and Ruby interfaces (c) ... fs = p4.run_fstat( “file.c” ) head = fs[ “headRev” ] (c) 2003 Perforce Software,](https://reader031.fdocuments.in/reader031/viewer/2022021704/5b1e82027f8b9af1328b8e54/html5/thumbnails/15.jpg)
(c) 2003 Perforce Software, Inc.
Overriding methods
ØShorthand methods can be easily overridden with custom implementations• Just define the method• Call the [Rr]un() method to execute the
base command• Process the results as normal
![Page 16: Scripting with Perforce · Scripting with Perforce Using the Perl and Ruby interfaces (c) ... fs = p4.run_fstat( “file.c” ) head = fs[ “headRev” ] (c) 2003 Perforce Software,](https://reader031.fdocuments.in/reader031/viewer/2022021704/5b1e82027f8b9af1328b8e54/html5/thumbnails/16.jpg)
(c) 2003 Perforce Software, Inc.
Overriding Methods (Perl)ØCustom implementation of “p4 filelog”
use P4;package P4;sub Filelog {
my $self = shift;my @results = $self->Run( “filelog”, @_ );# Post process @resultsreturn @results;
}package main;
![Page 17: Scripting with Perforce · Scripting with Perforce Using the Perl and Ruby interfaces (c) ... fs = p4.run_fstat( “file.c” ) head = fs[ “headRev” ] (c) 2003 Perforce Software,](https://reader031.fdocuments.in/reader031/viewer/2022021704/5b1e82027f8b9af1328b8e54/html5/thumbnails/17.jpg)
(c) 2003 Perforce Software, Inc.
Overriding Methods (Ruby)ØCustom implementation of “p4 filelog”
require “P4”class P4
def filelog( *args )results = self.run( “filelog”, args )# Post process resultsreturn results
endend
![Page 18: Scripting with Perforce · Scripting with Perforce Using the Perl and Ruby interfaces (c) ... fs = p4.run_fstat( “file.c” ) head = fs[ “headRev” ] (c) 2003 Perforce Software,](https://reader031.fdocuments.in/reader031/viewer/2022021704/5b1e82027f8b9af1328b8e54/html5/thumbnails/18.jpg)
(c) 2003 Perforce Software, Inc.
Tagged Mode
ØTagged data from server is returned as a hashØAllows direct access to the data you are
interested in without having to parse the output
![Page 19: Scripting with Perforce · Scripting with Perforce Using the Perl and Ruby interfaces (c) ... fs = p4.run_fstat( “file.c” ) head = fs[ “headRev” ] (c) 2003 Perforce Software,](https://reader031.fdocuments.in/reader031/viewer/2022021704/5b1e82027f8b9af1328b8e54/html5/thumbnails/19.jpg)
(c) 2003 Perforce Software, Inc.
Tagged Mode (Example1)
ØPerlmy $fs = $p4->Fstat( “file.c” );my $head = $fs->{ “headRev” };
ØRubyfs = p4.run_fstat( “file.c” )head = fs[ “headRev” ]
![Page 20: Scripting with Perforce · Scripting with Perforce Using the Perl and Ruby interfaces (c) ... fs = p4.run_fstat( “file.c” ) head = fs[ “headRev” ] (c) 2003 Perforce Software,](https://reader031.fdocuments.in/reader031/viewer/2022021704/5b1e82027f8b9af1328b8e54/html5/thumbnails/20.jpg)
(c) 2003 Perforce Software, Inc.
Tagged Mode (Example2)
ØPerlmy $fs = $p4->Fstat( “file.c” );foreach my( $key, $value) ( @$fs ) {
print( $key, “ à “ $value );}
![Page 21: Scripting with Perforce · Scripting with Perforce Using the Perl and Ruby interfaces (c) ... fs = p4.run_fstat( “file.c” ) head = fs[ “headRev” ] (c) 2003 Perforce Software,](https://reader031.fdocuments.in/reader031/viewer/2022021704/5b1e82027f8b9af1328b8e54/html5/thumbnails/21.jpg)
(c) 2003 Perforce Software, Inc.
Tagged Mode (Example2)
ØRubyp4.run_fstat( “file.c” ).each do
|key,value|puts( key + “ à “ + value )
end
![Page 22: Scripting with Perforce · Scripting with Perforce Using the Perl and Ruby interfaces (c) ... fs = p4.run_fstat( “file.c” ) head = fs[ “headRev” ] (c) 2003 Perforce Software,](https://reader031.fdocuments.in/reader031/viewer/2022021704/5b1e82027f8b9af1328b8e54/html5/thumbnails/22.jpg)
(c) 2003 Perforce Software, Inc.
Form Handling
ØBoth P4Perl and P4Ruby can convert Perforce forms into hashesØBoth can also convert hashes back into
Perforce formsØEditing a clientspec or a changelist is as
simple as updating a hash and saving your changes
![Page 23: Scripting with Perforce · Scripting with Perforce Using the Perl and Ruby interfaces (c) ... fs = p4.run_fstat( “file.c” ) head = fs[ “headRev” ] (c) 2003 Perforce Software,](https://reader031.fdocuments.in/reader031/viewer/2022021704/5b1e82027f8b9af1328b8e54/html5/thumbnails/23.jpg)
(c) 2003 Perforce Software, Inc.
Form Handing (Perl)
ØExamplemy $c = $p4->FetchChange();$c->{ “Description” } = “some text…”;$p4->SaveChange( $c );
![Page 24: Scripting with Perforce · Scripting with Perforce Using the Perl and Ruby interfaces (c) ... fs = p4.run_fstat( “file.c” ) head = fs[ “headRev” ] (c) 2003 Perforce Software,](https://reader031.fdocuments.in/reader031/viewer/2022021704/5b1e82027f8b9af1328b8e54/html5/thumbnails/24.jpg)
(c) 2003 Perforce Software, Inc.
Form Handling (Ruby)ØExample 1
c = p4.fetch_changec[ “Description” ] = “some text…”p4.save_change( c )
ØExample 2c = p4.fetch_clientc[ “Root” ] = ‘d:\work’c[ “Options” ].sub!( “normdir”, “rmdir”)p4.save_client( c )
![Page 25: Scripting with Perforce · Scripting with Perforce Using the Perl and Ruby interfaces (c) ... fs = p4.run_fstat( “file.c” ) head = fs[ “headRev” ] (c) 2003 Perforce Software,](https://reader031.fdocuments.in/reader031/viewer/2022021704/5b1e82027f8b9af1328b8e54/html5/thumbnails/25.jpg)
(c) 2003 Perforce Software, Inc.
Language Wars
![Page 26: Scripting with Perforce · Scripting with Perforce Using the Perl and Ruby interfaces (c) ... fs = p4.run_fstat( “file.c” ) head = fs[ “headRev” ] (c) 2003 Perforce Software,](https://reader031.fdocuments.in/reader031/viewer/2022021704/5b1e82027f8b9af1328b8e54/html5/thumbnails/26.jpg)
(c) 2003 Perforce Software, Inc.
P4Perl vs. P4RubyØFunctionally equivalentØBig difference is Exception base error
handling in P4Ruby• Smaller, more reliable code• Handles warnings
• (e.g. “File(s) up-to-date”)
ØSome extra support for handling “p4 filelog” output in P4RubyØRuby is much nicer than Perl. Try it!
![Page 27: Scripting with Perforce · Scripting with Perforce Using the Perl and Ruby interfaces (c) ... fs = p4.run_fstat( “file.c” ) head = fs[ “headRev” ] (c) 2003 Perforce Software,](https://reader031.fdocuments.in/reader031/viewer/2022021704/5b1e82027f8b9af1328b8e54/html5/thumbnails/27.jpg)
(c) 2003 Perforce Software, Inc.
P4Perl/P4Ruby vs. p4 -G
ØMostly personal preference• Multiple commands per connection• Separation of output and error streams• Not Python! ☺
![Page 28: Scripting with Perforce · Scripting with Perforce Using the Perl and Ruby interfaces (c) ... fs = p4.run_fstat( “file.c” ) head = fs[ “headRev” ] (c) 2003 Perforce Software,](https://reader031.fdocuments.in/reader031/viewer/2022021704/5b1e82027f8b9af1328b8e54/html5/thumbnails/28.jpg)
(c) 2003 Perforce Software, Inc.
Questions?
ØNeither P4Perl nor P4Ruby is supported by Perforce Software.ØBoth are supported by me personallyØQuestions, comments etc. to me directly
at either• [email protected] or• [email protected]