blib/lib/File/Rotate/Simple.pm

filter: Statementstmt 93.3% 113 / 121 Branchbran 79.5% 35 / 44 Conditioncond 94.4% 17 / 18 MC/DCmcdc 72.7% 8 / 11 Subroutinesub 100% 17 / 17 Podpod 100% 2 / 2 totaltotal 90.1% 192 / 213 CC 32 SCAR 24.2
CC
32
Cov
90%
rotate
17.4
_build_files_to_rotate
16.1
_rotated_name
3.0
CRAP 11.3
↑ 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.0';
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 1 ▶ 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 sub rotate {
98 11 ▶ my $self = shift;
Subroutine rotate: called CC=15 SCAR=28.6
Pod documented
99  
100 11 ▶ unless (is_blessed_ref $self) {
Branch: 1 branch
BranchTrueFalseTotal
unless (Ref::Util::XS::is_blessed_ref($self)) 5 6 11
101 5 ▶ 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 11 my $max = $self->max;
114 11 ▶ my $age = ($self->age)
Branch: 1 branch
BranchTrueFalseTotal
$self->age ? : 1 10 11
115 ? $self->_epoch - ($self->age * ONE_DAY)
116 : 0;
117  
118 11 my @files = @{ $self->_build_files_to_rotate };
119  
120 11 my $index = scalar( @files );
121  
122 11 ▶ while ($index--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index--) 22 11 33
123  
124 22 ▶ my $file = $files[$index] or next;
Branch: 1 branch
BranchTrueFalseTotal
my $file = $files[$index] or next 0 22 22
125  
126 22 my $current = $file->{current};
127 22 my $rotated = $file->{rotated};
128  
129 22 ▶ unless (defined $rotated) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rotated) 1 21 22
130 1 $current->remove;
131 1 next;
132 }
133  
134 21 ▶ if ($max && $index >= $max) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max and $index >= $max) 0 21 21
Condition66%2/3
$max and $index >= $max
!ll&&!rl&&r
1830
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 21 ▶ if ($age && $current->stat->mtime < $age) {
Branch: 1 branch
BranchTrueFalseTotal
if ($age and $current->stat->mtime < $age) 1 20 21
Condition100%3/3
$age and $current->stat->mtime < $age
!ll&&!rl&&r
1551
MC/DC100%2/2
$age and $current->stat->mtime < $age
$age $current->stat->mtime < $age
Truth table100%3/3
A and B
A= $ageB= $current->stat->mtime < $age
ABresult
0X0
100
111
140 1 $current->remove;
141 1 next;
142 }
143  
144 20 ▶ die "Cannot move ${current} -> ${rotated}: file exists"
Branch: 1 branch
BranchTrueFalseTotal
if $rotated->exists 0 20 20
145 if $rotated->exists;
146  
147 20 $current->move($rotated);
148 }
149  
150 11 ▶ $self->file->touch if $self->touch;
Branch: 1 branch
BranchTrueFalseTotal
if $self->touch 1 10 11
151  
152 # TODO: chmod/chown arguments
153 }
154  
155  
156 sub _build_files_to_rotate {
157 11 ▶ my ($self) = @_;
Subroutine _build_files_to_rotate: called CC=16 SCAR=27.8
158  
159 11 my %files;
160  
161 11 my $num = $self->start_num;
162  
163 11 my $file = $self->_rotated_name( $num );
164 11 my $orig = $self->file;
165 11 ▶ if ( $orig->exists || -l $orig ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($orig->exists or -l $orig) { } 4 7 11
Condition100%3/3
$orig->exists or -l $orig
l!l&&r!l&&!r
317
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 4 $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 11 my $max = $self->max;
179 11 ▶ while ($file->exists || ($max && $num <= $max)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file->exists or $max and $num <= $max) 19 11 30
Condition100%6/6
$file->exists or $max and $num <= $max
l!l&&r!l&&!r
1839
$max and $num <= $max
!ll&&!rl&&r
1011
MC/DC66%2/3
$file->exists or $max and $num <= $max
$file->exists $max $num <= $max
Truth table75%3/4
A or B and C
A= $file->existsB= $maxC= $num <= $max
ABCresult
1XX1
0111
00X0
0100
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) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file->exists) 18 1 19
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 11 my $g = Graph->new;
201 11 foreach my $file (values %files) {
202 22 my $current = $file->{current};
203 22 ▶ if (my $rotated = $file->{rotated}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $rotated = $file->{'rotated'}) { } 21 1 22
204 21 $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 11 my %rotated;
215 11 $rotated{$_->[1]}++ for ($g->edges);
216  
217 11 ▶ 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 11 11
218 0 die "multiple files are rotated to '${duplicate}'";
219 }
220  
221 11 ▶ die "dependency chain is cyclic"
Branch: 1 branch
BranchTrueFalseTotal
if $g->has_a_cycle 0 11 11
222 if $g->has_a_cycle;
223  
224 return [
225 29 grep { defined $_ }
226 11 map { $files{$_} } $g->topological_sort()
227 ];
228  
229 }
230  
231  
232 sub _rotated_name {
233 137 ▶ my ($self, $index) = @_;
Subroutine _rotated_name: called CC=3 SCAR=11.0
234  
235 137 my $format = $self->extension_format;
236 {
237 5 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
238 137 $format =~ s/\%(\d+)*#/sprintf("\%0$1d", $index)/gea;
239 }
240  
241 137 my $file = $self->file->stringify;
242 137 ▶ my $extension = ($format =~ /\%/) ? $self->_strftime($format) : $format;
Branch: 1 branch
BranchTrueFalseTotal
$format =~ /\%/u ? : 4 133 137
243 137 my $replace = $self->replace_extension;
244  
245 137 ▶ if (defined $replace) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $replace) { } 4 133 137
246  
247 4 my $re = quotemeta($replace);
248 4 return path($file =~ s/${re}$/${extension}/r );
249  
250 } else {
251  
252 133 return path( $file . $extension );
253  
254 }
255 }
256  
257  
258 sub rotate_files {
259 1 ▶ __PACKAGE__->rotate( @_ );
Subroutine rotate_files: called CC=1 SCAR=0.0
Pod documented
260 }
261  
262  
263 5 1;
264  
265 __END__
↑ Summary