source: LMDZ6/branches/Amaury_dev/tools/fcm/lib/FCM/Util/Locator/FS.pm @ 5129

Last change on this file since 5129 was 5129, checked in by abarral, 8 weeks ago

Re-add removed by mistake fcm

File size: 5.3 KB
Line 
1# ------------------------------------------------------------------------------
2# Copyright (C) 2006-2021 British Crown (Met Office) & Contributors.
3#
4# This file is part of FCM, tools for managing and building source code.
5#
6# FCM is free software: you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation, either version 3 of the License, or
9# (at your option) any later version.
10#
11# FCM 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 FCM. If not, see <http://www.gnu.org/licenses/>.
18# ------------------------------------------------------------------------------
19use strict;
20use warnings;
21
22# ------------------------------------------------------------------------------
23package FCM::Util::Locator::FS;
24use base qw{FCM::Class::CODE};
25
26use File::Basename qw{dirname};
27use File::Find qw{};
28use File::Spec;
29
30our %ACTION_OF = (
31    can_work_with     => sub {1},
32    can_work_with_rev => sub {},
33    cat               => \&_cat,
34    dir               => \&_dir,
35    find              => \&_find,
36    origin            => \&_parse,
37    parse             => \&_parse,
38    reader            => \&_reader,
39    read_property     => sub {},
40    test_exists       => \&_test_exists,
41    trunk_at_head     => sub {},
42);
43
44# Creates the class.
45__PACKAGE__->class({util => '&'}, {action_of => \%ACTION_OF});
46
47# Joins @paths to the end of $value.
48sub _cat {
49    my ($attrib_ref, $value, @paths) = @_;
50    _parse(
51        $attrib_ref,
52        File::Spec->catfile(scalar(_parse($attrib_ref, $value)), @paths),
53    );
54}
55
56# Returns the directory name of $value.
57sub _dir {
58    my ($attrib_ref, $value) = @_;
59    dirname(scalar(_parse($attrib_ref, $value)));
60}
61
62# Searches directory tree.
63sub _find {
64    my ($attrib_ref, $value, $callback) = @_;
65    my $found;
66    File::Find::find(
67        sub {
68            $found ||= 1;
69            my $path = $File::Find::name;
70            my $ns = File::Spec->abs2rel($path, $value);
71            if ($ns eq q{.}) {
72                $ns = q{};
73            }
74            else {
75                for my $name (split(q{/}, $ns)) {
76                    if (index($name, q{.}) == 0) {
77                        return; # ignore Unix hidden/system files
78                    }
79                }
80            }
81            my $last_mod_time = (-l $path ? lstat($path) : stat($path))[9];
82            $callback->(
83                $path,
84                {   is_dir        => scalar(-d $path),
85                    last_mod_rev  => undef,
86                    last_mod_time => $last_mod_time,
87                    ns            => $ns,
88                },
89            );
90        },
91        $value,
92    );
93    return $found;
94}
95
96# Returns $value in scalar context, or ($value,undef) in list context.
97sub _parse {
98    my ($attrib_ref, $value) = @_;
99    $value = $attrib_ref->{util}->file_tilde_expand($value);
100    $value = File::Spec->rel2abs($value);
101    my ($vol, $dir_name, $base) = File::Spec->splitpath($value);
102    my @dir_names;
103    my %HANDLER_OF = (
104        q{}   => sub {push(@dir_names, $_[0])},
105        q{.}  => sub {},
106        q{..} => sub {if (@dir_names > 1) {pop(@dir_names)}},
107    );
108    for my $name (File::Spec->splitdir($dir_name)) {
109        my $handler
110            = exists($HANDLER_OF{$name}) ? $HANDLER_OF{$name} : $HANDLER_OF{q{}};
111        $handler->($name);
112    }
113    $value = File::Spec->catpath($vol, File::Spec->catdir(@dir_names), $base);
114    return (wantarray() ? ($value, undef) : $value);
115}
116
117# Returns a reader (file handle) for a given file system value.
118sub _reader {
119    my ($attrib_ref, $value) = @_;
120    $value = _parse($attrib_ref, $value);
121    open(my $handle, '<', $value) || die("$!\n");
122    return $handle;
123}
124
125# Return a true value if the location $value exists.
126sub _test_exists {
127    my ($attrib_ref, $value) = @_;
128    -e $value;
129}
130
131# ------------------------------------------------------------------------------
1321;
133__END__
134
135=head1 NAME
136
137FCM::Util::Locator::FS
138
139=head1 SYNOPSIS
140
141    use FCM::Util::Locator::FS;
142    $util = FCM::Util::Locator::FS->new(\%option);
143    $handle = $util->reader($value);
144
145=head1 DESCRIPTION
146
147Provides utilities to manipulate the values of file system locators.
148
149=head1 METHODS
150
151=over 4
152
153=item $util->can_work_with($value)
154
155Dummy. Always returns true.
156
157=item $util->can_work_with_rev($revision)
158
159Dummy. Always returns false.
160
161=item $util->cat($value,@paths)
162
163Joins @paths to the end of $value.
164
165=item $util->dir($value)
166
167Returns the parent directory of $value.
168
169=item $util->find($value,$callback)
170
171Searches directory tree of $value.
172
173=item $util->origin($value)
174
175Alias of $util->parse($value).
176
177=item $util->parse($value)
178
179In scalar context, returns $value. In list context, returns ($value,undef).
180
181=item $util->reader($value)
182
183Returns a file handle for $value, if it is a readable regular file.
184
185=item $util->read_property($value,$property_name)
186
187Dummy. Always returns undef.
188
189=item $util->test_exists($value)
190
191Return a true value if the location $value exists.
192
193=item $util->trunk_at_head($value)
194
195Dummy. Always returns undef.
196
197=back
198
199=head1 COPYRIGHT
200
201Copyright (C) 2006-2021 British Crown (Met Office) & Contributors.
202
203=cut
Note: See TracBrowser for help on using the repository browser.