patir 0.2 → 0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/History.txt +4 -0
  2. data/lib/patir/base.rb +15 -2
  3. data/lib/patir/command.rb +137 -139
  4. metadata +2 -2
data/History.txt CHANGED
@@ -1,3 +1,7 @@
1
+ == 0.3.0 / 2007-05-20
2
+ * Bugfix for the nesting of CommandSequenceStatus
3
+ * PatirLoggerFormatter added and used in setup_logger
4
+
1
5
  == 0.2.0 / 2007-03-22
2
6
  * Added the Command library
3
7
  * Implementations: ShellCommand, RubyCommand
data/lib/patir/base.rb CHANGED
@@ -5,7 +5,7 @@ module Patir
5
5
  require 'logger'
6
6
  #Version information
7
7
  VERSION_MAJOR="0"
8
- VERSION_MINOR="2"
8
+ VERSION_MINOR="3"
9
9
  VERSION="#{VERSION_MAJOR}.#{VERSION_MINOR}"
10
10
  #Error thrown usually in initialize methods when missing required parameters
11
11
  #from the initialization hash.
@@ -15,6 +15,18 @@ module Patir
15
15
  def self.drb_service ip,port
16
16
  return "druby://#{ip}:#{port}"
17
17
  end
18
+
19
+ class PatirLoggerFormatter<Logger::Formatter
20
+ Format="[%s] %5s: %s\n"
21
+ def initialize
22
+ @datetime_format="%Y%m%d %H:%M:%S"
23
+ end
24
+
25
+ def call severity, time, progname, msg
26
+ Format % [format_datetime(time), severity,
27
+ msg2str(msg)]
28
+ end
29
+ end
18
30
  #Just making Logger usage easier
19
31
  #
20
32
  #This is for use on top level scripts.
@@ -40,7 +52,8 @@ module Patir
40
52
  logger.level=Logger::FATAL if mode==:mute
41
53
  logger.level=Logger::WARN if mode==:silent
42
54
  logger.level=Logger::DEBUG if mode==:debug || $DEBUG
43
- logger.datetime_format="%Y%m%d %H:%M:%S"
55
+ logger.formatter=PatirLoggerFormatter.new
56
+ #logger.datetime_format="%Y%m%d %H:%M:%S"
44
57
  return logger
45
58
  end
46
59
  end
data/lib/patir/command.rb CHANGED
@@ -97,7 +97,7 @@ module Patir
97
97
  return @status
98
98
  end
99
99
  end
100
-
100
+
101
101
  #This class wraps the http://codeforpeople.com/lib/ruby/systemu/ as a Command duck.
102
102
  #
103
103
  #It allows for execution of any shell command.
@@ -125,7 +125,7 @@ module Patir
125
125
  @command=params[:cmd]
126
126
  @status=:not_executed
127
127
  end
128
-
128
+
129
129
  #Executes the shell command and returns the status
130
130
  def run
131
131
  start_time=Time.now
@@ -148,12 +148,12 @@ module Patir
148
148
  @exec_time=Time.now-start_time
149
149
  return @status
150
150
  end
151
-
151
+
152
152
  def to_s
153
153
  return "#{@name}: #{@command} in #{File.expand_path(@working_directory)}"
154
154
  end
155
155
  end
156
-
156
+
157
157
  #CommandSequence describes a set of commands to be executed in sequence.
158
158
  #
159
159
  #Each instance of CommandSequence contains a set of Patir::Command instances, which are the steps to perform.
@@ -173,7 +173,7 @@ module Patir
173
173
  attr_reader :name,:state,:steps
174
174
  attr_reader :sequence_runner
175
175
  attr_reader :sequence_id
176
-
176
+
177
177
  def initialize name,sequence_runner=""
178
178
  @name=name
179
179
  @steps||=Array.new
@@ -181,13 +181,13 @@ module Patir
181
181
  #intialize the status for the currently active build (not executed)
182
182
  reset
183
183
  end
184
-
184
+
185
185
  #sets the sequence runner attribute updating status
186
186
  def sequence_runner=name
187
187
  @sequence_runner=name
188
188
  @state.sequence_runner=name
189
189
  end
190
-
190
+
191
191
  #sets the sequence id attribute updating status
192
192
  def sequence_id=name
193
193
  @sequence_id=name
@@ -221,26 +221,26 @@ module Patir
221
221
  notify(:sequence_status=>@state)
222
222
  #evaluate the results' effect on execution status at the end
223
223
  case result
