From 4211ccb1838b1f03c59e2ceaee37041733377329 Mon Sep 17 00:00:00 2001 From: April Rieger Date: Thu, 22 Feb 2024 11:06:47 -0800 Subject: [PATCH 1/3] First pass at adding the script, need to make sure the paths work on demo aka prod env --- app/jobs/process_files_job.rb | 42 +++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 app/jobs/process_files_job.rb diff --git a/app/jobs/process_files_job.rb b/app/jobs/process_files_job.rb new file mode 100644 index 00000000..120a2e4c --- /dev/null +++ b/app/jobs/process_files_job.rb @@ -0,0 +1,42 @@ +# app/jobs/process_files_job.rb +class ProcessFilesJob < ApplicationJob + queue_as :default + + def perform(file_path) + puts "file_path=#{file_path}" + # Extract the directory where the file is located and the base name without .txt extension + dir_path = File.dirname(file_path) + puts "dir_path=#{dir_path}" + base_name = File.basename(file_path, '.txt') + puts "base_name=#{base_name}" + + # Create a new directory named after the base name of the file + new_dir_path = File.join(dir_path, base_name) + puts "new_dir_path=#{new_dir_path}" + FileUtils.mkdir_p(new_dir_path) unless Dir.exist?(new_dir_path) + + # Process the file lines as before + file_lines = File.readlines(file_path) + + file_lines.each do |line| + filename = line.strip.sub(/^cpb-aacip-/, '') + '.xml' + # Correctly execute the find command and capture its output + puts "before find . -type f -name '#{filename}'" + find_command = "find . -type f -name '#{filename}'" + puts "after find . -type f -name '#{filename}'" + found_files = `#{find_command}`.split("\n") + + # Adjust the destination_path to be the new directory + destination_path = File.join("./tmp/imports", base_name) + puts "destination_path=#{destination_path}" + FileUtils.mkdir_p(destination_path) unless Dir.exist?(destination_path) + + found_files.each do |found_file_path| + if File.exist?(found_file_path) + FileUtils.mv(found_file_path, destination_path) + puts "Moved #{found_file_path} (found_file_path) to #{destination_path} (destination_path)" + end + end + end + end +end From f4c5ded9077cbdeb6f73d9207ac514939f2f3783 Mon Sep 17 00:00:00 2001 From: April Rieger Date: Fri, 23 Feb 2024 13:40:51 -0800 Subject: [PATCH 2/3] Convert to script --- app/jobs/process_files_job.rb | 85 +++++++++++++++++++---------------- 1 file changed, 47 insertions(+), 38 deletions(-) mode change 100644 => 100755 app/jobs/process_files_job.rb diff --git a/app/jobs/process_files_job.rb b/app/jobs/process_files_job.rb old mode 100644 new mode 100755 index 120a2e4c..ea9fb106 --- a/app/jobs/process_files_job.rb +++ b/app/jobs/process_files_job.rb @@ -1,42 +1,51 @@ -# app/jobs/process_files_job.rb -class ProcessFilesJob < ApplicationJob - queue_as :default - - def perform(file_path) - puts "file_path=#{file_path}" - # Extract the directory where the file is located and the base name without .txt extension - dir_path = File.dirname(file_path) - puts "dir_path=#{dir_path}" - base_name = File.basename(file_path, '.txt') - puts "base_name=#{base_name}" - - # Create a new directory named after the base name of the file - new_dir_path = File.join(dir_path, base_name) - puts "new_dir_path=#{new_dir_path}" - FileUtils.mkdir_p(new_dir_path) unless Dir.exist?(new_dir_path) - - # Process the file lines as before - file_lines = File.readlines(file_path) - - file_lines.each do |line| - filename = line.strip.sub(/^cpb-aacip-/, '') + '.xml' - # Correctly execute the find command and capture its output - puts "before find . -type f -name '#{filename}'" - find_command = "find . -type f -name '#{filename}'" - puts "after find . -type f -name '#{filename}'" - found_files = `#{find_command}`.split("\n") - - # Adjust the destination_path to be the new directory - destination_path = File.join("./tmp/imports", base_name) - puts "destination_path=#{destination_path}" - FileUtils.mkdir_p(destination_path) unless Dir.exist?(destination_path) - - found_files.each do |found_file_path| - if File.exist?(found_file_path) - FileUtils.mv(found_file_path, destination_path) - puts "Moved #{found_file_path} (found_file_path) to #{destination_path} (destination_path)" - end +#!/usr/bin/env ruby +require 'fileutils' + +# Method to process files +def process_file(file_path) + puts "file_path=#{file_path}" + + # Extract the directory where the file is located and the base name without .txt extension + dir_path = File.dirname(file_path) + puts "dir_path=#{dir_path}" + base_name = File.basename(file_path, '.txt') + puts "base_name=#{base_name}" + + # Create a new directory named after the base name of the file + new_dir_path = File.join(dir_path, base_name) + puts "new_dir_path=#{new_dir_path}" + FileUtils.mkdir_p(new_dir_path) unless Dir.exist?(new_dir_path) + + # Process the file lines + file_lines = File.readlines(file_path) + + file_lines.each do |line| + filename = line.strip.sub(/^cpb-aacip-/, '') + '.xml' + # Correctly execute the find command and capture its output + puts "before find . -type f -name '#{filename}'" + find_command = "find . -type f -name '#{filename}'" + puts "after find . -type f -name '#{filename}'" + found_files = `#{find_command}`.split("\n") + + # Adjust the destination_path to be the new directory + destination_path = File.join("./tmp/imports", base_name) + puts "destination_path=#{destination_path}" + FileUtils.mkdir_p(destination_path) unless Dir.exist?(destination_path) + + found_files.each do |found_file_path| + if File.exist?(found_file_path) + FileUtils.mv(found_file_path, destination_path) + puts "Moved #{found_file_path} to #{destination_path}" end end end end + +# Main execution starts here +if ARGV.length != 1 + puts "Usage: #{$PROGRAM_NAME} file_path" + exit +end + +file_path = ARGV[0] +process_file(file_path) \ No newline at end of file From da575e292bb18d612e02754fbfd70d8034f4bd61 Mon Sep 17 00:00:00 2001 From: April Rieger Date: Fri, 23 Feb 2024 23:29:49 -0800 Subject: [PATCH 3/3] revert back git push --- app/jobs/process_files_job.rb | 85 ++++++++++++++++------------------- 1 file changed, 38 insertions(+), 47 deletions(-) diff --git a/app/jobs/process_files_job.rb b/app/jobs/process_files_job.rb index ea9fb106..120a2e4c 100755 --- a/app/jobs/process_files_job.rb +++ b/app/jobs/process_files_job.rb @@ -1,51 +1,42 @@ -#!/usr/bin/env ruby -require 'fileutils' - -# Method to process files -def process_file(file_path) - puts "file_path=#{file_path}" - - # Extract the directory where the file is located and the base name without .txt extension - dir_path = File.dirname(file_path) - puts "dir_path=#{dir_path}" - base_name = File.basename(file_path, '.txt') - puts "base_name=#{base_name}" - - # Create a new directory named after the base name of the file - new_dir_path = File.join(dir_path, base_name) - puts "new_dir_path=#{new_dir_path}" - FileUtils.mkdir_p(new_dir_path) unless Dir.exist?(new_dir_path) - - # Process the file lines - file_lines = File.readlines(file_path) - - file_lines.each do |line| - filename = line.strip.sub(/^cpb-aacip-/, '') + '.xml' - # Correctly execute the find command and capture its output - puts "before find . -type f -name '#{filename}'" - find_command = "find . -type f -name '#{filename}'" - puts "after find . -type f -name '#{filename}'" - found_files = `#{find_command}`.split("\n") - - # Adjust the destination_path to be the new directory - destination_path = File.join("./tmp/imports", base_name) - puts "destination_path=#{destination_path}" - FileUtils.mkdir_p(destination_path) unless Dir.exist?(destination_path) - - found_files.each do |found_file_path| - if File.exist?(found_file_path) - FileUtils.mv(found_file_path, destination_path) - puts "Moved #{found_file_path} to #{destination_path}" +# app/jobs/process_files_job.rb +class ProcessFilesJob < ApplicationJob + queue_as :default + + def perform(file_path) + puts "file_path=#{file_path}" + # Extract the directory where the file is located and the base name without .txt extension + dir_path = File.dirname(file_path) + puts "dir_path=#{dir_path}" + base_name = File.basename(file_path, '.txt') + puts "base_name=#{base_name}" + + # Create a new directory named after the base name of the file + new_dir_path = File.join(dir_path, base_name) + puts "new_dir_path=#{new_dir_path}" + FileUtils.mkdir_p(new_dir_path) unless Dir.exist?(new_dir_path) + + # Process the file lines as before + file_lines = File.readlines(file_path) + + file_lines.each do |line| + filename = line.strip.sub(/^cpb-aacip-/, '') + '.xml' + # Correctly execute the find command and capture its output + puts "before find . -type f -name '#{filename}'" + find_command = "find . -type f -name '#{filename}'" + puts "after find . -type f -name '#{filename}'" + found_files = `#{find_command}`.split("\n") + + # Adjust the destination_path to be the new directory + destination_path = File.join("./tmp/imports", base_name) + puts "destination_path=#{destination_path}" + FileUtils.mkdir_p(destination_path) unless Dir.exist?(destination_path) + + found_files.each do |found_file_path| + if File.exist?(found_file_path) + FileUtils.mv(found_file_path, destination_path) + puts "Moved #{found_file_path} (found_file_path) to #{destination_path} (destination_path)" + end end end end end - -# Main execution starts here -if ARGV.length != 1 - puts "Usage: #{$PROGRAM_NAME} file_path" - exit -end - -file_path = ARGV[0] -process_file(file_path) \ No newline at end of file