P4Record (Class)

In: p4table.rb
Parent: Object

*******************************************************************************

  • Main record manipulation class. Handles the loading and saving of record
  • as both jobs and files.

*******************************************************************************

Methods

abandon   add_file   create   delete   each_file   exists=   exists?   exists?   file_list   get_file   load   metafile   method_missing   new   obliterate   query   rm_file   save  

Attributes

files  [R] 
id  [RW]  ***************************************************************************
 Instance methods

***************************************************************************

meta  [RW]  ***************************************************************************
 Instance methods

***************************************************************************

p4  [RW]  ***************************************************************************
 Instance methods

***************************************************************************

seq  [RW]  ***************************************************************************
 Instance methods

***************************************************************************

spec  [RW]  ***************************************************************************
 Instance methods

***************************************************************************

table  [RW]  ***************************************************************************
 Instance methods

***************************************************************************

Public Class methods

[Source]

     # File p4table.rb, line 315
315:     def P4Record.create( p4, table )
316:         id = P4RecId.next( p4, table )
317:         rec = P4Record.new( p4, table, id )
318:         rec.spec = p4.fetch_job( id.to_s )
319:         return rec
320:     end

[Source]

     # File p4table.rb, line 325
325:     def P4Record.exists?( p4, id )
326:         jobs = p4.run_jobs( "-e", "job=" + id.to_s )
327:         return jobs.length > 0 
328:     end

[Source]

     # File p4table.rb, line 268
