bin/rrr-init

filter: Statementstmt 77.4% 48 / 62 Branchbran 38.8% 7 / 18 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 100% 10 / 10 Podpod n/a totaltotal 70.9% 66 / 93 CC 1 SCAR 0.0
CC
1
Cov
68%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« bin/rrr-aggregate ↑ Summary bin/rrr »
1 #!/usr/bin/perl
2  
3 =head1 NAME
4  
5 rrr-init - set up RECENT files for a directory tree
6  
7 =head1 SYNOPSIS
8  
9 rrr-init [options] directory
10  
11 =head1 OPTIONS
12  
13 =over 8
14  
15 =cut
16  
17 1 my @opt = <<'=back' =~ /B<--(\S+)>/g;
18  
19 =item B<--aggregator=s>
20  
21 Comma separated list of aggregator specifications, e.g.
22  
23 --aggregator=1h,6h,1d,1W,1M,1Q,1Y,Z
24  
25 Defaults to C<1h,1d,1M,1Y,Z>
26  
27 =item B<--force|f>
28  
29 Forces an overwrite of an already existing recentfile in the target
30 directory.
31  
32 =item B<--help|h>
33  
34 Prints a brief message and exits.
35  
36 =item B<--serializer_suffix=s>
37  
38 Defaults to C<.yaml>. Supported values are listed in
39 L<File::Rsync::Mirror::Recentfile> under the heading SERIALIZERS.
40  
41 =item B<--verbose|v+>
42  
43 More feedback. Requires Time::Progress installed.
44  
45 =back
46  
47 =head1 DESCRIPTION
48  
49 Walk through a tree and fill all files into initial recentfiles.
50  
51 =cut
52  
53  
54 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
55 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
56  
57 1 ▶use File::Find::Rule;
Subroutine BEGIN: called CC=1 SCAR=0.0
58 1 ▶use File::Rsync::Mirror::Recent;
Subroutine BEGIN: called CC=1 SCAR=0.0
59 1 ▶use File::Rsync::Mirror::Recentfile;
Subroutine BEGIN: called CC=1 SCAR=0.0
60 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
61 1 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
62 1 ▶use Pod::Usage qw(pod2usage);
Subroutine BEGIN: called CC=1 SCAR=0.0
63 1 ▶use Time::HiRes qw(time);
Subroutine BEGIN: called CC=1 SCAR=0.0
64  
65 1 our %Opt;
66 1 ▶GetOptions(\%Opt,
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions(\%main::Opt, @opt) or pod2usage(1) 1 0 1
67 @opt,
68 ) or pod2usage(1);
69  
70 1 ▶if ($Opt{help}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($main::Opt{"help"}) 0 1 1
71 0 pod2usage(0);
72 }
73  
74 1 ▶if (@ARGV != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ARGV != 1) 0 1 1
75 0 pod2usage(1);
76 }
77  
78 1 ▶if ($Opt{verbose}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($main::Opt{"verbose"}) 0 1 1
79 # speed up fail on missing module:
80 0 require Time::Progress;
81 }
82  
83 1 my($rootdir) = @ARGV;
84 1 ▶my $aggregator_string = $Opt{aggregator} || "1h,1d,1M,1Y,Z";
Condition50%1/2
$main::Opt{'aggregator'} || '1h,1d,1M,1Y,Z'
l!l
01
MC/DC0%0/1
$main::Opt{'aggregator'} || '1h,1d,1M,1Y,Z'
$main::Opt{'aggregator'}
Truth table50%1/2
A
A= $main::Opt{'aggregator'}
Aresult
11
00
85 1 my @aggregator = split /\s*,\s*/, $aggregator_string;
86 1 my $localroot = File::Spec->rel2abs($rootdir);
87 1 ▶my $rfconstructor = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
88 return File::Rsync::Mirror::Recentfile->new
89 (
90 aggregator => \@aggregator,
91 interval => $aggregator[0],
92 localroot => $localroot,
93 verbose => $Opt{verbose},
94 serializer_suffix => $Opt{serializer_suffix},
95 1 );
96 1 };
97 1 my $rf = $rfconstructor->();
98 1 my $rfilename = File::Spec->catfile
99 (
100 $rootdir,
101 $rf->rfilename,
102 );
103  
104 1 ▶if (-e $rfilename) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $rfilename) 0 1 1
105 0 ▶ if ($Opt{force}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($main::Opt{'force'}) { } 0 0 0
106 0 ▶ unlink $rfilename or die sprintf "Could not unlink '%s': %s", $rfilename, $!;
Branch: 1 branch
BranchTrueFalseTotal
unlink $rfilename or die sprintf(q[Could not unlink '%s': %s], $rfilename, $!) 0 0 0
107 0 $rf = $rfconstructor->();
108 } else {
109 0 die sprintf "Alert: Found an already existing file '%s'. Won't overwrite. Either use --force or remove the file before calling me again", $rfilename;
110 }
111 }
112 1 my @t = time;
113 1 my @batch;
114 1 foreach my $file ( File::Find::Rule->new->file->in($rootdir) ) {
115 0 my $path = File::Spec->rel2abs($file);
116 0 my $epoch = (lstat $path)[9];
117 0 push @batch, {path=>$path,type=>"new",epoch=>$epoch};
118 }
119 1 ▶if ($Opt{verbose}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($main::Opt{"verbose"}) 0 1 1
120 0 $t[1] = time;
121 0 warn sprintf "Found %d files to register in %.6f s. Writing to %s\n", scalar @batch, $t[1]-$t[0], $rfilename;
122 }
123 1 $rf->batch_update(\@batch);
124 1 ▶if ($Opt{verbose}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($main::Opt{"verbose"}) 0 1 1
125 0 $t[2] = time;
126 0 warn sprintf "Registered %d files in %.6f s\n", scalar @batch, $t[2]-$t[1];
127 }
128  
129 __END__
« bin/rrr-aggregate ↑ Summary bin/rrr »