Module: Advance
- Defined in:
- lib/advance.rb,
lib/advance/version.rb
Constant Summary collapse
- RESET =
"\e[0m"- BOLD =
"\e[1m"- ITALIC =
"\e[3m"- UNDERLINE =
"\e[4m"- CYAN =
"\e[36m"- GRAY =
"\e[37m"- GREEN =
"\e[32m"- MAGENTA =
"\e[35m"- RED =
"\e[31m"- WHITE =
"\e[1;37m"- YELLOW =
"\e[33m"- VERSION =
"0.1.13"
Instance Method Summary collapse
- #add_dir_to_path(dir) ⇒ Object
- #advance(processing_mode, label, command) ⇒ Object
- #clean_previous_step_dirs(dir_prefix) ⇒ Object
- #do_command(command, feedback = true) ⇒ Object
-
#ensure_bin_on_path ⇒ Object
def find_step_dir dirs = Dir.entries(“.”) dirs.find { |d| d =~ /^#step_dir_prefix($step)/ } end.
- #file_path_template(dir_path, files) ⇒ Object
- #find_step_dir(dir_prefix) ⇒ Object
- #get_previous_dir_path ⇒ Object
- #multi(label, command, previous_dir_path, dir_prefix, dir_name) ⇒ Object
- #previous_file_path(previous_dir_path) ⇒ Object
- #single(label, command, previous_dir_path, dir_prefix, dir_name) ⇒ Object
- #static(processing_mode, label, command) ⇒ Object
- #static_dir_prefix(step_no) ⇒ Object
- #step_dir_prefix(step_no) ⇒ Object
- #work_in_sub_dir(dir_name) ⇒ Object
Instance Method Details
#add_dir_to_path(dir) ⇒ Object
189 190 191 192 193 194 195 |
# File 'lib/advance.rb', line 189 def add_dir_to_path(dir) bin_dir = File.(dir) path = ENV["PATH"] return if path.include?(bin_dir) ENV["PATH"] = [path, bin_dir].join(":") end |
#advance(processing_mode, label, command) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/advance.rb', line 21 def advance(processing_mode, label, command) $redo_mode ||= :checking $step ||= 0 previous_dir_path = get_previous_dir_path $step += 1 dir_prefix = step_dir_prefix($step) dir_name = "#{dir_prefix}_#{label}" 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) end |
#clean_previous_step_dirs(dir_prefix) ⇒ Object
70 71 72 73 74 75 |
# File 'lib/advance.rb', line 70 def clean_previous_step_dirs(dir_prefix) while (step_dir = find_step_dir(dir_prefix)) puts "## removing #{step_dir}" FileUtils.rm_rf step_dir end end |
#do_command(command, feedback = true) ⇒ Object
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/advance.rb', line 157 def do_command(command, feedback = true) puts "#{YELLOW}#{command}#{RESET} " if feedback start_time = Time.now stdout, stderr, status = Open3.capture3(command) elapsed_time = Time.now - start_time File.open("log", "w") do |f| f.puts "%%% command: >#{command}<" f.puts "%%% returned status: >#{status}<" f.puts "%%% elapsed time: #{elapsed_time} seconds" f.puts "%%% stdout:" f.puts stdout f.puts "%%% stderr:" f.puts stderr end if !status.success? raise "step #{$step} failed with #{status}\n#{stderr}" end end |
#ensure_bin_on_path ⇒ Object
def find_step_dir
dirs = Dir.entries(".")
dirs.find { |d| d =~ /^#{step_dir_prefix($step)}/ }
end
181 182 183 184 185 186 187 |
# File 'lib/advance.rb', line 181 def ensure_bin_on_path advance_path = File.dirname(__FILE__) add_dir_to_path(advance_path) caller_path = File.dirname(caller[0].split(/:/).first) add_dir_to_path(caller_path) end |
#file_path_template(dir_path, files) ⇒ Object
147 148 149 150 151 152 153 154 155 |
# File 'lib/advance.rb', line 147 def file_path_template(dir_path, files) file = files.first file_path = File.join(dir_path, file) if File.directory?(file_path) File.join(dir_path, "{file}", "{file}") else File.join(dir_path, "{file}") end end |
#find_step_dir(dir_prefix) ⇒ Object
77 78 79 80 |
# File 'lib/advance.rb', line 77 def find_step_dir(dir_prefix) dirs = Dir.entries(".") dirs.find { |d| d =~ /^#{dir_prefix}/ } end |
#get_previous_dir_path ⇒ Object
52 53 54 55 56 57 58 59 60 |
# File 'lib/advance.rb', line 52 def get_previous_dir_path relative_path = case $step when 0 "." else File.join(".", Dir.entries(".").find { |d| d =~ /^#{step_dir_prefix($step)}/ }) end File.(relative_path) end |
#multi(label, command, previous_dir_path, dir_prefix, dir_name) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/advance.rb', line 97 def multi(label, command, previous_dir_path, dir_prefix, 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 = "" progress_proc = ->(index, max_index) do latest_progress = sprintf("%3i%", index.to_f / max_index * 100) puts latest_progress if last_progress != latest_progress 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 puts "#{YELLOW}#{command}#{RESET}" work_in_sub_dir(file) do do_command command, no_feedback end rescue puts "%%%% error while processing >>#{file_path}<<" raise end end end end |
#previous_file_path(previous_dir_path) ⇒ Object
143 144 145 |
# File 'lib/advance.rb', line 143 def previous_file_path(previous_dir_path) Find.find(previous_dir_path).reject { |p| FileTest.directory?(p) || File.basename(p) == "log" }.first end |
#single(label, command, previous_dir_path, dir_prefix, dir_name) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/advance.rb', line 82 def single(label, command, previous_dir_path, dir_prefix, 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\}/ command.gsub!("{previous_dir}", previous_dir_path) end if command =~ /\{file\}/ command.gsub!("{file}", File.basename(previous_file_path(previous_dir_path))) end do_command command end end |
#static(processing_mode, label, command) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/advance.rb', line 38 def static(processing_mode, label, command) $redo_mode ||= :checking $step ||= 0 previous_dir_path = get_previous_dir_path dir_prefix = static_dir_prefix($step) dir_name = "#{dir_prefix}_#{label}" 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) end |
#static_dir_prefix(step_no) ⇒ Object
66 67 68 |
# File 'lib/advance.rb', line 66 def static_dir_prefix(step_no) "static_%03d" % [step_no] end |
#step_dir_prefix(step_no) ⇒ Object
62 63 64 |
# File 'lib/advance.rb', line 62 def step_dir_prefix(step_no) "step_%03d" % [step_no] end |
#work_in_sub_dir(dir_name) ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/advance.rb', line 125 def work_in_sub_dir(dir_name) if $redo_mode == :checking && Dir.exist?(dir_name) return end $redo_mode = :replacing tmp_dir_name = "tmp_#{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 end |