puppet 2.7.13 → 2.7.14
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of puppet might be problematic. Click here for more details.
- data/CHANGELOG +144 -1
- data/conf/osx/preflight +1 -1
- data/conf/redhat/puppet.spec +9 -5
- data/conf/suse/puppet.spec +4 -1
- data/conf/windows/eventlog/Rakefile +32 -0
- data/conf/windows/eventlog/puppetres.dll +0 -0
- data/conf/windows/eventlog/puppetres.mc +18 -0
- data/ext/rack/files/apache2.conf +3 -0
- data/install.rb +23 -1
- data/lib/puppet.rb +1 -1
- data/lib/puppet/agent.rb +1 -14
- data/lib/puppet/application/kick.rb +1 -1
- data/lib/puppet/application/module.rb +11 -0
- data/lib/puppet/daemon.rb +74 -3
- data/lib/puppet/defaults.rb +1 -1
- data/lib/puppet/face/certificate.rb +1 -1
- data/lib/puppet/face/help/man.erb +1 -1
- data/lib/puppet/face/module.rb +17 -0
- data/lib/puppet/face/module/build.rb +10 -4
- data/lib/puppet/face/module/changes.rb +5 -5
- data/lib/puppet/face/module/generate.rb +6 -4
- data/lib/puppet/face/module/install.rb +122 -32
- data/lib/puppet/face/module/list.rb +234 -33
- data/lib/puppet/face/module/search.rb +56 -23
- data/lib/puppet/face/module/uninstall.rb +33 -38
- data/lib/puppet/face/module/upgrade.rb +84 -0
- data/lib/puppet/feature/eventlog.rb +6 -0
- data/lib/puppet/forge.rb +67 -122
- data/lib/puppet/forge/cache.rb +1 -1
- data/lib/puppet/forge/repository.rb +6 -25
- data/lib/puppet/indirector/facts/network_device.rb +1 -1
- data/lib/puppet/interface/action.rb +1 -1
- data/lib/puppet/module.rb +79 -28
- data/lib/puppet/module_tool.rb +72 -34
- data/lib/puppet/module_tool/applications.rb +12 -14
- data/lib/puppet/module_tool/applications/application.rb +21 -19
- data/lib/puppet/module_tool/applications/builder.rb +4 -4
- data/lib/puppet/module_tool/applications/checksummer.rb +12 -3
- data/lib/puppet/module_tool/applications/generator.rb +1 -1
- data/lib/puppet/module_tool/applications/installer.rb +163 -34
- data/lib/puppet/module_tool/applications/searcher.rb +2 -3
- data/lib/puppet/module_tool/applications/uninstaller.rb +84 -36
- data/lib/puppet/module_tool/applications/unpacker.rb +4 -26
- data/lib/puppet/module_tool/applications/upgrader.rb +109 -0
- data/lib/puppet/module_tool/checksums.rb +2 -2
- data/lib/puppet/module_tool/contents_description.rb +1 -1
- data/lib/puppet/module_tool/dependency.rb +2 -2
- data/lib/puppet/module_tool/errors.rb +9 -0
- data/lib/puppet/module_tool/errors/base.rb +15 -0
- data/lib/puppet/module_tool/errors/installer.rb +90 -0
- data/lib/puppet/module_tool/errors/shared.rb +115 -0
- data/lib/puppet/module_tool/errors/uninstaller.rb +45 -0
- data/lib/puppet/module_tool/errors/upgrader.rb +72 -0
- data/lib/puppet/module_tool/metadata.rb +2 -2
- data/lib/puppet/module_tool/modulefile.rb +7 -7
- data/lib/puppet/module_tool/shared_behaviors.rb +161 -0
- data/lib/puppet/module_tool/skeleton.rb +1 -1
- data/lib/puppet/node/environment.rb +4 -2
- data/lib/puppet/parser/ast/leaf.rb +1 -1
- data/lib/puppet/parser/functions/create_resources.rb +3 -2
- data/lib/puppet/parser/scope.rb +44 -9
- data/lib/puppet/provider/augeas/augeas.rb +2 -2
- data/lib/puppet/provider/exec.rb +8 -3
- data/lib/puppet/provider/exec/shell.rb +1 -2
- data/lib/puppet/provider/nameservice/directoryservice.rb +10 -4
- data/lib/puppet/provider/package/gem.rb +1 -1
- data/lib/puppet/provider/package/pkg.rb +10 -21
- data/lib/puppet/provider/selmodule/semodule.rb +1 -2
- data/lib/puppet/provider/service/upstart.rb +33 -17
- data/lib/puppet/provider/ssh_authorized_key/parsed.rb +1 -1
- data/lib/puppet/rails/inventory_node.rb +7 -7
- data/lib/puppet/reports/http.rb +4 -1
- data/lib/puppet/reports/tagmail.rb +8 -1
- data/lib/puppet/resource/type.rb +1 -1
- data/lib/puppet/test/test_helper.rb +138 -0
- data/lib/puppet/type.rb +9 -1
- data/lib/puppet/type/file.rb +18 -10
- data/lib/puppet/type/package.rb +13 -9
- data/lib/puppet/type/resources.rb +1 -1
- data/lib/puppet/type/ssh_authorized_key.rb +3 -4
- data/lib/puppet/type/sshkey.rb +4 -4
- data/lib/puppet/type/user.rb +1 -0
- data/lib/puppet/type/vlan.rb +1 -1
- data/lib/puppet/util.rb +31 -14
- data/lib/puppet/util/autoload.rb +1 -1
- data/lib/puppet/util/command_line.rb +2 -6
- data/lib/puppet/util/instrumentation/indirection_probe.rb +1 -1
- data/lib/puppet/util/instrumentation/instrumentable.rb +1 -1
- data/lib/puppet/util/instrumentation/listeners/log.rb +1 -1
- data/lib/puppet/util/instrumentation/listeners/performance.rb +1 -1
- data/lib/puppet/util/log.rb +3 -1
- data/lib/puppet/util/log/destinations.rb +38 -0
- data/lib/puppet/util/monkey_patches.rb +45 -0
- data/lib/puppet/util/network_device/base.rb +1 -1
- data/lib/puppet/util/network_device/cisco.rb +1 -1
- data/lib/puppet/util/network_device/cisco/facts.rb +1 -1
- data/lib/puppet/util/network_device/cisco/interface.rb +1 -1
- data/lib/puppet/util/network_device/config.rb +1 -1
- data/lib/puppet/util/network_device/ipcalc.rb +1 -1
- data/lib/puppet/util/network_device/transport.rb +1 -1
- data/lib/puppet/util/network_device/transport/base.rb +1 -1
- data/lib/puppet/util/network_device/transport/ssh.rb +1 -1
- data/lib/puppet/util/settings.rb +2 -11
- data/lib/puppet/util/settings/file_setting.rb +3 -5
- data/lib/puppet/util/terminal.rb +16 -0
- data/lib/puppet/util/zaml.rb +3 -1
- data/lib/semver.rb +15 -7
- data/spec/fixtures/releases/jamtur01-apache/metadata.json +1 -1
- data/spec/fixtures/unit/parser/lexer/arithmetic_expression.pp +1 -1
- data/spec/fixtures/unit/provider/package/pkg/dummy +1 -0
- data/spec/fixtures/unit/provider/package/pkg/incomplete +1 -0
- data/spec/fixtures/unit/provider/package/pkg/publisher +2 -0
- data/spec/fixtures/unit/provider/package/pkg/simple +4 -0
- data/spec/fixtures/unit/reports/tagmail/tagmail_email.conf +2 -0
- data/spec/fixtures/yaml/report0.25.x.yaml +1 -1
- data/spec/fixtures/yaml/report2.6.x.yaml +1 -1
- data/spec/integration/faces/documentation_spec.rb +1 -1
- data/spec/integration/network/rest_authconfig_spec.rb +1 -1
- data/spec/lib/puppet_spec/compiler.rb +6 -0
- data/spec/lib/puppet_spec/database.rb +30 -0
- data/spec/lib/puppet_spec/files.rb +4 -2
- data/spec/shared_behaviours/path_parameters.rb +2 -29
- data/spec/shared_contexts/platform.rb +43 -0
- data/spec/spec_helper.rb +36 -65
- data/spec/unit/agent_spec.rb +0 -32
- data/spec/unit/application/kick_spec.rb +2 -2
- data/spec/unit/daemon_spec.rb +1 -17
- data/spec/unit/face/module/install_spec.rb +158 -0
- data/spec/unit/face/module/list_spec.rb +182 -0
- data/spec/unit/face/module/search_spec.rb +163 -0
- data/spec/unit/face/module/uninstall_spec.rb +77 -0
- data/spec/unit/face/module/upgrade_spec.rb +26 -0
- data/spec/unit/forge/repository_spec.rb +0 -30
- data/spec/unit/forge_spec.rb +28 -86
- data/spec/unit/indirector/catalog/active_record_spec.rb +45 -65
- data/spec/unit/indirector/facts/inventory_active_record_spec.rb +5 -18
- data/spec/unit/indirector/resource/active_record_spec.rb +2 -11
- data/spec/unit/indirector/resource/ral_spec.rb +7 -2
- data/spec/unit/module_spec.rb +240 -107
- data/spec/unit/module_tool/application_spec.rb +3 -5
- data/spec/unit/module_tool/applications/application_spec.rb +19 -0
- data/spec/unit/module_tool/applications/installer_spec.rb +205 -0
- data/spec/unit/module_tool/applications/uninstaller_spec.rb +206 -0
- data/spec/unit/module_tool/applications/upgrader_spec.rb +37 -0
- data/spec/unit/module_tool/metadata_spec.rb +2 -2
- data/spec/unit/module_tool_spec.rb +109 -1
- data/spec/unit/node/environment_spec.rb +16 -1
- data/spec/unit/parser/ast/leaf_spec.rb +16 -1
- data/spec/unit/parser/collector_spec.rb +2 -12
- data/spec/unit/parser/functions/create_resources_spec.rb +135 -86
- data/spec/unit/parser/functions/generate_spec.rb +2 -10
- data/spec/unit/parser/scope_spec.rb +345 -16
- data/spec/unit/provider/augeas/augeas_spec.rb +19 -0
- data/spec/unit/provider/exec/shell_spec.rb +17 -14
- data/spec/unit/provider/exec/windows_spec.rb +1 -7
- data/spec/unit/provider/exec_spec.rb +35 -0
- data/spec/unit/provider/nameservice/directoryservice_spec.rb +10 -0
- data/spec/unit/provider/package/dpkg_spec.rb +2 -1
- data/spec/unit/provider/package/gem_spec.rb +15 -0
- data/spec/unit/provider/package/openbsd_spec.rb +5 -4
- data/spec/unit/provider/package/pacman_spec.rb +3 -2
- data/spec/unit/provider/package/pkg_spec.rb +56 -33
- data/spec/unit/provider/selmodule_spec.rb +11 -4
- data/spec/unit/provider/service/redhat_spec.rb +1 -3
- data/spec/unit/provider/service/smf_spec.rb +1 -3
- data/spec/unit/provider/service/upstart_spec.rb +38 -0
- data/spec/unit/provider/ssh_authorized_key/parsed_spec.rb +28 -0
- data/spec/unit/rails/host_spec.rb +6 -12
- data/spec/unit/rails/param_value_spec.rb +3 -8
- data/spec/unit/rails/resource_spec.rb +2 -8
- data/spec/unit/reports/http_spec.rb +47 -31
- data/spec/unit/reports/tagmail_spec.rb +77 -0
- data/spec/unit/resource/type_spec.rb +2 -2
- data/spec/unit/resource_spec.rb +18 -20
- data/spec/unit/semver_spec.rb +31 -13
- data/spec/unit/type/exec_spec.rb +8 -15
- data/spec/unit/type/group_spec.rb +0 -9
- data/spec/unit/type/package_spec.rb +10 -0
- data/spec/unit/type/resources_spec.rb +4 -5
- data/spec/unit/type/ssh_authorized_key_spec.rb +4 -15
- data/spec/unit/type/sshkey_spec.rb +9 -11
- data/spec/unit/type/user_spec.rb +123 -127
- data/spec/unit/type_spec.rb +20 -0
- data/spec/unit/util/command_line_spec.rb +2 -2
- data/spec/unit/util/instrumentation/data_spec.rb +1 -1
- data/spec/unit/util/instrumentation/indirection_probe_spec.rb +1 -1
- data/spec/unit/util/instrumentation/instrumentable_spec.rb +1 -1
- data/spec/unit/util/instrumentation/listener_spec.rb +1 -1
- data/spec/unit/util/instrumentation/listeners/log_spec.rb +1 -1
- data/spec/unit/util/instrumentation/listeners/performance_spec.rb +1 -1
- data/spec/unit/util/instrumentation_spec.rb +1 -1
- data/spec/unit/util/log/destinations_spec.rb +4 -8
- data/spec/unit/util/log_spec.rb +47 -0
- data/spec/unit/util/reference_spec.rb +1 -1
- data/spec/unit/util/settings/file_setting_spec.rb +9 -0
- data/spec/unit/util/settings_spec.rb +0 -53
- data/spec/unit/util/terminal_spec.rb +42 -0
- data/spec/unit/util/zaml_spec.rb +7 -0
- data/spec/unit/util_spec.rb +63 -20
- data/tasks/rake/manpages.rake +1 -1
- data/test/data/snippets/arithmetic_expression.pp +1 -1
- data/test/other/puppet.rb +0 -1
- data/test/util/log.rb +6 -6
- metadata +41 -16
- data/lib/puppet/external/event-loop.rb +0 -1
- data/lib/puppet/external/event-loop/better-definers.rb +0 -367
- data/lib/puppet/external/event-loop/event-loop.rb +0 -355
- data/lib/puppet/external/event-loop/signal-system.rb +0 -218
- data/lib/puppet/face/module/clean.rb +0 -30
- data/lib/puppet/module_tool/applications/cleaner.rb +0 -16
- data/lib/puppet/module_tool/skeleton/templates/generator/metadata.json +0 -12
- data/lib/puppet/module_tool/utils.rb +0 -5
- data/lib/puppet/module_tool/utils/interrogation.rb +0 -25
- data/spec/integration/module_tool_spec.rb +0 -475
- data/spec/unit/module_tool/uninstaller_spec.rb +0 -124
@@ -1,355 +0,0 @@
|
|
1
|
-
## event-loop.rb --- high-level IO multiplexer
|
2
|
-
# Copyright (C) 2005 Daniel Brockman
|
3
|
-
|
4
|
-
# This program is free software; you can redistribute it
|
5
|
-
# and/or modify it under the terms of the GNU General Public
|
6
|
-
# License as published by the Free Software Foundation;
|
7
|
-
# either version 2 of the License, or (at your option) any
|
8
|
-
# later version.
|
9
|
-
|
10
|
-
# This file is distributed in the hope that it will be useful,
|
11
|
-
# but WITHOUT ANY WARRANTY; without even the implied warranty
|
12
|
-
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
13
|
-
# See the GNU General Public License for more details.
|
14
|
-
|
15
|
-
# You should have received a copy of the GNU General Public
|
16
|
-
# License along with this program; if not, write to the Free
|
17
|
-
# Software Foundation, 51 Franklin Street, Fifth Floor,
|
18
|
-
# Boston, MA 02110-1301, USA.
|
19
|
-
|
20
|
-
require "puppet/external/event-loop/better-definers"
|
21
|
-
require "puppet/external/event-loop/signal-system"
|
22
|
-
|
23
|
-
require "fcntl"
|
24
|
-
|
25
|
-
class EventLoop
|
26
|
-
include SignalEmitter
|
27
|
-
|
28
|
-
IO_STATES = [:readable, :writable, :exceptional]
|
29
|
-
|
30
|
-
class << self
|
31
|
-
def default ; @default ||= new end
|
32
|
-
def default= x ; @default = x end
|
33
|
-
|
34
|
-
def current
|
35
|
-
Thread.current["event-loop::current"] || default end
|
36
|
-
def current= x
|
37
|
-
Thread.current["event-loop::current"] = x end
|
38
|
-
|
39
|
-
def with_current (new)
|
40
|
-
if current == new
|
41
|
-
yield
|
42
|
-
else
|
43
|
-
begin
|
44
|
-
old = self.current
|
45
|
-
self.current = new
|
46
|
-
yield
|
47
|
-
ensure
|
48
|
-
self.current = old
|
49
|
-
end
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
def method_missing (name, *args, &block)
|
54
|
-
if current.respond_to? name
|
55
|
-
current.__send__(name, *args, &block)
|
56
|
-
else
|
57
|
-
super
|
58
|
-
end
|
59
|
-
end
|
60
|
-
end
|
61
|
-
|
62
|
-
define_signals :before_sleep, :after_sleep
|
63
|
-
|
64
|
-
def initialize
|
65
|
-
@running = false
|
66
|
-
@awake = false
|
67
|
-
@wakeup_time = nil
|
68
|
-
@timers = []
|
69
|
-
|
70
|
-
@io_arrays = [[], [], []]
|
71
|
-
@ios = Hash.new do |h, k| raise ArgumentError,
|
72
|
-
"invalid IO event: #{k}", caller(2) end
|
73
|
-
IO_STATES.each_with_index { |x, i| @ios[x] = @io_arrays[i] }
|
74
|
-
|
75
|
-
@notify_src, @notify_snk = IO.pipe
|
76
|
-
|
77
|
-
# prevent file descriptor leaks
|
78
|
-
if @notify_src.respond_to?(:fcntl) and defined?(Fcntl) and defined?(Fcntl::F_SETFD) and defined?(Fcntl::FD_CLOEXEC)
|
79
|
-
@notify_src.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC)
|
80
|
-
@notify_snk.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC)
|
81
|
-
end
|
82
|
-
|
83
|
-
@notify_src.will_block = false
|
84
|
-
@notify_snk.will_block = false
|
85
|
-
|
86
|
-
# Each time a byte is sent through the notification pipe
|
87
|
-
# we need to read it, or IO.select will keep returning.
|
88
|
-
monitor_io(@notify_src, :readable)
|
89
|
-
@notify_src.extend(Watchable)
|
90
|
-
@notify_src.on_readable do
|
91
|
-
begin
|
92
|
-
@notify_src.sysread(256)
|
93
|
-
rescue Errno::EAGAIN
|
94
|
-
# The pipe wasn't readable after all.
|
95
|
-
end
|
96
|
-
end
|
97
|
-
end
|
98
|
-
|
99
|
-
define_opposite_accessors \
|
100
|
-
:stopped? => :running?,
|
101
|
-
:sleeping? => :awake?
|
102
|
-
|
103
|
-
def run
|
104
|
-
if block_given?
|
105
|
-
thread = Thread.new { run }
|
106
|
-
yield ; quit ; thread.join
|
107
|
-
else
|
108
|
-
running!
|
109
|
-
iterate while running?
|
110
|
-
end
|
111
|
-
ensure
|
112
|
-
quit
|
113
|
-
end
|
114
|
-
|
115
|
-
def iterate (user_timeout=nil)
|
116
|
-
t1, t2 = user_timeout, max_timeout
|
117
|
-
timeout = t1 && t2 ? [t1, t2].min : t1 || t2
|
118
|
-
select(timeout).zip(IO_STATES) do |ios, state|
|
119
|
-
ios.each { |x| x.signal(state) } if ios
|
120
|
-
end
|
121
|
-
end
|
122
|
-
|
123
|
-
private
|
124
|
-
|
125
|
-
def select (timeout)
|
126
|
-
@wakeup_time = timeout ? Time.now + timeout : nil
|
127
|
-
# puts "waiting: #{timeout} seconds"
|
128
|
-
signal :before_sleep ; sleeping!
|
129
|
-
IO.select(*@io_arrays + [timeout]) || []
|
130
|
-
ensure
|
131
|
-
awake! ; signal :after_sleep
|
132
|
-
@timers.each { |x| x.sound_alarm if x.ready? }
|
133
|
-
end
|
134
|
-
|
135
|
-
public
|
136
|
-
|
137
|
-
def quit ; stopped! ; wake_up ; self end
|
138
|
-
|
139
|
-
def monitoring_io? (io, event)
|
140
|
-
@ios[event].include? io end
|
141
|
-
def monitoring_timer? (timer)
|
142
|
-
@timers.include? timer end
|
143
|
-
|
144
|
-
def monitor_io (io, *events)
|
145
|
-
for event in events do
|
146
|
-
@ios[event] << io ; wake_up unless monitoring_io?(io, event)
|
147
|
-
end
|
148
|
-
end
|
149
|
-
|
150
|
-
def monitor_timer (timer)
|
151
|
-
@timers << timer unless monitoring_timer? timer
|
152
|
-
end
|
153
|
-
|
154
|
-
def check_timer (timer)
|
155
|
-
wake_up if timer.end_time < @wakeup_time
|
156
|
-
end
|
157
|
-
|
158
|
-
def ignore_io (io, *events)
|
159
|
-
events = IO_STATES if events.empty?
|
160
|
-
for event in events do
|
161
|
-
wake_up if @ios[event].delete(io)
|
162
|
-
end
|
163
|
-
end
|
164
|
-
|
165
|
-
def ignore_timer (timer)
|
166
|
-
# Don't need to wake up for this.
|
167
|
-
@timers.delete(timer)
|
168
|
-
end
|
169
|
-
|
170
|
-
def max_timeout
|
171
|
-
return nil if @timers.empty?
|
172
|
-
[@timers.collect { |x| x.time_left }.min, 0].max
|
173
|
-
end
|
174
|
-
|
175
|
-
def wake_up
|
176
|
-
@notify_snk.write('.') if sleeping?
|
177
|
-
end
|
178
|
-
end
|
179
|
-
|
180
|
-
class Symbol
|
181
|
-
def io_state?
|
182
|
-
EventLoop::IO_STATES.include? self
|
183
|
-
end
|
184
|
-
end
|
185
|
-
|
186
|
-
module EventLoop::Watchable
|
187
|
-
include SignalEmitter
|
188
|
-
|
189
|
-
define_signals :readable, :writable, :exceptional
|
190
|
-
|
191
|
-
def monitor_events (*events)
|
192
|
-
EventLoop.monitor_io(self, *events) end
|
193
|
-
def ignore_events (*events)
|
194
|
-
EventLoop.ignore_io(self, *events) end
|
195
|
-
|
196
|
-
define_soft_aliases \
|
197
|
-
:monitor_event => :monitor_events,
|
198
|
-
:ignore_event => :ignore_events
|
199
|
-
|
200
|
-
def close ; super
|
201
|
-
ignore_events end
|
202
|
-
def close_read ; super
|
203
|
-
ignore_event :readable end
|
204
|
-
def close_write ; super
|
205
|
-
ignore_event :writable end
|
206
|
-
|
207
|
-
module Automatic
|
208
|
-
include EventLoop::Watchable
|
209
|
-
|
210
|
-
def add_signal_handler (name, &handler) super
|
211
|
-
monitor_event(name) if name.io_state?
|
212
|
-
end
|
213
|
-
|
214
|
-
def remove_signal_handler (name, handler) super
|
215
|
-
if @signal_handlers[name].empty?
|
216
|
-
ignore_event(name) if name.io_state?
|
217
|
-
end
|
218
|
-
end
|
219
|
-
end
|
220
|
-
end
|
221
|
-
|
222
|
-
class IO
|
223
|
-
def on_readable &block
|
224
|
-
extend EventLoop::Watchable::Automatic
|
225
|
-
on_readable(&block)
|
226
|
-
end
|
227
|
-
|
228
|
-
def on_writable &block
|
229
|
-
extend EventLoop::Watchable::Automatic
|
230
|
-
on_writable(&block)
|
231
|
-
end
|
232
|
-
|
233
|
-
def on_exceptional &block
|
234
|
-
extend EventLoop::Watchable::Automatic
|
235
|
-
on_exceptional(&block)
|
236
|
-
end
|
237
|
-
|
238
|
-
def will_block?
|
239
|
-
if respond_to?(:fcntl) and defined?(Fcntl) and defined?(Fcntl::F_GETFL) and defined?(Fcntl::O_NONBLOCK)
|
240
|
-
fcntl(Fcntl::F_GETFL, 0) & Fcntl::O_NONBLOCK == 0
|
241
|
-
end
|
242
|
-
end
|
243
|
-
|
244
|
-
def will_block= (wants_blocking)
|
245
|
-
if respond_to?(:fcntl) and defined?(Fcntl) and defined?(Fcntl::F_GETFL) and defined?(Fcntl::O_NONBLOCK)
|
246
|
-
flags = fcntl(Fcntl::F_GETFL, 0)
|
247
|
-
if wants_blocking
|
248
|
-
flags &= ~Fcntl::O_NONBLOCK
|
249
|
-
else
|
250
|
-
flags |= Fcntl::O_NONBLOCK
|
251
|
-
end
|
252
|
-
fcntl(Fcntl::F_SETFL, flags)
|
253
|
-
end
|
254
|
-
end
|
255
|
-
end
|
256
|
-
|
257
|
-
class EventLoop::Timer
|
258
|
-
include SignalEmitter
|
259
|
-
|
260
|
-
DEFAULT_INTERVAL = 0.0
|
261
|
-
DEFAULT_TOLERANCE = 0.001
|
262
|
-
|
263
|
-
def initialize (options={}, &handler)
|
264
|
-
@running = false
|
265
|
-
@start_time = nil
|
266
|
-
|
267
|
-
options = { :interval => options } if options.kind_of? Numeric
|
268
|
-
|
269
|
-
if options[:interval]
|
270
|
-
@interval = options[:interval].to_f
|
271
|
-
else
|
272
|
-
@interval = DEFAULT_INTERVAL
|
273
|
-
end
|
274
|
-
|
275
|
-
if options[:tolerance]
|
276
|
-
@tolerance = options[:tolerance].to_f
|
277
|
-
elsif DEFAULT_TOLERANCE < @interval
|
278
|
-
@tolerance = DEFAULT_TOLERANCE
|
279
|
-
else
|
280
|
-
@tolerance = 0.0
|
281
|
-
end
|
282
|
-
|
283
|
-
@event_loop = options[:event_loop] || EventLoop.current
|
284
|
-
|
285
|
-
if block_given?
|
286
|
-
add_signal_handler(:alarm, &handler)
|
287
|
-
start unless options[:start?] == false
|
288
|
-
else
|
289
|
-
start if options[:start?]
|
290
|
-
end
|
291
|
-
end
|
292
|
-
|
293
|
-
define_readers :interval, :tolerance
|
294
|
-
define_signal :alarm
|
295
|
-
|
296
|
-
def stopped? ; @start_time == nil end
|
297
|
-
def running? ; @start_time != nil end
|
298
|
-
|
299
|
-
def interval= (new_interval)
|
300
|
-
old_interval = @interval
|
301
|
-
@interval = new_interval
|
302
|
-
@event_loop.check_timer(self) if new_interval < old_interval
|
303
|
-
end
|
304
|
-
|
305
|
-
def end_time
|
306
|
-
@start_time + @interval end
|
307
|
-
def time_left
|
308
|
-
end_time - Time.now end
|
309
|
-
def ready?
|
310
|
-
time_left <= @tolerance end
|
311
|
-
|
312
|
-
def restart
|
313
|
-
@start_time = Time.now
|
314
|
-
end
|
315
|
-
|
316
|
-
def sound_alarm
|
317
|
-
signal :alarm
|
318
|
-
restart if running?
|
319
|
-
end
|
320
|
-
|
321
|
-
def start
|
322
|
-
@start_time = Time.now
|
323
|
-
@event_loop.monitor_timer(self)
|
324
|
-
end
|
325
|
-
|
326
|
-
def stop
|
327
|
-
@start_time = nil
|
328
|
-
@event_loop.ignore_timer(self)
|
329
|
-
end
|
330
|
-
end
|
331
|
-
|
332
|
-
if __FILE__ == $0
|
333
|
-
require "test/unit"
|
334
|
-
|
335
|
-
class TimerTest < Test::Unit::TestCase
|
336
|
-
def setup
|
337
|
-
@timer = EventLoop::Timer.new(:interval => 0.001)
|
338
|
-
end
|
339
|
-
|
340
|
-
def test_timer
|
341
|
-
@timer.on_alarm do
|
342
|
-
puts "[#{@timer.time_left} seconds left after alarm]"
|
343
|
-
EventLoop.quit
|
344
|
-
end
|
345
|
-
8.times do
|
346
|
-
t0 = Time.now
|
347
|
-
@timer.start ; EventLoop.run
|
348
|
-
t1 = Time.now
|
349
|
-
assert(t1 - t0 > @timer.interval - @timer.tolerance)
|
350
|
-
end
|
351
|
-
end
|
352
|
-
end
|
353
|
-
end
|
354
|
-
|
355
|
-
## event-loop.rb ends here.
|
@@ -1,218 +0,0 @@
|
|
1
|
-
## signal-system.rb --- simple intra-process signal system
|
2
|
-
# Copyright (C) 2005 Daniel Brockman
|
3
|
-
|
4
|
-
# This program is free software; you can redistribute it
|
5
|
-
# and/or modify it under the terms of the GNU General Public
|
6
|
-
# License as published by the Free Software Foundation;
|
7
|
-
# either version 2 of the License, or (at your option) any
|
8
|
-
# later version.
|
9
|
-
|
10
|
-
# This file is distributed in the hope that it will be useful,
|
11
|
-
# but WITHOUT ANY WARRANTY; without even the implied warranty
|
12
|
-
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
13
|
-
# See the GNU General Public License for more details.
|
14
|
-
|
15
|
-
# You should have received a copy of the GNU General Public
|
16
|
-
# License along with this program; if not, write to the Free
|
17
|
-
# Software Foundation, 51 Franklin Street, Fifth Floor,
|
18
|
-
# Boston, MA 02110-1301, USA.
|
19
|
-
|
20
|
-
require "puppet/external/event-loop/better-definers"
|
21
|
-
|
22
|
-
module SignalEmitterModule
|
23
|
-
def self.extended (object)
|
24
|
-
if object.kind_of? Module and not object < SignalEmitter
|
25
|
-
if object.respond_to? :fcall
|
26
|
-
# This is the way to call private methods
|
27
|
-
# in Ruby 1.9 as of November 16.
|
28
|
-
object.fcall :include, SignalEmitter
|
29
|
-
else
|
30
|
-
object.__send__ :include, SignalEmitter
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
def define_signal (name, slot=:before, &body)
|
36
|
-
# Can't use `define_method' and take a block pre-1.9.
|
37
|
-
class_eval %{ def on_#{name} &block
|
38
|
-
add_signal_handler(:#{name}, &block) end }
|
39
|
-
define_signal_handler(name, :before, &lambda {|*a|})
|
40
|
-
define_signal_handler(name, :after, &lambda {|*a|})
|
41
|
-
define_signal_handler(name, slot, &body) if block_given?
|
42
|
-
end
|
43
|
-
|
44
|
-
def define_signals (*names, &body)
|
45
|
-
names.each { |x| define_signal(x, &body) }
|
46
|
-
end
|
47
|
-
|
48
|
-
def define_signal_handler (name, slot=:before, &body)
|
49
|
-
case slot
|
50
|
-
when :before
|
51
|
-
define_protected_method "handle_#{name}", &body
|
52
|
-
when :after
|
53
|
-
define_protected_method "after_handle_#{name}", &body
|
54
|
-
else
|
55
|
-
raise ArgumentError, "invalid slot `#{slot.inspect}'; " +
|
56
|
-
"should be `:before' or `:after'", caller(1)
|
57
|
-
end
|
58
|
-
end
|
59
|
-
end
|
60
|
-
|
61
|
-
# This is an old name for the same thing.
|
62
|
-
SignalEmitterClass = SignalEmitterModule
|
63
|
-
|
64
|
-
module SignalEmitter
|
65
|
-
def self.included (includer)
|
66
|
-
includer.extend SignalEmitterClass if not includer.kind_of? SignalEmitterClass
|
67
|
-
end
|
68
|
-
|
69
|
-
def __maybe_initialize_signal_emitter
|
70
|
-
@signal_handlers ||= Hash.new { |h, k| h[k] = Array.new }
|
71
|
-
@allow_dynamic_signals ||= false
|
72
|
-
end
|
73
|
-
|
74
|
-
define_accessors :allow_dynamic_signals?
|
75
|
-
|
76
|
-
def add_signal_handler (name, &handler)
|
77
|
-
__maybe_initialize_signal_emitter
|
78
|
-
@signal_handlers[name] << handler
|
79
|
-
handler
|
80
|
-
end
|
81
|
-
|
82
|
-
define_soft_aliases [:on, :on_signal] => :add_signal_handler
|
83
|
-
|
84
|
-
def remove_signal_handler (name, handler)
|
85
|
-
__maybe_initialize_signal_emitter
|
86
|
-
@signal_handlers[name].delete(handler)
|
87
|
-
end
|
88
|
-
|
89
|
-
def __signal__ (name, *args, &block)
|
90
|
-
__maybe_initialize_signal_emitter
|
91
|
-
respond_to? "on_#{name}" or allow_dynamic_signals? or
|
92
|
-
fail "undefined signal `#{name}' for #{self}:#{self.class}"
|
93
|
-
__send__("handle_#{name}", *args, &block) if
|
94
|
-
respond_to? "handle_#{name}"
|
95
|
-
@signal_handlers[name].each { |x| x.call(*args, &block) }
|
96
|
-
__send__("after_handle_#{name}", *args, &block) if
|
97
|
-
respond_to? "after_handle_#{name}"
|
98
|
-
end
|
99
|
-
|
100
|
-
define_soft_alias :signal => :__signal__
|
101
|
-
end
|
102
|
-
|
103
|
-
# This module is indended to be a convenience mixin to be used by
|
104
|
-
# classes whose objects need to observe foreign signals. That is,
|
105
|
-
# if you want to observe some signals coming from an object, *you*
|
106
|
-
# should mix in this module.
|
107
|
-
#
|
108
|
-
# You cannot use this module at two different places of the same
|
109
|
-
# inheritance chain to observe signals coming from the same object.
|
110
|
-
#
|
111
|
-
# XXX: This has not seen much use, and I'd like to provide a
|
112
|
-
# better solution for the problem in the future.
|
113
|
-
module SignalObserver
|
114
|
-
def __maybe_initialize_signal_observer
|
115
|
-
@observed_signals ||= Hash.new do |signals, object|
|
116
|
-
signals[object] = Hash.new do |handlers, name|
|
117
|
-
handlers[name] = Array.new
|
118
|
-
end
|
119
|
-
end
|
120
|
-
end
|
121
|
-
|
122
|
-
def observe_signal (subject, name, &handler)
|
123
|
-
__maybe_initialize_signal_observer
|
124
|
-
@observed_signals[subject][name] << handler
|
125
|
-
subject.add_signal_handler(name, &handler)
|
126
|
-
end
|
127
|
-
|
128
|
-
def map_signals (source, pairs={})
|
129
|
-
pairs.each do |src_name, dst_name|
|
130
|
-
observe_signal(source, src_name) do |*args|
|
131
|
-
__signal__(dst_name, *args)
|
132
|
-
end
|
133
|
-
end
|
134
|
-
end
|
135
|
-
|
136
|
-
def absorb_signals (subject, *names)
|
137
|
-
names.each do |name|
|
138
|
-
observe_signal(subject, name) do |*args|
|
139
|
-
__signal__(name, *args)
|
140
|
-
end
|
141
|
-
end
|
142
|
-
end
|
143
|
-
|
144
|
-
define_soft_aliases \
|
145
|
-
:map_signal => :map_signals,
|
146
|
-
:absorb_signal => :absorb_signals
|
147
|
-
|
148
|
-
def ignore_signal (subject, name)
|
149
|
-
__maybe_initialize_signal_observer
|
150
|
-
__ignore_signal_1(subject, name)
|
151
|
-
@observed_signals.delete(subject) if
|
152
|
-
@observed_signals[subject].empty?
|
153
|
-
end
|
154
|
-
|
155
|
-
def ignore_signals (subject, *names)
|
156
|
-
__maybe_initialize_signal_observer
|
157
|
-
names = @observed_signals[subject] if names.empty?
|
158
|
-
names.each { |x| __ignore_signal_1(subject, x) }
|
159
|
-
end
|
160
|
-
|
161
|
-
private
|
162
|
-
|
163
|
-
def __ignore_signal_1(subject, name)
|
164
|
-
@observed_signals[subject][name].each do |handler|
|
165
|
-
subject.remove_signal_handler(name, handler) end
|
166
|
-
@observed_signals[subject].delete(name)
|
167
|
-
end
|
168
|
-
end
|
169
|
-
|
170
|
-
if __FILE__ == $0
|
171
|
-
require "test/unit"
|
172
|
-
class SignalEmitterTest < Test::Unit::TestCase
|
173
|
-
class X
|
174
|
-
include SignalEmitter
|
175
|
-
define_signal :foo
|
176
|
-
end
|
177
|
-
|
178
|
-
def setup
|
179
|
-
@x = X.new
|
180
|
-
end
|
181
|
-
|
182
|
-
def test_on_signal
|
183
|
-
moomin = 0
|
184
|
-
@x.on_signal(:foo) { moomin = 1 }
|
185
|
-
@x.signal :foo
|
186
|
-
assert moomin == 1
|
187
|
-
end
|
188
|
-
|
189
|
-
def test_on_foo
|
190
|
-
moomin = 0
|
191
|
-
@x.on_foo { moomin = 1 }
|
192
|
-
@x.signal :foo
|
193
|
-
assert moomin == 1
|
194
|
-
end
|
195
|
-
|
196
|
-
def test_multiple_on_signal
|
197
|
-
moomin = 0
|
198
|
-
@x.on_signal(:foo) { moomin += 1 }
|
199
|
-
@x.on_signal(:foo) { moomin += 2 }
|
200
|
-
@x.on_signal(:foo) { moomin += 4 }
|
201
|
-
@x.on_signal(:foo) { moomin += 8 }
|
202
|
-
@x.signal :foo
|
203
|
-
assert moomin == 15
|
204
|
-
end
|
205
|
-
|
206
|
-
def test_multiple_on_foo
|
207
|
-
moomin = 0
|
208
|
-
@x.on_foo { moomin += 1 }
|
209
|
-
@x.on_foo { moomin += 2 }
|
210
|
-
@x.on_foo { moomin += 4 }
|
211
|
-
@x.on_foo { moomin += 8 }
|
212
|
-
@x.signal :foo
|
213
|
-
assert moomin == 15
|
214
|
-
end
|
215
|
-
end
|
216
|
-
end
|
217
|
-
|
218
|
-
## application-signals.rb ends here.
|