]> git.donarmstrong.com Git - biopieces.git/blob - code_ruby/lib/maasha/filesys.rb
gzip fiddling
[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 require 'open3'
28
29 # Error class for all exceptions to do with Filesys.
30 class FilesysError < StandardError; end
31
32 class Filesys
33   include Enumerable
34
35   # Class method that returns a path to a unique temporary file.
36   # If no directory is specified reverts to the systems tmp directory.
37   def self.tmpfile(tmp_dir = ENV["TMPDIR"])
38     time = Time.now.to_i
39     user = ENV["USER"]
40     pid  = $$
41     path = tmp_dir + [user, time + pid, pid].join("_") + ".tmp"
42     path
43   end
44
45   def self.open(*args)
46     file    = args.shift
47     mode    = args.shift
48     options = args.shift || {}
49
50     if mode == 'w'
51       case options[:compress]
52       when :gzip
53         # ios = Zlib::GzipWriter.new File.open(file, mode, options)
54         ios, = Open3.pipeline_w("gzip -f", out: file)
55       when :bzip, :bzip2
56         # ios = Bzip2::Writer.new File.open(file, mode, options)   # TODO being pissed with buggy bzip2 gem.
57         ios, = Open3.pipeline_w("bzip2 -c", out: file)
58       else 
59         ios = File.open(file, mode, options)
60       end
61     else
62       if file == '-'
63         ios = STDIN
64       else
65         case `file -L #{file}`
66         when /gzip/
67           # ios = Zlib::GzipReader.new File.open(file, mode, options)
68           ios = IO.popen("gzip -cd #{file}")
69         when /bzip/
70           # ios = Bzip2::Reader.new File.open(file, mode, options)   # TODO this method is buggy, investigate
71           ios = IO.popen("bzcat #{file}")
72         else
73           ios = File.open(file, mode, options)
74         end
75       end
76     end
77
78     if block_given?
79       begin
80         yield self.new(ios)
81       ensure
82         ios.close
83       end
84     else
85       return self.new(ios)
86     end
87   end
88
89   def initialize(ios)
90     @io = ios
91   end
92
93   def puts(*args)
94     @io.puts(*args)
95   end
96
97   def close
98     @io.close
99   end
100
101   def eof?
102     @io.eof?
103   end
104
105   # Iterator method for parsing entries.
106   def each
107     while entry = get_entry do
108       yield entry
109     end
110   end
111 end
112