blib/lib/File/Rotate/Simple.pm

filter: Statementstmt 93.3% 113 / 121 Branchbran 79.5% 35 / 44 Conditioncond 83.3% 25 / 30 MC/DCmcdc 62.5% 10 / 16 Subroutinesub 100% 17 / 17 Podpod 100% 2 / 2 totaltotal 87.8% 202 / 230 CC 36 SCAR 26.0
CC
36
Cov
89%
rotate
20.6
_build_files_to_rotate
18.4
_rotated_name
3.0
CRAP 13.5
↑ Summary
1 package File::Rotate::Simple;
2  
3 5 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 5 ▶use Moo 1.001000;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 extends 'Exporter';
7  
8 5 ▶use Graph;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use List::Util 1.43, qw/ first /;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use Module::Runtime qw/ require_module /;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use Path::Tiny 0.015;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use Ref::Util qw/ is_blessed_ref /;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶use Time::Seconds qw/ ONE_DAY /;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use Types::Standard -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 5 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 5 our $VERSION = 'v0.4.2';
19  
20 # ABSTRACT: no-frills file rotation
21  
22 # RECOMMEND PREREQ: Class::Load::XS
23 # RECOMMEND PREREQ: Ref::Util::XS
24 # RECOMMEND PREREQ: Type::Tiny::XS
25  
26 5 our @EXPORT_OK = qw/ rotate_files /;
27  
28  
29 5 has age => (
30 is => 'ro',
31 isa => Int,
32 default => 0,
33 );
34  
35  
36 5 has max => (
37 is => 'ro',
38 isa => Int,
39 default => 0,
40 );
41  
42  
43 5 has file => (
44 is => 'ro',
45 isa => InstanceOf['Path::Tiny'],
46 coerce => \&path,
47 required => 1,
48 );
49  
50  
51 5 has start_num => (
52 is => 'ro',
53 isa => Int,
54 default => 1,
55 );
56  
57  
58 5 has extension_format => (
59 is => 'ro',
60 isa => Str,
61 default => '.%#',
62 );
63  
64  
65 5 has replace_extension => (
66 is => 'ro',
67 isa => Maybe[Str],
68 );
69  
70  
71 5 has if_missing => (
72 is => 'ro',
73 isa => Bool,
74 default => 1,
75 );
76  
77  
78 5 has touch => (
79 is => 'ro',
80 isa => Bool,
81 default => 0,
82 );
83  
84  
85 has time => (
86 is => 'rw',
87 isa => InstanceOf[qw/ Time::Piece Time::Moment DateTime DateTime::Lite DateTime::Tiny /],
88 lazy => 1,
89 3 ▶ default => sub { require_module('Time::Piece'); Time::Piece::localtime() },
Subroutine __ANON__: called CC=1 SCAR=0.0
90 5 handles => {
91 _strftime => 'strftime',
92 _epoch => 'epoch',
93 },
94 );
95  
96  
97 13 ▶sub rotate {
Subroutine rotate: called CC=17 SCAR=30.2
Pod documented
98 13 my $self = shift;
99  
100 13 ▶ unless (is_blessed_ref $self) {
Branch: 1 branch
BranchTrueFalseTotal
unless (Ref::Util::XS::is_blessed_ref($self)) 5 8 13
101 0 ▶ my %args = (@_ == 1) ? %{ $_[0] } : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 0 5 5
102  
103 5 ▶ if (my $files = delete $args{files}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $files = delete $args{"files"}) 0 5 5
104 0 foreach my $file (@{$files}) {
105 0 $self->new( %args, file => $file )->rotate;
106 }
107 0 return;
108 }
109  
110 5 $self = $self->new(%args);
111 }
112  
113 13 my $max = $self->max;
114 13 ▶ my $age = ($self->age)
Branch: 1 branch
BranchTrueFalseTotal
$self->age ? : 3 10 13
115 ? $self->_epoch - ($self->age * ONE_DAY)
116 : 0;
117  
118 13 my @files = @{ $self->_build_files_to_rotate };
119  
120 13 my $index = scalar( @files );
121  
122 13 ▶ while ($index--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index--) 24 13 37
123  
124 24 ▶ my $file = $files[$index] or next;
Branch: 1 branch
BranchTrueFalseTotal
my $file = $files[$index] or next 24 0 24
125  
126 24 my $current = $file->{current};
127 24 my $rotated = $file->{rotated};
128  
129 24 ▶ unless (defined $rotated) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rotated) 1 23 24
130 1 $current->remove;
131 1 next;
132 }
133  
134 23 ▶ if ($max && $index >= $max) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max and $index >= $max) 0 23 23
Condition66%2/3
$max and $index >= $max
!ll&&!rl&&r
2030
MC/DC0%0/2
$max and $index >= $max
$max $index >= $max
Truth table66%2/3
A and B
A= $maxB= $index >= $max
ABresult
0X0
100
111
135 0 $current->remove;
136 0 next;
137 }
138  
139 23 ▶ if ( $age && ( ( $current->exists || -l $current ) && $current->lstat->mtime < $age ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($age and ($current->exists || -l $current and $current->lstat->mtime < $age)) 1 22 23
Condition77%7/9
$age and ($current->exists || -l $current and $current->lstat->mtime < $age)
!ll&&!rl&&r
1571
$current->exists || -l $current and $current->lstat->mtime < $age
!ll&&!rl&&r
071
$current->exists or -l $current
l!l&&r!l&&!r
620
MC/DC50%2/4
$age and ($current->exists || -l $current and $current->lstat->mtime < $age)
$age $current->exists -l $current $current->lstat->mtime < $age
Truth table66%4/6
A and B or C and D
A= $ageB= $current->existsC= -l $currentD= $current->lstat->mtime < $age
ABCDresult
0XXX0
100X0
11X00
10100
11X11
10111
140 1 $current->remove;
141 1 next;
142 }
143  
144 22 ▶ die "Cannot move ${current} -> ${rotated}: file exists"
Branch: 1 branch
BranchTrueFalseTotal
if $rotated->exists 0 22 22
145 if $rotated->exists;
146  
147 22 $current->move($rotated);
148 }
149  
150 13 ▶ $self->file->touch if $self->touch;
Branch: 1 branch
BranchTrueFalseTotal
if $self->touch 3 10 13
151  
152 # TODO: chmod/chown arguments
153 }
154  
155  
156 13 ▶sub _build_files_to_rotate {
Subroutine _build_files_to_rotate: called CC=18 SCAR=29.1
157 13 my ($self) = @_;
158  
159 13 my %files;
160  
161 13 my $num = $self->start_num;
162  
163 13 my $file = $self->_rotated_name( $num );
164 13 my $orig = $self->file;
165 13 ▶ if ( $orig->exists || -l $orig ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($orig->exists or -l $orig) { } 6 7 13
Condition100%3/3
$orig->exists or -l $orig
l!l&&r!l&&!r
337
MC/DC100%2/2
$orig->exists or -l $orig
$orig->exists -l $orig
Truth table100%3/3
A or B
A= $orig->existsB= -l $orig
ABresult
1X1
011
000
166  
167 6 $files{ $orig } = {
168 current => $orig,
169 rotated => $file,
170 };
171  
172 } else {
173  
174 7 ▶ return [ ] unless $self->if_missing;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->if_missing 0 7 7
175  
176 }
177  
178 13 my $max = $self->max;
179 13 ▶ while ($file->exists || -l $file || ($max && $num <= $max)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file->exists or -l $file or $max and $num <= $max) 19 13 32
Condition88%8/9
$file->exists or -l $file or $max and $num <= $max
l!l&&r!l&&!r
18113
$file->exists or -l $file
l!l&&r!l&&!r
18014
$max and $num <= $max
!ll&&!rl&&r
1211
MC/DC75%3/4
$file->exists or -l $file or $max and $num <= $max
$file->exists -l $file $max $num <= $max
Truth table80%4/5
A or B or C and D
A= $file->existsB= -l $fileC= $maxD= $num <= $max
ABCDresult
1XXX1
01XX1
00111
000X0
00100
180  
181 19 my $rotated = $self->_rotated_name( ++$num );
182  
183 19 ▶ last if $rotated eq $file;
Branch: 1 branch
BranchTrueFalseTotal
if $rotated eq $file 0 19 19
184  
185 19 ▶ if ( $file->exists || -l $file ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file->exists or -l $file) 18 1 19
Condition66%2/3
$file->exists or -l $file
l!l&&r!l&&!r
1801
MC/DC50%1/2
$file->exists or -l $file
$file->exists -l $file
Truth table66%2/3
A or B
A= $file->existsB= -l $file
ABresult
1X1
011
000
186 18 ▶ $files{ $file } = {
Branch: 1 branch
BranchTrueFalseTotal
!$max || $num <= $max ? : 17 1 18
Condition100%3/3
not $max or $num <= $max
l!l&&r!l&&!r
1431
MC/DC100%2/2
not $max or $num <= $max
not $max $num <= $max
Truth table100%3/3
not(A) or B
A= not $maxB= $num <= $max
ABresult
1X1
011
000
187 current => $file,
188 rotated => (!$max || $num <= $max) ? $rotated : undef,
189 };
190 }
191  
192 19 $file = $rotated;
193  
194 }
195  
196 # Using a topoligical sort is probably overkill, but it allows us
197 # to use more complicated filename rotation schemes in a subclass
198 # without having to worry about file order.
199  
200 13 my $g = Graph->new;
201 13 foreach my $file (values %files) {
202 24 my $current = $file->{current};
203 24 ▶ if (my $rotated = $file->{rotated}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $rotated = $file->{'rotated'}) { } 23 1 24
204 23 $g->add_edge( $current->stringify,
205 $rotated->stringify );
206 } else {
207 1 $g->add_vertex( $current->stringify );
208 }
209 }
210  
211 # Now check that there is not more than one file being rotated to
212 # the same name.
213  
214 13 my %rotated;
215 13 $rotated{$_->[1]}++ for ($g->edges);
216  
217 13 ▶ if (my $duplicate = first { $rotated{$_} > 1 } keys %rotated) {
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
if (my $duplicate = &first(sub { $rotated{$_} > 1; } , keys %rotated)) 0 13 13
218 0 die "multiple files are rotated to '${duplicate}'";
219 }
220  
221 13 ▶ die "dependency chain is cyclic"
Branch: 1 branch
BranchTrueFalseTotal
if $g->has_a_cycle 0 13 13
222 if $g->has_a_cycle;
223  
224 return [
225 33 grep { defined $_ }
226 13 map { $files{$_} } $g->topological_sort()
227 ];
228  
229 }
230  
231  
232 139 ▶sub _rotated_name {
Subroutine _rotated_name: called CC=3 SCAR=11.0
233 139 my ($self, $index) = @_;
234  
235 139 my $format = $self->extension_format;
236 {
237 5 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
238 139 $format =~ s/\%(\d+)*#/sprintf("\%0$1d", $index)/gea;
239 }
240  
241 139 my $file = $self->file->stringify;
242 139 ▶ my $extension = ($format =~ /\%/) ? $self->_strftime($format) : $format;
Branch: 1 branch
BranchTrueFalseTotal
$format =~ /\%/u ? : 4 135 139
243 139 my $replace = $self->replace_extension;
244  
245 139 ▶ if (defined $replace) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $replace) { } 4 135 139
246  
247 4 my $re = quotemeta($replace);
248 4 return path($file =~ s/${re}$/${extension}/r );
249  
250 } else {
251  
252 135 return path( $file . $extension );
253  
254 }
255 }
256  
257  
258 1 ▶sub rotate_files {
Subroutine rotate_files: called CC=1 SCAR=0.0
Pod documented
259 1 __PACKAGE__->rotate( @_ );
260 }
261  
262  
263 5 1;
264  
265 __END__
↑ Summary