childprocess 0.8.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. checksums.yaml +5 -5
  2. data/.document +6 -6
  3. data/.gitignore +28 -28
  4. data/.rspec +1 -1
  5. data/.travis.yml +42 -36
  6. data/CHANGELOG.md +67 -44
  7. data/Gemfile +18 -15
  8. data/LICENSE +20 -20
  9. data/README.md +216 -192
  10. data/Rakefile +61 -61
  11. data/appveyor.yml +42 -43
  12. data/childprocess.gemspec +32 -30
  13. data/ext/mkrf_conf.rb +24 -0
  14. data/lib/childprocess/abstract_io.rb +36 -36
  15. data/lib/childprocess/abstract_process.rb +192 -192
  16. data/lib/childprocess/errors.rb +37 -26
  17. data/lib/childprocess/jruby/io.rb +16 -16
  18. data/lib/childprocess/jruby/process.rb +184 -159
  19. data/lib/childprocess/jruby/pump.rb +53 -53
  20. data/lib/childprocess/jruby.rb +56 -56
  21. data/lib/childprocess/tools/generator.rb +145 -145
  22. data/lib/childprocess/unix/fork_exec_process.rb +78 -70
  23. data/lib/childprocess/unix/io.rb +21 -21
  24. data/lib/childprocess/unix/lib.rb +186 -186
  25. data/lib/childprocess/unix/platform/i386-linux.rb +12 -12
  26. data/lib/childprocess/unix/platform/i386-solaris.rb +11 -11
  27. data/lib/childprocess/unix/platform/x86_64-linux.rb +12 -12
  28. data/lib/childprocess/unix/platform/x86_64-macosx.rb +11 -11
  29. data/lib/childprocess/unix/posix_spawn_process.rb +134 -134
  30. data/lib/childprocess/unix/process.rb +90 -89
  31. data/lib/childprocess/unix.rb +9 -9
  32. data/lib/childprocess/version.rb +3 -3
  33. data/lib/childprocess/windows/handle.rb +91 -91
  34. data/lib/childprocess/windows/io.rb +25 -25
  35. data/lib/childprocess/windows/lib.rb +416 -416
  36. data/lib/childprocess/windows/process.rb +130 -130
  37. data/lib/childprocess/windows/process_builder.rb +178 -175
  38. data/lib/childprocess/windows/structs.rb +148 -148
  39. data/lib/childprocess/windows.rb +33 -33
  40. data/lib/childprocess.rb +210 -205
  41. data/spec/abstract_io_spec.rb +12 -12
  42. data/spec/childprocess_spec.rb +447 -391
  43. data/spec/get_env.ps1 +13 -0
  44. data/spec/io_spec.rb +228 -228
  45. data/spec/jruby_spec.rb +24 -24
  46. data/spec/pid_behavior.rb +12 -12
  47. data/spec/platform_detection_spec.rb +86 -86
  48. data/spec/spec_helper.rb +270 -261
  49. data/spec/unix_spec.rb +57 -57
  50. data/spec/windows_spec.rb +23 -23
  51. metadata +18 -33
