Simple.pm

filter: Statementstmt 90.9% 111 / 122 Branchbran 69.4% 25 / 36 Conditioncond 54.5% 6 / 11 MC/DCmcdc 28.5% 2 / 7 Subroutinesub 93.1% 27 / 29 Podpod 0.0% 0 / 6 totaltotal 81.0% 171 / 211 CC 26 SCAR 16.3
CC
26
Cov
84%
DEMOLISH
20.0
handle_event
10.7
_watch_directory
5.1
CRAP 5.1
↑ Summary EventReceiver/Debug.pm »
1 package AnyEvent::Inotify::Simple;
2 3 $AnyEvent::Inotify::Simple::VERSION = '0.04';
3 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 # ABSTRACT: monitor a directory tree in a non-blocking way
6  
7 3 ▶use MooseX::FileAttribute;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use MooseX::Types::Moose qw(HashRef CodeRef ArrayRef Int);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use MooseX::Types -declare => ['Receiver'];
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3 ▶use AnyEvent::Inotify::EventReceiver;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use AnyEvent::Inotify::EventReceiver::Callback;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 3 role_type Receiver, { role => 'AnyEvent::Inotify::EventReceiver' };
15  
16 5 ▶coerce Receiver, from CodeRef, via {
Subroutine __ANON__: called CC=1 SCAR=0.0
17 5 AnyEvent::Inotify::EventReceiver::Callback->new(
18 callback => $_,
19 ),
20 3 };
21  
22 3 ▶use AnyEvent;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 3 ▶use Linux::Inotify2;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 3 ▶use File::Next;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 3 ▶use namespace::clean -except => ['meta'];
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 3 has_directory 'directory' => ( must_exist => 1, required => 1);
29  
30 has 'filter' => (
31 traits => ['Code'],
32 is => 'ro',
33 isa => CodeRef,
34 handles => { is_filtered => 'execute' },
35 3 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
36 21 ▶ sub { return 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
37 3 },
38 3 );
39  
40 3 has 'event_receiver' => (
41 is => 'ro',
42 isa => Receiver,
43 handles => 'AnyEvent::Inotify::EventReceiver',
44 required => 1,
45 coerce => 1,
46 );
47  
48 3 has 'inotify' => (
49 init_arg => undef,
50 is => 'ro',
51 isa => 'Linux::Inotify2',
52 handles => [qw/poll fileno watch/],
53 lazy_build => 1,
54 );
55  
56 5 ▶sub _build_inotify {
Subroutine _build_inotify: called CC=2 SCAR=7.0
57 5 my $self = shift;
58  
59 5 ▶ my $inotify = Linux::Inotify2->new or confess "Inotify initialization failed: $!";
Branch: 1 branch
BranchTrueFalseTotal
my $inotify = "Linux::Inotify2"->new or confess("Inotify initialization failed: $!") 5 0 5
60 # Ignore overflows, rather than broadcasting to every watcher
61 5 ▶ $inotify->on_overflow(sub {});
Subroutine __ANON__: not called CC=1 SCAR=0.0
62 5 return $inotify;
63 }
64  
65 has 'wanted_events' => (
66 is => 'ro',
67 isa => ArrayRef,
68 2 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
69 2 [ qw(access modify attribute_change close_write close_nowrite open create delete move) ]
70 },
71 3 );
72  
73 3 has '_wanted_events_mask' => (
74 is => 'ro',
75 isa => Int,
76 lazy_build => 1,
77 );
78  
79 3 my %event_to_mask = (
80 access => IN_ACCESS,
81 modify => IN_MODIFY,
82 attribute_change => IN_ATTRIB,
83 close_write => IN_CLOSE_WRITE,
84 close_nowrite => IN_CLOSE_NOWRITE,
85 open => IN_OPEN,
86 create => IN_CREATE,
87 delete => IN_DELETE,
88 move => IN_MOVED_FROM | IN_MOVED_TO,
89 );
90  
91 5 ▶sub _build__wanted_events_mask {
Subroutine _build__wanted_events_mask: called CC=3 SCAR=11.0
92 5 my $mask = 0;
93 5 for (@{$_[0]->wanted_events}) {
94 23 ▶ my $event_mask = $event_to_mask{$_}
Condition50%1/2
$event_to_mask{$_} || die("Unknown wanted event: $_")
l!l
230
MC/DC0%0/1
$event_to_mask{$_} || die("Unknown wanted event: $_")
$event_to_mask{$_}
Truth table50%1/2
A
A= $event_to_mask{$_}
Aresult
11
00
95 || die "Unknown wanted event: $_";
96 23 $mask |= $event_mask;
97 }
98 5 return $mask;
99 }
100  
101 3 has 'io_watcher' => (
102 init_arg => undef,
103 is => 'ro',
104 builder => '_build_io_watcher',
105 required => 1,
106 );
107  
108 5 ▶sub _build_io_watcher {
Subroutine _build_io_watcher: called CC=1 SCAR=0.0
109 5 my $self = shift;
110  
111 return AnyEvent->io(
112 fh => $self->fileno,
113 poll => 'r',
114 1 ▶ cb => sub { $self->poll },
Subroutine __ANON__: called CC=1 SCAR=0.0
115 5 );
116 }
117  
118 has 'cookie_jar' => (
119 init_arg => undef,
120 is => 'ro',
121 isa => HashRef,
122 required => 1,
123 5 ▶ default => sub { +{} },
Subroutine __ANON__: called CC=1 SCAR=0.0
124 3 );
125  
126 10 ▶sub _watch_directory {
Subroutine _watch_directory: called CC=5 SCAR=16.3
127 10 my ($self, $dir) = @_;
128  
129 10 my $next = File::Next::dirs({
130 follow_symlinks => 0,
131 }, $dir);
132  
133 10 ▶ while ( my $entry = $next->() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $entry = &$next()) 12 10 22
134 12 ▶ last unless defined $entry;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $entry 0 12 12
135 12 ▶ next if $self->is_filtered($entry);
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_filtered($entry) 1 11 12
136  
137 11 ▶ if( -d $entry ){
Branch: 1 branch
BranchTrueFalseTotal
if (-d $entry) { } 11 0 11
138 11 $entry = Path::Class::dir($entry);
139 }
140 else {
141 0 $entry = Path::Class::file($entry);
142 }
143  
144 $self->watch(
145 $entry->stringify,
146 $self->_wanted_events_mask,
147 33 ▶ sub { $self->handle_event($entry, $_[0]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
148 11 );
149 }
150 }
151  
152 5 ▶sub BUILD {
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
153 5 my $self = shift;
154  
155 5 $self->_watch_directory($self->directory->resolve->absolute);
156 }
157  
158 3 my %events = (
159 IN_ACCESS => 'handle_access',
160 IN_MODIFY => 'handle_modify',
161 IN_ATTRIB => 'handle_attribute_change',
162 IN_CLOSE_WRITE => 'handle_close_write',
163 IN_CLOSE_NOWRITE => 'handle_close_nowrite',
164 IN_OPEN => 'handle_open',
165 IN_CREATE => 'handle_create',
166 IN_DELETE => 'handle_delete',
167 );
168  
169 33 ▶sub handle_event {
Subroutine handle_event: called CC=10 SCAR=23.7
Pod undocumented
170 33 my ($self, $file, $event) = @_;
171  
172 33 ▶ my $wrapper = $event->IN_ISDIR ? 'subdir' : 'file';
Branch: 1 branch
BranchTrueFalseTotal
$event->IN_ISDIR ? : 16 17 33
173 33 my $event_file = $file->$wrapper($event->name);
174  
175 33 ▶ if( $event->IN_DELETE_SELF || $event->IN_MOVE_SELF ){
Branch: 1 branch
BranchTrueFalseTotal
if ($event->IN_DELETE_SELF or $event->IN_MOVE_SELF) 0 33 33
Condition33%1/3
$event->IN_DELETE_SELF or $event->IN_MOVE_SELF
l!l&&r!l&&!r
0033
MC/DC0%0/2
$event->IN_DELETE_SELF or $event->IN_MOVE_SELF
$event->IN_DELETE_SELF $event->IN_MOVE_SELF
Truth table33%1/3
A or B
A= $event->IN_DELETE_SELFB= $event->IN_MOVE_SELF
ABresult
1X1
011
000
176 #warn "canceling $file";
177 #$event->w->cancel;
178 0 return;
179 }
180  
181 33 ▶ if($self->is_filtered($event_file)){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_filtered($event_file)) 8 25 33
182 # we get this when a directory watcher notices something
183 # about a file that should be ignored
184 8 return;
185 }
186  
187 25 my $relative = $event_file->relative($self->directory);
188 25 my $handled = 0;
189  
190 25 for my $type (keys %events){
191 200 my $method = $events{$type};
192 200 ▶ if( $event->$type ){
Branch: 1 branch
BranchTrueFalseTotal
if ($event->$type) 21 179 200
193 21 $self->$method($relative);
194 21 $handled = 1;
195 }
196 }
197  
198 25 ▶ if( $event->IN_MOVED_FROM ){
Branch: 1 branch
BranchTrueFalseTotal
if ($event->IN_MOVED_FROM) 2 23 25
199 2 $self->handle_move_from($relative, $event->cookie);
200 2 $handled = 1;
201 }
202  
203 25 ▶ if( $event->IN_MOVED_TO ){
Branch: 1 branch
BranchTrueFalseTotal
if ($event->IN_MOVED_TO) 2 23 25
204 2 $self->handle_move_to($relative, $event->cookie);
205 2 $handled = 1;
206 }
207  
208 25 ▶ if (!$handled){
Branch: 1 branch
BranchTrueFalseTotal
if (not $handled) 0 25 25
209 0 require Data::Dumper;
210 0 local $Data::Dumper::Maxdepth = 2;
211 0 Carp::cluck "BUGBUG: Unhandled event: ".
212 Data::Dumper->Dump($event);
213 }
214  
215 }
216  
217 14 ▶sub rel2abs {
Subroutine rel2abs: called CC=2 SCAR=7.1
Pod undocumented
218 14 my ($self, $file) = @_;
219  
220 14 ▶ return $file if $file->is_absolute;
Branch: 1 branch
BranchTrueFalseTotal
if $file->is_absolute 0 14 14
221 14 return $file->absolute($self->directory)->resolve->absolute;
222 }
223  
224 2 ▶sub handle_move_from {
Subroutine handle_move_from: called CC=1 SCAR=0.0
Pod undocumented
225 2 my ($self, $file, $cookie) = @_;
226  
227 2 $self->cookie_jar->{from}{$cookie} = $file;
228 }
229  
230 2 ▶sub handle_move_to {
Subroutine handle_move_to: called CC=4 SCAR=14.0
Pod undocumented
231 2 my ($self, $to, $cookie) = @_;
232  
233 2 my $from = delete $self->cookie_jar->{from}{$cookie};
234 2 ▶ confess "Invalid move cookie '$cookie' (moved to '$to')"
Branch: 1 branch
BranchTrueFalseTotal
unless $from 0 2 2
235 unless $from;
236  
237 2 my $abs = eval { $self->rel2abs($to) };
238 2 ▶ $self->_watch_directory($abs) if $abs && -d $abs;
Branch: 1 branch
BranchTrueFalseTotal
if $abs and -d $abs 1 1 2
Condition66%2/3
$abs and -d $abs
!ll&&!rl&&r
011
MC/DC50%1/2
$abs and -d $abs
$abs -d $abs
Truth table66%2/3
A and B
A= $absB= -d $abs
ABresult
0X0
100
111
239  
240 2 $self->handle_move($from, $to);
241 }
242  
243 # inject our magic
244 12 ▶before 'handle_create' => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
245 12 my ($self, $dir) = @_;
246 12 my $abs = eval { $self->rel2abs($dir) };
247 12 ▶ return unless $abs && -d $abs;
Branch: 1 branch
BranchTrueFalseTotal
unless $abs and -d $abs 8 4 12
Condition66%2/3
$abs and -d $abs
!ll&&!rl&&r
084
MC/DC50%1/2
$abs and -d $abs
$abs -d $abs
Truth table66%2/3
A and B
A= $absB= -d $abs
ABresult
0X0
100
111
248 4 $self->_watch_directory($abs);
249 3 };
250  
251 0 ▶sub DEMOLISH {
Subroutine DEMOLISH: not called CC=4 SCAR=30.0
Pod undocumented
252 0 my $self = shift;
253 0 ▶ return unless $self->inotify;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->inotify 0 0 0
254 0 for my $w (values %{$self->inotify->{w}}){
255 0 ▶ next unless $w;
Branch: 1 branch
BranchTrueFalseTotal
unless $w 0 0 0
256 0 $w->cancel;
257 }
258 }
259  
260 3 1;
261  
262 __END__
↑ Summary EventReceiver/Debug.pm »