224
- when :success
225
- #everything is fine, continue
226
- when :error
227
- #this will be the final status
228
- running_status=:error
229
- #stop if we fail on error
230
- if :fail_on_error==step.strategy
231
- @state.status=:error
232
- break
233
- end
234
- when :warning
235
- #a previous failure overrides a warning
236
- running_status=:warning unless :error==running_status
237
- #escalate this to a failure if the strategy says so
238
- running_status=:error if :flunk_on_warning==step.strategy
239
- #stop if we fail on warning
240
- if :fail_on_warning==step.strategy
241
- @state.status=:error
242
- break
243
- end
224
+ when :success
225
+ #everything is fine, continue
226
+ when :error
227
+ #this will be the final status
228
+ running_status=:error
229
+ #stop if we fail on error
230
+ if :fail_on_error==step.strategy
231
+ @state.status=:error
232
+ break
233
+ end
234
+ when :warning
235
+ #a previous failure overrides a warning
236
+ running_status=:warning unless :error==running_status
237
+ #escalate this to a failure if the strategy says so
238
+ running_status=:error if :flunk_on_warning==step.strategy
239
+ #stop if we fail on warning
240
+ if :fail_on_warning==step.strategy
241
+ @state.status=:error
242
+ break
243
+ end
244
244
  end
245
245
  end
246
246
  #we finished
@@ -271,7 +271,7 @@ module Patir
271
271
  @state.step=bstep
272
272
  notify(:sequence_status=>@state)
273
273
  end
274
-
274
+
275
275
  #Resets the status. This will set :not_executed status,
276
276
  #and set the start and end times to nil.
277
277
  def reset
@@ -286,12 +286,12 @@ module Patir
286
286
  #tell the world
287
287
  notify(:sequence_status=>@state)
288
288
  end
289
-
289
+
290
290
  #Returns true if the sequence has finished executing
291
291
  def completed?
292
292
  return @state.completed?
293
293
  end
294
-
294
+
295
295
  def to_s
296
296
  "#{sequence_id}:#{:name} on #{@sequence_runner}, #{@steps.size} steps"
297
297
  end
@@ -321,7 +321,7 @@ module Patir
321
321
  #
322
322
  #:error is set when after execution at least one step has the :error status
323
323
  class CommandSequenceStatus
324
- attr_accessor :start_time,:stop_time,:sequence_runner,:sequence_name,:status,:step_states,:sequence_id
324
+ attr_accessor :start_time,:stop_time,:sequence_runner,:sequence_name,:status,:step_states,:sequence_id,:strategy
325
325
  #You can pass an array of Commands to initialize CommandSequenceStatus
326
326
  def initialize sequence_name,steps=nil
327
327
  @sequence_name=sequence_name
@@ -343,7 +343,7 @@ module Patir
343
343
  return true if :success==@status
344
344
  return false
345
345
  end
346
-
346
+
347
347
  #A sequence is considered completed when:
348
348
  #
349
349
  #a step has errors and the :fail_on_error strategy is used
@@ -369,118 +369,116 @@ module Patir
369
369
  #Adds a step to the state. The step state is inferred from the Command instance __step__
370
370
  def step=step
371
371
  @step_states[step.number]={:name=>step.name,
372
- :status=>step.status,
373
- :output=>step.output,
374
- :duration=>step.exec_time,
375
- :error=>step.error,
376
- :strategy=>step.strategy}
377
- #this way we don't have to compare all the step states we always get the worst last stable state
378
- #:not_executed<:success<:warning<:success
379
- @previous_status=@status unless @status==:running
380
- case step.status
381
- when :running
382
- @status=:running
383
- when :warning
384
- @status=:warning unless @status==:error
385
- @status=:error if @previous_status==:error
386
- when :error
387
- @status=:error
388
- when :success
389
- @status=:success unless @status==:error || @status==:warning
390
- @status=:warning if @previous_status==:warning
391
- @status=:error if @previous_status==:error
392
- when :not_executed
393
- @status=@previous_status
372
+ :status=>step.status,
373
+ :output=>step.output,
374
+ :duration=>step.exec_time,
375
+ :error=>step.error,
376
+ :strategy=>step.strategy}
377
+ #this way we don't have to compare all the step states we always get the worst last stable state
378
+ #:not_executed<:success<:warning<:success
379
+ @previous_status=@status unless @status==:running
380
+ case step.status
381
+ when :running
382
+ @status=:running
383
+ when :warning
384
+ @status=:warning unless @status==:error
385
+ @status=:error if @previous_status==:error
386
+ when :error
387
+ @status=:error
388
+ when :success
389
+ @status=:success unless @status==:error || @status==:warning
390
+ @status=:warning if @previous_status==:warning
391
+ @status=:error if @previous_status==:error
392
+ when :not_executed
393
+ @status=@previous_status
394
+ end
394
395
  end
395
- end
396
- #produces a brief text summary for this status
397
- def summary
398
- sum=""
399
- sum<<"#{@sequence_id}:" if @sequence_id
400
- sum<<"#{@sequence_name}. " unless @sequence_name.empty?
401
- sum<<"Status - #{@status}"
402
- if !@step_states.empty?
403
- sum<<".States #{@step_states.size}\nStep status summary:"
404
- if :not_executed!=@status
396
+ #produces a brief text summary for this status
397
+ def summary
398
+ sum=""
399
+ sum<<"#{@sequence_id}:" if @sequence_id
400
+ sum<<"#{@sequence_name}. " unless @sequence_name.empty?
401
+ sum<<"Status - #{@status}"
402
+ if !@step_states.empty? && @status!=:not_executed
403
+ sum<<". States #{@step_states.size}\nStep status summary:"
405
404
  @step_states.each do |number,state|
