#!/usr/bin/ruby #-- #------------------------------------------------------------------------------- #++ # # == Introduction # # This script ensures that the default client view offered to users # for new client workspaces is based on a template client specified # in the trigger definition. # # == Synopsis # # defaultclient.rb # # == Sample Trigger Definition # # Using a trigger spec like this: # # defaultclient out client "defaultclient.rb baseclient %formname% %formfile%" # # would make the default client view offered to any user based on # the view defined in the client called 'baseclient'. Changing that # client spec would change the default view. # #-- #------------------------------------------------------------------------------- #++ require "P4" # # Show a message explaining correct usage of this script and exit. # def croakusage() puts < Where: - the name of an existing client workspace on which all future client workspaces should be based (by default). - The name of the client spec the user's asking for - The name of the temporary file in which the current client spec is stored EOS exit( 1 ) end # # Check whether or not a client already exists. # def client_exists?( p4, name ) p4.run_clients.each do |c| if( c[ "client" ] == name ) return true end end return false end # # Load the skeleton spec from the formfile # def load_spec( formfile ) f = File.open( formfile, "r" ) buf = f.read f.close buf end # # Update the existing formfile with a new spec. Since the comments # that appear at the top of a spec file are quite useful to the users, # we preserve them and only update the end of the file with the new spec. # def write_spec( spec, formfile ) comments = "" File.open( formfile, "r" ) do |f| f.each_line do |line| if( line[0...1] != '#' ) break end comments += line end end comments += "\n" File.open( formfile, "w" ) do |f| f.write( comments ) f.write( spec ) end end #-- #------------------------------------------------------------------------------- # START OF MAIN SCRIPT #------------------------------------------------------------------------------- #++ croakusage() unless ARGV.length() == 3 template = ARGV.shift; client = ARGV.shift; formfile = ARGV.shift; exit( 0 ) if( client == template) p4 = P4.new p4.parse_forms p4.exception_level = 1 begin p4.connect exit( 0 ) if( client_exists?( p4, client ) ) exit( 0 ) unless( client_exists?( p4, template ) ) # First fetch the template spec. This will cause our Perforce # server to execute this script again in another thread/process # but because of the first test after argument parsing above we # don't get stuck in a loop. Remove that test at your peril. # # Note that this also gets the specdef for clients from this # server so that P4Ruby can cache it. tspec = p4.fetch_client( template ) cspec = p4.parse_client( load_spec( formfile ) ) # Now copy across the options and view from the template clientspec. For # the view, we have to replace the template name with the client name as # we copy. cspec[ "View" ] = tspec[ "View" ].collect do |mapping| mapping.sub( "//#{template}/", "//#{client}/" ) end cspec[ "Options" ] = tspec[ "Options" ] # Now update the existing formfile with the new spec. write_spec( p4.format_client( cspec ), formfile ) rescue P4Exception puts( "A trigger script encountered an error:" ) p4.errors.each do |e| $stderr.puts( e ) puts( "\t" + e ) end puts( "Please contact your system administrator" ) exit( 1 ) end