lib/systemu.rb in systemu-2.5.2 vs lib/systemu.rb in systemu-2.6.0

- old
+ new

@@ -12,11 +12,11 @@ class SystemUniversal # # constants # - SystemUniversal::VERSION = '2.5.2' unless SystemUniversal.send(:const_defined?, :VERSION) + SystemUniversal::VERSION = '2.6.0' unless SystemUniversal.send(:const_defined?, :VERSION) def SystemUniversal.version() SystemUniversal::VERSION end def version() SystemUniversal::VERSION end # # class methods # @@ -71,11 +71,11 @@ begin thread = nil quietly{ - IO.popen "#{ quote(@ruby) } #{ quote(c['program']) }", 'r+' do |pipe| + IO.popen "#{ quote(@ruby) } #{ quote(c['program']) }", 'rb+' do |pipe| line = pipe.gets case line when %r/^pid: \d+$/ cid = Integer line[%r/\d+/] else @@ -106,15 +106,15 @@ end end end if @stdout or @stderr - open(c['stdout']){|f| relay f => @stdout} if @stdout - open(c['stderr']){|f| relay f => @stderr} if @stderr + open(c['stdout'], 'rb'){|f| relay f => @stdout} if @stdout + open(c['stderr'], 'rb'){|f| relay f => @stderr} if @stderr status else - [status, IO.read(c['stdout']), IO.read(c['stderr'])] + [status, open(c['stdout'], 'rb'){|f| f.read}, open(c['stderr'], 'rb'){|f| f.read}] end end end def quote *args, &block @@ -138,11 +138,11 @@ stderr = File.expand_path(File.join(tmp, 'stderr')) program = File.expand_path(File.join(tmp, 'program')) config = File.expand_path(File.join(tmp, 'config')) if @stdin - open(stdin, 'w'){|f| relay @stdin => f} + open(stdin, 'wb'){|f| relay @stdin => f} else FileUtils.touch stdin end FileUtils.touch stdout FileUtils.touch stderr @@ -153,13 +153,13 @@ c['cwd'] = @cwd c['stdin'] = stdin c['stdout'] = stdout c['stderr'] = stderr c['program'] = program - open(config, 'w'){|f| Marshal.dump(c, f)} + open(config, 'wb'){|f| Marshal.dump(c, f)} - open(program, 'w'){|f| f.write child_program(config)} + open(program, 'wb'){|f| f.write child_program(config)} c end def quietly @@ -215,14 +215,24 @@ src.each{|buf| dst << buf} end end end + def slug_for(*args) + options = args.last.is_a?(Hash) ? args.pop : {} + join = (options[:join] || options['join'] || '_').to_s + string = args.flatten.compact.join(join) + words = string.to_s.scan(%r|[/\w]+|) + words.map!{|word| word.gsub %r|[^/0-9a-zA-Z_-]|, ''} + words.delete_if{|word| word.nil? or word.strip.empty?} + words.join(join).downcase.gsub('/', (join * 2)) + end + 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 }" + tmp = File.join(d, slug_for("systemu_#{ @host }_#{ @ppid }_#{ @pid }_#{ rand }_#{ i += 1 }")) begin Dir.mkdir tmp rescue Errno::EEXIST raise if i >= max