406
405
  sum<<"\n\t#{number}:'#{state[:name]}' - #{state[:status]}"
407
406
  end
408
- end
409
- end
410
- return sum
411
- end
412
- def to_s
413
- "'#{sequence_id}':'#{@sequence_name}' on '#{@sequence_runner}' started at #{@start_time}.#{@step_states.size} steps"
414
- end
415
- def exec_time
416
- return @stop_time-@start_time if @stop_time
417
- return 0
418
- end
419
- def name
420
- return @sequence_name
421
- end
422
- def number
423
- return @sequence_id
424
- end
425
- def output
426
- return self.summary
427
- end
428
- def error
429
- return ""
430
- end
431
- def executed?
432
- return true unless @status==:not_executed
433
- return false
434
- end
435
- end
436
-
437
- #This class allows you to wrap Ruby blocks and handle them like Command
438
- #
439
- #Provide a block to RubyCommand#new and you can execute the block using
440
- #RubyCommand#run
441
- #
442
- #The block receives the instance of RubyCommand so you can set the output and error output.
443
- #
444
- #The return value of the block is assigned as the command status.
445
- #
446
- #== Examples
447
- #An example (using the excellent HighLine lib) of a CLI prompt as a RubyCommand
448
- # RubyCommand.new("prompt") do |cmd|
449
- # cmd.output=""
450
- # cmd.error=""
451
- # if HighLine.agree("#{step.text}?")
452
- # :success
453
- # else
454
- # :error
455
- # end
456
- # end
457
- class RubyCommand
458
- include Patir::Command
459
- def initialize name,working_directory=nil,&block
460
- @name=name
461
- @working_directory=working_directory
462
- if block_given?
463
- @cmd=block
464
- else
465
- raise "You need to provide a block"
407
+ end
408
+ return sum
409
+ end
410
+ def to_s
411
+ "'#{sequence_id}':'#{@sequence_name}' on '#{@sequence_runner}' started at #{@start_time}.#{@step_states.size} steps"
412
+ end
413
+ def exec_time
414
+ return @stop_time-@start_time if @stop_time
415
+ return 0
416
+ end
417
+ def name
418
+ return @sequence_name
419
+ end
420
+ def number
421
+ return @sequence_id
422
+ end
423
+ def output
424
+ return self.summary
425
+ end
426
+ def error
427
+ return ""
428
+ end
429
+ def executed?
430
+ return true unless @status==:not_executed
431
+ return false
466
432
  end
467
433
  end
468
- #Runs the associated block
469
- def run
470
- @run=true
471
- prev_dir=Dir.pwd
472
- begin
473
- Dir.chdir(@working_directory) if @working_directory
474
- t1=Time.now
475
- @status=@cmd.call(self)
476
- @exec_time=Time.now-t1
477
- rescue SystemCallError
478
- @output="#{$!}"
479
- @success=false
480
- ensure
481
- Dir.chdir(prev_dir)
434
+
435
+ #This class allows you to wrap Ruby blocks and handle them like Command
436
+ #
437
+ #Provide a block to RubyCommand#new and you can execute the block using
438
+ #RubyCommand#run
439
+ #
440
+ #The block receives the instance of RubyCommand so you can set the output and error output.
441
+ #
442
+ #The return value of the block is assigned as the command status.
443
+ #
444
+ #== Examples
445
+ #An example (using the excellent HighLine lib) of a CLI prompt as a RubyCommand
446
+ # RubyCommand.new("prompt") do |cmd|
447
+ # cmd.output=""
448
+ # cmd.error=""
449
+ # if HighLine.agree("#{step.text}?")
450
+ # :success
451
+ # else
452
+ # :error
453
+ # end
454
+ # end
455
+ class RubyCommand
456
+ include Patir::Command
457
+ def initialize name,working_directory=nil,&block
458
+ @name=name
459
+ @working_directory=working_directory
460
+ if block_given?
461
+ @cmd=block
462
+ else
463
+ raise "You need to provide a block"
464
+ end
465
+ end
466
+ #Runs the associated block
467
+ def run
468
+ @run=true
469
+ prev_dir=Dir.pwd
470
+ begin
471
+ Dir.chdir(@working_directory) if @working_directory
472
+ t1=Time.now
473
+ @status=@cmd.call(self)
474
+ @exec_time=Time.now-t1
475
+ rescue SystemCallError
476
+ @output="#{$!}"
477
+ @success=false
478
+ ensure
479
+ Dir.chdir(prev_dir)
480
+ end
481
+ return @success
482
482
  end
483
- return @success
484
483
  end
485
- end
486
- end
484
+ end
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.1
3
3
  specification_version: 1
4
4
  name: patir
5
5
  version: !ruby/object:Gem::Version
6
- version: "0.2"
7
- date: 2007-05-17 00:00:00 +02:00
6
+ version: "0.3"
7
+ date: 2007-05-21 00:00:00 +02:00
8
8
  summary: patir (Project Automation Tools in Ruby) provides libraries for use in automation tools
9
9
  require_paths:
10
10
  - lib