Tool/File.pm

filter: Statementstmt 50.7% 64 / 126 Branchbran 17.8% 10 / 56 Conditioncond 16.1% 5 / 31 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 56.2% 9 / 16 Podpod 100% 3 / 3 totaltotal 36.6% 91 / 248 CC 40 SCAR 45.9
CC
40
Cov
37%
get_line
182.0
_get_file_handle
160.1
watch
27.0
CRAP 98.4
« Tool.pm ↑ Summary Tool/Plugin/GroupLines.pm »
1 package Tail::Tool::File;
2  
3 # Created on: 2010-10-25 11:11:38
4 # Create by: Ivan Wills
5 # $Id$
6 # $Revision$, $HeadURL$, $Date$
7 # $Revision$, $Source$, $Date$
8  
9 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use Scalar::Util qw/openhandle/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use English qw/ -no_match_vars /;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use AnyEvent;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 3 our $VERSION = version->new('0.4.8');
18  
19 3 has name => (
20 is => 'rw',
21 isa => 'Str',
22 documentation => 'The name of a file to be watched',
23 );
24 3 has remote => (
25 is => 'rw',
26 isa => 'Bool',
27 default => 0,
28 init_arg => undef,
29 documentation => 'Flags that the file is located on a remote server',
30 );
31 3 has cmd => (
32 is => 'rw',
33 isa => 'Str',
34 documentation => '',
35 );
36 3 has pid => (
37 is => 'rw',
38 isa => 'Str',
39 documentation => '',
40 );
41 3 has handle => (
42 is => 'rw',
43 isa => 'FileHandle',
44 clearer => 'clear_handle',
45 documentation => 'The opened filehandle of name',
46 );
47 3 has size => (
48 is => 'rw',
49 isa => 'Int',
50 init_arg => undef,
51 default => 0,
52 documentation => 'The size of file when last read',
53 );
54 3 has pause => (
55 is => 'rw',
56 isa => 'Bool',
57 documentation => 'Flags not to display any lines from the file',
58 );
59 3 has auto_unpause => (
60 is => 'rw',
61 isa => 'Bool',
62 default => 0,
63 init_arg => undef,
64 documentation => 'If a file was missing moved or deleted this flags that tailing should be restarted when the file reappears',
65 );
66 3 has no_inotify => (
67 is => 'ro',
68 isa => 'Bool',
69 documentation => 'Flags not to use the INotify the file (can be useful when a file is on a network file system like sshfs)',
70 );
71 3 has watcher => (
72 is => 'rw',
73 init_arg => undef,
74 clearer => 'clear_watcher',
75 documentation => 'This is the event watcher ojbect handle',
76 );
77 3 has runner => (
78 is => 'rw',
79 isa => 'CodeRef',
80 documentation => 'This is the subroutine reference that should be run with each file change',
81 );
82 3 has started => (
83 is => 'rw',
84 isa => 'Bool',
85 default => 0,
86 init_arg => undef,
87 documentation => 'Flags that tailing has started and not to limit the number of lines any more',
88 );
89 3 has stat_time => (
90 is => 'rw',
91 isa => 'Int',
92 default => time,
93 init_arg => undef,
94 documentation => 'The last time a file was stat()ed',
95 );
96 3 has stat_period => (
97 is => 'rw',
98 isa => 'Int',
99 default => 1,
100 documentation => 'The time period between checks if a file has been moved or deleted',
101 );
102 3 has tailer => (
103 is => 'rw',
104 isa => 'Tail::Tool',
105 documentation => 'The object that this file belongs to',
106 );
107 3 has restart => (
108 is => 'ro',
109 default => 0,
110 );
111  
112 3 my $inotify;
113 my $watcher;
114 1 ▶sub watch {
Subroutine watch: called CC=11 SCAR=33.0
Pod documented
115 1 my ($self, $lines) = @_;
116  
117 1 ▶ return 0 if $self->pause;
Branch: 1 branch
BranchTrueFalseTotal
if $self->pause 0 1 1
118 1 ▶ return $self->watcher if $self->watcher;
Branch: 1 branch
BranchTrueFalseTotal
if $self->watcher 0 1 1
119  
120 1 $self->_get_file_handle();
121  
122 1 ▶ if ( !defined $inotify ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $inotify) 1 0 1
123 1 eval { require Linux::Inotify2 };
124 1 ▶ if ($EVAL_ERROR) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Tail::Tool::File::EVAL_ERROR) { } 0 1 1
125 0 $inotify = 0;
126 }
127 else {
128 1 $inotify = Linux::Inotify2->new;
129 }
130 }
131  
132 1 my $w;
133 1 ▶ if ( $self->name ne '-' && !$self->remote && $inotify && !$self->no_inotify ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->name ne '-' and not $self->remote and $inotify and not $self->no_inotify) { } 1 0 1
elsif ($self->name eq '-') { } 0 0 0
Condition33%3/9
$self->name ne "-" and not $self->remote and $inotify and not $self->no_inotify
!ll&&!rl&&r
001
$self->name ne "-" and not $self->remote and $inotify
!ll&&!rl&&r
001
$self->name ne "-" and not $self->remote
!ll&&!rl&&r
001
MC/DC0%0/4
$self->name ne "-" and not $self->remote and $inotify and not $self->no_inotify
$self->name ne "-" not $self->remote $inotify not $self->no_inotify
Truth table20%1/5
A and not(B) and C and not(D)
A= $self->name ne "-"B= not $self->remoteC= $inotifyD= not $self->no_inotify
ABCDresult
0XXX0
10XX0
110X0
11100
11111
134 # IN_MODIFY | IN_ALL_EVENTS & ~IN_ACCESS
135 0 ▶ $w = $inotify->watch( $self->name, Linux::Inotify2::IN_ALL_EVENTS(), sub { $self->run } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
136 1 ▶ if ( !$watcher ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($watcher) 1 0 1
137 0 ▶ $watcher = AE::io $inotify->fileno, 0, sub { $inotify->poll };
Subroutine __ANON__: not called CC=1 SCAR=1.2
138 }
139 }
140 elsif ( $self->name eq '-' ) {
141 0 $self->started(1);
142 0 ▶ $w = AE::io \*STDIN, 0, sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
143 0 ▶ if ( !defined fileno \*STDIN ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined fileno \*STDIN) 0 0 0
144 0 close STDIN;
145 }
146 $self->run
147 0 };
148 # TODO work out how to end if STDIN closed
149 }
150 else {
151 0 ▶ $w = AE::timer 0, 1, sub { $self->run };
Subroutine __ANON__: not called CC=1 SCAR=6.9
152 }
153  
154 1 $self->watcher($w);
155  
156 1 return $self->watcher;
157 }
158  
159 0 ▶sub run {
Subroutine run: not called CC=1 SCAR=6.9
Pod documented
160 0 my ($self, $first) = @_;
161 0 $self->runner->($self, $first);
162 }
163  
164 0 ▶sub get_line {
Subroutine get_line: not called CC=13 SCAR=52.0
Pod documented
165 0 my ($self) = @_;
166 0 my $fh = $self->_get_file_handle;
167  
168 0 ▶ return if $self->pause;
Branch: 1 branch
BranchTrueFalseTotal
if $self->pause 0 0 0
169  
170 0 ▶ if ( !$self->remote ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->remote) 0 0 0
171 0 ▶ my $size = -s $self->name || 0;
Condition0%0/2
-s $self->name || 0
l!l
00
MC/DC0%0/1
-s $self->name || 0
-s $self->name
Truth table0%0/2
A
A= -s $self->name
Aresult
11
00
172 0 ▶ if ( $size < $self->size ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($size < $self->size) { } 0 0 0
elsif ($self->restart) { } 0 0 0
173 0 warn $self->name . " was truncated!\n";
174 0 close $fh;
175 0 $self->clear_handle;
176 0 $fh = $self->_get_file_handle;
177 }
178 elsif ($self->restart) {
179 # reset file handle
180 0 seek $fh, 0, 1;
181 }
182 else {
183 0 $self->clear_watcher;
184 }
185 0 ▶ $self->size($size || 0);
Condition0%0/2
$size || 0
l!l
00
MC/DC0%0/1
$size || 0
$size
Truth table0%0/2
A
A= $size
Aresult
11
00
186 }
187  
188 0 my @lines = <$fh>;
189  
190 # re-check the stat time of the file to make sure that the file has not been rotated
191 0 ▶ if ( !$self->remote && !@lines && time > $self->stat_time + $self->stat_period * 60 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->remote || @lines and time > $self->stat_time + $self->stat_period * 60) 0 0 0
Condition0%0/6
not $self->remote || @lines and time > $self->stat_time + $self->stat_period * 60
!ll&&!rl&&r
000
$self->remote || @lines
l!l&&r!l&&!r
000
MC/DC0%0/3
not $self->remote || @lines and time > $self->stat_time + $self->stat_period * 60
$self->remote @lines time > $self->stat_time + $self->stat_period * 60
Truth table0%0/4
not(A || B) and C
A= $self->remoteB= @linesC= time > $self->stat_time + $self->stat_period * 60
ABCresult
1XX0
01X0
0000
0011
192 0 $self->stat_time(time);
193 # TODO why is this being run if the file has finished? Should not be run for STDIN reading
194 0 my @stat_file = stat $self->name;
195 0 my @stat_handle = stat $fh;
196 # check if the file handle's modified time is not the same as files'
197 0 ▶ if ( !defined $stat_handle[1] || !defined $stat_file[1] || $stat_handle[1] != $stat_file[1] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $stat_handle[1] && defined $stat_file[1] or $stat_handle[1] != $stat_file[1]) 0 0 0
Condition0%0/6
not defined $stat_handle[1] && defined $stat_file[1] or $stat_handle[1] != $stat_file[1]
l!l&&r!l&&!r
000
defined $stat_handle[1] && defined $stat_file[1]
!ll&&!rl&&r
000
MC/DC0%0/3
not defined $stat_handle[1] && defined $stat_file[1] or $stat_handle[1] != $stat_file[1]
defined $stat_handle[1] defined $stat_file[1] $stat_handle[1] != $stat_file[1]
Truth table0%0/4
not(A && B) or C
A= defined $stat_handle[1]B= defined $stat_file[1]C= $stat_handle[1] != $stat_file[1]
ABCresult
0XX1
10X1
1111
1100
198 # close and reopen file incase the file has been rotated
199 0 close $fh;
200 0 $self->_get_file_handle();
201 }
202 }
203 0 return @lines;
204 }
205  
206 1 ▶sub _get_file_handle {
Subroutine _get_file_handle: called CC=17 SCAR=50.8
207 1 my ($self) = @_;
208  
209 1 my $fh = $self->handle;
210 1 ▶ if ( $self->name eq '-' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->name eq "-") 0 1 1
211 0 ▶ if ( !$fh ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $fh) { } 0 0 0
elsif (not &openhandle($fh)) { } 0 0 0
212 0 $self->handle(\*STDIN);
213 }
214 elsif ( !openhandle($fh) ) {
215 0 $self->clear_watcher;
216 }
217 0 return $self->handle;
218 }
219  
220 1 ▶ if ( $self->remote || $self->name =~ m{^s(sh|cp)://}xms ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->remote or $self->name =~ m[^s(sh|cp)://]msx) { } 0 1 1
elsif (not $fh or tell $fh == -1) { } 1 0 1
Condition33%1/3
$self->remote or $self->name =~ m[^s(sh|cp)://]msx
l!l&&r!l&&!r
001
MC/DC0%0/2
$self->remote or $self->name =~ m[^s(sh|cp)://]msx
$self->remote $self->name =~ m[^s(sh|cp)://]msx
Truth table33%1/3
A or B
A= $self->remoteB= $self->name =~ m[^s(sh|cp)://]msx
ABresult
1X1
011
000
221 0 $self->remote(1);
222 0 ▶ return if $self->pause;
Branch: 1 branch
BranchTrueFalseTotal
if $self->pause 0 0 0
223  
224 0 my $host_re = qr/( [\w.-]+ )/xms;
225 0 my $user_re = qr/([^@]+) [@]/xms;
226 0 my $port_re = qr/[:] (\d*)/xms;
227 0 my $file_re = qr/(.*)/xms;
228 0 my $ssh_re = qr{^ssh://(?: $user_re )? $host_re (?: $port_re )? / $file_re$}xms;
229 0 my $scp_re = qr{^scp://(?: $user_re )? $host_re $port_re $file_re$}xms;
230 0 ▶ my $re = $self->name =~ /^ssh/ ? $ssh_re : $scp_re;
Branch: 1 branch
BranchTrueFalseTotal
$self->name =~ /^ssh/ ? : 0 0 0
231  
232 0 my ($user, $host, $port, $file) = $self->name =~ m{$re}xms;
233  
234 0 ▶ if ( !$fh ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh) 0 0 0
235 0 ▶ my $cmd = sprintf "ssh %s$host %s 'tail -f -n %d %s'",
Branch: 3 branches
BranchTrueFalseTotal
$user ? : 0 0 0
$port ? : 0 0 0
$self->tailer ? : 0 0 0
236 ( $user ? "$user\@" : '' ),
237 ( $port ? "-P $port" : '' ),
238 ( $self->tailer ? $self->tailer->lines : 10 ),
239 _shell_quote($file);
240  
241 0 ▶ if ( my $pid = open $fh, '-|', $cmd ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pid = open($fh, '-|', $cmd)) { } 0 0 0
242 0 $fh->blocking(0);
243 0 $self->pid($pid);
244 0 $self->handle($fh);
245 }
246 else {
247 0 $self->pause(1);
248 0 warn "Could not tail remote file (" . $self->name . "): $!";
249 }
250 }
251 }
252 ▶ elsif ( !$fh || tell $fh == -1 ) {
Condition33%1/3
not $fh or tell $fh == -1
l!l&&r!l&&!r
100
MC/DC0%0/2
not $fh or tell $fh == -1
not $fh tell $fh == -1
Truth table33%1/3
not(A) or B
A= not $fhB= tell $fh == -1
ABresult
1X1
011
000
253 1 ▶ if ( open $fh, '<', $self->name ) {
Branch: 1 branch
BranchTrueFalseTotal
if (open $fh, '<', $self->name) { } 1 0 1
254 1 $self->handle($fh);
255 1 $self->size(-s $self->name);
256 }
257 else {
258 0 ▶ warn "Could not open '".$self->name."': $!\n" if !$self->auto_unpause;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->auto_unpause 0 0 0
259 0 $self->pause(1);
260 0 $self->auto_unpause(1);
261 }
262 }
263  
264 1 return $fh;
265 }
266  
267 0 ▶sub _shell_quote {
Subroutine _shell_quote: not called CC=1 SCAR=6.9
268 0 my ($file) = @_;
269 0 $file =~ s{ ( [^\w\-./?*] ) }{\\$1}gxms;
270  
271 0 return $file;
272 }
273  
274 3 1;
275  
276 __END__
« Tool.pm ↑ Summary Tool/Plugin/GroupLines.pm »