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.3.16"
Class Method Summary collapse
Instance Method Summary collapse
- #add_dir_to_path(dir) ⇒ Object
- #advance(processing_mode, label, command) ⇒ Object
- #capture_column_names_from_csv ⇒ Object
- #clean_previous_step_dirs(dir_prefix) ⇒ Object
- #count_files(dir) ⇒ Object
- #do_command(command, feedback = true) ⇒ Object
- #do_command_wo_log(command, feedback = true) ⇒ Object
- #ensure_bin_on_path ⇒ Object
- #file_path_template(dir_path, files) ⇒ Object
- #find_step_dir(dir_prefix) ⇒ Object
- #get_previous_dir_path ⇒ Object
- #multi(command, previous_dir_path, dir_name) ⇒ Object
- #previous_file_path(previous_dir_path) ⇒ Object
- #single(command, previous_dir_path, dir_name) ⇒ Object
- #static(processing_mode, label, command) ⇒ Object
- #static_dir_prefix(step_no) ⇒ Object
- #step_dir_prefix(step_no) ⇒ Object
- #strip_extensions(dir_name) ⇒ Object
- #update_meta(step_number, processing_mode, label, command, start_time, duration, file_count) ⇒ Object
- #work_in_sub_dir(dir_name) ⇒ Object
Class Method Details
.included(pipeline_module) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/advance.rb', line 24 def self.included(pipeline_module) $pipeline = caller_locations.first.path = if File.exist?(".meta") JSON.parse(File.read(".meta")) else {} end last_run_number = ["last_run_number"] ||= -1 $run_number = last_run_number + 1 $cores=`nproc`.to_i puts "Multi steps will use #{$cores} cores" $verbose_logging = case ENV["ADVANCE_VERBOSE_LOGGING"] when "true"; true when "false"; false when nil puts "For detailed logging of multi steps, rerun after 'export ADVANCE_VERBOSE_LOGGING=true'" false else puts "env variable ADVANCE_VERBOSE_LOGGING should be 'true', 'false', or not present (defaults to 'false')" puts "currently set to >#{ENV["ADVANCE_VERBOSE_LOGGING"]}<" end end |
Instance Method Details
#add_dir_to_path(dir) ⇒ Object
331 332 333 334 335 336 337 |
# File 'lib/advance.rb', line 331 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
90 91 92 93 94 95 96 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 |
# File 'lib/advance.rb', line 90 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}" if $redo_mode != :checking || !(File.exist?(dir_name) || File.exist?(dir_name + '.tgz')) clean_previous_step_dirs(dir_prefix) if previous_dir_path =~ /\.tgz$/ do_command_wo_log "tar xzf #{previous_dir_path}" end previous_dir_path = previous_dir_path.gsub(/\.tgz$/, "") start_time = Time.now send(processing_mode, command, previous_dir_path, dir_name) file_count = count_files(dir_name) duration = Time.now - start_time ($step, processing_mode, label, command, start_time, duration, file_count) end previous_dir_path = previous_dir_path.gsub(/\.tgz$/, "") if File.basename(previous_dir_path) =~ /^step_/ if !File.exist?("#{previous_dir_path}.tgz") do_command_wo_log "tar czf #{previous_dir_path}.tgz #{File.basename(previous_dir_path)}" end do_command_wo_log "rm -rf #{previous_dir_path}" end end |
#capture_column_names_from_csv ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/advance.rb', line 77 def capture_column_names_from_csv if $step.nil? raise "capture_column_names_from_csv cannot be the first step" end previous_dir_path = get_previous_dir_path input_file_path = previous_file_path(previous_dir_path) CSV.foreach(input_file_path, :headers => true) do |row| $column_names = row.headers.map(&:to_sym) break end end |
#clean_previous_step_dirs(dir_prefix) ⇒ Object
166 167 168 169 170 171 |
# File 'lib/advance.rb', line 166 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 |
#count_files(dir) ⇒ Object
123 124 125 126 127 128 129 130 131 132 |
# File 'lib/advance.rb', line 123 def count_files(dir) file_count = 0 Find.find(dir) do |path| next if File.directory?(path) next if File.basename(path) == "log" next if File.basename(path) =~ /^\./ file_count += 1 end file_count end |
#do_command(command, feedback = true) ⇒ Object
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 |
# File 'lib/advance.rb', line 286 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 |
#do_command_wo_log(command, feedback = true) ⇒ Object
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 |
# File 'lib/advance.rb', line 305 def do_command_wo_log(command, feedback = true) puts "#{YELLOW}#{command}#{RESET} " if feedback stdout, stderr, status = Open3.capture3(command) if !status.success? error_msg = [ "step #{$step} failed", "%%% command: >#{command}<", "%%% returned status: >#{status}<", "%%% stdout:", stdout, "%%% stderr:", stderr ].join("\n") raise error_msg end end |
#ensure_bin_on_path ⇒ Object
323 324 325 326 327 328 329 |
# File 'lib/advance.rb', line 323 def ensure_bin_on_path advance_bin_path = File.(File.join(File.dirname(__FILE__), "../bin")) add_dir_to_path(advance_bin_path) caller_path = File.dirname(caller[0].split(/:/).first) add_dir_to_path(caller_path) end |
#file_path_template(dir_path, files) ⇒ Object
276 277 278 279 280 281 282 283 284 |
# File 'lib/advance.rb', line 276 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
173 174 175 176 |
# File 'lib/advance.rb', line 173 def find_step_dir(dir_prefix) dirs = Dir.entries(".") dirs.find { |d| d =~ /^#{dir_prefix}/ } end |
#get_previous_dir_path ⇒ Object
148 149 150 151 152 153 154 155 156 |
# File 'lib/advance.rb', line 148 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(command, previous_dir_path, dir_name) ⇒ Object
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/advance.rb', line 192 def multi(command, previous_dir_path, dir_name) work_in_sub_dir(dir_name) do file_paths = Find.find(previous_dir_path).reject { |p| File.basename(p) =~ %r(^\.) || FileTest.directory?(p) || File.basename(p) == "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 path_relative_to_step_dir = File.dirname(file_path.gsub(%r(^#{previous_dir_path}/?), "")) path_relative_to_step_dir = path_relative_to_step_dir == "." ? "" : path_relative_to_step_dir basename = File.basename(file_path) new_dir_name = path_relative_to_step_dir == "" ? basename : File.join(path_relative_to_step_dir, basename) root_file_name = basename.gsub(%r(\.[^.]+$), '') command.gsub!("{input_dir}", File.dirname(file_path)) command.gsub!("{input_file}", file_path) command.gsub!("{file_name}", basename) command.gsub!("{file_name_without_extension}", root_file_name) puts "#{YELLOW}#{command}#{RESET} " if $verbose_logging work_in_sub_dir(new_dir_name) do do_command command, $verbose_logging end rescue puts "%%%% error while processing >>#{file_path}<<" raise end end end end |
#previous_file_path(previous_dir_path) ⇒ Object
272 273 274 |
# File 'lib/advance.rb', line 272 def previous_file_path(previous_dir_path) Find.find(previous_dir_path).reject {|p| File.basename(p) =~ %r(^\.) || FileTest.directory?(p) || File.basename(p) == "log"}.first end |
#single(command, previous_dir_path, dir_name) ⇒ Object
178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/advance.rb', line 178 def single(command, previous_dir_path, dir_name) work_in_sub_dir(dir_name) do input_file_path = previous_file_path(previous_dir_path) basename = File.basename(input_file_path) root_file_name = basename.gsub(%r(\.[^.]+$), '') command.gsub!("{input_dir}", previous_dir_path) command.gsub!("{input_file}", input_file_path) command.gsub!("{file_name}", basename) command.gsub!("{file_name_without_extension}", root_file_name) do_command command end end |
#static(processing_mode, label, command) ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/advance.rb', line 134 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, command, previous_dir_path, dir_name) end |
#static_dir_prefix(step_no) ⇒ Object
162 163 164 |
# File 'lib/advance.rb', line 162 def static_dir_prefix(step_no) "static_%03d" % [step_no] end |
#step_dir_prefix(step_no) ⇒ Object
158 159 160 |
# File 'lib/advance.rb', line 158 def step_dir_prefix(step_no) "step_%03d" % [step_no] end |
#strip_extensions(dir_name) ⇒ Object
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 |
# File 'lib/advance.rb', line 248 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 |
#update_meta(step_number, processing_mode, label, command, start_time, duration, file_count) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/advance.rb', line 49 def (step_number, processing_mode, label, command, start_time, duration, file_count) = if File.exist?(".meta") JSON.parse(File.read(".meta")) else {} end ["pipeline"] ||= $pipeline ["last_run_number"] = $run_number ["runs"] ||= [] step_data = { "step_number" => step_number, "start_time" => start_time, "duration" => duration, "file_count" => file_count, "processing_mode" => processing_mode, "label" => label, "command" => command, "columns" => $column_names } ["runs"][$run_number] ||= [] ["runs"][$run_number] << step_data File.write(".meta", JSON.pretty_generate()) end |
#work_in_sub_dir(dir_name) ⇒ Object
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
# File 'lib/advance.rb', line 226 def work_in_sub_dir(dir_name) starting_dir = FileUtils.pwd stripped_dir_name = File.join(*(strip_extensions(dir_name).split("/").uniq)) if $redo_mode == :checking && Dir.exist?(stripped_dir_name) return end $redo_mode = :replacing dirs = stripped_dir_name.split("/") dirs[-1] = "tmp_#{dirs[-1]}" tmp_dir = File.join(dirs) FileUtils.rm_rf tmp_dir FileUtils.mkdir_p tmp_dir FileUtils.cd tmp_dir yield FileUtils.cd starting_dir FileUtils.mv tmp_dir, stripped_dir_name end |