require 'zipruby' require 'net/http/post/multipart' module Transcriptic::Command # upload and run a protocol # class Run < Base # run [FILENAME|DIRECTORY] # # upload FILENAME or DIRECTORY and launch it # def index path = args.shift fd = create_protocol_fd_for_path(path) if 1 == fd error "Couldn't create run!" end display "Uploading `#{path}` to Transcriptic..." run_id = transcriptic.create_run(fd)["run_id"] display "Run launched (#{run_id})" end # transcriptic run:analyze [FILENAME|DIRECTORY] # # upload FILENAME or DIRECTORY and analyze it # def analyze path = args.shift end # transcriptic run:status [RUNID] # # show details of RUNID # def status run_id = args.shift if run_id.nil? error("Usage: transcriptic status RUNID\nMust specify RUNID to get run status.") end ret = transcriptic.status(run_id) if ret.nil? error("#{run_id} not found for #{transcriptic.user}") return end display(ret.inspect) end # transcriptic run:list # # list active runs # def list ret = transcriptic.list if ret.empty? error("No runs for #{transcriptic.user}") return end display("Runs:") ret.each do |run| display " #{run.name}" end end # transcriptic run:logs RUNID # # get log data for RUNID # def logs run_id = args.shift transcriptic.read_logs(run_id) end private def create_protocol_fd_for_path(path) begin stat = File.stat(path) rescue display "No such path: #{path}" return 1 end upfile = Tempfile.new('protocol') if stat.directory? files = Pathname.glob("#{path}/**/**") file_count = files.reject(&:directory?).length dir_count = files.reject(&:file?).length display "Package detected, compressing #{file_count} files (#{dir_count} directories)..." Zip::Archive.open(upfile.path, Zip::CREATE) do |ar| ar.add_dir(path) Dir.glob("#{path}/**/**").each do |path| if File.directory?(path) ar.add_dir(path) else ar.add_file(path, path) end end end else Zip::Archive.open(upfile.path, Zip::CREATE) do |ar| ar.add_file(path, path) end end upfile end end end