zerg_support 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,5 @@
1
+ v0.0.5. Added forgotten spawn.rb to Manifest.
2
+
1
3
  v0.0.4. Polished implementation and tests for process spawning.
2
4
 
3
5
  v0.0.3. Preliminary implementation of process spawning.
data/Manifest CHANGED
@@ -1,6 +1,7 @@
1
1
  CHANGELOG
2
2
  lib/zerg_support/gems.rb
3
3
  lib/zerg_support/process.rb
4
+ lib/zerg_support/spawn.rb
4
5
  lib/zerg_support.rb
5
6
  LICENSE
6
7
  Manifest
@@ -8,3 +9,4 @@ Rakefile
8
9
  README
9
10
  test/test_gems.rb
10
11
  test/test_process.rb
12
+ test/test_spawn.rb
@@ -0,0 +1,126 @@
1
+ #:nodoc:
2
+ module Zerg::Support::Process
3
+ # perhaps this should be merged in process
4
+ end
5
+
6
+ if RUBY_PLATFORM =~ /win/ and RUBY_PLATFORM !~ /darwin/
7
+ require 'win32-process'
8
+
9
+ #:nodoc: all
10
+ module Zerg::Support::Process
11
+ def self.spawn(binary, args = [], options = {})
12
+ info = Process.create :app_name => binary,
13
+ :cwd => options[:chdir] || Dir.pwd,
14
+ :environment => options[:env],
15
+ :creation_flags => 0
16
+ return info[:process_id]
17
+ end
18
+ end
19
+
20
+ else
21
+
22
+ #:nodoc:
23
+ module Zerg::Support::Process
24
+ # Spawns a new process and returns its pid immediately.
25
+ # Like Kernel.spawn in ruby1.9, except that the environment is passed
26
+ # as an option with the key :env
27
+ def self.spawn(binary, args = [], options = {})
28
+ if Kernel.respond_to? :spawn
29
+ # ruby1.9+: spawn!
30
+ options = options.dup
31
+ env = options.delete(:env)
32
+ Kernel.spawn *([env, binary] + args + [options])
33
+ else
34
+ # below 1.9: emulate
35
+
36
+ # chdir option
37
+ Dir.chdir options[:chdir] if options[:chdir]
38
+
39
+ child_pid = fork do
40
+ Helpers.do_redirects options
41
+ Helpers.close_fds options
42
+ Helpers.set_process_group options
43
+ Helpers.set_environment options
44
+ Helpers.set_rlimits options
45
+
46
+ Kernel.exec *([binary] + args)
47
+ end
48
+ end
49
+
50
+ return child_pid
51
+ end
52
+ end
53
+ end
54
+
55
+ # Helpers for spawning processes.
56
+ module Zerg::Support::Process::Helpers
57
+ # Closes all open file descriptors except for stdin/stdout/stderr
58
+ def self.close_fds(options)
59
+ return if options[:close_others] == false
60
+
61
+ ObjectSpace.each_object(IO) do |io|
62
+ next if [STDIN, STDOUT, STDERR].include? io
63
+
64
+ begin
65
+ io.close unless io.closed?
66
+ rescue Exception
67
+ end
68
+ end
69
+ end
70
+
71
+ # Sets process limits (rlimits) according to the given options. The options
72
+ # follow the convention of Kernel.spawn in ruby1.9
73
+ def self.set_rlimits(options)
74
+ # rlimit options
75
+ options.each do |k, v|
76
+ next unless k.kind_of? Symbol and k.to_s[0, 7] == 'rlimit_'
77
+ rconst = Process.const_get k.to_s.upcase.to_sym
78
+ if v.kind_of? Enumerable
79
+ Process.setrlimit rconst, v.first, v.last
80
+ else
81
+ Process.setrlimit rconst, v
82
+ end
83
+ end
84
+ end
85
+
86
+ # Sets the process' group according to the given options. The options
87
+ # follow the convention of Kernel.spawn in ruby1.9
88
+ def self.set_process_group(options)
89
+ return unless options[:pgroup]
90
+
91
+ if options[:pgroup].kind_of? Numeric and options[:pgroup] > 0
92
+ Process.setpgid 0, options[:pgroup]
93
+ else
94
+ Process.setsid
95
+ end
96
+ end
97
+
98
+ # Sets the process' environment according to the given options. The options
99
+ # follow the convention of Kernel.spawn in ruby1.9
100
+ def self.set_environment(options)
101
+ return unless options[:env]
102
+
103
+ ENV.clear if options[:unsetenv_others]
104
+ options[:env].each do |key, value|
105
+ ENV[key] = value
106
+ end
107
+ end
108
+
109
+ # Performs IO redirections according to the given options. The options
110
+ # follow the convention of Kernel.spawn in ruby1.9
111
+ def self.do_redirects(options)
112
+ file_redirects = []
113
+ fd_redirects = []
114
+ options.each do |key, value|
115
+ next unless key.kind_of? IO
116
+ if value.kind_of? String
117
+ file_redirects << [key, value]
118
+ else
119
+ fd_redirects << [key, value]
120
+ end
121
+ end
122
+ (file_redirects + fd_redirects).each do |r|
123
+ r.first.reopen r.last
124
+ end
125
+ end
126
+ end
data/zerg_support.gemspec CHANGED
@@ -1,22 +1,20 @@
1
- # -*- encoding: utf-8 -*-
2
-
3
1
  Gem::Specification.new do |s|
