]> git.donarmstrong.com Git - biopieces.git/blob - code_perl/Maasha/Filesys.pm
cb27c95ff62d4544f6c0c607d085e76ce5aca875
[biopieces.git] / code_perl / Maasha / Filesys.pm
1 package Maasha::Filesys;
2
3
4 # Copyright (C) 2006-2008 Martin A. Hansen.
5
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
10
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
20 # http://www.gnu.org/copyleft/gpl.html
21
22
23 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
24
25
26 # This module contains routines for manipulation of files and directories.
27
28
29 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
30
31
32 use strict;
33 use IO::File;
34 use Maasha::Common;
35
36 use Exporter;
37
38 use vars qw( @ISA @EXPORT @EXPORT_OK );
39
40 @ISA = qw( Exporter ) ;
41
42
43 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> FILES <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
44
45
46 sub file_read_open
47 {
48     # Martin A. Hansen, January 2004.
49
50     # Read opens a file that may be gzipped and returns a filehandle.
51
52     my ( $path,   # full path to file
53        ) = @_;
54
55     # Returns filehandle
56
57     my ( $fh );
58
59     if ( is_gzipped( $path ) ) {
60         $fh = new IO::File "zcat $path|" or Maasha::Common::error( qq(Could not read-open file "$path": $!) );
61     } else {
62         $fh = new IO::File $path, "r" or Maasha::Common::error( qq(Could not read-open file "$path": $!) );
63     }
64
65     return $fh;
66 }
67
68
69 sub file_write_open
70 {
71     # Martin A. Hansen, January 2004.
72
73     # write opens a file and returns a filehandle
74
75     my ( $path,   # full path to file
76          $gzip,   # flag if data is to be gzipped - OPRIONAL
77        ) = @_;
78
79     # returns filehandle
80
81     my ( $fh );
82
83     if ( $gzip ) {
84         $fh = new IO::File "|gzip -f>$path" or Maasha::Common::error( qq(Could not write-open file "$path": $!) );
85     } else {
86         $fh = new IO::File $path, "w" or Maasha::Common::error( qq(Could not write-open file "$path": $!) );
87     }
88
89     return $fh;
90 }
91
92
93 sub file_append_open
94 {
95     # Martin A. Hansen, February 2006.
96
97     # append opens file and returns a filehandle
98
99     my ( $path,     # path to file
100        ) = @_;
101
102     # returns filehandle
103
104     my ( $fh );
105
106     $fh = new IO::File $path, "a" or Maasha::Common::error( qq(Could not append-open file "$path": $!) );
107
108     return $fh;
109 }
110
111
112 sub stdin_read
113 {
114     # Martin A. Hansen, July 2007.
115
116     # Returns a filehandle to STDIN
117
118     my ( $fh );
119
120     $fh = new IO::File "<&STDIN" or Maasha::Common::error( qq(Could not read from STDIN: $!) );
121
122     return $fh;
123 }
124
125
126 sub stdout_write
127 {
128     # Martin A. Hansen, July 2007.
129
130     # Returns a filehandle to STDOUT
131
132     my ( $fh );
133
134     $fh = new IO::File ">&STDOUT" or Maasha::Common::error( qq(Could not write to STDOUT: $!) );
135
136     return $fh;
137 }
138
139
140 sub file_read
141 {
142     # Martin A. Hansen, December 2004.
143
144     # given a file, a seek beg position and
145     # length, returns the corresponding string.
146     
147     my ( $fh,     # file handle to file
148          $beg,    # read start in file
149          $len,    # read length of block
150         ) = @_;
151
152     # returns string
153
154     my ( $string );
155
156     Maasha::Common::error( qq(Negative length: $len) ) if $len < 0;
157
158     sysseek $fh, $beg, 0;
159     sysread $fh, $string, $len;
160
161     return $string;
162 }
163
164
165 sub file_copy
166 {
167     # Martin A. Hansen, November 2008.
168
169     # Copy the content of a file from source path to
170     # destination path.
171
172     my ( $src,   # source path
173          $dst,   # destination path
174        ) = @_;
175
176     # Returns nothing.
177
178     my ( $fh_in, $fh_out, $line );
179
180     Maasha::Common::error( qq(copy failed: destination equals source "$src") ) if $src eq $dst;
181
182     $fh_in  = file_read_open( $src );
183     $fh_out = file_write_open( $dst );
184
185     while ( $line = <$fh_in> ) {
186         print $fh_out $line;
187     } 
188
189     close $fh_in;
190     close $fh_out;
191 }
192
193
194 sub is_gzipped
195 {
196     # Martin A. Hansen, November 2008.
197
198     # Checks if a given file is gzipped.
199     # Currrently uses a call to the systems
200     # file tool. Returns 1 if gzipped otherwise
201     # returns 0.
202
203     my ( $path,   # path to file
204        ) = @_;
205
206     # Returns boolean.
207
208     my ( $type );
209     
210     $type = `file $path`;
211
212     if ( $type =~ /gzip compressed/ ) {
213         return 1;
214     } else {
215         return 0;
216     }
217 }
218
219
220 sub file_size
221 {
222     # Martin A. Hansen, March 2007
223
224     # returns the file size for a given file
225
226     my ( $path,   # full path to file
227        ) = @_;
228
229     # returns integer
230
231     my $file_size = ( stat ( $path ) )[ 7 ];
232
233     return $file_size;
234 }
235
236
237 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DIRECTORIES <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
238
239
240 sub dir_create
241 {
242     # Martin A. Hansen, July 2007.
243
244     # Creates a directory.
245
246     my ( $path,   # full path to dir
247        ) = @_;
248
249     # Returns nothing.
250
251     if ( -d $path ) {
252         Maasha::Common::error( qq(Directory already exists "$path": $!) );
253     } else {
254         mkdir $path or Maasha::Common::error( qq(Could not create directory "$path": $!) );
255     }
256 }
257
258
259 sub dir_create_if_not_exists
260 {
261     # Martin A. Hansen, May 2008.
262
263     # Creates a directory if it does not already exists.
264
265     my ( $path,   # full path to dir
266        ) = @_;
267
268     # Returns nothing.
269
270     if ( not -d $path ) {
271         mkdir $path or Maasha::Common::error( qq(Could not create directory "$path": $!) );
272     }
273 }
274
275
276 sub dir_remove
277 {
278     # Martin A. Hansen, April 2008.
279
280     # Removes a directory recursively.
281
282     my ( $path,   # directory
283        ) = @_;
284
285     Maasha::Common::run( "rm", "-rf $path" ) if -d $path;
286 }
287
288
289 sub ls_dirs
290 {
291     # Martin A. Hansen, June 2007.
292
293     # returns all dirs in a given directory.
294
295     my ( $path,   # full path to directory
296        ) = @_;
297
298     # returns a list of filenames.
299
300     my ( $dh, @dirs );
301
302     $dh = open_dir( $path );
303
304     @dirs =  read_dir( $dh );
305     @dirs = grep { -d "$path/$_" } @dirs;
306
307     map { $_ = "$path/$_" } @dirs;
308
309     close $dh;
310
311     return wantarray ? @dirs : \@dirs;
312 }
313
314
315 sub ls_files
316 {
317     # Martin A. Hansen, June 2007.
318
319     # returns all files in a given directory.
320
321     my ( $path,   # full path to directory
322        ) = @_;
323
324     # returns a list of filenames.
325
326     my ( $dh, @files );
327
328     $dh = open_dir( $path );
329
330     @files =  read_dir( $dh );
331     @files = grep { -f "$path/$_" } @files;
332
333     map { $_ = "$path/$_" } @files;
334
335     close $dh;
336
337     return wantarray ? @files : \@files;
338 }
339
340
341 sub open_dir
342 {
343     # Martin A. Hansen, June 2007.
344
345     # open a directory and returns a directory handle
346
347     use IO::Dir;
348
349     my ( $path,   # full path to directory
350        ) = @_;
351
352     # returns object
353
354     my $dh;
355
356     $dh = IO::Dir->new( $path ) or Maasha::Common::error( qq(Could not open dir "$path": $!) );
357
358     return $dh;
359 }
360
361
362 sub read_dir
363 {
364     # Martin A. Hansen, June 2007.
365
366     # read all files and directories from a directory.
367
368     my ( $dh,   # directory handle object
369        ) = @_;
370
371     # returns list
372
373     my ( $elem, @elems );
374
375     while ( defined( $elem = $dh->read ) ) {
376         push @elems, $elem;
377     }
378
379     return wantarray ? @elems : \@elems;
380 }
381
382
383 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
384
385
386 1;
387
388
389 __END__