Cascade/FileReader.pm

filter: Statementstmt 89.0% 65 / 73 Branchbran 70.5% 24 / 34 Conditioncond 81.8% 9 / 11 MC/DCmcdc 42.8% 3 / 7 Subroutinesub 100% 11 / 11 Podpod 100% 4 / 4 totaltotal 82.8% 116 / 140 CC 22 SCAR 22.1
CC
22
Cov
83%
close
27.5
search
8.0
_prepare_command
6.0
CRAP 9.1
↑ Summary Cascade.pm »
1  
2 package Launcher::Cascade::FileReader;
3  
4 =head1 NAME
5  
6 Launcher::Cascade::FileReader - a class to read a file or the output of a command, locally or through ssh.
7  
8 =head1 SYNOPSIS
9  
10 use Launcher::Cascade::FileReader;
11  
12 my $f = new Launcher::Cascade::FileReader
13 -path => q{date '+%H:%M:%S' |},
14 -host => q{host.domain},
15 ;
16 my $fh = $f->open();
17 while ( <$fh> ) {
18 print; # what time is it over there?
19 }
20 close $fh;
21  
22 =head1 DESCRIPTION
23  
24 The purpose of this class is to provide a file handle that gives access to a
25 file or the output of a command. If a host() is provided, the file or the
26 command are fetched through ssh, otherwise, run locally. The class takes care
27 of escaping quote characters as appropriate.
28  
29 =cut
30  
31 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 6 ▶use base qw( Launcher::Cascade );
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 6 ▶use Launcher::Cascade::ListOfStrings::Context;
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 =head2 Attributes
39  
40 =over 4
41  
42 =item B<path>
43  
44 The path to the file to open. If the last character is a vertical bar (or
45 "pipe", ASCII 0x7c), path() will be considered a command and run. The standard
46 ouput will be available in the filehandle returned by method open().
47  
48 =item B<host>
49  
50 If set, the path() will be considered remote, i.e., the command actually run by
51 method open() will be a C<ssh host ...>. Either the content of the remote file
52 or the standard output of the remote command will be made available in the
53 filehandle returned by open().
54  
55 =item B<user>
56  
57 The remote user to login as. When omitted, ssh(1) will login with the same user
58 as the local user.
59  
60 =item B<filehandle>
61  
62 The filehandle as returned by method open().
63  
64 =item B<context>
65  
66 An array reference containing the line that matched the pattern given to method
67 search(), plus lines of context if either of context_after() or
68 context_before() are not null.
69  
70 =item B<context_after>
71  
72 =item B<context_before>
73  
74 Determines the number of lines to include in context() after, respectively
75 before, a pattern has been matched. The number of lines in context() should be
76 context_before() + 1 + context_after(), unless the end of the file was reached
77 too soon to provide enough context after the match.
78  
79 Both attributes default to 0, so the default context contains only one line,
80 the one that matched the pattern.
81  
82 =back
83  
84 =cut
85  
86 6 Launcher::Cascade::make_accessors qw( path host user filehandle );
87 6 Launcher::Cascade::make_accessors_with_defaults
88 context_before => 0,
89 context_after => 0,
90 ;
91  
92 5 ▶sub _context_arguments {
Subroutine _context_arguments: called CC=1 SCAR=0.0
93  
94 5 my $self = shift;
95  
96 5 my $header = '-' x 0;
97 #$header .= ' Excerpt from ' . $self->path();
98 #$header .= ' on host ' . $self->host() if $self->host();
99
100 5 return (-string_before => $header);
101 }
102 5 ▶sub context {
Subroutine context: called CC=4 SCAR=14.2
Pod documented
103  
104 5 my $self = shift;
105  
106 5 ▶ my $old = $self->{_context} ||= new Launcher::Cascade::ListOfStrings::Context
Condition66%2/3
$self->{"_context"} ||= "Launcher::Cascade::ListOfStrings::Context"->new("-list", [], $self->_context_arguments)
l!l&&r!l&&!r
320
MC/DC0%0/2
$self->{"_context"} ||= "Launcher::Cascade::ListOfStrings::Context"->new("-list", [], $self->_context_arguments)
$self->{"_context"} "Launcher::Cascade::ListOfStrings::Context"->new("-list", [], $self->_context_arguments)
Truth table66%2/3
A ||= B
A= $self->{"_context"}B= "Launcher::Cascade::ListOfStrings::Context"->new("-list", [], $self->_context_arguments)
ABresult
1X1
011
000
107 -list => [],
108 $self->_context_arguments(),
109 ;
110 5 ▶ if ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 3 2 5
111 3 ▶ if ( UNIVERSAL::isa($_[0], 'Launcher::Cascade::ListOfStrings::Context') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($_[0], 'Launcher::Cascade::ListOfStrings::Context')) { } 0 3 3
112 0 $self->{_context} = $_[0];
113 }
114 else {
115 3 $self->{_context} = new Launcher::Cascade::ListOfStrings::Context
116 -list => $_[0],
117 $self->_context_arguments(),
118 ;
119 }
120 }
121 5 return $old;
122 }
123  
124 =head2 Methods
125  
126 =over 4
127  
128 =item B<open>
129  
130 Opens the file or command specified by attribute path(), possible over ssh on
131 remote host(), and returns a filehandle make its content (for a file) or
132 standard output (for a command) available for reading.
133  
134 =cut
135  
136 6 ▶sub open {
Subroutine open: called CC=2 SCAR=6.9
Pod documented
137  
138 6 my $self = shift;
139 6 my $filename = $self->_prepare_command();
140  
141 6 ▶ open my $fh, $filename or die "Cannot read $filename: $!";
Branch: 1 branch
BranchTrueFalseTotal
CORE::open my $fh, $filename or die "Cannot read ${filename}: $!" 5 1 6
142 5 $self->filehandle($fh);
143 5 return $fh;
144 }
145  
146 =item B<close>
147  
148 Closes the filehandle.
149  
150 =cut
151  
152 4 ▶sub close {
Subroutine close: called CC=6 SCAR=33.1
Pod documented
153  
154 4 my $self = shift;
155  
156 4 ▶ if ( ! CORE::close $self->filehandle(undef) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not CORE::close $self->filehandle(undef)) 0 4 4
157 0 my $path = $self->path();
158 0 my $cmd = $path;
159 0 ▶ $cmd = '' unless $cmd =~ s/\s*\|$//;
Branch: 1 branch
BranchTrueFalseTotal
unless $cmd =~ s/\s*\|$// 0 0 0
160  
161 0 ▶ my $what = $self->host() ? 'ssh to host ' . $self->host()
Branch: 2 branches
BranchTrueFalseTotal
$self->host ? : 0 0 0
$cmd ? : 0 0 0
162 : $cmd ? "external command ($cmd)"
163 : "closing of file $path"
164 ;
165 0 ▶ if ( $! ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($!) { } 0 0 0
166 0 die "$what failed: $!";
167 }
168 else {
169 0 die "$what returned status $?";
170 }
171 }
172 }
173  
174 2 ▶sub _remote_cat {
Subroutine _remote_cat: called CC=1 SCAR=0.0
175  
176 2 my $self = shift;
177 2 my $path = shift;
178 2 return "cat $path";
179 }
180  
181 12 ▶sub _prepare_command {
Subroutine _prepare_command: called CC=6 SCAR=17.9
182  
183 12 my $self = shift;
184  
185 12 my $path = $self->path();
186  
187 12 ▶ if ( $self->host() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->host) { } 4 8 12
188 4 $path =~ s/'/'\\''/g;
189 4 ▶ if ( $path !~ s/\s*\|$// ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($path =~ s/\s*\|$//) 2 2 4
190 2 $path = $self->_remote_cat($path);
191 }
192 4 ▶ my $user = $self->user() || '';
Condition100%2/2
$self->user || ''
l!l
22
MC/DC100%1/1
$self->user || ''
$self->user
Truth table100%2/2
A
A= $self->user
Aresult
11
00
193 4 my $host = $self->host();
194 4 ▶ $user .= '@' if $user;
Branch: 1 branch
BranchTrueFalseTotal
if $user 2 2 4
195 4 return "ssh $user$host '$path' |";
196 }
197 else {
198 8 ▶ $path = "< $path" unless $path =~ /\|$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $path =~ /\|$/ 7 1 8
199 8 return $path;
200 }
201 }
202  
203 =item B<search> I<PATTERN>, I<PATTERN>, ...
204  
205 Search the filehandle for I<PATTERN>s and returns the index of the one that
206 matched (starting with 0), or C<-1> if the end of file was reached before any
207 pattern could be matched.
208  
209 I<PATTERN>s should be regular expressions, possibly pre-compiled with the
210 C<qr//> operator.
211  
212 After a search(), the context() attribute contains an arrayref containing the
213 line that matched, plus a number of context lines before and after the match,
214 as defined by the context_after() and context_before() attributes.
215  
216 The filehandle is closed after the search().
217  
218 =cut
219  
220 4 ▶sub search {
Subroutine search: called CC=8 SCAR=20.8
Pod documented
221  
222 4 my $self = shift;
223  
224 4 my @pattern = @_;
225  
226 # a buffer to contain the context before the match
227 4 my @fifo = ();
228 4 my $fifo_size = $self->context_before() + 1;
229  
230 4 my $result = -1;
231  
232 # Let's open the file if not yet done
233 4 ▶ my $fh = $self->filehandle() || $self->open();
Condition66%2/3
$self->filehandle || $self->open
l!l&&r!l&&!r
031
MC/DC0%0/2
$self->filehandle || $self->open
$self->filehandle $self->open
Truth table33%1/3
A || B
A= $self->filehandleB= $self->open
ABresult
1X1
011
000
234 3 ▶ LINE: while ( <$fh> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 53 1 54
235 # Store the line in the context buffer
236 53 push @fifo, $_;
237 53 ▶ shift @fifo if @fifo > $fifo_size;
Branch: 1 branch
BranchTrueFalseTotal
if @fifo > $fifo_size 43 10 53
238  
239 # try all patterns
240 53 ▶ for ( my $i = 0 ; $i < @pattern ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @pattern) 58 51 109
241 58 ▶ if ( /$pattern[$i]/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/$pattern[$i]/) 2 56 58
242 2 $result = $i;
243 2 last LINE;
244 }
245 }
246 }
247  
248 # Now fetch the context after the match
249 3 $fifo_size += $self->context_after();
250 3 ▶ while ( defined($_ = <$fh>) && @fifo < $fifo_size ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh) and @fifo < $fifo_size) 6 3 9
Condition100%3/3
defined($_ = readline $fh) and @fifo < $fifo_size
!ll&&!rl&&r
126
MC/DC100%2/2
defined($_ = readline $fh) and @fifo < $fifo_size
defined($_ = readline $fh) @fifo < $fifo_size
Truth table100%3/3
A and B
A= defined($_ = readline $fh)B= @fifo < $fifo_size
ABresult
0X0
100
111
251 6 push @fifo, $_;
252 }
253  
254 3 $self->context(\@fifo);
255 3 $self->close();
256 3 return $result;
257 }
258  
259 =back
260  
261 =head1 BUGS AND CAVEATS
262  
263 =over 4
264  
265 =item *
266  
267 ssh(1) must be in one of directories listed in the C<PATH> environment variable.
268  
269 =item *
270  
271 there is nothing provided for non-interactive logging. The DSA or RSA key pairs
272 should be properly generated and ssh configured to avoid interactive login.
273  
274 =back
275  
276 =head1 SEE ALSO
277  
278 L<Launcher::Cascade>.
279  
280 =head1 AUTHOR
281  
282 Cédric Bouvier C<< <cbouvi@cpan.org> >>
283  
284 =head1 COPYRIGHT & LICENSE
285  
286 Copyright (C) 2006 Cédric Bouvier, All Rights Reserved.
287  
288 This program is free software; you can redistribute it and/or modify it under
289 the same terms as Perl itself.
290  
291 =cut
292  
293 6 1; # end of Launcher::Cascade::FileReader
↑ Summary Cascade.pm »