class P4
Add the extra's written purely in ruby.
Constants
- CANCEL
- EV_ADMIN
- EV_CLIENT
- EV_COMM
- EV_CONFIG
- EV_CONTEXT
- EV_EMPTY
No fault at all
- EV_FAULT
not the fault of the user
- EV_ILLEGAL
- EV_NONE
Named values for generic error codes returned by P4::Message#generic
- EV_NOTYET
- EV_PROTECT
- EV_TOOBIG
- EV_UNKNOWN
- EV_UPGRADE
- EV_USAGE
The fault of the user
- E_EMPTY
Named values for error severities returned by P4::Message#severity
- E_FAILED
- E_FATAL
- E_INFO
- E_WARN
- HANDLED
- MERGE_ACCEPT_EDIT
- MERGE_ACCEPT_MERGED
- MERGE_ACCEPT_THEIRS
- MERGE_ACCEPT_YOURS
- MERGE_SKIP
Named values for merge actions. Values taken from clientmerge.h in the Perforce API
- PROG_DONE
- PROG_FAILDONE
- PROG_FLUSH
- PROG_NORMAL
Client progress 'done' state
- RAISE_ALL
- RAISE_ERRORS
- RAISE_NONE
Named constants for the exception levels. Note they are cumulative, so RAISE_ALL includes RAISE_ERRORS (as you'd expect).
- REPORT
OutputHandler return values constants
- SpecTypes
Mappings for P4#each_<spec> Hash of type vs. key
- Version
Public Instance Methods
Allow the user to run commands at a temporarily altered exception level. Pass the new exception level desired, and a block to be executed at that level.
# File lib/P4.rb, line 393 def at_exception_level( level ) return self unless block_given? old_level = self.exception_level self.exception_level = level begin yield( self ) ensure self.exception_level = old_level end self end
# File lib/P4.rb, line 259 def delete_shelve( *args ) if( ! args.include?( "-c" ) ) args.unshift( "-c") end return self.run( "shelve", "-d", args) end
Show some handy information when using irb
# File lib/P4.rb, line 425 def inspect sprintf( 'P4: [%s] %s@%s (%s)', self.port, self.user, self.client, self.connected? ? 'connected' : 'not connected' ) end
# File lib/P4.rb, line 136 def method_missing( m, *a ) # Generic run_* methods if ( m.to_s =~ /^run_(.*)/ ) return self.run( $1, a ) # Generic fetch_* methods elsif ( m.to_s =~ /^fetch_(.*)/ ) return self.run( $1, "-o", a ).shift # Generic save_* methods elsif ( m.to_s =~ /^save_(.*)/ ) if ( a.length == 0 ) raise( P4Exception, "Method P4##{m.to_s} requires an argument", caller) end self.input = a.shift return self.run( $1, "-i", a ) # Generic delete_* methods elsif ( m.to_s =~ /^delete_(.*)/ ) if ( a.length == 0 ) raise( P4Exception, "Method P4##{m.to_s} requires an argument", caller) end return self.run( $1, "-d", a ) # Generic parse_* methods elsif ( m.to_s == "parse_forms" ) raise( NoMethodError, "undefined method 'P4#parse_forms'", caller ) elsif ( m.to_s =~ /^parse_(.*)/ ) if ( a.length != 1 ) raise( P4Exception, "Method P4##{m.to_s} requires an argument", caller) end return self.parse_spec( $1, a.shift ) # Generic format_* methods elsif ( m.to_s =~ /^format_(.*)/ ) if ( a.length != 1 ) raise( P4Exception, "Method P4##{m.to_s} requires an argument", caller) end return self.format_spec( $1, a.shift ) # # Generic each_* methods # Simple method to iterate over a particular type of spec # This is a convenient wrapper for the pattern: # clients = p4.run_clients # clients.each do # |c| # client = p4.fetch_client( c['client'] ) # <do something with client> # end # # NOTE: It's not possible to implicitly pass a block to a # delegate method, so I've implemented it here directly. Could use # Proc.new.call, but it looks like there is a serious performance # impact with that method. # elsif ( m.to_s =~ /^each_(.*)/ ) raise( P4Exception, "No such method P4##{m.to_s}", caller) unless SpecTypes.has_key?( $1 ) raise( P4Exception, "Method P4##{m.to_s} requires block", caller) unless block_given? specs = self.run( $1, a ) cmd = SpecTypes[ $1 ][0].downcase key = SpecTypes[ $1 ][1] specs.each{ |spec| spec = self.run( cmd, "-o", spec[key] ).shift yield spec } return specs # That's all folks! else raise NameError, "No such method #{m.to_s} in class P4", caller end end
#run_filelog: convert “p4 filelog” responses into objects with useful
methods
Requires tagged output to be of any real use. If tagged output it not enabled then you just get the raw data back
# File lib/P4.rb, line 337 def run_filelog( *args ) raw = self.run( 'filelog', args.flatten ) raw.collect do |h| if ( ! h.kind_of?( Hash ) ) h else df = P4::DepotFile.new( h[ "depotFile" ] ) h[ "rev" ].each_index do |n| # If rev is nil, there's nothing here for us next unless h[ "rev" ][ n ] # Create a new revision of this file ready for populating r = df.new_revision h.each do |key,value| next unless( value.kind_of?( Array ) ) next unless value[ n ] next if( value[ n ].kind_of?( Array ) ) # If the field is the revision time, convert it to a Time object value[ n ] = Time.at( value[ n ].to_i ) if key == "time" r.set_attribute( key, value[ n ] ) end # Now if there are any integration records for this revision, # add them in too next unless ( h[ "how" ] ) next unless ( h[ "how" ][ n ] ) h[ "how" ][ n ].each_index do |m| how = h[ "how" ][ n ][ m ] file = h[ "file" ][ n ][ m ] srev = h[ "srev" ][ n ][ m ] erev = h[ "erev" ][ n ][ m ] srev.gsub!( /^#/, "" ) erev.gsub!( /^#/, "" ) srev = ( srev == "none" ? 0 : srev.to_i ) erev = ( erev == "none" ? 0 : erev.to_i ) r.integration( how, file, srev, erev ) end end df end end end
Simple interface for using “p4 login”
# File lib/P4.rb, line 269 def run_login( *args ) self.input = self.password return self.run( "login", args ) end
Interface for changing the user's password. Supply the old password and the new one.
# File lib/P4.rb, line 314 def run_password( oldpass, newpass ) if( oldpass && oldpass.length > 0 ) self.input = [ oldpass, newpass, newpass ] else self.input = [ newpass, newpass ] end self.run( "password" ) end
# File lib/P4.rb, line 274 def run_resolve( *args ) if( block_given? ) self.run( "resolve", args ) do |default| yield( default ) end else self.run( "resolve", args ) end end
Simple interface for shelving. Same rules as for submit apply
# File lib/P4.rb, line 240 def run_shelve( *args ) form = nil nargs = args.flatten.collect do |a| if( a.kind_of?( Hash ) ) form = a nil else a end end.compact if( form ) self.input = form nargs.push( "-i" ) end return self.run( "shelve", nargs ) end
Simple interface for submitting. If any argument is a Hash, (or subclass thereof - like P4::Spec), then it will be assumed to contain the change form. All other arguments are passed on to the server unchanged.
# File lib/P4.rb, line 218 def run_submit( *args ) form = nil nargs = args.flatten.collect do |a| if( a.kind_of?( Hash ) ) form = a nil else a end end.compact if( form ) self.input = form nargs.push( "-i" ) end return self.run( "submit", nargs ) end
Simple interface to 'p4 tickets'
# File lib/P4.rb, line 288 def run_tickets path = self.ticket_file # return an empty array if the file doesn't exist # or is a directory. results = Array.new re = Regexp.new( /([^=]*)=(.*):([^:]*)$/ ) if( File.exist?( path ) and !File.directory?( path ) ) File.open( path ) do |file| file.each_line do |line| res = re.match( line ) if( res ) tickets = { 'Host' => res[1], 'User' => res[2], 'Ticket' => res[3] } results.push( tickets ) end end end end return results end
Allow users to run commands using a specified handler. Pass a handler and the block that will be executed using this handler The handler will be reset to its previous value at the end of this block
# File lib/P4.rb, line 410 def with_handler( handler ) return self unless block_given? old_handler = self.handler self.handler = handler begin yield( self ) ensure self.handler = old_handler end self end