lib/capsaicin/files/remote.rb in capsaicin-0.1.9 vs lib/capsaicin/files/remote.rb in capsaicin-0.1.10

- old
+ new

@@ -15,10 +15,18 @@ end EODEF end end + FILE_TESTS.each do |m,t| + class_eval <<-EODEF + def #{m}(a, options={}) + _t "test #{t}", a + end + EODEF + end + def chmod(mode, list, options={}) _r 'chmod', Array(list).unshift(mode.to_s(8)) end def chmod_R(mode, list, options={}) @@ -34,18 +42,10 @@ src << '-m' << options[:mode] end _r 'install', src.push(dest) end - FILE_TESTS.each do |m,t| - class_eval <<-EODEF - def #{m}(a, options={}) - _t "test #{t}", a - end - EODEF - end - def tail_f(file, n=10) cmd = "tail -n #{n} -f #{_q file}" case v = _via when :system, :local_run send v, cmd @@ -54,10 +54,19 @@ end rescue Interrupt logger.trace "interrupted (Ctrl-C)" if logger end + def put(data, path, options={}) + case _via + when :system, :local_run + FileUtils::Verbose.copy_stream StringIO.new(from), to + else + upload StringIO.new(data), path, options + end + end + def upload(from, to, options={}, &block) case _via when :system, :local_run cp from, to else @@ -68,15 +77,24 @@ else tof = File.basename to to2, to = to, "/tmp/#{tof}-#{Time.now.utc.to_i}" end end + options = options.dup + mode = options.delete(:mode) @config.upload(from, to, options, &block) if to2 - run "chmod 0644 #{to}" - cp to, to2 - run "rm -f #{to}" + begin + run "chmod 0644 #{to}", :via=>:run + cp to, to2 + if mode + mode = mode.is_a?(Numeric) ? '0'+mode.to_s(8) : mode.to_s + run "chmod #{mode} #{to2}" + end + ensure + run "rm -f #{to}", :via=>:run + end end end end def download(*args) @@ -86,31 +104,9 @@ else @config.download(*args) end end - def put(data, path, options={}) - case _via - when :system, :local_run - FileUtils::Verbose.copy_stream StringIO.new(from), to - else - if _via.to_s[0,4] == 'sudo' - if path[-1]==?/ || path[-1]==?\\ || directory?(path) - abort "Target path is a directory!" - else - pathf = File.basename path - path2, path = path, "/tmp/#{pathf}-#{Time.now.utc.to_i}" - end - end - @config.put(data, path, options) - if path2 - run "chmod 0644 #{path}" - cp path, path2 - run "rm -f #{path}" - end - end - end - def cd(dir, options={}) if block_given? dir, dir2 = pwd, dir _r 'cd', dir2 yield