4
2
  s.name = %q{zerg_support}
5
- s.version = "0.0.4"
3
+ s.version = "0.0.5"
6
4
 
7
5
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
6
  s.authors = ["Victor Costan"]
9
- s.date = %q{2008-11-13}
7
+ s.date = %q{2008-11-19}
10
8
  s.description = %q{Support libraries used by the Zerg system.}
11
9
  s.email = %q{victor@zergling.net}
12
- s.extra_rdoc_files = ["CHANGELOG", "lib/zerg_support/gems.rb", "lib/zerg_support/process.rb", "lib/zerg_support.rb", "LICENSE", "README"]
13
- s.files = ["CHANGELOG", "lib/zerg_support/gems.rb", "lib/zerg_support/process.rb", "lib/zerg_support.rb", "LICENSE", "Manifest", "Rakefile", "README", "test/test_gems.rb", "test/test_process.rb", "zerg_support.gemspec", "test/test_spawn.rb"]
10
+ s.extra_rdoc_files = ["CHANGELOG", "lib/zerg_support/gems.rb", "lib/zerg_support/process.rb", "lib/zerg_support/spawn.rb", "lib/zerg_support.rb", "LICENSE", "README"]
11
+ s.files = ["CHANGELOG", "lib/zerg_support/gems.rb", "lib/zerg_support/process.rb", "lib/zerg_support/spawn.rb", "lib/zerg_support.rb", "LICENSE", "Manifest", "Rakefile", "README", "test/test_gems.rb", "test/test_process.rb", "test/test_spawn.rb", "zerg_support.gemspec"]
14
12
  s.has_rdoc = true
15
13
  s.homepage = %q{http://www.zergling.net}
16
14
  s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Zerg_support", "--main", "README"]
17
15
  s.require_paths = ["lib"]
18
16
  s.rubyforge_project = %q{rails-pwnage}
19
- s.rubygems_version = %q{1.3.1}
17
+ s.rubygems_version = %q{1.2.0}
20
18
  s.summary = %q{Support libraries used by the Zerg system.}
21
19
  s.test_files = ["test/test_gems.rb", "test/test_process.rb", "test/test_spawn.rb"]
22
20
 
@@ -24,7 +22,7 @@ Gem::Specification.new do |s|
24
22
  current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
25
23
  s.specification_version = 2
26
24
 
27
- if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
25
+ if current_version >= 3 then
28
26
  s.add_development_dependency(%q<flexmock>, [">= 0.8.3"])
29
27
  else
30
28
  s.add_dependency(%q<flexmock>, [">= 0.8.3"])
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zerg_support
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Victor Costan
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-11-13 00:00:00 -05:00
12
+ date: 2008-11-19 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -32,6 +32,7 @@ extra_rdoc_files:
32
32
  - CHANGELOG
33
33
  - lib/zerg_support/gems.rb
34
34
  - lib/zerg_support/process.rb
35
+ - lib/zerg_support/spawn.rb
35
36
  - lib/zerg_support.rb
36
37
  - LICENSE
37
38
  - README
@@ -39,6 +40,7 @@ files:
39
40
  - CHANGELOG
40
41
  - lib/zerg_support/gems.rb
41
42
  - lib/zerg_support/process.rb
43
+ - lib/zerg_support/spawn.rb
42
44
  - lib/zerg_support.rb
43
45
  - LICENSE
44
46
  - Manifest
@@ -46,6 +48,7 @@ files:
46
48
  - README
47
49
  - test/test_gems.rb
48
50
  - test/test_process.rb
51
+ - test/test_spawn.rb
49
52
  - zerg_support.gemspec
50
53
  has_rdoc: true
51
54
  homepage: http://www.zergling.net
@@ -74,7 +77,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
74
77
  requirements: []
75
78
 
76
79
  rubyforge_project: rails-pwnage
77
- rubygems_version: 1.3.1
80
+ rubygems_version: 1.2.0
78
81
  signing_key:
79
82
  specification_version: 2
80
83
  summary: Support libraries used by the Zerg system.