| 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
| |||||||||||||
| 67 | @opt, | ||||||||||||
| 68 | ) or pod2usage(1); | ||||||||||||
| 69 | |||||||||||||
| 70 | 1 | ▶ | if ($Opt{help}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 71 | 0 | pod2usage(0); | |||||||||||
| 72 | } | ||||||||||||
| 73 | |||||||||||||
| 74 | 1 | ▶ | if (@ARGV != 1) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 75 | 0 | pod2usage(1); | |||||||||||
| 76 | } | ||||||||||||
| 77 | |||||||||||||
| 78 | 1 | ▶ | if ($Opt{verbose}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 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'
MC/DC0%0/1
$main::Opt{'aggregator'} || '1h,1d,1M,1Y,Z'
$main::Opt{'aggregator'}
Truth table50%1/2
A
A= $main::Opt{'aggregator'}
| |||||||||||||
| 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
| |||||||||||||
| 105 | 0 | ▶ | if ($Opt{force}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 106 | 0 | ▶ | unlink $rfilename or die sprintf "Could not unlink '%s': %s", $rfilename, $!; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 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
| |||||||||||||
| 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
| |||||||||||||
| 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__ | ||||||||||||