public
Description: A process execution library which doesn't suck.
Clone URL: git://github.com/codahale/ropen.git
Search Repo:
Refactor the hell out of Ropen::Command.
codahale (author)
Thu May 15 14:51:17 -0700 2008
commit  d65e7a88207dce5a81c4536b822bacdfbbd8f683
tree    b486e40ea12ce34a4aa30ca1e0722985bc956f95
parent  0c2fa7886f32f108f8a4dfefedbbab2e17361596
...
22
23
24
25
26
27
 
28
29
30
31
32
33
34
35
36
37
 
 
 
38
39
40
41
42
43
...
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
 
73
74
75
76
77
78
79
80
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81
82
83
...
86
87
88
 
 
 
 
 
 
 
 
 
 
89
...
22
23
24
 
 
 
25
26
27
 
 
 
 
 
 
 
 
28
29
30
31
32
33
34
35
36
...
46
47
48
 
 
 
 
 
 
 
 
 
 
49
50
51
52
53
54
 
55
56
57
58
 
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
...
84
85
86
87
88
89
90
91
92
93
94
95
96
97
0
@@ -22,19 +22,12 @@
0
   def run
0
     initialize_streams
0
     pid = fork do
0
- @stdin.bind_input(STDIN)
0
- @stdout.bind_output(STDOUT)
0
- @stderr.bind_output(STDERR)
0
+ redirect_streams
0
       exec(@executable, *@arguments)
0
     end
0
- stdin, stdout, stderr = open_streams(pid)
0
- @stdin_io = stdin
0
- @stdout_events.run(stdout)
0
- @stderr_events.run(stderr)
0
- @stdin_spool.replay(stdin)
0
- [@stdout_events, @stderr_events].each { |e| e.finish }
0
- Process.waitpid(pid)
0
- return @exit_status = $?
0
+ stdin, stdout, stderr = open_streams
0
+ process_streams(stdin, stdout, stderr, pid)
0
+ return @exit_status
0
   ensure
0
     finalize_streams
0
   end
0
0
0
0
@@ -53,31 +46,36 @@
0
   
0
 private
0
   
0
- def check_executable(executable)
0
- unless File.exist?(@executable)
0
- raise Ropen::InvalidExecutableError.new("#{executable} does not exist")
0
- end
0
-
0
- unless File.executable?(@executable)
0
- raise Ropen::InvalidExecutableError.new("#{executable} is not executable")
0
- end
0
- end
0
-
0
   def initialize_streams
0
     @stdin = Ropen::Pipe.new
0
     @stdout = Ropen::Pipe.new
0
     @stderr = Ropen::Pipe.new
0
   end
0
   
0
- def open_streams(pid)
0
+ def open_streams
0
     @stdin.close_reader
0
     @stdout.close_writer
0
     @stderr.close_writer
0
-# Process.waitpid(pid)
0
     @stdin.writer.sync = true
0
     return [@stdin.writer, @stdout.reader, @stderr.reader]
0
   end
0
   
0
+ def redirect_streams
0
+ @stdin.bind_input(STDIN)
0
+ @stdout.bind_output(STDOUT)
0
+ @stderr.bind_output(STDERR)
0
+ end
0
+
0
+ def process_streams(stdin, stdout, stderr, child_pid)
0
+ @stdin_io = stdin
0
+ @stdout_events.run(stdout)
0
+ @stderr_events.run(stderr)
0
+ @stdin_spool.replay(stdin)
0
+ [@stdout_events, @stderr_events].each { |e| e.finish }
0
+ Process.waitpid(child_pid)
0
+ @exit_status = $?
0
+ end
0
+
0
   def finalize_streams
0
     [@stdin, @stdout, @stderr].each { |s| s.close }
0
     @stdin = nil
0
@@ -86,5 +84,15 @@
0
     @stderr = nil
0
   end
0
   
0
+ def check_executable(executable_name)
0
+ unless File.exist?(@executable)
0
+ raise Ropen::InvalidExecutableError.new("#{executable_name} does not exist")
0
+ end
0
+
0
+ unless File.executable?(@executable)
0
+ raise Ropen::InvalidExecutableError.new("#{executable_name} is not executable")
0
+ end
0
+ end
0
+
0
 end

Comments

    No one has commented yet.