GX/File/Watcher.pm

filter: Statementstmt 94.5% 69 / 73 Branchbran 71.1% 37 / 52 Conditioncond 40.0% 6 / 15 MC/DCmcdc 20.0% 2 / 10 Subroutinesub 100% 7 / 7 Podpod 100% 2 / 2 totaltotal 77.3% 123 / 159 CC 35 SCAR 27.9
CC
35
Cov
80%
find_changes
21.1
watch
18.0
__ANON__
5.9
CRAP 16.4
« GX/Script.pm ↑ Summary GX/View/Template/HTC.pm »
1 # ----------------------------------------------------------------------------------------------------------------------
2 # GX Framework (c) 2009-2011 Jörg A. Uzarek <uzarek@runlevelnull.de>
3 # File: GX/File/Watcher.pm
4 # ----------------------------------------------------------------------------------------------------------------------
5  
6 package GX::File::Watcher;
7  
8 2 ▶use GX::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 ▶use File::Find ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13  
14 # ----------------------------------------------------------------------------------------------------------------------
15 # Class setup
16 # ----------------------------------------------------------------------------------------------------------------------
17  
18 2 ▶use GX::Class::Object;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 2 has 'directories' => (
21 isa => 'Hash',
22 initialize => 1,
23 accessors => {
24 'directories' => { type => 'get_keys' },
25 '_directories' => { type => 'get_reference' }
26 }
27 );
28  
29 2 has 'files' => (
30 isa => 'Hash',
31 initialize => 1,
32 accessors => {
33 'files' => { type => 'get_keys' },
34 '_files' => { type => 'get_reference' }
35 }
36 );
37  
38 2 build;
39  
40  
41 # ----------------------------------------------------------------------------------------------------------------------
42 # Public methods
43 # ----------------------------------------------------------------------------------------------------------------------
44  
45 26 ▶sub find_changes {
Subroutine find_changes: called CC=20 SCAR=30.5
Pod documented
46  
47 26 my $self = shift;
48  
49 26 ▶ my $fast_mode = wantarray ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 13 13 26
50  
51 26 my $directories = $self->_directories;
52 26 my $files = $self->_files;
53  
54 26 my @changes;
55  
56 CHECK:
57 {
58  
59 26 ▶ while ( my ( $directory, $info ) = each %$directories ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$directories) 24 21 45
60  
61 24 ▶ if ( ! -d $directory ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $directory) 2 22 24
62 2 push @changes, $directory;
63 2 ▶ $fast_mode ? last CHECK : next;
Branch: 1 branch
BranchTrueFalseTotal
$fast_mode ? : 1 1 2
64 }
65  
66 22 ▶ my @stat = stat $directory or complain "Cannot stat \"$directory\"";
Branch: 1 branch
BranchTrueFalseTotal
my(@stat) = stat $directory or &complain(qq[Cannot stat "$directory"]) 22 0 22
67  
68 22 ▶ if ( $stat[9] != $info->[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stat[9] != $info->[0]) 0 22 22
69 0 push @changes, $directory;
70 0 ▶ $fast_mode ? last CHECK : next;
Branch: 1 branch
BranchTrueFalseTotal
$fast_mode ? : 0 0 0
71 }
72  
73 22 ▶ opendir DIR, $directory or complain "Cannot open \"$directory\" ($!)";
Branch: 1 branch
BranchTrueFalseTotal
opendir DIR, $directory or &complain(qq[Cannot open "$directory" ($!)]) 22 0 22
74 22 my @content = readdir DIR;
75 22 closedir DIR;
76  
77 22 ▶ if ( @content != $info->[1] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@content != $info->[1]) 8 14 22
78 8 push @changes, $directory;
79 8 ▶ $fast_mode ? last CHECK : next;
Branch: 1 branch
BranchTrueFalseTotal
$fast_mode ? : 4 4 8
80 }
81  
82 }
83  
84 21 ▶ while ( my ( $file, $info ) = each %$files ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$files) 25 19 44
85  
86 25 ▶ if ( ! -f $file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $file) 5 20 25
87 5 push @changes, $file;
88 5 ▶ $fast_mode ? last CHECK : next;
Branch: 1 branch
BranchTrueFalseTotal
$fast_mode ? : 0 5 5
89 }
90  
91 20 ▶ my @stat = stat $file or complain "Cannot stat \"$file\"";
Branch: 1 branch
BranchTrueFalseTotal
my(@stat) = stat $file or &complain(qq[Cannot stat "$file"]) 20 0 20
92  
93 20 ▶ if ( $stat[9] != $info->[0] || $stat[7] != $info->[1] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stat[9] != $info->[0] or $stat[7] != $info->[1]) 5 15 20
Condition66%2/3
$stat[9] != $info->[0] or $stat[7] != $info->[1]
l!l&&r!l&&!r
0515
MC/DC50%1/2
$stat[9] != $info->[0] or $stat[7] != $info->[1]
$stat[9] != $info->[0] $stat[7] != $info->[1]
Truth table66%2/3
A or B
A= $stat[9] != $info->[0]B= $stat[7] != $info->[1]
ABresult
1X1
011
000
94 5 push @changes, $file;
95 5 ▶ $fast_mode ? last CHECK : next;
Branch: 1 branch
BranchTrueFalseTotal
$fast_mode ? : 2 3 5
96 }
97  
98 }
99  
100 }
101  
102 26 ▶ if ( $fast_mode ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fast_mode) { } 13 13 26
103 13 keys %$directories;
104 13 keys %$files;
105 13 ▶ return @changes ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
@changes ? : 7 6 13
106 }
107 else {
108 13 return @changes;
109 }
110  
111 }
112  
113 6 ▶sub watch {
Subroutine watch: called CC=12 SCAR=28.9
Pod documented
114  
115 6 my $self = shift;
116  
117 6 my @files;
118 my @directories;
119  
120 6 for ( @_ ) {
121 6 ▶ -d && push( @directories, $_ ) && next;
Branch: 1 branch
BranchTrueFalseTotal
-d $_ and push @directories, $_ and next 6 0 6
Condition33%1/3
-d $_ and push @directories, $_
!ll&&!rl&&r
006
MC/DC0%0/2
-d $_ and push @directories, $_
-d $_ push @directories, $_
Truth table33%1/3
A and B
A= -d $_B= push @directories, $_
ABresult
0X0
100
111
122 0 ▶ -f && push( @files, $_ ) && next;
Branch: 1 branch
BranchTrueFalseTotal
-f $_ and push @files, $_ and next 0 0 0
Condition0%0/3
-f $_ and push @files, $_
!ll&&!rl&&r
000
MC/DC0%0/2
-f $_ and push @files, $_
-f $_ push @files, $_
Truth table0%0/3
A and B
A= -f $_B= push @files, $_
ABresult
0X0
100
111
123 0 complain "Invalid argument";
124 }
125  
126 6 ▶ if ( @directories ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@directories) 6 0 6
127  
128 File::Find::find(
129 {
130 13 ▶ wanted => sub {
Subroutine __ANON__: called CC=5 SCAR=17.8
131 13 ▶ -f && push( @files, $_ ) && return;
Branch: 1 branch
BranchTrueFalseTotal
-f $_ and push @files, $_ and return 7 6 13
Condition66%2/3
-f $_ and push @files, $_
!ll&&!rl&&r
607
MC/DC50%1/2
-f $_ and push @files, $_
-f $_ push @files, $_
Truth table66%2/3
A and B
A= -f $_B= push @files, $_
ABresult
0X0
100
111
132 6 ▶ -d && push( @directories, $_ ) && return;
Branch: 1 branch
BranchTrueFalseTotal
-d $_ and push @directories, $_ and return 6 0 6
Condition33%1/3
-d $_ and push @directories, $_
!ll&&!rl&&r
006
MC/DC0%0/2
-d $_ and push @directories, $_
-d $_ push @directories, $_
Truth table33%1/3
A and B
A= -d $_B= push @directories, $_
ABresult
0X0
100
111
133 },
134 6 no_chdir => 1
135 },
136 @directories
137 )
138  
139 }
140  
141 6 my $directories = $self->_directories;
142  
143 6 for my $directory ( map { File::Spec->canonpath( $_ ) } @directories ) {
144  
145 12 ▶ my @stat = stat $directory or complain "Cannot stat \"$directory\"";
Branch: 1 branch
BranchTrueFalseTotal
my(@stat) = stat $directory or &complain(qq[Cannot stat "$directory"]) 12 0 12
146  
147 12 ▶ opendir DIR, $directory or complain "Cannot open \"$directory\" ($!)";
Branch: 1 branch
BranchTrueFalseTotal
opendir DIR, $directory or &complain(qq[Cannot open "$directory" ($!)]) 12 0 12
148 12 my @content = readdir DIR;
149 12 closedir DIR;
150  
151 12 $directories->{$directory} = [ $stat[9], scalar @content ];
152  
153 }
154  
155 6 my $files = $self->_files;
156  
157 6 for my $file ( map { File::Spec->canonpath( $_ ) } @files ) {
158 7 ▶ my @stat = stat $file or complain "Cannot stat \"$file\"";
Branch: 1 branch
BranchTrueFalseTotal
my(@stat) = stat $file or &complain(qq[Cannot stat "$file"]) 7 0 7
159 7 $files->{$file} = [ $stat[9], $stat[7] ];
160 }
161  
162 6 return;
163  
164 }
165  
166  
167 2 1;
168  
169 __END__
« GX/Script.pm ↑ Summary GX/View/Template/HTC.pm »