lib/advance.rb in advance-0.1.13 vs lib/advance.rb in advance-0.2.0

- old
+ new

@@ -1,7 +1,8 @@ -require "advance/version" +require_relative "./advance/version" require "find" +require "fileutils" require "open3" require "team_effort" module Advance RESET="\e[0m" @@ -16,10 +17,12 @@ MAGENTA="\e[35m" RED="\e[31m" WHITE="\e[1;37m" YELLOW="\e[33m" + puts "loading module" + def advance(processing_mode, label, command) $redo_mode ||= :checking $step ||= 0 previous_dir_path = get_previous_dir_path @@ -30,11 +33,11 @@ puts "#{CYAN}advance #{$step} #{label}#{WHITE}... #{RESET}" return if $redo_mode == :checking && Dir.exist?(dir_name) clean_previous_step_dirs(dir_prefix) - send(processing_mode, label, command, previous_dir_path, dir_prefix, dir_name) + send(processing_mode, command, previous_dir_path, dir_name) end def static(processing_mode, label, command) $redo_mode ||= :checking $step ||= 0 @@ -44,11 +47,11 @@ puts "#{CYAN}static #{$step} #{label}#{WHITE}... #{RESET}" return if $redo_mode == :checking && Dir.exist?(dir_name) FileUtils.rm_rf dir_name - send(processing_mode, label, command, previous_dir_path, dir_prefix, dir_name) + send(processing_mode, command, previous_dir_path, dir_name) end def get_previous_dir_path relative_path = case $step when 0 @@ -77,11 +80,11 @@ def find_step_dir(dir_prefix) dirs = Dir.entries(".") dirs.find { |d| d =~ /^#{dir_prefix}/ } end - def single(label, command, previous_dir_path, dir_prefix, dir_name) + def single(command, previous_dir_path, dir_name) work_in_sub_dir(dir_name) do if command =~ /\{previous_file\}/ command.gsub!("{previous_file}", previous_file_path(previous_dir_path)) end if command =~ /\{previous_dir\}/ @@ -92,11 +95,11 @@ end do_command command end end - def multi(label, command, previous_dir_path, dir_prefix, dir_name) + def multi(command, previous_dir_path, dir_name) no_feedback = false work_in_sub_dir(dir_name) do file_paths = Find.find(previous_dir_path).reject { |path| FileTest.directory?(path) || File.basename(path) == "log" } last_progress = "" @@ -106,12 +109,12 @@ last_progress = latest_progress end TeamEffort.work(file_paths, $cores, progress_proc: progress_proc) do |file_path| begin file = File.basename(file_path) - command.gsub!("{file_path}", file_path) unless $step == 1 - command.gsub!("{file}", file) unless $step == 1 + command.gsub!("{file_path}", file_path) + command.gsub!("{file}", file) puts "#{YELLOW}#{command}#{RESET}" work_in_sub_dir(file) do do_command command, no_feedback end rescue @@ -121,27 +124,52 @@ end end end def work_in_sub_dir(dir_name) - if $redo_mode == :checking && Dir.exist?(dir_name) + stripped_dir_name = strip_extensions(dir_name) + if $redo_mode == :checking && Dir.exist?(stripped_dir_name) return end $redo_mode = :replacing - tmp_dir_name = "tmp_#{dir_name}" + tmp_dir_name = "tmp_#{stripped_dir_name}" FileUtils.rm_rf tmp_dir_name FileUtils.mkdir_p tmp_dir_name FileUtils.cd tmp_dir_name yield FileUtils.cd ".." - FileUtils.mv tmp_dir_name, dir_name + FileUtils.mv tmp_dir_name, stripped_dir_name end + def strip_extensions(dir_name) + extensions = %w( + csv + csv_nh + geo_json + geojson + gz + json + tar + tgz + zip + ) + + changed_dir_name = dir_name + last_dir_name = nil + until last_dir_name == changed_dir_name do + last_dir_name = changed_dir_name + extensions.each do |extension| + changed_dir_name = changed_dir_name.gsub(%r(\.#{extension}$), "") + end + end + changed_dir_name + end + def previous_file_path(previous_dir_path) Find.find(previous_dir_path).reject { |p| FileTest.directory?(p) || File.basename(p) == "log" }.first end def file_path_template(dir_path, files) @@ -171,14 +199,9 @@ if !status.success? raise "step #{$step} failed with #{status}\n#{stderr}" end end - # def find_step_dir - # dirs = Dir.entries(".") - # dirs.find { |d| d =~ /^#{step_dir_prefix($step)}/ } - # end - # def ensure_bin_on_path advance_path = File.dirname(__FILE__) add_dir_to_path(advance_path) caller_path = File.dirname(caller[0].split(/:/).first)