Class: SystemUniversal
Instance Method Summary collapse
- #child_program(config) ⇒ Object
- #child_setup(tmp) ⇒ Object
- #getopts(opts = {}) ⇒ Object
-
#initialize(argv, opts = {}, &block) ⇒ SystemUniversal
constructor
instance methods.
- #quietly ⇒ Object
- #relay(srcdst) ⇒ Object
- #systemu ⇒ Object
- #tmpdir(d = Dir.tmpdir, max = 42, &b) ⇒ Object
- #version ⇒ Object
Constructor Details
#initialize(argv, opts = {}, &block) ⇒ SystemUniversal
instance methods
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/systemu.rb', line 44 def initialize argv, opts = {}, &block #--{{{ getopt = getopts opts @argv = argv @block = block @stdin = getopt[ ['stdin', 'in', '0', 0] ] @stdout = getopt[ ['stdout', 'out', '1', 1] ] @stderr = getopt[ ['stderr', 'err', '2', 2] ] @env = getopt[ 'env' ] @cwd = getopt[ 'cwd' ] @host = getopt[ 'host', self.class.host ] @ppid = getopt[ 'ppid', self.class.ppid ] @pid = getopt[ 'pid', self.class.pid ] @ruby = getopt[ 'ruby', self.class.ruby ] #--}}} end |
Instance Method Details
#child_program(config) ⇒ Object
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 |
# File 'lib/systemu.rb', line 156 def child_program config #--{{{ <<-program PIPE = STDOUT.dup begin require 'yaml' config = YAML.load(IO.read('#{ config }')) argv = config['argv'] env = config['env'] cwd = config['cwd'] stdin = config['stdin'] stdout = config['stdout'] stderr = config['stderr'] Dir.chdir cwd if cwd env.each{|k,v| ENV[k.to_s] = v.to_s} if env STDIN.reopen stdin STDOUT.reopen stdout STDERR.reopen stderr PIPE.puts "pid: #{ Process.pid }" PIPE.flush ### the process is ready yo! PIPE.close exec *argv rescue Exception => e PIPE.write Marshal.dump(e) rescue nil exit 42 end program #--}}} end |
#child_setup(tmp) ⇒ Object
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 |
# File 'lib/systemu.rb', line 114 def child_setup tmp #--{{{ stdin = File.(File.join(tmp, 'stdin')) stdout = File.(File.join(tmp, 'stdout')) stderr = File.(File.join(tmp, 'stderr')) program = File.(File.join(tmp, 'program')) config = File.(File.join(tmp, 'config')) if @stdin open(stdin, 'w'){|f| relay @stdin => f} else FileUtils.touch stdin end FileUtils.touch stdout FileUtils.touch stderr c = {} c['argv'] = @argv c['env'] = @env c['cwd'] = @cwd c['stdin'] = stdin c['stdout'] = stdout c['stderr'] = stderr c['program'] = program open(config, 'w'){|f| YAML.dump c, f} open(program, 'w'){|f| f.write child_program(config)} c #--}}} end |
#getopts(opts = {}) ⇒ Object
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
# File 'lib/systemu.rb', line 232 def getopts opts = {} #--{{{ lambda do |*args| keys, default, ignored = args catch('opt') do [keys].flatten.each do |key| [key, key.to_s, key.to_s.intern].each do |key| throw 'opt', opts[key] if opts.has_key?(key) end end default end end #--}}} end |
#quietly ⇒ Object
146 147 148 149 150 151 152 153 154 |
# File 'lib/systemu.rb', line 146 def quietly #--{{{ v = $VERBOSE $VERBOSE = nil yield ensure $VERBOSE = v #--}}} end |
#relay(srcdst) ⇒ Object
192 193 194 195 196 197 198 199 200 201 |
# File 'lib/systemu.rb', line 192 def relay srcdst #--{{{ src, dst, ignored = srcdst.to_a.first if src.respond_to? 'read' while((buf = src.read(8192))); dst << buf; end else src.each{|buf| dst << buf} end #--}}} end |
#systemu ⇒ Object
64 65 66 67 68 69 70 71 72 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 |
# File 'lib/systemu.rb', line 64 def systemu #--{{{ tmpdir do |tmp| c = child_setup tmp status = nil if @block q = Queue.new t = Thread.new{ Thread.current_abort_on_exception = true; @block[ q.pop ] } end begin quietly{ IO.popen "#{ @ruby } #{ c['program'] }", 'r+' do |pipe| line = pipe.gets case line when %r/^pid: \d+$/ cid = Integer line[%r/\d+/] else begin buf = pipe.read buf = "#{ line }#{ buf }" e = Marshal.load buf raise unless Exception === e raise e rescue raise "wtf?\n#{ buf }\n" end end cid = Integer pipe.gets q.push cid if @block pipe.read rescue nil end } status = $? ensure t.kill rescue nil if t end if @stdout or @stderr open(c['stdout']){|f| relay f => @stdout} if @stdout open(c['stderr']){|f| relay f => @stderr} if @stderr status else [status, IO.read(c['stdout']), IO.read(c['stderr'])] end end #--}}} end |
#tmpdir(d = Dir.tmpdir, max = 42, &b) ⇒ Object
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 |
# File 'lib/systemu.rb', line 203 def tmpdir d = Dir.tmpdir, max = 42, &b #--{{{ i = -1 and loop{ i += 1 tmp = File.join d, "systemu_#{ @host }_#{ @ppid }_#{ @pid }_#{ rand }_#{ i += 1 }" begin Dir.mkdir tmp rescue Errno::EEXIST raise if i >= max next end break( if b begin b.call tmp ensure FileUtils.rm_rf tmp unless SystemU.turd end else tmp end ) } #--}}} end |
#version ⇒ Object
18 |
# File 'lib/systemu.rb', line 18 def version() VERSION end |