Class: Dev::SystemCall

Inherits:
Object
  • Object
show all
Defined in:
lib/dev/SystemCall.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cmd) ⇒ SystemCall

Returns a new instance of SystemCall.



10
11
12
13
14
15
16
17
18
19
# File 'lib/dev/SystemCall.rb', line 10

def initialize(cmd)
  if(cmd.kind_of?(Hash))
    @command=cmd[:cmd]
    @dir=cmd[:dir] unless cmd[:dir].nil?
    @cache=cmd[:cache] unless[:cache].nil?
  else
    @command=cmd
  end
  execute(@command);
end

Instance Attribute Details

#cacheObject

Returns the value of attribute cache.



8
9
10
# File 'lib/dev/SystemCall.rb', line 8

def cache
  @cache
end

#commandObject

Returns the value of attribute command.



8
9
10
# File 'lib/dev/SystemCall.rb', line 8

def command
  @command
end

#dirObject

Returns the value of attribute dir.



8
9
10
# File 'lib/dev/SystemCall.rb', line 8

def dir
  @dir
end

#end_timeObject

Returns the value of attribute end_time.



8
9
10
# File 'lib/dev/SystemCall.rb', line 8

def end_time
  @end_time
end

#errorObject

Returns the value of attribute error.



8
9
10
# File 'lib/dev/SystemCall.rb', line 8

def error
  @error
end

#outputObject

Returns the value of attribute output.



8
9
10
# File 'lib/dev/SystemCall.rb', line 8

def output
  @output
end

#start_timeObject

Returns the value of attribute start_time.



8
9
10
# File 'lib/dev/SystemCall.rb', line 8

def start_time
  @start_time
end

#statusObject

Returns the value of attribute status.



8
9
10
# File 'lib/dev/SystemCall.rb', line 8

def status
  @status
end

#timed_outObject

Returns the value of attribute timed_out.



8
9
10
# File 'lib/dev/SystemCall.rb', line 8

def timed_out
  @timed_out
end

Instance Method Details

#cache_nameObject



23
24
25
26
27
# File 'lib/dev/SystemCall.rb', line 23

def cache_name
  name = @dir+"/" + @command.gsub(" ","_").gsub("\\","-").gsub("/","-") unless @dir.nil?
  name = @command.gsub(" ","_").gsub("\\","-").gsub("/","-") if @dir.nil?
  return name
end

#elapsedObject



21
# File 'lib/dev/SystemCall.rb', line 21

def elapsed; @end_time-@start_time; end

#execute(cmd) ⇒ Object



73
74
75
76
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/dev/SystemCall.rb', line 73

def execute(cmd) 
  
  if !@cache.nil? && has_cache
    load_cache
    return
  end
  @start_time=Time.now
  filename=Dir.tmpdir + "/" + (0...8).map{65.+(rand(25)).chr}.join
  begin
      if(@dir.nil?)
        if(!@outfile.nil?)
          system("#{@command} > #{outfile}")
        else
          system("#{@command} >#{filename}.out 2>#{filename}.err")
        end
      else
        Dir.chdir(dir) do
          if(!@outfile.nil?)
            system("#{@command} > #{outfile}")
          else
            system("#{@command} >#{filename}.out 2>#{filename}.err")
          end
        end
      end
      if(File.exist?("#{filename}.out"))
        File.open("#{filename}.out",'r') {|f|
       @output = f.read
       f.close
        }
      end
      if(File.exist?("#{filename}.err"))
        File.open("#{filename}.err",'r') {|f|
       @error = f.read
       f.close
        }
      end
      @status=$?.exitstatus 
      @end_time=Time.now
  rescue
      puts "error executing ruby code"
  ensure
      write_cache if !@cache.nil?
      begin
       File.delete("#{filename}.out") if File.exists?("#{filename}.out")
       File.delete("#{filename}.err") if File.exists?("#{filename}.err")
      rescue
       puts "temp directory file was not cleaned up."
      end
  end
end

#get_hashObject



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/dev/SystemCall.rb', line 33

def get_hash
  hash = Hash.new()
  hash[:cmd]=@command
  hash[:dir]=@dir
  hash[:output]=@output
  hash[:error]=@error
  hash[:status]=@status
  hash[:start_time]=@start_time
  hash[:end_time]=@end_time
  hash[:timed_out]=@timed_out
  hash[:cache]=@cache
  return hash
end

#has_cacheObject



29
30
31
# File 'lib/dev/SystemCall.rb', line 29

def has_cache
  File.exist?(cache_name)
end

#load_cacheObject



66
67
68
69
70
71
# File 'lib/dev/SystemCall.rb', line 66

def load_cache
  file=File.open(cache_name,'rb')
  hash = Marshal.load file.read
  file.close
  set_hash hash
end

#puts_summaryObject



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/dev/SystemCall.rb', line 124

def puts_summary
  if(@status != 0)
    summary =  "  [".foreground(:cyan) + "X".foreground(:red).bright + "]".foreground(:cyan) + " system(\"" + @command.foreground(:green) + "\") has exit status of " + @status.to_s
    summary += " dir: " + @dir unless @dir.nil?
    summary += " cache: true" unless @cache.nil?
    puts summary
    puts @output if !@output.nil?
    warn @error if !@error.nil?
    throw "exit status was " + @status.to_s
  else
    elapsed_str="[" + "%.0f" %(elapsed) + "s]"
    summary = "  [".foreground(:cyan) + "+".foreground(:green) + "]".foreground(:cyan) + " system(\"" + @command.foreground(:green) + "\") " + elapsed_str.foreground(:cyan) 
    summary += " dir: " + @dir unless @dir.nil?
    summary += " cache: true" unless @cache.nil?
    puts summary
  end
end

#set_hash(hash) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/dev/SystemCall.rb', line 47

def set_hash(hash)
  @command=hash[:cmd] unless hash[:cmd].nil?
  @dir=hash[:dir] unless hash[:dir].nil?
  @output=hash[:output] unless hash[:output].nil?
  @error=hash[:error] unless hash[:error].nil?
  @status=hash[:status] unless hash[:status].nil?
  @start_time=hash[:start_time] unless hash[:start_time].nil?
  @end_time=hash[:end_time] unless hash[:end_time].nil?
  @timed_out=hash[:timed_out] unless hash[:timed_out].nil?
  @cache=hash[:cache] unless hash[:cache].nil?
end

#write_cacheObject



59
60
61
62
63
64
# File 'lib/dev/SystemCall.rb', line 59

def write_cache
  marshal_dump = Marshal.dump(get_hash)
  file=File.new(cache_name,'wb')
  file.write marshal_dump
  file.close
end