#!/usr/software/bin/ruby-2.2.2 $VERBOSE = nil require 'find' require 'parallel' require 'thread' test_root_dir = File.expand_path('../../', __FILE__) puts "Test Root Dir: #{test_root_dir}\n" find_dirs = !ARGV.empty? ? ARGV : %w(NACL) find_dirs.map! { |dir| "#{test_root_dir}/#{dir}" } test_root_dir = test_root_dir.chomp Dir.chdir("#{test_root_dir}/NACL") puts "Current working directory: " puts Dir.pwd codeline = `/usr/software/bin/bash -c 'p4 where | cut -d " " -f1'` codeline = codeline.chomp puts "Codeline is: #{codeline} \n" work_sp = `/usr/software/bin/bash -c 'p4 opened #{codeline} | cut -d "#" -f1 | cut -d "/" -f10-'` puts "Opened Files : #{work_sp} \n" puts "Directory to run natelint is : #{find_dirs} \n" #work_sp = '' #-- hard coded to test scheduled run with no opened files. #Array to contain files that needs to be run with natelint. files = [] #Hash to contain mapping of change and files related to change. change = Hash.new #Get the files to run natelint on, according to the cit-nacl run (either scheduled or manual from user workspace) if ( defined?(work_sp) && work_sp == '' ) # will now return true or false puts "No opened files found, looks like a scheduled cit run. \n" date_threehr_back = `/usr/software/bin/bash -c 'TZ=PST8PDT date --date="3 hours ago" +%Y/%m/%d:%T'` date_threehr_back = date_threehr_back.chomp puts "date 3 hour back : #{date_threehr_back}\n" changes = `/usr/software/bin/bash -c 'p4 changes -s submitted -t #{codeline}@"#{date_threehr_back}","@now" | cut -d " " -f 2'` puts "List of changes submitted in last three hours : #{changes} \n" changes.each_line do |ch_number| ch_number = ch_number.chomp list_file = `/usr/software/bin/bash -c 'p4 files #{codeline}@="#{ch_number}" | cut -d "#" -f1 | cut -d "/" -f10- '` change["#{ch_number}"] = list_file list_file.each_line do |file| file = file.chomp file = "#{find_dirs[0]}/#{file}" files << file if file =~ /\.(thpl|pl|pm)$/ end end else puts "There are opened files, looks to be triggered from client workspace, extracting opened files to run natelint. \n" work_sp.each_line do |file| file = file.chomp file = "#{find_dirs[0]}/#{file}" files << file if file =~ /\.(thpl|pl|pm)$/ end end files.sort! files = files.uniq #List of files to validate. puts 'Files To Validate:' files.each do |file| puts file end puts '' #Code to run natelint on each file and capture the failed result and associated change number. failed_results = [] num_threads = 8 num_threads = ENV['CHECK_NATELINT_MAX_THREADS'].to_i if ENV.key?('CHECK_NATELINT_MAX_THREADS') puts "Number of threads: #{num_threads}" Parallel.each(files, in_threads: num_threads) do |file| print "Validating #{file}\n" result = '' if file =~ /\.pm$/ || file =~ /\.thpl$/ rel_file = file.gsub("#{test_root_dir}/NACL/", '') result = `/usr/software/bin/bash -c 'cd "#{test_root_dir}/NACL" ; /usr/software/test/bin/natelint -prepend_inc "#{test_root_dir}" -quiet #{rel_file} 2>&1'` else result = `/usr/software/bin/bash -c '/usr/software/test/bin/natelint -prepend_inc "#{test_root_dir};../" -quiet #{file} 2>&1'` end result = result.split("\n") result.reject! do |line| line =~ /ERROR: Attempt to DESTROY object ID/ || line =~ /NATE.+Sockets/ || line =~ /Constant subroutine nfs::Rpc::GSS/ || # natelint internally has these if this script is called in a subtest line =~ /main::.+used only once.+Tharn\.pm/ end result.map! { |line| line.gsub(/^.*syntax OK.*$/, 'syntax OK') } if result.any? { |line| line !~ /syntax OK/ } failed_results.push("Validation Failed For #{file}:\n#{result.join("\n")}") if ( defined?(work_sp) && work_sp == '' ) change.each do |key, value| value.each_line do |ch_file| ch_file = ch_file.chomp ch_file = "#{find_dirs[0]}/#{ch_file}" if ch_file == file failed_results.push("Change associated with file #{file} : #{key} \n") end end end end end end puts '' if !failed_results.empty? puts 'Validation Failed' failed_results.each do |result| puts "#{result}\n" end exit 254 else puts 'Validation Succeeded' exit 0 end