#!/usr/bin/env ruby require 'optparse' require 'ostruct' require 'cgi' options = OpenStruct.new OptionParser.new do |opts| opts.banner = "USAGE: mandy-local my_script.rb local_input_file local_output_folder [options]" opts.on("-v", '--variables name=value', "Pass additional parameters to jobs") do |config| options.cmdenv = config end opts.on("-j", '--json {"key":"1 value"}', "Pass JSON encoded parameters to jobs") do |config| options.cmdenv = "json=#{CGI.escape(config)}" end opts.on_tail("-h", "--help", "Show this message") do puts opts exit end end.parse! exec('mandy-local -h') unless ARGV.size >= 3 require "fileutils" def absolute_path(path) path =~ /^\// ? path : File.join(Dir.pwd, path) end def set_env(opts_string) opts_string.split(' ').each do |pair| key, value = pair.split("=") ENV[key] = value end end set_env(options.cmdenv) if options.cmdenv file = absolute_path(ARGV[0]) input = ARGV[1].split(",").map {|f| absolute_path(f)}.join(" ") output_folder = FileUtils.mkdir_p(absolute_path(ARGV[2])) require file out = nil Mandy::Job.jobs.each_with_index do |job, i| out = File.join(output_folder, "#{i+1}-#{job.name.downcase.gsub(/\W/, '-')}") puts "Running #{job.name}..." reduce_phase = job.reducer_defined? ? %(| sort | mandy-reduce #{file} "#{job.name}") : '' `cat #{input} | mandy-map #{file} "#{job.name}" #{reduce_phase} > #{out}` input = out end puts out