Module: CMD
- Defined in:
- lib/rbbt/util/cmd.rb
Constant Summary collapse
- TOOLS =
IndiferentHash.setup({})
Class Method Summary collapse
- .bash(cmd) ⇒ Object
- .cmd(tool, cmd = nil, options = {}, &block) ⇒ Object
- .cmd_log(*args) ⇒ Object
- .cmd_pid(*args) ⇒ Object
- .conda(tool, env = nil, channel = 'bioconda') ⇒ Object
- .get_tool(tool) ⇒ Object
- .gzip_pipe(file) ⇒ Object
- .process_cmd_options(options = {}) ⇒ Object
- .tool(tool, claim = nil, test = nil, cmd = nil, &block) ⇒ Object
- .versions ⇒ Object
Class Method Details
.bash(cmd) ⇒ Object
73 74 75 |
# File 'lib/rbbt/util/cmd.rb', line 73 def self.bash(cmd) %Q(bash <<EOF\n#{cmd}\nEOF\n) end |
.cmd(tool, cmd = nil, options = {}, &block) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 |
# File 'lib/rbbt/util/cmd.rb', line 105 def self.cmd(tool, cmd = nil, = {}, &block) , cmd = cmd, nil if Hash === cmd = Misc.add_defaults , :stderr => Log::DEBUG in_content = .delete(:in) stderr = .delete(:stderr) pipe = .delete(:pipe) post = .delete(:post) log = .delete(:log) no_fail = .delete(:no_fail) no_fail = .delete(:nofail) if no_fail.nil? no_wait = .delete(:no_wait) xvfb = .delete(:xvfb) = .delete(:progress_bar) save_stderr = .delete(:save_stderr) dont_close_in = .delete(:dont_close_in) log = true if log.nil? if cmd.nil? && ! Symbol === tool cmd = tool else tool = get_tool(tool) if cmd.nil? cmd = tool else cmd = tool + ' ' + cmd end end case xvfb when TrueClass cmd = "xvfb-run --server-args='-screen 0 1024x768x24' --auto-servernum #{cmd}" when String cmd = "xvfb-run --server-args='#{xvfb}' --auto-servernum --server-num=1 #{cmd}" when String end if stderr == true stderr = Log::HIGH end = if cmd =~ /'\{opt\}'/ cmd.sub!('\'{opt}\'', ) else cmd << " " << end in_content = StringIO.new in_content if String === in_content sin, sout, serr, wait_thr = begin Open3.popen3(ENV, cmd) rescue Log.warn $!. raise ProcessFailed, nil, cmd unless no_fail return end pid = wait_thr.pid Log.debug{"CMD: [#{pid}] #{cmd}" if log} if in_content.respond_to?(:read) in_thread = Thread.new(Thread.current) do |parent| Thread.current.report_on_exception = false if no_fail begin begin while c = in_content.readpartial(Misc::BLOCK_SIZE) sin << c end rescue EOFError end sin.close unless sin.closed? unless dont_close_in in_content.close unless in_content.closed? in_content.join if in_content.respond_to? :join end rescue Log.error "Error in CMD [#{pid}] #{cmd}: #{$!.}" unless no_fail raise $! end end else in_thread = nil sin.close end pids = [pid] if pipe ConcurrentStream.setup sout, :pids => pids, :autojoin => no_wait, :no_fail => no_fail if (Integer === stderr and log) || err_thread = Thread.new do while line = serr.gets .process(line) if sout.log = line sout.std_err << line if save_stderr Log.log "STDERR [#{pid}]: " + line, stderr if log end serr.close end else err_thread = Misc.consume_stream(serr, true) end sout.threads = [in_thread, err_thread, wait_thr].compact sout else if err = "" err_thread = Thread.new do while not serr.eof? line = serr.gets .process(line) err << line if Integer === stderr and log end serr.close end elsif log and Integer === stderr err = "" err_thread = Thread.new do while not serr.eof? err << serr.gets end serr.close end else Misc.consume_stream(serr, true) #serr.close err_thread = nil err = "" end ConcurrentStream.setup sout, :pids => pids, :threads => [in_thread, err_thread].compact, :autojoin => no_wait, :no_fail => no_fail out = StringIO.new sout.read sout.close unless sout.closed? status = wait_thr.value if not status.success? and not no_fail if !err.empty? raise ProcessFailed.new pid, "#{cmd} failed with error status #{status.exitstatus}.\n#{err}" else raise ProcessFailed.new pid, "#{cmd} failed with error status #{status.exitstatus}" end else Log.log err, stderr if Integer === stderr and log end out end end |
.cmd_log(*args) ⇒ Object
313 314 315 316 |
# File 'lib/rbbt/util/cmd.rb', line 313 def self.cmd_log(*args) cmd_pid(*args) nil end |
.cmd_pid(*args) ⇒ Object
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 |
# File 'lib/rbbt/util/cmd.rb', line 265 def self.cmd_pid(*args) all_args = *args = all_args.last[:progress_bar] if Hash === all_args.last all_args << {} unless Hash === all_args.last level = all_args.last[:log] || 0 level = 0 if TrueClass === level level = 10 if FalseClass === level level = level.to_i all_args.last[:log] = true all_args.last[:pipe] = true io = cmd(*all_args) pid = io.pids.first line = "" if starting = true while c = io.getc if starting if pid Log.logn "STDOUT [#{pid}]: ", level else Log.logn "STDOUT: ", level end starting = false end STDERR << c if Log.severity <= level line << c if if c == "\n" .process(line) if starting = true line = "" if end end begin io.join .remove if rescue .remove(true) if raise $! end nil end |
.conda(tool, env = nil, channel = 'bioconda') ⇒ Object
13 14 15 16 17 18 19 |
# File 'lib/rbbt/util/cmd.rb', line 13 def self.conda(tool, env = nil, channel = 'bioconda') if env CMD.cmd("bash -l -c '(conda activate #{env} && conda install #{tool} -c #{channel})'") else CMD.cmd("bash -l -c 'conda install #{tool} -c #{channel}'") end end |
.get_tool(tool) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/rbbt/util/cmd.rb', line 21 def self.get_tool(tool) return tool.to_s unless TOOLS[tool] @@init_cmd_tool ||= IndiferentHash.setup({}) claim, test, block, cmd = TOOLS[tool] cmd = tool.to_s if cmd.nil? if !@@init_cmd_tool[tool] begin if test CMD.cmd(test + " ") else CMD.cmd("#{cmd} --help") end rescue if claim claim.produce else block.call end end version_txt = "" version = nil ["--version", "-version", "--help", ""].each do |f| begin version_txt += CMD.cmd("#{cmd} #{f} 2>&1", :nofail => true).read version = Misc.scan_version_text(version_txt, tool) break if version rescue Log.exception $! end end @@init_cmd_tool[tool] = version || true return cmd if cmd end cmd end |
.gzip_pipe(file) ⇒ Object
69 70 71 |
# File 'lib/rbbt/util/cmd.rb', line 69 def self.gzip_pipe(file) Open.gzip?(file) ? "<(gunzip -c '#{file}')" : "'#{file}'" end |
.process_cmd_options(options = {}) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/rbbt/util/cmd.rb', line 77 def self.( = {}) add_dashes = Misc. , :add_option_dashes string = "" .each do |option, value| raise "Invalid option key: #{option.inspect}" if option.to_s !~ /^[a-z_0-9\-=.]+$/i #raise "Invalid option value: #{value.inspect}" if value.to_s.include? "'" value = value.gsub("'","\\'") if value.to_s.include? "'" option = "--" << option.to_s if add_dashes and option.to_s[0] != '-' case when value.nil? || FalseClass === value next when TrueClass === value string << "#{option} " else if option.to_s.chars.to_a.last == "=" string << "#{option}'#{value}' " else string << "#{option} '#{value}' " end end end string.strip end |
.tool(tool, claim = nil, test = nil, cmd = nil, &block) ⇒ Object
9 10 11 |
# File 'lib/rbbt/util/cmd.rb', line 9 def self.tool(tool, claim = nil, test = nil, cmd = nil, &block) TOOLS[tool] = [claim, test, block, cmd] end |
.versions ⇒ Object
64 65 66 67 |
# File 'lib/rbbt/util/cmd.rb', line 64 def self.versions return {} unless defined? @@init_cmd_tool @@init_cmd_tool.select{|k,v| v =~ /\d+\./ } end |