source: subversion/applications/utils/perl_lib/Utils/File.pm @ 3004

Last change on this file since 3004 was 2558, checked in by joerg, 13 years ago

print clearer update needed message in debug mode

File size: 4.5 KB
Line 
1##################################################################
2package Utils::File;
3##################################################################
4
5use Exporter;
6@ISA = qw( Exporter );
7use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $VERSION);
8@EXPORT = qw( data_open
9              file_needs_re_generation
10              mkdir_if_needed
11              newest_unpacked_filename
12              expand_filename
13              );
14use strict;
15use warnings;
16
17use IO::File;
18use Utils::Debug;
19
20use File::Basename;
21use File::Copy;
22use File::Path;
23use Time::Local;
24
25# -----------------------------------------------------------------------------
26# Expand ~/ against Homedir of user
27sub expand_filename($){
28    my $filename = shift;
29    $filename =~ s/^\~/$ENV{HOME}/;
30    return $filename;
31}
32
33# -----------------------------------------------------------------------------
34# Open Data File in predefined Directories
35sub data_open($){
36    my $filename = expand_filename(shift);
37    my $fh;
38
39    # If it's already an open File
40    if ( ref($filename) =~ m/IO::File/ ) {
41        return $filename;
42    }
43
44    if ( $filename eq "-" ) {
45        $fh = IO::File->new('<&STDIN');
46        $fh or  die("cannot open $filename: $!");
47        return $fh;
48    } else {
49        my $size = (-s $filename)||0;
50        if ( $size < 270 ) {
51            warn "cannot Open $filename ($size) Bytes is too small)\n"
52                if $VERBOSE || $DEBUG;
53            return undef;
54        }
55    }
56
57    printf STDERR "Opening $filename\n" if $DEBUG;
58    if ( $filename =~ m/\.gz$/ ) {
59        $fh = IO::File->new("gzip -dc $filename|")
60            or die("cannot open $filename: $!");
61    } elsif ( $filename =~ m/\.bz2$/ ) {
62        $fh = IO::File->new("bzip2 -dc $filename|")
63            or die("cannot open $filename: $!");
64    } else {
65        $fh = IO::File->new("$filename",'r')
66            or die("cannot open $filename: $!");
67    }
68    return $fh;
69}
70
71# ------------------------------------------------------------------
72# Open Data File in predefined Directories
73sub file_needs_re_generation($$){
74    my $src_filename = expand_filename(shift);
75    my $dst_filename = expand_filename(shift);
76
77    unless ( -e $src_filename ){
78        print STDERR "No Update makes sense, since we lack the source File: $src_filename\n" 
79            if $VERBOSE>1;
80        return 0;
81    }
82
83    # dst file does not exist
84    unless ( $dst_filename && -e $dst_filename ){
85        print STDERR "Update needed. $dst_filename has no size\n" 
86            if $VERBOSE>1;
87        return 1;
88    }
89
90    my ($src_mtime) = (stat($src_filename))[9] || 0;
91    my ($dst_mtime) = (stat($dst_filename))[9] || 0;
92
93    my $update_needed=$src_mtime > $dst_mtime;
94    if ( $VERBOSE>5 ) {
95        print STDERR "Update needed.\n";
96        print STDERR localtime($dst_mtime)."\t$dst_filename is ";
97        print STDERR ($update_needed?"older":"newer")." than \n";
98        print STDERR localtime($src_mtime)."\t$src_filename\n";
99    }
100    return $update_needed;
101}
102
103# ------------------------------------------------------------------
104# Given a filename it checks if we have an unpacked
105# Version which is new enough
106# ARGS: filename.osm.gz|filename.osm
107# RETURNS:
108#   filename.osm if: it exists and is the newest
109#   filename.osm.gz: if no current filename.osm exists
110#   undef:           if we cant find any of the files
111sub newest_unpacked_filename($){
112    my $filename = shift;
113
114    my $filename_unpacked = $filename;
115    $filename_unpacked =~ s/\.(gz|bz2|bz)$//;
116    if ( file_needs_re_generation($filename,$filename_unpacked)) {
117        return $filename if -s $filename;
118    } else {
119        return $filename_unpacked if -s $filename_unpacked;
120    }
121    return undef;
122}
123
124# ------------------------------------------------------------------
125# Create Directory if needed and die if not possible
126sub mkdir_if_needed($){
127    my $dir = expand_filename(shift);
128    -d "$dir" or mkpath "$dir"
129        or die "Cannot create Directory $dir: $!\n";
130}
131
1321;
133
134__END__
135
136=head1 NAME
137
138File.pm
139
140=head1 COPYRIGHT
141
142Copyright 2006, Jörg Ostertag
143
144This program is free software; you can redistribute it and/or
145modify it under the terms of the GNU General Public License
146as published by the Free Software Foundation; either version 2
147of the License, or (at your option) any later version.
148
149This program is distributed in the hope that it will be useful,
150but WITHOUT ANY WARRANTY; without even the implied warranty of
151MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
152GNU General Public License for more details.
153
154You should have received a copy of the GNU General Public License
155along with this program; if not, write to the Free Software
156Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
157
158=head1 AUTHOR
159
160Jörg Ostertag (planet-count-for-openstreetmap@ostertag.name)
161
162=head1 SEE ALSO
163
164http://www.openstreetmap.org/
165
166=cut
Note: See TracBrowser for help on using the repository browser.