require Time::Progress earlier to fail early on missing module
[rersyncrecent.git] / bin / rrr-init
blob48f647d1e175aeb9aec7ece6a56446edcbfb8455
1 #!/usr/bin/perl
3 =head1 NAME
5 rrr-init - set up RECENT files for a directory tree
7 =head1 SYNOPSIS
9 rrr-init [options] directory
11 =head1 OPTIONS
13 =over 8
15 =cut
17 my @opt = <<'=back' =~ /B<--(\S+)>/g;
19 =item B<--aggregator=s>
21 Comma separated list of aggregator specifications, e.g.
23 --aggregator=1h,6h,1d,1W,1M,1Q,1Y,Z
25 Defaults to C<1h,1d,1M,1Y,Z>
27 =item B<--force|f>
29 Forces an overwrite of an already existing recentfile in the target
30 directory.
32 =item B<--help|h>
34 Prints a brief message and exists.
36 =item B<--verbose|v+>
38 More feedback.
40 =back
42 =head1 DESCRIPTION
44 Walk through a tree and fill all files into initial recentfiles.
46 =cut
49 use strict;
50 use warnings;
52 use File::Find::Rule;
53 use lib "/home/k/sources/rersyncrecent/lib";
54 use File::Rsync::Mirror::Recent;
55 use File::Rsync::Mirror::Recentfile;
56 use File::Spec;
57 use Getopt::Long;
58 use Pod::Usage qw(pod2usage);
59 use Time::HiRes qw(time);
61 our %Opt;
62 GetOptions(\%Opt,
63 @opt,
64 ) or pod2usage(1);
66 if ($Opt{help}) {
67 pod2usage(0);
70 if (@ARGV != 1) {
71 pod2usage(1);
74 if ($Opt{verbose}) {
75 # speed up fail on missing module:
76 require Time::Progress;
79 my($rootdir) = @ARGV;
80 my $aggregator_string = $Opt{aggregator} || "1h,1d,1M,1Y,Z";
81 my @aggregator = split /\s*,\s*/, $aggregator_string;
82 my $localroot = File::Spec->rel2abs($rootdir);
83 my $rfconstructor = sub {
84 return File::Rsync::Mirror::Recentfile->new
86 aggregator => \@aggregator,
87 interval => $aggregator[0],
88 localroot => $localroot,
89 verbose => $Opt{verbose},
92 my $rf = $rfconstructor->();
93 my $rfilename = File::Spec->catfile
95 $rootdir,
96 $rf->rfilename,
99 if (-e $rfilename) {
100 if ($Opt{force}) {
101 unlink $rfilename or die "Could not unlink '%s': %s", $rfilename, $!;
102 $rf = $rfconstructor->();
103 } else {
104 die sprintf "Alert: Found an already existing file '%s'. Won't overwrite. Either use --force or remove the file before calling me again", $rfilename;
106 } else {
107 warn "Debug: rfilename[$rfilename]";
109 my @t = time;
110 my @batch;
111 foreach my $file ( File::Find::Rule->new->file->in($rootdir) ) {
112 my $path = File::Spec->rel2abs($file);
113 my $epoch = (stat $path)[9];
114 push @batch, {path=>$path,type=>"new",epoch=>$epoch};
116 if ($Opt{verbose}) {
117 $t[1] = time;
118 warn sprintf "Found %d files to register in %.6f s. Writing to %s\n", scalar @batch, $t[1]-$t[0], $rfilename;
120 $rf->batch_update(\@batch);
121 if ($Opt{verbose}) {
122 $t[2] = time;
123 warn sprintf "Registered %d files in %.6f s\n", scalar @batch, $t[2]-$t[1];
126 __END__
129 # Local Variables:
130 # mode: cperl
131 # coding: utf-8
132 # cperl-indent-level: 4
133 # End: