From 710e372b17e16f4a7cb5cc999bbbbfc49edfae26 Mon Sep 17 00:00:00 2001 From: martinahansen Date: Wed, 4 Aug 2010 13:24:43 +0000 Subject: [PATCH] polishing ruby code git-svn-id: http://biopieces.googlecode.com/svn/trunk@1032 74ccb610-7750-0410-82ae-013aeee3265d --- code_ruby/Maasha/lib/biopieces.rb | 83 ++++++------------------------- 1 file changed, 15 insertions(+), 68 deletions(-) diff --git a/code_ruby/Maasha/lib/biopieces.rb b/code_ruby/Maasha/lib/biopieces.rb index 9c48992..6af2ad0 100644 --- a/code_ruby/Maasha/lib/biopieces.rb +++ b/code_ruby/Maasha/lib/biopieces.rb @@ -4,19 +4,6 @@ require 'optparse' require 'open3' require 'pp' -@exit_status = nil - -%w( INT TERM QUIT ).each do |signal| - Signal.trap(signal) do - exit_status = signal - exit - end -end - -at_exit { - pp @exit_status -} - # Biopieces are command line scripts and uses OptionParser to parse command line # options according to a list of casts. Each cast prescribes the long and short # name of the option, the type, if it is mandatory, the default value, and allowed @@ -35,7 +22,6 @@ class Biopieces @output = output @status = Status.new @status.set unless @test - #prime_teardown end # Check the integrity of a list of casts, followed by parsion options from argv @@ -95,7 +81,11 @@ class Biopieces path end - def prime_teardown + # Stuff to be done at begin such as setting the exit status + # and signal handlers + def self.do_at_begin + status = Status.new + status.set exit_status = "OK" # Install signal handlers @@ -106,12 +96,18 @@ class Biopieces end end + exit_status + end + + # Make sure that file streams are closed, tmpdir removed, and + # exit status is written to log file. + def self.do_at_exit(exit_status) at_exit do begin - @in.close if @in.respond_to? :close - @out.close if @out.respond_to? :close - + @in.close if @in.respond_to? :close # FIXME: This can never work + @out.close if @out.respond_to? :close # FIXME: Same rescue Exception => exception + $stderr.puts "Exception caught!" # DEBUG case exit_status when 'INT' then exit_status = 'INTERRUPTED' when 'TERM' then exit_status = 'TERMINATED' @@ -119,12 +115,12 @@ class Biopieces exit_status = "DIED" unless exit_status =~ /INT|TERM|QUIT/ $stderr.puts exit_status $stderr.puts exception.backtrace - ensure status = Status.new FileUtils.remove_entry_secure(status.get_tmpdir) status.log(exit_status) status.delete + puts "EXIT STATUS: #{exit_status}" end end end @@ -632,53 +628,4 @@ class Stream < IO end -# Stuff to be done at begin such as setting the exit status -# and signal handlers -def do_at_begin - status = Status.new - status.set - exit_status = "OK" - - # Install signal handlers - %w( INT TERM QUIT ).each do |signal| - Signal.trap(signal) do - exit_status = signal - exit - end - end - - exit_status -end - -# Make sure that file streams are closed, tmpdir removed, and -# exit status is written to log file. -def do_at_exit(exit_status) - at_exit do - begin - @in.close if @in.respond_to? :close - @out.close if @out.respond_to? :close - rescue Exception => exception - $stderr.puts "Exception caught!" # DEBUG - case exit_status - when 'INT' then exit_status = 'INTERRUPTED' - when 'TERM' then exit_status = 'TERMINATED' - end - exit_status = "DIED" unless exit_status =~ /INT|TERM|QUIT/ - $stderr.puts exit_status - $stderr.puts exception.backtrace - - ensure - status = Status.new - FileUtils.remove_entry_secure(status.get_tmpdir) - status.log(exit_status) - status.delete - puts "EXIT STATUS: #{exit_status}" - end - end -end - -exit_status = do_at_begin -do_at_exit(exit_status) - - __END__ -- 2.39.5