]> git.donarmstrong.com Git - biopieces.git/blob - code_ruby/lib/maasha/filesys.rb
2ee775760fa36faabcabcbac99f193bdbc4524dc
[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
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   # Class method allowing open to be used on (zipped) files.
44   # See File.open.
45   def self.open(*args)
46     args = *args
47     file = args.first
48
49     if file == "-"
50       ios = self.new(STDIN)
51     elsif File.pipe? file
52       ios = self.new(File.open(*args))
53     else
54       ios = self.zopen(*args)
55     end
56
57     if block_given?
58       begin
59         yield ios
60       ensure
61         ios.close
62       end
63     else
64       return ios
65     end
66   end
67
68   def initialize(io)
69     @io = io
70   end
71
72   # Method to close ios.
73   def close
74     @io.close
75   end
76
77   # Method to check if io is closed.
78   def closed?
79     @io.closed?
80   end
81
82   # Iterator method for parsing entries.
83   def each
84     while entry = get_entry do
85       yield entry
86     end
87   end
88
89   # Method to puts directoy on Filesys objects.
90   def puts(*args)
91     @io.puts(*args)
92   end
93
94   private
95
96   # Helper method to return an ios to a file that may be zipped in which case
97   # the ios is unzipped on the fly. See File.open.
98   def self.zopen(*args)
99     ios = File.open(*args)
100
101     begin
102       ios = Zlib::GzipReader.new(ios)
103     rescue
104       ios.rewind
105     end
106
107     self.new(ios)
108   end
109 end
110