268:     def P4Record.load( p4, table, id )
269: 
270:         if ( ! P4Record.exists?( p4, id ) )
271:             raise( RuntimeError, "Record #{id.to_s} does not exist", caller )
272:         end
273: 
274:         rec = P4Record.new( p4, table, id )
275:         rec.spec = p4.fetch_job( id.to_s )
276:         if rec.spec.has_key?( "files" )
277:             rec.spec[ "files" ].each do
278:                 |file|
279:                 name = file.sub( /.*\//, "" ).chomp
280:                 f = P4RecFile.new( p4, name, rec ) 
281:                 f.exists = true
282:                 f.depot_path = file.chomp
283:                 rec.files[ name ] =  f
284:             end
285:         end
286:         rec.exists = true
287:         return rec
288:     end

[Source]

     # File p4table.rb, line 333
333:     def initialize( p4, table, id )
334:         @p4    = p4
335:         @id    = id
336:         @seq   = id.seq
337:         @table = table
338:         @spec  = nil
339:         @exists        = false
340:         @files = Hash.new
341: 
342:         @files[ "meta" ] = P4RecFile.new( @p4, "meta", self )
343:     end

[Source]

     # File p4table.rb, line 293
293:     def P4Record.query( p4, table, expr )
294:         expr = "job=#{table} & ( " + expr + " )"
295:         p4.run_jobs( "-e", expr ).collect do
296:             |job|
297:             rec = P4Record.new(p4, table, P4RecId.new_from_job( p4,job["Job"]))
298:             rec.spec = job
299:             rec.spec[ "files" ].each do
300:                 |file|
301:                 name = file.sub( /.*\//, "" ).chomp
302:                 f = P4RecFile.new( p4, name, id ) 
303:                 f.exists = true
304:                 f.depot_path = file
305:                 rec.files[ name ] =  f
306:             end
307:             rec.exists = true
308:             rec
309:         end
310:     end

Public Instance methods

[Source]

     # File p4table.rb, line 418
418:     def abandon
419:         each_file do
420:             |f|
421:             fs = p4.run_fstat( f.ws_path )
422:             if ( fs && fs[ "action" ] == "add" )
423:                 File.unlink( f.ws_path )
424:             end
425:             p4.run_revert( f.depot_path )
426:         end
427:     end

[Source]

     # File p4table.rb, line 392
392:     def add_file( name )
393:         if ( name == "meta" )
394:             raise( RuntimeError, "The meta file already exists.", caller )
395:         end
396: 
397:         nfile = P4RecFile.new( p4, name, self )
398:         nfile.add
399:         @files[ "name" ] =  nfile
400:     end

[Source]

     # File p4table.rb, line 470
470:     def delete( desc )
471:         if ( ! exists? )
472:             raise( RuntimeError, "Can't delete. Record doesn't exist.", caller )
473:         end
474: 
475:         @files.values.each { |f| f.delete }
476:         change = p4.fetch_change
477:         change[ "Description" ] = desc
478:         p4.submit_spec( change )
479:         p4.run_job( "-d", @spec[ "Job" ] )
480:         @files = Hash.new
481:         @spec[ "files" ] = Array.new
482:         @exists = false
483:     end

[Source]

     # File p4table.rb, line 385
385:     def each_file
386:         @files.each_value { |f| yield( f ) }
387:     end

[Source]

     # File p4table.rb, line 362
362:     def exists=( bool )
363:         @exists = bool
364:     end

[Source]

     # File p4table.rb, line 355
355:     def exists?
356:         return @exists
357:     end

[Source]

     # File p4table.rb, line 432
432:     def file_list
433:         @files.values.collect do
434:             |f|
435:             f.depot_path
436:         end.compact.join( "\n" )
437:     end

[Source]

     # File p4table.rb, line 370
370:     def get_file( name )
371:         return @files[ name ] if @files.has_key?( name )
372:         raise( RuntimeError, "Record contains no file called #{name}", caller )
373:     end

[Source]

     # File p4table.rb, line 378
378:     def metafile
379:         get_file( "meta" )
380:     end

[Source]

     # File p4table.rb, line 513
513:     def method_missing( meth, *args )
514:         meth = meth.to_s
515:         raise if ( meth[0..0] != "_" )
516:         meth = meth[ 1..-1 ]
517:         if ( meth =~ /^(.*)=$/ )
518:             meth = $1
519:             @spec[ meth ] = args.shift
520:         elsif ( args.length == 0 && @spec.has_key?( meth ) )
521:             @spec[ meth ]
522:         else
523:             ""
524:         end
525:     end

[Source]

     # File p4table.rb, line 489
489:     def obliterate
490:         if ( ! exists? )
491:             raise( RuntimeError, "Can't oblit. Record doesn't exist.", caller )
492:         end
493: 
494:         args = @files.values.collect { |f| f.depot_path } 
495:         el = p4.exception_level?
496:         begin
497:             p4.exception_level = 1
498:             p4.run_sync( args.collect { |a| a += "#none" } )
499:         ensure
500:             p4.exception_level = el
501:         end
502:         p4.run_obliterate( "-y", args )
503:         p4.run_job( "-d", @spec[ "Job" ] )
504:         @spec[ "files" ] = Array.new
505:         @files = Hash.new
506:         @exists = false
507:     end

[Source]

     # File p4table.rb, line 405
405:     def rm_file( file )
406:         if ( file == "meta" )
407:             raise( RuntimeError, "You can't delete the meta file", caller )
408:         end
409: 
410:         f = get_file( file )
411:         f.delete
412:         @files.delete( file )
413:     end

[Source]

     # File p4table.rb, line 443
443:     def save( desc )
444:         # First rewrite the files list in case it's been modified
445:         @spec[ "files" ] = file_list
446: 
447:         # Next update the job with the values in the spec
448:         p4.save_job( @spec )
449: 
450:         # Now update the metafile with the job -o output
451:         if ( exists? )
452:             metafile.edit
453:         else
454:             metafile.add
455:         end
456: 
457:         metafile.write( p4.format_job( @spec ) )
458: 
459:         # Now submit
460:         change = p4.fetch_change
461:         change[ "Description" ] = desc
462:         p4.submit_spec( change )
463: 
464:         @exists = true
465:     end

[Validate]