class NliPipeline::FileManager

Manage example files

EXAMPLE:

>> NliPipeline::FileManager.new.copy_example_files()
=> Moving 1 .EXAMPLE files

Public Class Methods

new(**kwargs) click to toggle source

automatically set backup path init_with_attrs handles the rest @param kwargs [Hash] all keyword (only supported_args) will be added as vars

# File lib/nli_pipeline/file_manager.rb, line 38
def initialize(**kwargs)
  # backup_path should not be configurable
  # set it directly regardless of whether it's passed in
  kwargs[:backup_path] = "#{kwargs[:path]}/**/*.backup*"
  init_with_attrs(**kwargs)
end
required_args() click to toggle source

@see NliPipeline::AbstractUtil#init_with_attrs @see NliPipeline::AbstractUtil::ClassMethods#required_args? @return [Array]

# File lib/nli_pipeline/file_manager.rb, line 31
def self.required_args
  [:path]
end
supported_args() click to toggle source

static methods required by NliPipeline::AbstractUtil::init_attrs @see NliPipeline::AbstractUtil#init_with_attrs @see NliPipeline::AbstractUtil#get_allowed_args @return [Hash]

# File lib/nli_pipeline/file_manager.rb, line 20
def self.supported_args
  {
    path: '', extension: '.EXAMPLE', debug: false,
    fail_on_error: false, log_dir: '.pipeline_logs',
    created_get_log_name: 'created_files_', backup_path: ''
  }
end

Public Instance Methods

all_backups() click to toggle source

get backup dates in order from newest to oldest

# File lib/nli_pipeline/file_manager.rb, line 91
def all_backups
  backup_files = Dir.glob(@backup_path)
  backup_numbers = backup_files.map { |x| x.split('.backup').last }
  backup_numbers.uniq.sort
end
all_example_files() click to toggle source

@return [Array] all example files under @path

# File lib/nli_pipeline/file_manager.rb, line 46
def all_example_files
  example_file_path = "#{@path}/**/*#{@extension}"
  example_files = Dir.glob(example_file_path)
  if example_files.empty?
    raise ArgumentError, "No #{@extension} Files found at #{example_file_path}"
  end
  puts("Moving #{example_files.count} #{@extension} files".green)
  example_files
end
backup_non_example_files(example_files, time_stamp, command = 'cp') click to toggle source

for each example file, if a non-example version of that file exists, back it up.

@param example_files [Array] list of example files @param time_stamp [String] time of backup @param command [String] command to run (cp/mv)

# File lib/nli_pipeline/file_manager.rb, line 104
def backup_non_example_files(example_files, time_stamp, command = 'cp')
  possible_files = example_files.map { |f| f.gsub(@extension, '') }
  files_to_backup = possible_files.select { |file| File.exist?(file) }
  # raise ArgumentError.new("#{files_to_backup.to_s} #{example_files.to_s}")
  return false if files_to_backup.empty?

  # preset backup time so all backups fomr same batch have the same timestamp
  puts("Backing up #{files_to_backup.count} files".green)
  files_to_backup.each do |file_to_backup|
    call_system("#{command} #{file_to_backup} #{file_to_backup}.backup#{time_stamp}")
  end
end
copy_example_files(command = 'cp') click to toggle source

copy all files ending in @extension under @path. if a non-exmaple version of the file exists, back it up. @param command [String] command to run (cp/mv)

# File lib/nli_pipeline/file_manager.rb, line 72
def copy_example_files(command = 'cp')
  time_stamp = Time.now.strftime('%Y%m%d%H%M')
  puts("Setting up pipeline in #{@path}".yellow)
  example_files = all_example_files

  backup_non_example_files(example_files, time_stamp)

  example_files.each do |example_file|
    target_file = example_file.gsub(@extension, '')
    call_system("#{command} #{example_file} #{target_file}")

    if File.exist?(target_file)
      add_to_log(target_file, time_stamp)
      puts("\t add #{target_file} to log #{get_log_name(time_stamp)}") if @debug
    end
  end
end
handle_backup_user_input(backups) click to toggle source

keeps asking the use to choose a backup until they choose a valid one @param backups [Array] @return [Array]

# File lib/nli_pipeline/file_manager.rb, line 143
def handle_backup_user_input(backups)
  puts "which backup would you like to load?\n#{backups}".yellow
  backup_date = STDIN.gets.chomp

  until backups.include?(backup_date)
    puts "#{backup_date} is not in backups".red
    puts "please choose from: #{backups}".yellow
    backup_date = STDIN.gets.chomp
  end
  ["#{@path}/**/*.backup#{backup_date}", backup_date]
end
last_created_files() click to toggle source

read latest log in .pipeline_logs @return [String] files created by last setup_pipeline

# File lib/nli_pipeline/file_manager.rb, line 58
def last_created_files
  logs = Dir.glob("#{@path}/#{@log_dir}/*")
  if logs.empty?
    puts("No logs found in #{@path}/#{@log_dir}".red)
    return false
  end
  log_numbers = logs.map { |x| x.split(@created_get_log_name.to_s).last }
  latest_log_number = log_numbers.uniq.min
  File.readlines(get_log_name(latest_log_number))
end
load_from_backup(command = 'cp') click to toggle source

@param command [String] system command.

use mv to remove backup,
use cp to keep backup.

@return Boolean

# File lib/nli_pipeline/file_manager.rb, line 121
def load_from_backup(command = 'cp')
  puts("loading backup in #{@path}".yellow)
  backups = all_backups
  if backups.empty?
    puts("No backups found in #{@backup_path}".red)
    return false
  end

  backup_path, backup_date = handle_backup_user_input(backups)
  files_to_load = Dir.glob(backup_path)
  puts "loading #{files_to_load.count} files from #{backup_path}".green

  files_to_load.each do |backup_file|
    target_file = backup_file.gsub("\.backup#{backup_date}", '')
    call_system("#{command} #{backup_file} #{target_file}")
  end
end

Private Instance Methods

add_to_log(line, date) click to toggle source

appends to log file @param line [String] line to append to log file @param date [String]

# File lib/nli_pipeline/file_manager.rb, line 172
def add_to_log(line, date)
  create_log_dir # if log doesn't exist
  File.open(get_log_name(date), 'a') { |file| file.write(line) }
end
create_log_dir() click to toggle source

create dir for pipeline logs

# File lib/nli_pipeline/file_manager.rb, line 158
def create_log_dir
  full_path_to_log = "#{@path}/#{@log_dir}"
  Dir.mkdir(full_path_to_log) unless File.directory?(full_path_to_log)
end
get_log_name(date) click to toggle source

@param date [String] @return [String] full path to log

# File lib/nli_pipeline/file_manager.rb, line 165
def get_log_name(date)
  "#{@path}/#{@log_dir}/#{@created_get_log_name}#{date}"
end