event-expectr 0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -0
- data/README.md +32 -0
- data/event-expectr.gemspec +18 -0
- data/lib/event-expectr/version.rb +3 -0
- data/lib/event-expectr.rb +69 -0
- metadata +52 -0
data/.gitignore
ADDED
data/README.md
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
Event Expectr
|
2
|
+
=============
|
3
|
+
|
4
|
+
This is a wraper around the Expectr gem (https://github.com/cwuest/expectr). It simply allows you to define a list of patterns and code blocks which will all be checked for a match against an instance of Expectr. When one of them matches, the code block is executed. This makes automating some processes easier, however it breaks with the original expect (ala TCL) workflow.
|
5
|
+
|
6
|
+
Example
|
7
|
+
-------
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
irb(main):001:0> require 'event-expectr'
|
11
|
+
=> true
|
12
|
+
irb(main):002:0> e = EventExpectr.new "ssh root@server.example.com", :flush_buffer => false
|
13
|
+
=> #<EventExpectr:0x007ff2eb09abd8 @timeout=30, @expectr=#<Expectr:0x007ff2eb09abb0 @buffer="", @discard="", @timeout=0.01, @flush_buffer=false, @buffer_size=8192, @constrain=false, @force_match=false, @out_mutex=#<Mutex:0x007ff2eb09aa70>, @out_update=false, @interact=false, @stdout=#<File:/dev/ttys004>, @stdin=#<File:/dev/ttys004>, @pid=8828>, @patterns={}>
|
14
|
+
irb(main):003:0> e.expect("Are you sure you want to continue connecting (yes/no)?") {|match| e.expectr.puts "yes"}
|
15
|
+
=> #<Proc:0x007ff2eb0201d0@(irb):5>
|
16
|
+
irb(main):004:0> e.expect(/Password: $/) {|match| e.expectr.puts "pa55w0rd"}
|
17
|
+
=> #<Proc:0x007ff2eb19f920@(irb):6>
|
18
|
+
irb(main):005:0> e.expect(/^root@([a-z0-9]+).*# /i) {|match| puts "Login to #{match[1]} successful"}
|
19
|
+
=> #<Proc:0x007ff2eb17d3c0@(irb):7>
|
20
|
+
irb(main):005:0> e.run!, :blocking => true
|
21
|
+
```
|
22
|
+
|
23
|
+
Each time a pattern matches, it is removed from the internal hash of patterns it checks in the EventExpectr#run! method. When there are no patterns left to check, run! will return true. It will also return with a false value if the timeout is reached before all the matches are made.
|
24
|
+
|
25
|
+
You can set the EventExpectr#running value to false at any time when the run! method is running to cause it to bail out immediatly. So, if you have many patterns and code blocks, and when one of them matches you want to stop all further matching, just do something like this:
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
e.expect /^root@([a-z0-9]+).*# /i do |match|
|
29
|
+
puts "Login to #{match[1]} successful"
|
30
|
+
e.running = false
|
31
|
+
end
|
32
|
+
```
|
@@ -0,0 +1,18 @@
|
|
1
|
+
$LOAD_PATH.unshift File.expand_path('../lib', __FILE__)
|
2
|
+
require 'event-expectr/version'
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = "event-expectr"
|
6
|
+
s.version = EventExpectr::VERSION
|
7
|
+
|
8
|
+
s.description = "Wraps the Expectr class so that you can \"register\" a list of several patterns to search for and have a block of code be executed when and if they match."
|
9
|
+
s.summary = "Wrapper for Ruby's expectr gem that enables a different type of workflow."
|
10
|
+
s.authors = ["Andrew Regner"]
|
11
|
+
s.email = "andrew@aregner.com"
|
12
|
+
s.homepage = "http://github.com/adregner/event-expectr"
|
13
|
+
|
14
|
+
s.files = `git ls-files`.split("\n")
|
15
|
+
#s.test_files = s.files.select { |f| f =~ /^test\/test_/ }
|
16
|
+
|
17
|
+
s.license = 'MIT'
|
18
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
require 'expectr'
|
2
|
+
require 'timeout'
|
3
|
+
|
4
|
+
class EventExpectr
|
5
|
+
# hash of patterns mapping to code blocks to execute when they match
|
6
|
+
attr_reader :patterns
|
7
|
+
|
8
|
+
# Expectr instance this class wraps
|
9
|
+
attr_reader :expectr
|
10
|
+
|
11
|
+
# boolean value that keeps the run! method running. set to false to bail out.
|
12
|
+
attr_accessor :running
|
13
|
+
|
14
|
+
def initialize(cmd, args={})
|
15
|
+
@timeout = args.delete(:timeout) || 30
|
16
|
+
|
17
|
+
@expectr = Expectr.new(cmd, args)
|
18
|
+
@expectr.timeout = 0.01
|
19
|
+
|
20
|
+
@patterns = {}
|
21
|
+
end
|
22
|
+
|
23
|
+
def expect(pattern, &block)
|
24
|
+
if not [String, Regexp].include? pattern.class
|
25
|
+
raise TypeError, "Pattren class should be String or Regexp"
|
26
|
+
end
|
27
|
+
|
28
|
+
@patterns[pattern] = block
|
29
|
+
end
|
30
|
+
|
31
|
+
def run!
|
32
|
+
if @patterns.count == 0
|
33
|
+
raise RuntimeError, "No patterns have been defined yet, this would just look forever"
|
34
|
+
end
|
35
|
+
|
36
|
+
@running = true
|
37
|
+
|
38
|
+
begin
|
39
|
+
Timeout::timeout(@timeout) do
|
40
|
+
while @running
|
41
|
+
@patterns.each_pair do |pattern, block|
|
42
|
+
match = @expectr.expect(pattern, true)
|
43
|
+
if match
|
44
|
+
# run the code provided with this pattern
|
45
|
+
block.call(match)
|
46
|
+
|
47
|
+
# remove it so we don't try to match again
|
48
|
+
@patterns.delete(pattern)
|
49
|
+
|
50
|
+
# the code block may have turned this off
|
51
|
+
break unless @running
|
52
|
+
end
|
53
|
+
|
54
|
+
@running = false if @patterns.count == 0
|
55
|
+
end
|
56
|
+
|
57
|
+
sleep 0.1
|
58
|
+
end
|
59
|
+
end
|
60
|
+
rescue Timeout::Error
|
61
|
+
# haven't found all the matches in the time given. oh well.
|
62
|
+
return false
|
63
|
+
end
|
64
|
+
|
65
|
+
# this is an intentional end of the run process
|
66
|
+
return true
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
metadata
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: event-expectr
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '0.1'
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Andrew Regner
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-11-28 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Wraps the Expectr class so that you can "register" a list of several
|
15
|
+
patterns to search for and have a block of code be executed when and if they match.
|
16
|
+
email: andrew@aregner.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- .gitignore
|
22
|
+
- README.md
|
23
|
+
- event-expectr.gemspec
|
24
|
+
- lib/event-expectr.rb
|
25
|
+
- lib/event-expectr/version.rb
|
26
|
+
homepage: http://github.com/adregner/event-expectr
|
27
|
+
licenses:
|
28
|
+
- MIT
|
29
|
+
post_install_message:
|
30
|
+
rdoc_options: []
|
31
|
+
require_paths:
|
32
|
+
- lib
|
33
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
34
|
+
none: false
|
35
|
+
requirements:
|
36
|
+
- - ! '>='
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
39
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ! '>='
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: '0'
|
45
|
+
requirements: []
|
46
|
+
rubyforge_project:
|
47
|
+
rubygems_version: 1.8.23
|
48
|
+
signing_key:
|
49
|
+
specification_version: 3
|
50
|
+
summary: Wrapper for Ruby's expectr gem that enables a different type of workflow.
|
51
|
+
test_files: []
|
52
|
+
has_rdoc:
|