Module: Bricolage::CommandUtils

Included in:
PSQLDataSource, TDDataSource, TDTask::Export
Defined in:
lib/bricolage/commandutils.rb

Instance Method Summary collapse

Instance Method Details

#command(*args, env: nil) ⇒ Object



7
8
9
10
11
12
13
14
15
# File 'lib/bricolage/commandutils.rb', line 7

def command(*args, env: nil)
  logger.info "command: #{args.join(' ')}"
  sargs = args.map {|a| a.to_s }
  sargs.unshift env if env
  system(*sargs)
  st = $?
  logger.info "status: #{st.exitstatus || 'nil'} (#{st})"
  st
end

#make_tmpfile(content, tmpdir: Dir.tmpdir) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/bricolage/commandutils.rb', line 17

def make_tmpfile(content, tmpdir: Dir.tmpdir)
  path = new_tmpfile_path(tmpdir)
  File.open(path, 'w') {|f|
    f.write content
  }
  yield path
ensure
  FileUtils.rm_f path
end

#new_tmpfile_path(tmpdir = Dir.tmpdir) ⇒ Object



27
28
29
# File 'lib/bricolage/commandutils.rb', line 27

def new_tmpfile_path(tmpdir = Dir.tmpdir)
  "#{tmpdir}/#{Time.now.to_i}_#{$$}_#{'%x' % Thread.current.object_id}_#{rand(2**16)}"
end

#retrieve_last_match_from_stderr(re, nth = 0) ⇒ Object

CLUDGE: FIXME: bricolage-jobnet command writes stderr to the file, we can find error messages from there. Using a temporary file or Ruby SQL driver is MUCH better.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/bricolage/commandutils.rb', line 33

def retrieve_last_match_from_stderr(re, nth = 0)
  return unless $stderr.stat.file?
  $stderr.flush
  f = $stderr.dup
  matched = nil
  begin
    f.seek(0)
    f.each do |line|
      m = line.slice(re, nth)
      matched = m if m
    end
  ensure
    f.close
  end
  matched = matched.to_s.strip
  matched.empty? ? nil : matched
end