require 'zipruby' require 'net/http/post/multipart' module Transcriptic::Command # upload and run a protocol # class Run < Base # transcriptic run [FILENAME|DIRECTORY] # # upload FILENAME or DIRECTORY and launch it # def index path = args.shift fd = create_protocol_fd_for_path(path) display "Uploading `#{path}` to Transcriptic..." run_id = transcriptic.create_run(fd)["run_id"] display "Run launched (#{run_id})" end def create_protocol_fd_for_path(path) begin stat = File.stat(path) rescue display "No such path: #{path}" return 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