]> git.donarmstrong.com Git - biopieces.git/blob - code_ruby/lib/maasha/filesys.rb
temporary fix of bzip2 read problem
[biopieces.git] / code_ruby / lib / maasha / filesys.rb
1 # Copyright (C) 2007-2011 Martin A. Hansen.
2
3 # This program is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License
5 # as published by the Free Software Foundation; either version 2
6 # of the License, or (at your option) any later version.
7
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 # GNU General Public License for more details.
12
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16
17 # http://www.gnu.org/copyleft/gpl.html
18
19 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
20
21 # This software is part of the Biopieces framework (www.biopieces.org).
22
23 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
24
25 require 'zlib'
26 require 'bzip2'
27
28 # Error class for all exceptions to do with Filesys.
29 class FilesysError < StandardError; end
30
31 class Filesys
32   include Enumerable
33
34   # Class method that returns a path to a unique temporary file.
35   # If no directory is specified reverts to the systems tmp directory.
36   def self.tmpfile(tmp_dir = ENV["TMPDIR"])
37     time = Time.now.to_i
38     user = ENV["USER"]
39     pid  = $$
40     path = tmp_dir + [user, time + pid, pid].join("_") + ".tmp"
41     path
42   end
43
44   def self.open(*args)
45     file    = args.shift
46     mode    = args.shift
47     options = args.shift || {}
48
49     if mode == 'w'
50       case options[:compress]
51       when :gzip
52         ios = Zlib::GzipWriter.new File.open(file, mode, options)
53       when :bzip, :bzip2
54         ios = Bzip2::Writer.new File.open(file, mode, options)
55       else 
56         ios = File.open(file, mode, options)
57       end
58     else
59       if file == '-'
60         ios = STDIN
61       else
62         case `file #{file}`
63         when /gzip/
64           ios = Zlib::GzipReader.new File.open(file, mode, options)
65         when /bzip/
66           # ios = Bzip2::Reader.new File.open(file, mode, options)   # TODO this method is buggy, investigate
67           ios = IO.popen("bzcat #{file}")
68         else
69           ios = File.open(file, mode, options)
70         end
71       end
72     end
73
74     if block_given?
75       begin
76         yield self.new(ios)
77       ensure
78         ios.close
79       end
80     else
81       return self.new(ios)
82     end
83   end
84
85   def initialize(ios)
86     @io = ios
87   end
88
89   def puts(*args)
90     @io.puts(*args)
91   end
92
93   def close
94     @io.close
95   end
96
97   def eof?
98     @io.eof?
99   end
100
101   # Iterator method for parsing entries.
102   def each
103     while entry = get_entry do
104       yield entry
105     end
106   end
107 end
108