bin/run_workflow.rb in rbbt-util-3.2.1 vs bin/run_workflow.rb in rbbt-util-4.0.0

- old
+ new

@@ -1,118 +1,191 @@ #!/usr/bin/env ruby -require 'rbbt-util' +require 'rbbt' require 'rbbt/util/simpleopt' -require 'rbbt/util/workflow' +require 'rbbt/workflow' require 'pp' def usage(task) puts task.usage exit -1 end -def SOPT_options(task) +def SOPT_options(workflow, task) sopt_options = [] - task.option_summary.flatten.each do |info| - name = info[:name] + workflow.rec_inputs(task.name).each do |name| short = name.to_s.chars.first - boolean = info[:type] == :boolean + boolean = workflow.rec_input_types(task.name)[name].to_sym == :boolean sopt_options << "-#{short}--#{name}#{boolean ? '' : '*'}" end sopt_options * ":" end -def fix_options(task, job_options) - option_types = task.option_summary.flatten.inject({}){|types, new| types[new[:name]] = new[:type]; types} +def fix_options(workflow, task, job_options) + option_types = workflow.rec_input_types(task.name) + workflow.resolve_locals(job_options) + job_options_cleaned = {} job_options.each do |name, value| - value = case - when option_types[name] == :float + value = case option_types[name].to_sym + when :float value.to_f - when option_types[name] == :integer + when :integer value.to_i - when option_types[name] == :array - value.split(/[,|]/) - when option_types[name] == :tsv - begin - if value == '-' - TSV.new(STDIN).to_s :sort + when :string, :text + case + when value == '-' + STDIN.read + when (String === value and File.exists?(value)) + Open.read(value) + else + value + end + when :array + if Array === value + value + else + case + when value == '-' + STDIN.read + when (String === value and File.exists?(value)) + Open.read(value) else - TSV.new(value).to_s :sort - end - rescue + value + end.split(/[,|\s]/) + end + when :tsv + if TSV === value value + else + begin + if value == '-' + TSV.open(STDIN).to_s :sort + else + TSV.new(value).to_s :sort + end + rescue + value + end end else value end job_options_cleaned[name] = value end job_options_cleaned end -options = SOPT.get "-t--task*:-l--log*:-h--help:-n--name:-cl--clean:-rcl-recursive_clean" +options = SOPT.get "-t--task*:-l--log*:-h--help:-n--name*:-cl--clean:-rcl-recursive_clean:-pn--printname:-srv--server" -# Set log, fork, clean, recursive_clean and help -Log.severity = options[:log].to_i if options.include? :log -help = !!options.delete(:help) -do_fork = !!options.delete(:fork) -clean = !!options.delete(:clean) -recursive_clean = !!options.delete(:recursive_clean) - -# Get workflow workflow = ARGV.first -WorkFlow.require_workflow workflow -# Set task -namespace, task = nil, nil +if options[:server] + require 'rbbt/util/log' + require 'rbbt/workflow' + require 'rbbt/workflow/rest' + require 'sinatra' + require 'compass' -case -when (not options[:task]) - workflow_usage if help - task = self.last_task - namespace = self -when (options[:task] =~ /\./) - namespace, task = options.delete(:task).split('.') - namespace = Misc.string2const(namespace) + Workflow.require_workflow workflow + WorkflowREST.add_workflows Workflow.workflows.last + + WorkflowREST.setup else - task_name = options.delete(:task) - task = self.tasks[task_name] -end -usage(task) if help + # Set log, fork, clean, recursive_clean and help + Log.severity = options[:log].to_i if options.include? :log + help = !!options.delete(:help) + do_fork = !!options.delete(:fork) + clean = !!options.delete(:clean) + recursive_clean = !!options.delete(:recursive_clean) -name = options.delete(:name) || "Default" -# get job args -sopt_option_string = SOPT_options(task) -job_options = SOPT.get sopt_option_string -job_options = fix_options(task, job_options) + # Get workflow + + if Rbbt.etc.remote_workflows.exists? + remote_workflows = Rbbt.etc.remote_workflows.yaml + else + remote_workflows = {} + end -#- get job -job = task.job(name, job_options) + if remote_workflows.include? workflow + require 'rbbt/workflow/rest/client' + workflow = RbbtRestClient.new remote_workflows[workflow] + else + Workflow.require_workflow workflow + workflow = Workflow.workflows.last + end -# clean job -job.clean if clean -job.recursive_clean if recursive_clean + # Set task + namespace, task = nil, nil -# run -if do_fork - job.fork - while not job.done? - puts "#{job.step}: #{job.messages.last}" - sleep 2 + case + when (not options[:task]) + usage if help + task = workflow.last_task + namespace = workflow + when (options[:task] =~ /\./) + namespace, task = options.delete(:task).split('.') + namespace = Misc.string2const(namespace) + else + task_name = options.delete(:task) + task = workflow.tasks[task_name] + raise "Task not found: #{ task_name }" if task.nil? end -else - job.run -end -#- error -raise job.messages.last if job.error? -#print -pp job.load + workflow.usage(task) if help + + name = options.delete(:name) || "Default" + + # get job args + sopt_option_string = SOPT_options(workflow,task) + job_options = SOPT.get sopt_option_string + job_options = fix_options(workflow,task, job_options) + + #- get job + job = workflow.job(task.name, name, job_options) + + # clean job + job.clean if clean + job.recursive_clean if recursive_clean + + # run + if do_fork + job.fork + while not job.done? + Log.debug "#{job.step}: #{job.messages.last}" + sleep 2 + end + raise job.messages.last if job.error? + res = job.load + else + res = job.run + end + + if options.delete(:printname) + puts job.name + exit + else + Log.low "Job name: #{job.name}" + end + + #- error + + #print + case + when Array === res + puts res * "\n" + when TSV === res + puts res + when Hash === res + puts res.to_yaml + else + puts res + end +end