Class: CGen::Util::ShellCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/cgen/util/shell_command.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command, execution_dir, log_file = nil) ⇒ ShellCommand

Returns a new instance of ShellCommand.



3
4
5
6
7
# File 'lib/cgen/util/shell_command.rb', line 3

def initialize(command, execution_dir, log_file=nil)
  @command = command
  @execution_dir = execution_dir
  @log_file = log_file
end

Class Method Details

.exist?(command) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
32
33
34
35
36
37
38
# File 'lib/cgen/util/shell_command.rb', line 29

def self.exist?(command)
  exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
  ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
    exts.collect do |ext|
      exe = File.join(path, "#{command}#{ext}")
      return true if File.executable? exe
    end
  end
  false # return
end

Instance Method Details

#exist?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/cgen/util/shell_command.rb', line 40

def exist?
  self.class.exist? @command
end

#find_executable_part(command) ⇒ Object



44
45
46
# File 'lib/cgen/util/shell_command.rb', line 44

def find_executable_part(command)
  command.split(' ').first
end

#runObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/cgen/util/shell_command.rb', line 9

def run
  CGen::Util::Logging.log(:executing_command, cmd: @command, exec_dir: @execution_dir, log_file: @log_file)

  status = true

  Process.waitpid(
      fork do
        original_stdout, original_stderr = $stdout, $stderr
        FileUtils.chdir @execution_dir do
          File.open(@log_file, 'a') do |log_file|
            $stderr = $stdout = log_file
            system @command
            $stdout, $stderr = original_stdout, original_stderr
          end
        end
      end)

  status # return
end