]> git.donarmstrong.com Git - biopieces.git/blobdiff - code_ruby/lib/maasha/biopieces.rb
added get_entry method to biopieces.rb
[biopieces.git] / code_ruby / lib / maasha / biopieces.rb
index 7a0c50e52fc1dadd0100008313fe822bf30f05fc..bd85afb0f4a86c02eaaa8a0dc07324489d8ed6f0 100644 (file)
@@ -79,18 +79,18 @@ class Biopieces
   # records. Records are read from STDIN (default) or file (possibly gzipped)
   # and written to STDOUT (default) or file.
   def self.open(input = STDIN, output = STDOUT)
-    input  = self.open_input(input)
-    output = self.open_output(output)
+    io_in  = self.open_input(input)
+    io_out = self.open_output(output)
 
     if block_given?
       begin
-        yield input, output
+        yield io_in, io_out
       ensure
-        input.close
-        output.close
+        io_in.close
+        io_out.close
       end
     else
-      return input, output
+      return io_in, io_out
     end
   end
 
@@ -123,6 +123,16 @@ class Biopieces
 
   # Method to parse and yield a Biopiece record from _ios_.
   def each_record
+    while record = get_entry
+      yield record
+    end
+
+    self # conventionally
+  end
+
+  alias :each :each_record
+
+  def get_entry
     record = {}
 
     @ios.each_line do |line|
@@ -130,19 +140,16 @@ class Biopieces
       when /^([^:]+): (.*)$/
         record[$1.to_sym] = $2
       when /^---$/
-        yield record unless record.empty?
-        record = {}
+        break
       else
         raise BiopiecesError, "Bad record format: #{line}"
       end
     end
 
-    yield record unless record.empty?
-
-    self # conventionally
+    return record unless record.empty?
   end
 
-  alias :each :each_record
+  alias :get_record :each_entry
 
   private
 
@@ -153,10 +160,10 @@ class Biopieces
       if STDIN.tty?
         input = self.new(StringIO.new)
       else
-        input = self.new(STDIN, stdio = true)
+        input = self.new(STDIN, true)
       end
     elsif File.exists? input
-      ios = File.open(input, mode='r')
+      ios = File.open(input, 'r')
 
       begin
         ios = Zlib::GzipReader.new(ios)
@@ -174,9 +181,9 @@ class Biopieces
   # Records are written to STDOUT (default) or file.
   def self.open_output(output)
     if output.nil?
-      output = self.new(STDOUT, stdio = true)
+      output = self.new(STDOUT, true)
     elsif not output.is_a? IO
-      output = self.new(File.open(output, mode='w'))
+      output = self.new(File.open(output, 'w'))
     end
 
     output
@@ -377,7 +384,7 @@ class OptionHandler
     option_parser.parse!(@argv)
 
     if print_usage_full?
-      print_usage_and_exit(full=true)
+      print_usage_and_exit(true)
     elsif print_usage_short?
       print_usage_and_exit
     end
@@ -566,7 +573,8 @@ class Status
   def set
     time0  = Time.new.strftime("%Y-%m-%d %X")
 
-    File.open(path, mode="w") do |fh|
+    File.open(path, "w") do |fh|
+      fh.flock(File::LOCK_EX)
       fh.puts [time0, ARGV.join(" ")].join(";")
     end
   end
@@ -575,13 +583,15 @@ class Status
   def set_tmpdir(tmpdir_path)
     status = ""
 
-    File.open(path, mode="r") do |fh|
+    File.open(path, "r") do |fh|
+      fh.flock(File::LOCK_SH)
       status = fh.read.chomp
     end
 
     status = "#{status};#{tmpdir_path}\n"
 
-    File.open(path, mode="w") do |fh|
+    File.open(path, "w") do |fh|
+      fh.flock(File::LOCK_EX)
       fh << status
     end
   end
@@ -589,7 +599,8 @@ class Status
   # Extract the temporary directory path from the status file,
   # and return this or nil if not found.
   def get_tmpdir
-    File.open(path, mode="r") do |fh|
+    File.open(path, "r") do |fh|
+      fh.flock(File::LOCK_SH)
       tmpdir_path = fh.read.chomp.split(";").last
       return tmpdir_path if File.directory?(tmpdir_path)
     end
@@ -603,15 +614,22 @@ class Status
     user    = ENV["USER"]
     script  = File.basename($0)
 
-    stream = File.open(path)
-    time0, args, tmp_dir = stream.first.split(";")
-    stream.close
+    time0 = nil
+    args  = nil
+
+    File.open(path, "r") do |fh|
+      fh.flock(File::LOCK_SH)
+      time0, args = fh.first.split(";")
+    end
 
     elap     = time_diff(time0, time1)
     command  = [script, args].join(" ") 
     log_file = File.join(ENV["BP_LOG"], "biopieces.log")
 
-    File.open(log_file, mode = "a") { |file| file.puts [time0, time1, elap, user, exit_status, command].join("\t") }
+    File.open(log_file, "a") do |fh|
+      fh.flock(File::LOCK_EX)
+      fh.puts [time0, time1, elap, user, exit_status, command].join("\t")
+    end
   end
 
   # Delete status file.
@@ -627,6 +645,8 @@ class Status
     script = File.basename($0)
     pid    = $$
     path   = File.join(ENV["BP_TMP"], [user, script, pid, "status"].join("."))
+
+    path
   end
 
   # Get the elapsed time from the difference between two time stamps.