data/lib/childprocess.rb CHANGED
@@ -1,205 +1,210 @@
1
- require 'childprocess/version'
2
- require 'childprocess/errors'
3
- require 'childprocess/abstract_process'
4
- require 'childprocess/abstract_io'
5
- require "fcntl"
6
- require 'logger'
7
-
8
- module ChildProcess
9
-
10
- @posix_spawn = false
11
-
12
- class << self
13
- attr_writer :logger
14
-
15
- def new(*args)
16
- case os
17
- when :macosx, :linux, :solaris, :bsd, :cygwin, :aix
18
- if posix_spawn?
19
- Unix::PosixSpawnProcess.new(args)
20
- elsif jruby?
21
- JRuby::Process.new(args)
22
- else
23
- Unix::ForkExecProcess.new(args)
24
- end
25
- when :windows
26
- Windows::Process.new(args)
27
- else
28
- raise Error, "unsupported platform #{platform_name.inspect}"
29
- end
30
- end
31
- alias_method :build, :new
32
-
33
- def logger
34
- return @logger if defined?(@logger) and @logger
35
-
36
- @logger = Logger.new($stderr)
37
- @logger.level = $DEBUG ? Logger::DEBUG : Logger::INFO
38
-
39
- @logger
40
- end
41
-
42
- def platform
43
- if RUBY_PLATFORM == "java"
44
- :jruby
45
- elsif defined?(RUBY_ENGINE) && RUBY_ENGINE == "ironruby"
46
- :ironruby
47
- else
48
- os
49
- end
50
- end
51
-
52
- def platform_name
53
- @platform_name ||= "#{arch}-#{os}"
54
- end
55
-
56
- def unix?
57
- !windows?
58
- end
59
-
60
- def linux?
61
- os == :linux
62
- end
63
-
64
- def jruby?
65
- platform == :jruby
66
- end
67
-
68
- def windows?
69
- os == :windows
70
- end
71
-
72
- def posix_spawn?
73
- enabled = @posix_spawn || %w[1 true].include?(ENV['CHILDPROCESS_POSIX_SPAWN'])
74
- return false unless enabled
75
-
76
- require 'ffi'
77
- begin
78
- require "childprocess/unix/platform/#{ChildProcess.platform_name}"
79
- rescue LoadError
80
- raise ChildProcess::MissingPlatformError
81
- end
82
-
83
- require "childprocess/unix/lib"
84
- require 'childprocess/unix/posix_spawn_process'
85
-
86
- true
87
- rescue ChildProcess::MissingPlatformError => ex
88
- warn_once ex.message
89
- false
90
- end
91
-
92
- #
93
- # Set this to true to enable experimental use of posix_spawn.
94
- #
95
-
96
- def posix_spawn=(bool)
97
- @posix_spawn = bool
98
- end
99
-
100
- def os
101
- @os ||= (
102
- require "rbconfig"
103
- host_os = RbConfig::CONFIG['host_os'].downcase
104
-
105
- case host_os
106
- when /linux/
107
- :linux
108
- when /darwin|mac os/
109
- :macosx
110
- when /mswin|msys|mingw32/
111
- :windows
112
- when /cygwin/
113
- :cygwin
114
- when /solaris|sunos/
115
- :solaris
116
- when /bsd/
117
- :bsd
118
- when /aix/
119
- :aix
120
- else
121
- raise Error, "unknown os: #{host_os.inspect}"
122
- end
123
- )
124
- end
125
-
126
- def arch
127
- @arch ||= (
128
- host_cpu = RbConfig::CONFIG['host_cpu'].downcase
129
- case host_cpu
130
- when /i[3456]86/
131
- if workaround_older_macosx_misreported_cpu?
132
- # Workaround case: older 64-bit Darwin Rubies misreported as i686
133
- "x86_64"
134
- else
135
- "i386"
136
- end
137
- when /amd64|x86_64/
138
- "x86_64"
139
- when /ppc|powerpc/
140
- "powerpc"
141
- else
142
- host_cpu
143
- end
144
- )
145
- end
146
-
147
- #
148
- # By default, a child process will inherit open file descriptors from the
149
- # parent process. This helper provides a cross-platform way of making sure
150
- # that doesn't happen for the given file/io.
151
- #
152
-
153
- def close_on_exec(file)
154
- if file.respond_to?(:close_on_exec=)
155
- file.close_on_exec = true
156
- elsif file.respond_to?(:fcntl) && defined?(Fcntl::FD_CLOEXEC)
157
- file.fcntl Fcntl::F_SETFD, Fcntl::FD_CLOEXEC
158
-
159
- if jruby? && posix_spawn?
160
- # on JRuby, the fcntl call above apparently isn't enough when
161
- # we're launching the process through posix_spawn.
162
- fileno = JRuby.posix_fileno_for(file)
163
- Unix::Lib.fcntl fileno, Fcntl::F_SETFD, Fcntl::FD_CLOEXEC
164
- end
165
- elsif windows?
166
- Windows::Lib.dont_inherit file
167
- else
168
- raise Error, "not sure how to set close-on-exec for #{file.inspect} on #{platform_name.inspect}"
169
- end
170
- end
171
-
172
- private
173
-
174
- def warn_once(msg)
175
- @warnings ||= {}
176
-
177
- unless @warnings[msg]
178
- @warnings[msg] = true
179
- logger.warn msg
180
- end
181
- end
182
-
183
- # Workaround: detect the situation that an older Darwin Ruby is actually
184
- # 64-bit, but is misreporting cpu as i686, which would imply 32-bit.
185
- #
186
- # @return [Boolean] `true` if:
187
- # (a) on Mac OS X
188
- # (b) actually running in 64-bit mode
189
- def workaround_older_macosx_misreported_cpu?
190
- os == :macosx && is_64_bit?
191
- end
192
-
193
- # @return [Boolean] `true` if this Ruby represents `1` in 64 bits (8 bytes).
194
- def is_64_bit?
195
- 1.size == 8
196
- end
197
-
198
- end # class << self
199
- end # ChildProcess
200
-
201
- require 'jruby' if ChildProcess.jruby?
202
-
203
- require 'childprocess/unix' if ChildProcess.unix?
204
- require 'childprocess/windows' if ChildProcess.windows?
205
- require 'childprocess/jruby' if ChildProcess.jruby?
1
+ require 'childprocess/version'
2
+ require 'childprocess/errors'
3
+ require 'childprocess/abstract_process'
4
+ require 'childprocess/abstract_io'
5
+ require "fcntl"
6
+ require 'logger'
7
+
8
+ module ChildProcess
9
+
10
+ @posix_spawn = false
11
+
12
+ class << self
13
+ attr_writer :logger
14
+
15
+ def new(*args)
16
+ case os
17
+ when :macosx, :linux, :solaris, :bsd, :cygwin, :aix
18
+ if posix_spawn?
19
+ Unix::PosixSpawnProcess.new(args)
20
+ elsif jruby?
21
+ JRuby::Process.new(args)
22
+ else
23
+ Unix::ForkExecProcess.new(args)
24
+ end
25
+ when :windows
26
+ Windows::Process.new(args)
27
+ else
28
+ raise Error, "unsupported platform #{platform_name.inspect}"
29
+ end
30
+ end
31
+ alias_method :build, :new
32
+
33
+ def logger
34
+ return @logger if defined?(@logger) and @logger
35
+
36
+ @logger = Logger.new($stderr)
37
+ @logger.level = $DEBUG ? Logger::DEBUG : Logger::INFO
38
+
39
+ @logger
40
+ end
41
+
42
+ def platform
43
+ if RUBY_PLATFORM == "java"
44
+ :jruby
45
+ elsif defined?(RUBY_ENGINE) && RUBY_ENGINE == "ironruby"
46
+ :ironruby
47
+ else
48
+ os
49
+ end
50
+ end
51
+
52
+ def platform_name
53
+ @platform_name ||= "#{arch}-#{os}"
54
+ end
55
+
56
+ def unix?
57
+ !windows?
58
+ end
59
+
60
+ def linux?
61
+ os == :linux
62
+ end
63
+
64
+ def jruby?
65
+ platform == :jruby
66
+ end
67
+
68
+ def windows?
69
+ os == :windows
70
+ end
71
+
72
+ def posix_spawn?
73
+ enabled = @posix_spawn || %w[1 true].include?(ENV['CHILDPROCESS_POSIX_SPAWN'])
74
+ return false unless enabled
75
+
76
+ begin
77
+ require 'ffi'
78
+ rescue LoadError
79
+ raise ChildProcess::MissingFFIError
80
+ end
81
+
82
+ begin
83
+ require "childprocess/unix/platform/#{ChildProcess.platform_name}"
84
+ rescue LoadError
85
+ raise ChildProcess::MissingPlatformError
86
+ end
87
+
88
+ require "childprocess/unix/lib"
89
+ require 'childprocess/unix/posix_spawn_process'
90
+
91
+ true
92
+ rescue ChildProcess::MissingPlatformError => ex
93
+ warn_once ex.message
94
+ false
95
+ end
96
+
97
+ #
98
+ # Set this to true to enable experimental use of posix_spawn.
99
+ #
100
+
101
+ def posix_spawn=(bool)
102
+ @posix_spawn = bool
103
+ end
104
+
105
+ def os
106
+ @os ||= (
107
+ require "rbconfig"
108
+ host_os = RbConfig::CONFIG['host_os'].downcase
109
+
110
+ case host_os
111
+ when /linux/
112
+ :linux
113
+ when /darwin|mac os/
114
+ :macosx
115
+ when /mswin|msys|mingw32/
116
+ :windows
117
+ when /cygwin/
118
+ :cygwin
119
+ when /solaris|sunos/
120
+ :solaris
121
+ when /bsd|dragonfly/
122
+ :bsd
123
+ when /aix/
124
+ :aix
125
+ else
126
+ raise Error, "unknown os: #{host_os.inspect}"
127
+ end
128
+ )
129
+ end
130
+
131
+ def arch
132
+ @arch ||= (
133
+ host_cpu = RbConfig::CONFIG['host_cpu'].downcase
134
+ case host_cpu
135
+ when /i[3456]86/
136
+ if workaround_older_macosx_misreported_cpu?
137
+ # Workaround case: older 64-bit Darwin Rubies misreported as i686
138
+ "x86_64"
139
+ else
140
+ "i386"
141
+ end
142
+ when /amd64|x86_64/
143
+ "x86_64"
144
+ when /ppc|powerpc/
145
+ "powerpc"
146
+ else
147
+ host_cpu
148
+ end
149
+ )
150
+ end
151
+
152
+ #
153
+ # By default, a child process will inherit open file descriptors from the
154
+ # parent process. This helper provides a cross-platform way of making sure
155
+ # that doesn't happen for the given file/io.
156
+ #
157
+
158
+ def close_on_exec(file)
159
+ if file.respond_to?(:close_on_exec=)
160
+ file.close_on_exec = true
161
+ elsif file.respond_to?(:fcntl) && defined?(Fcntl::FD_CLOEXEC)
162
+ file.fcntl Fcntl::F_SETFD, Fcntl::FD_CLOEXEC
163
+
164
+ if jruby? && posix_spawn?
165
+ # on JRuby, the fcntl call above apparently isn't enough when
166
+ # we're launching the process through posix_spawn.
167
+ fileno = JRuby.posix_fileno_for(file)
168
+ Unix::Lib.fcntl fileno, Fcntl::F_SETFD, Fcntl::FD_CLOEXEC
169
+ end
170
+ elsif windows?
171
+ Windows::Lib.dont_inherit file
172
+ else
173
+ raise Error, "not sure how to set close-on-exec for #{file.inspect} on #{platform_name.inspect}"
174
+ end
175
+ end
176
+
177
+ private
178
+
179
+ def warn_once(msg)
180
+ @warnings ||= {}
181
+
182
+ unless @warnings[msg]
183
+ @warnings[msg] = true
184
+ logger.warn msg
185
+ end
186
+ end
187
+
188
+ # Workaround: detect the situation that an older Darwin Ruby is actually
189
+ # 64-bit, but is misreporting cpu as i686, which would imply 32-bit.
190
+ #
191
+ # @return [Boolean] `true` if:
192
+ # (a) on Mac OS X
193
+ # (b) actually running in 64-bit mode
194
+ def workaround_older_macosx_misreported_cpu?
195
+ os == :macosx && is_64_bit?
196
+ end
197
+
198
+ # @return [Boolean] `true` if this Ruby represents `1` in 64 bits (8 bytes).
199
+ def is_64_bit?
200
+ 1.size == 8
201
+ end
202
+
203
+ end # class << self
204
+ end # ChildProcess
205
+
206
+ require 'jruby' if ChildProcess.jruby?
207
+
208
+ require 'childprocess/unix' if ChildProcess.unix?
209
+ require 'childprocess/windows' if ChildProcess.windows?
210
+ require 'childprocess/jruby' if ChildProcess.jruby?
@@ -1,12 +1,12 @@
1
- require File.expand_path('../spec_helper', __FILE__)
2
-
3
- describe ChildProcess::AbstractIO do
4
- let(:io) { ChildProcess::AbstractIO.new }
5
-
6
- it "inherits the parent's IO streams" do
7
- io.inherit!
8
-
9
- expect(io.stdout).to eq STDOUT
10
- expect(io.stderr).to eq STDERR
11
- end
12
- end
1
+ require File.expand_path('../spec_helper', __FILE__)
2
+
3
+ describe ChildProcess::AbstractIO do
4
+ let(:io) { ChildProcess::AbstractIO.new }
5
+
6
+ it "inherits the parent's IO streams" do
7
+ io.inherit!
8
+
9
+ expect(io.stdout).to eq STDOUT
10
+ expect(io.stderr).to eq STDERR
11
+ end
12
+ end