Class: P4

Inherits:
Object
  • Object
show all
Defined in:
lib/P4.rb,
lib/P4/version.rb

Overview

Add the extra’s written purely in ruby.

Defined Under Namespace

Classes: DepotFile, Integration, MergeInfo, OutputHandler, ReportHandler, Revision, SSOHandler, Spec

Constant Summary collapse

RAISE_NONE =

Named constants for the exception levels. Note they are cumulative, so RAISE_ALL includes RAISE_ERRORS (as you’d expect).

0
RAISE_ERRORS =
1
RAISE_ALL =
2
MERGE_SKIP =

Named values for merge actions. Values taken from clientmerge.h in the Perforce API

1
MERGE_ACCEPT_MERGED =
2
MERGE_ACCEPT_EDIT =
3
MERGE_ACCEPT_THEIRS =
4
MERGE_ACCEPT_YOURS =
5
EV_NONE =

Named values for generic error codes returned by P4::Message#generic

0
EV_USAGE =

The fault of the user

0x01
EV_UNKNOWN =

request not consistent with dox

0x02
EV_CONTEXT =

using unknown entity

0x03
EV_ILLEGAL =

using entity in wrong context

0x04
EV_NOTYET =

trying to do something you can’t

0x05
EV_PROTECT =

something must be corrected first

0x06
EV_EMPTY =

No fault at all

0x11
EV_FAULT =

not the fault of the user

0x21
EV_CLIENT =

inexplicable program fault

0x22
EV_ADMIN =

client side program errors

0x23
EV_CONFIG =

server administrative action required

0x24
EV_UPGRADE =

client configuration inadequate

0x25
EV_COMM =

client or server too old to interact

0x26
EV_TOOBIG =

communications error

0x27
E_EMPTY =

Named values for error severities returned by P4::Message#severity

0
E_INFO =

nothing yet

1
E_WARN =

something good happened

2
E_FAILED =

something not good happened

3
E_FATAL =

user did something wrong

4
REPORT =

OutputHandler return values constants

0
HANDLED =
1
CANCEL =
2
PROG_NORMAL =

Client progress ‘done’ state

0
PROG_DONE =
1
PROG_FAILDONE =
2
PROG_FLUSH =
3
SSO_PASS =

SSO Handler return values constants

0
SSO_FAIL =

SSO succeeded (result is an authentication token)

1
SSO_UNSET =

SSO failed (result will be logged as error message)

2
SSO_EXIT =

Client has no SSO support

3
SSO_SKIP =

Stop login process

4
SpecTypes =

Mappings for P4#each_<spec> Hash of type vs. key

{
  "clients" => ["client", "client"],
  "labels" => ["label", "label"],
  "branches" => ["branch", "branch"],
  "changes" => ["change", "change"],
  "streams" => ["stream", "Stream"],
  "jobs" => ["job", "Job"],
  "users" => ["user", "User"],
  "groups" => ["group", "group"],
  "depots" => ["depot", "name"],
  "servers" => ["server", "Name"],
}
Version =
VERSION = '2025.2.2863692'

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *a) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/P4.rb', line 169

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

Instance Method Details

#at_exception_level(level) ⇒ Object

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.



424
425
426
427
428
429
430
431
432
433
434
# File 'lib/P4.rb', line 424

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

#delete_shelve(*args) ⇒ Object



292
293
294
295
296
297
# File 'lib/P4.rb', line 292

def delete_shelve( *args )
  if( ! args.include?( "-c" ) )
    args.unshift( "-c")
  end
  return self.run( "shelve", "-d", args)
end

#inspectObject

Show some handy information when using irb



456
457
458
459
460
# File 'lib/P4.rb', line 456

def inspect
  sprintf( 'P4: [%s] %s@%s (%s)',
          self.port, self.user, self.client,
          self.connected? ? 'connected' : 'not connected' )
end

#run_filelog(*args) ⇒ Object

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



370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
# File 'lib/P4.rb', line 370

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 ) )
          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

#run_login(*args) ⇒ Object

Simple interface for using “p4 login”



302
303
304
305
# File 'lib/P4.rb', line 302

def ( *args )
  self.input = self.password
  return self.run( "login", args )
end

#run_password(oldpass, newpass) ⇒ Object

Interface for changing the user’s password. Supply the old password and the new one.



347
348
349
350
351
352
353
354
# File 'lib/P4.rb', line 347

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

#run_resolve(*args) ⇒ Object



307
308
309
310
311
312
313
314
315
316
# File 'lib/P4.rb', line 307

def run_resolve( *args )
  if( block_given? )
    self.run( "resolve", args ) do
      |default|
      yield( default )
    end
  else
    self.run( "resolve", args )
  end
end

#run_shelve(*args) ⇒ Object

Simple interface for shelving. Same rules as for submit apply



273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/P4.rb', line 273

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

#run_submit(*args) ⇒ Object

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.



251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/P4.rb', line 251

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

#run_ticketsObject

Simple interface to ‘p4 tickets’



321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'lib/P4.rb', line 321

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

#with_handler(handler) ⇒ Object

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



441
442
443
444
445
446
447
448
449
450
451
# File 'lib/P4.rb', line 441

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