lib/ratch/batchable.rb in ratch-0.2.2 vs lib/ratch/batchable.rb in ratch-0.2.3

- old
+ new

@@ -1,30 +1,56 @@ +# TITLE: +# +# Batchable +# +# COPYING: +# +# Copyright (c) 2007 Psi T Corp. +# +# This file is part of the ProUtils' Ratch program. +# +# Ratch is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ratch is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ratch. If not, see <http://www.gnu.org/licenses/>. + module Ratch class BatchManager # Task cache, which prevents batch runs from re-executing. attr :cache # New BatchManager. - def initialize + def initialize(runspace) + @runspace = runspace @cache = {} end # def batch(batchfile, arguments=nil) @cache[batchfile] ||= run(batchfile, arguments) end - # TODO How to handle arguments? + # Run a batch file. + # TODO: How to handle arguments? + def run(batchfile, arguments=nil) - BatchFile.new(batchfile).call - # # TODO probably should raise error instead - # abort "missing batch file -- #{batchfile}" unless File.file?(batchfile) - # script = File.read($0 = batchfile) - # #instance_eval(script) - # eval(script, binding, $0) + # # TODO probably should raise error instead + # abort "missing batch file -- #{batchfile}" unless File.file?(batchfile) + + #BatchFile.new(batchfile).call # Old way with batch execution context object. + script = File.read($0 = batchfile) + eval(script, $batch_binding, $0) end # def done?(batchfile) batchfile == $0 or @cache.key?(batchfile) @@ -36,11 +62,11 @@ module Batchable # Reference batch manager. def batch_manager - @batch_manager ||= BatchManager.new + @batch_manager ||= BatchManager.new(self) end # Batch run, ie. run and cache. # Usually this can be take care of by method_missing. # But, in some cases, built in method names block task @@ -69,21 +95,24 @@ puts "--> system call: #{cmd}" if trace? system(cmd) end end + # Is a file a local batch file? + + def batch?(path) + b = File.dirname($0) + "/#{path}" + b.chomp!('!') + b if FileTest.file?(b) && FileTest.executable?(b) + end + # Abort running. #def abort(msg=nil) # puts msg if msg # exit 0 #end - end - - # - module OpenBatchable - # If method is missing try to run an external task # or binary by that name. If it is a binary, arguments # translate into commandline parameters. For example: # # tar 'foo/', :x=>true, :v=>true, :z=>true, :f=>'foo.tar.gz' @@ -105,32 +134,33 @@ def method_missing(sym,*args) puts "method_missing: #{sym}" if debug? name = sym.to_s - task = task?(name) - done = task && done?(task) - cache = task && !done && name[1,-1] != '!' - bin = bin?(name) if (!task || done) - none = task && done && !bin - #task = name if bin + bat = batch?(name) + done = bat && done?(bat) + cache = bat && !done && name[1,-1] != '!' + bin = bin?(name) if (!bat || done) + none = bat && done && !bin + #bat = name if bin - return super unless task || bin + return super unless bat || bin return if none # nothing to do params = args.to_params if bin cmd = "#{File.basename(bin)} #{params}" res = sh(cmd) - elsif task - cmd = "./#{task} #{params}" + elsif bat + cmd = "./#{bat} #{params}" puts "--> #{cache ? '' : 'not-'}cached execution: #{cmd}" if trace? - res = run(task, args) + res = batch(bat, args) if cache - #@batch_catch[task] ||= (system(cmd); true) - batch_catch[task] ||= res + #@batch_catch[bat] ||= (system(cmd); true) + #batch_cache[bat] ||= res + batch_manager.cache ||= res end end return res end