#!/usr/local/bin/ruby
#
# Copyright 2005 Perforce Software. All rights reserved.
#
#
# ejsjobin in job "job_in.rb %user% %formfile%"
#
if ARGV[0] == nil || ARGV[1] == nil then
print "Trigger Error: Missing argument\n"
exit 1
end
require "P4Data.rb"
require "P4Form.rb"
require "P4Valid.rb"
require "P4Logic.rb"
require "P4Mail.rb"
#
# Initialize key variables
#
invalid = [] # No initial invalid fields
rejected = "" # No initial rejection statement
user=ARGV[0]
jobfile=ARGV[1]
datestamp = Time.now.strftime("%Y/%m/%d %H:%M:%S")
#
# Read in tmp file for revised/new job details
#
t_file = File.new(jobfile, "r")
t_form = P4Form.new(t_file)
t_file.close
logfile = nil
File.open( ##DEBUG
P4LOGDIR + "jobin.log", ##DEBUG
"a") do |logfile| ##DEBUG
logfile.printf("%s User(%s) JobFile(%s)\n", datestamp, user, jobfile) ##DEBUG
if t_form.userid != nil then
user = t_form.userid
logfile.printf(" Changing User to %s\n", user) ##DEBUG
logfile.printf(" Chain: %s\n", t_form.chain.join(", ")) ##DEBUG
logfile.printf("TempFile:\n") ##DEBUG
t_form.print(logfile) ##DEBUG
end
#
# Extract the jobid for comparing with any previous version of job
#
job = t_form.fields[P4JOBFIELD]
logfile.printf("JobID: [%s]\n", job) ##DEBUG
if "#{job}" != "new" && isjobid(job) then
#
# Retrieve last version of job for comparison
#
a_form = nil
if t_form.revnum != nil then
rev = t_form.revnum
a_form =
P4Form.new(`#{P4_BIN} -u #{P4USER} -p #{P4PORT} print -q //spec/job/#{job}#{P4SPECSUFFIX}\##{rev}`.split("\n"))
logfile.printf("Using Rev: %s\n", rev) ##DEBUG
else
a_form = P4Form.new(`#{P4_BIN} -u #{P4USER} -p #{P4PORT} print -q //spec/job/#{job}#{P4SPECSUFFIX}`.split("\n"))
end
#
# Derive changed, added, deleted, unchanged field information
#
in_both = a_form.fields.keys & t_form.fields.keys
deleted = a_form.fields.keys - t_form.fields.keys
added = t_form.fields.keys - a_form.fields.keys
changed = []
for f in in_both
if a_form.fields[f].strip != t_form.fields[f].strip then
changed << f
end
end
same = in_both - changed
logfile.printf("NEW vs OLD JOB DETAILS:\n") ##DEBUG
logfile.printf("Unchanged fields: %s\n", same.join(", ")) ##DEBUG
logfile.printf("Deleted fields: %s\n", deleted.join(", ")) ##DEBUG
logfile.printf("Added fields: %s\n", added.join(", ")) ##DEBUG
logfile.printf("Changed fields: %s\n", changed.join(", ")) ##DEBUG
#
# Run validation checks against any new or changed fields
#
invalid = t_form.validate_fields((changed + added), P4VALIDATION)
if invalid.length == 0 && t_form.userid == nil then
#
# Check for user changes to READONLY fields
#
P4READONLY.each do |field|
logfile.printf("RO:%s\n", field) ##DEBUG
if changed.include?(field) || added.include?(field) ||
deleted.include?(field) then
logfile.printf("Change to RO Field: %s\n", field) ##DEBUG
invalid << field
else ##DEBUG
logfile.printf("RO Field OK: %s\n", field) ##DEBUG
end
end
end
#
# RP4RELATEDJOBSFIELD cannot contain this job
#
rel_jobs = extract_set(t_form.fields[P4RELATEDJOBSFIELD])
if rel_jobs.include?(job) then
logfile.printf("%s references self %s\n", P4RELATEDJOBSFIELD, job)
logfile.printf("Related jobs: %s\n", rel_jobs.join(", "))
invalid << P4RELATEDJOBSFIELD
end
if invalid.length == 0 then
#
# No invalid fields so run job changed logic from P4Data
#
cleanup_sets(t_form.fields, P4SETS, changed + added)
rejected = job_changed(datestamp, user, logfile,
t_form, a_form, changed, added, deleted)
end
else
logfile.printf("New job\n") ##DEBUG
#
# Run validation against all fields
#
invalid = t_form.validate_fields(t_form.fields.keys, P4VALIDATION)
if invalid.length == 0 && t_form.userid == nil then
#
# Check for user entries for READONLY fields
#
P4READONLY.each do |field|
logfile.printf("NewRO:%s\n", field) ##DEBUG
if t_form.fields.keys.include?(field) then
logfile.printf("Added RO Field: %s\n", field) ##DEBUG
invalid << field
else ##DEBUG
logfile.printf("RO Field OK: %s\n", field) ##DEBUG
end
end
end
if invalid.length == 0 then
#
# No invalid fields so run new job logic from P4Data
#
cleanup_sets(t_form.fields, P4SETS, t_form.fields.keys)
rejected = job_added(datestamp, user, logfile,
t_form)
end
end
if invalid.length > 0 then ##DEBUG
logfile.printf("Invalid Fields: %s\n", invalid.join(", ")) ##DEBUG
elsif rejected != "" then ##DEBUG
logfile.printf("Rejected: %s\n", rejected) ##DEBUG
else ##DEBUG
logfile.printf("REVISED FILE:\n") ##DEBUG
t_form.print(logfile) ##DEBUG
logfile.printf("\n") ##DEBUG
end ##DEBUG
end # close logfile ##DEBUG
if invalid.length > 0 then
$stdout.printf("%s contain(s) invalid value(s)\n", invalid.join(", "))
exit 1 # Not OK to proceed
end
if rejected != "" then
$stdout.printf("%s\n", rejected)
exit 1 # Not OK to proceed
end
#
# Update Job File
#
t_file = File.new(jobfile, "w")
t_form.print(t_file)
t_file.close
exit 0 # OK to proceed