em_pessimistic 0.1.0 → 0.1.1

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.
@@ -0,0 +1,59 @@
1
+ # encoding: utf-8
2
+ # --
3
+ # The MIT License (MIT)
4
+ #
5
+ # Copyright (C) 2012 Gitorious AS
6
+ #
7
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ # of this software and associated documentation files (the "Software"), to deal
9
+ # in the Software without restriction, including without limitation the rights
10
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ # copies of the Software, and to permit persons to whom the Software is
12
+ # furnished to do so, subject to the following conditions:
13
+ #
14
+ # The above copyright notice and this permission notice shall be included in all
15
+ # copies or substantial portions of the Software.
16
+ #
17
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ # SOFTWARE.
24
+ #++
25
+ require "em_pessimistic/popen3"
26
+ require "eventmachine"
27
+
28
+ module EMPessimistic
29
+ class DeferrableChildProcess < EventMachine::Connection
30
+ include EventMachine::Deferrable
31
+
32
+ def initialize
33
+ super
34
+ @stdout = []
35
+ @stderr = []
36
+ end
37
+
38
+ def self.open(cmd)
39
+ EMPessimistic.popen3(cmd, DeferrableChildProcess)
40
+ end
41
+
42
+ def receive_data(data)
43
+ @stdout << data
44
+ end
45
+
46
+ def receive_stderr(data)
47
+ @stderr << data
48
+ end
49
+
50
+ def unbind
51
+ status = get_status
52
+ if status.exitstatus != 0
53
+ fail(@stderr.join.strip, status)
54
+ else
55
+ succeed(@stdout.join.strip, status)
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,50 @@
1
+ # encoding: utf-8
2
+ # --
3
+ # The MIT License (MIT)
4
+ #
5
+ # Copyright (C) 2012 Gitorious AS
6
+ #
7
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ # of this software and associated documentation files (the "Software"), to deal
9
+ # in the Software without restriction, including without limitation the rights
10
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ # copies of the Software, and to permit persons to whom the Software is
12
+ # furnished to do so, subject to the following conditions:
13
+ #
14
+ # The above copyright notice and this permission notice shall be included in all
15
+ # copies or substantial portions of the Software.
16
+ #
17
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ # SOFTWARE.
24
+ #++
25
+ require "eventmachine"
26
+
27
+ # Original implementation at
28
+ # https://gist.github.com/535644/
29
+ #
30
+ module EMPessimistic
31
+ class Popen3StderrHandler < EventMachine::Connection
32
+ def initialize(connection)
33
+ @connection = connection
34
+ end
35
+
36
+ def receive_data(data)
37
+ @connection.receive_stderr(data)
38
+ end
39
+ end
40
+
41
+ def self.popen3(*args)
42
+ new_stderr = $stderr.dup
43
+ rd, wr = IO::pipe
44
+ $stderr.reopen(wr)
45
+ connection = EM.popen(*args)
46
+ $stderr.reopen(new_stderr)
47
+ EM.attach(rd, Popen3StderrHandler, connection)
48
+ connection
49
+ end
50
+ end
@@ -0,0 +1,28 @@
1
+ # encoding: utf-8
2
+ # --
3
+ # The MIT License (MIT)
4
+ #
5
+ # Copyright (C) 2012 Gitorious AS
6
+ #
7
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ # of this software and associated documentation files (the "Software"), to deal
9
+ # in the Software without restriction, including without limitation the rights
10
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ # copies of the Software, and to permit persons to whom the Software is
12
+ # furnished to do so, subject to the following conditions:
13
+ #
14
+ # The above copyright notice and this permission notice shall be included in all
15
+ # copies or substantial portions of the Software.
16
+ #
17
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ # SOFTWARE.
24
+ #++
25
+
26
+ module EMPessimistic
27
+ VERSION = "0.1.1"
28
+ end
@@ -0,0 +1,27 @@
1
+ # encoding: utf-8
2
+ # --
3
+ # The MIT License (MIT)
4
+ #
5
+ # Copyright (C) 2012 Gitorious AS
6
+ #
7
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ # of this software and associated documentation files (the "Software"), to deal
9
+ # in the Software without restriction, including without limitation the rights
10
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ # copies of the Software, and to permit persons to whom the Software is
12
+ # furnished to do so, subject to the following conditions:
13
+ #
14
+ # The above copyright notice and this permission notice shall be included in all
15
+ # copies or substantial portions of the Software.
16
+ #
17
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ # SOFTWARE.
24
+ #++
25
+ require "em_pessimistic/version"
26
+ require "em_pessimistic/popen3"
27
+ require "em_pessimistic/deferrable_child_process"
@@ -0,0 +1,51 @@
1
+ # encoding: utf-8
2
+ # --
3
+ # The MIT License (MIT)
4
+ #
5
+ # Copyright (C) 2012 Gitorious AS
6
+ #
7
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ # of this software and associated documentation files (the "Software"), to deal
9
+ # in the Software without restriction, including without limitation the rights
10
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ # copies of the Software, and to permit persons to whom the Software is
12
+ # furnished to do so, subject to the following conditions:
13
+ #
14
+ # The above copyright notice and this permission notice shall be included in all
15
+ # copies or substantial portions of the Software.
16
+ #
17
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ # SOFTWARE.
24
+ #++
25
+ require "test_helper"
26
+ require "em_pessimistic/deferrable_child_process"
27
+
28
+ describe EMPessimistic::DeferrableChildProcess do
29
+ include EM::MiniTest::Spec
30
+
31
+ it "passes stdout data and status to callback on success" do
32
+ process = EMPessimistic::DeferrableChildProcess.open("ls -l")
33
+ process.callback do |data, status|
34
+ assert_match /em_pessimistic\.gemspec/, data
35
+ assert_equal 0, status.exitstatus
36
+ done!
37
+ end
38
+ wait!
39
+ end
40
+
41
+ it "passes stderr data and status to errback on error" do
42
+ cmd = "git ls-tree master:Gemfile"
43
+ process = EMPessimistic::DeferrableChildProcess.open(cmd)
44
+ process.errback do |data, status|
45
+ assert_equal "fatal: not a tree object", data
46
+ assert_equal 128, status.exitstatus
47
+ done!
48
+ end
49
+ wait!
50
+ end
51
+ end
@@ -0,0 +1,30 @@
1
+ # encoding: utf-8
2
+ # --
3
+ # The MIT License (MIT)
4
+ #
5
+ # Copyright (C) 2012 Gitorious AS
6
+ #
7
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ # of this software and associated documentation files (the "Software"), to deal
9
+ # in the Software without restriction, including without limitation the rights
10
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ # copies of the Software, and to permit persons to whom the Software is
12
+ # furnished to do so, subject to the following conditions:
13
+ #
14
+ # The above copyright notice and this permission notice shall be included in all
15
+ # copies or substantial portions of the Software.
16
+ #
17
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ # SOFTWARE.
24
+ #++
25
+ require "bundler/setup"
26
+ require "minitest/autorun"
27
+ require "em/minitest/spec"
28
+ require "eventmachine"
29
+
30
+ Bundler.require(:default, :test)
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: em_pessimistic
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 0
10
- version: 0.1.0
9
+ - 1
10
+ version: 0.1.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Christian Johansen
@@ -87,8 +87,13 @@ extensions: []
87
87
 
88
88
  extra_rdoc_files: []
89
89
 
90
- files: []
91
-
90
+ files:
91
+ - lib/em_pessimistic.rb
92
+ - lib/em_pessimistic/deferrable_child_process.rb
93
+ - lib/em_pessimistic/popen3.rb
94
+ - lib/em_pessimistic/version.rb
95
+ - test/em_pessimistic/deferrable_child_process_test.rb
96
+ - test/test_helper.rb
92
97
  has_rdoc: true
93
98
  homepage: http://gitorious.org/gitorious/em_pessimistic
94
99
  licenses: []
@@ -123,5 +128,6 @@ rubygems_version: 1.4.2
123
128
  signing_key:
124
129
  specification_version: 3
125
130
  summary: popen with stderr and DeferrableChildProcess with errback for EventMachine
126
- test_files: []
127
-
131
+ test_files:
132
+ - test/em_pessimistic/deferrable_child_process_test.rb
133
+ - test/test_helper.rb