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