#!/usr/local/bin/ruby # # Copyright 2005 Perforce Software. All rights reserved. # # # ejsspecsave save spec "spec_save.rb %user% %formfile% %formname% %formtype%" # if ARGV[0] == nil || ARGV[1] == nil then print "Trigger Error: Missing argument\n" exit 1 end user=ARGV[0] jobfile=ARGV[1] formname=ARGV[2] formtype=ARGV[3] if formname != nil && formname == "job" && formtype != nil && formtype == "spec" then require "P4Form.rb" require "P4Data.rb" # # Paths to where the parsed information is to be saved # OLDP4VALIDITY=P4BINDIR + "P4Valid.old" P4VALIDITY=P4BINDIR + "P4Valid.rb" NEWP4VALIDITY=P4BINDIR + "P4Valid.new" LOGFILE=P4LOGDIR + "specsave.log" logfile = nil File.open(LOGFILE, "a") do |logfile| ##DEBUG datestamp = Time.now.strftime("%Y/%m/%d %H:%M:%S") logfile.printf("%s User(%s) Jobspec(%s) Formname(%s) Formtype(%s)\n",##DEBUG datestamp, user, jobfile, formname, formtype) ##DEBUG a_file = File.new(jobfile) a_form = P4Form.new(a_file) a_file.close logfile.printf("\nTmpFile:\n") ##DEBUG a_form.print(logfile) ##DEBUG # # Extract the Field:Value information from the Values section of # the jobspec # values = P4Form.new(a_form.fields["Values"].split("\n").collect!{|x| x.gsub(/^[ \t]*([^ \t]*) /,'\1: ')}) of = File.new(NEWP4VALIDITY, "w") of.printf("P4VALIDATION = {\n") listfields = {} setfields = {} # # For each entry in the Values field, generate the appropriate # field validation to be performed # values.fields.keys.each do |field| logfile.printf("Field[%s] Value[%s]\n", field, values.fields[field])##DEBUG of.printf("\t\"%s\" => ", field) case values.fields[field] when /^[ \t]*ListOf[(]/ listtype = values.fields[field].gsub(/^[ \t]*ListOf[(](.*)[)][ \t]*$/, '\1') listtype.strip! case listtype when /^Changelist[(][)]/ of.printf("\"islistof(\\\"$VALUE\\\", \\\"ischangelist\\\")\"") listfields[field] = "" when /^UserID[(][)]/ of.printf("\"islistof(\\\"$VALUE\\\", \\\"isuserid\\\")\"") listfields[field] = "" when /^JobID[(][)]/ of.printf("\"islistof(\\\"$VALUE\\\", \\\"isjobid\\\")\"") listfields[field] = "" when /^DepotPath[(][)]/ of.printf("\"islistof(\\\"$VALUE\\\", \\\"isdepotpath\\\")\"") listfields[field] = "" when /^FilePath[(][)]/ of.printf("\"islistof(\\\"$VALUE\\\", \\\"isfilepath\\\")\"") listfields[field] = "" when /^word/ of.printf("\"true\"") listfields[field] = "" else # Ignore unhandled validation specification of.printf("\"true\"") end when /^[ \t]*SetOf[(]/ listtype = values.fields[field].gsub(/^[ \t]*SetOf[(](.*)[)][ \t]*$/, '\1') listtype.strip! case listtype when /^Changelist[(][)]/ of.printf("\"islistof(\\\"$VALUE\\\", \\\"ischangelist\\\")\"") setfields[field] = "" when /^UserID[(][)]/ of.printf("\"islistof(\\\"$VALUE\\\", \\\"isuserid\\\")\"") setfields[field] = "" when /^JobID[(][)]/ of.printf("\"islistof(\\\"$VALUE\\\", \\\"isjobid\\\")\"") setfields[field] = "" when /^DepotPath[(][)]/ of.printf("\"islistof(\\\"$VALUE\\\", \\\"isdepotpath\\\")\"") setfields[field] = "" when /^FilePath[(][)]/ of.printf("\"islistof(\\\"$VALUE\\\", \\\"isfilepath\\\")\"") setfields[field] = "" when /^word/ of.printf("\"true\"") setfields[field] = "" else # Ignore unhandled validation specification of.printf("\"true\"") end when /^[ \t]*Changelist[(][)]/ of.printf("\"ischangelist(\\\"$VALUE\\\")\"") when /^[ \t]*UserID[(][)]/ of.printf("\"isuserid(\\\"$VALUE\\\")\"") when /^[ \t]*JobID[(][)]/ of.printf("\"isjobid(\\\"$VALUE\\\")\"") when /^[ \t]*DepotPath[(][)]/ of.printf("\"isdepotpath(\\\"$VALUE\\\")\"") when /^[ \t]*FilePath[(][)]/ of.printf("\"isfilepath(\\\"$VALUE\\\")\"") else # Ignore unhandled validation specification of.printf("\"true\"") end of.printf(",\n") end of.printf("}\n\n") # # Extract the Field:Value information from the Fields section # so that the format type of the field can be derived, either # 1 line or N-lines # fieldform = P4Form.new(a_form.fields["Fields"].split("\n").collect!{|x| x.gsub(/^[ \t]*([^ \t]*) /,'\1: ')}) of.printf("P4HISTORY = {\n") fieldform.fields.keys.each do |field| fieldinfo = fieldform.fields[field].split(/[ \t]+/) if setfields.keys.include?(fieldinfo[0]) then if fieldinfo[1] == "line" then if !P4IGNORE.include?(fieldinfo[0]) then of.printf("\t\"%s\" => \"set1\",\n", fieldinfo[0]) # one line end setfields[fieldinfo[0]] = "1" else if !P4IGNORE.include?(fieldinfo[0]) then of.printf("\t\"%s\" => \"setN\",\n", fieldinfo[0]) # multi-line end setfields[fieldinfo[0]] = "N" end elsif listfields.keys.include?(fieldinfo[0]) then if !P4IGNORE.include?(fieldinfo[0]) then of.printf("\t\"%s\" => \"%s\",\n", fieldinfo[0], fieldinfo[1]) end if fieldinfo[1] == "line" then listfields[fieldinfo[0]] = "1" else listfields[fieldinfo[0]] = "N" end elsif !P4IGNORE.include?(fieldinfo[0]) then of.printf("\t\"%s\" => \"%s\",\n", fieldinfo[0], fieldinfo[1]) end end of.printf("}\n\n") of.printf("P4SETS = {\n") setfields.keys.each do |field| of.printf("\t\"%s\" => \"%s\",\n", field, setfields[field]) end of.printf("}\n\n") of.printf("P4LISTS = {\n") listfields.keys.each do |field| of.printf("\t\"%s\" => \"%s\",\n", field, listfields[field]) end of.printf("}\n\n") of.close # # Replace any existing P4Valid.rb file with the new contents # using link/rename so that the action is atomic # begin File.delete(OLDP4VALIDITY) rescue => err # ignore file not found end begin File.link(P4VALIDITY, OLDP4VALIDITY) rescue => err # ignore file not found end File.rename(NEWP4VALIDITY, P4VALIDITY) end # close logfile ##DEBUG end exit 0 # OK to proceed