1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
|
# Copyright (C) 2018 Felix Lechner
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, you can find it on the World Wide
# Web at https://www.gnu.org/copyleft/gpl.html, or write to the Free
# Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
# MA 02110-1301, USA
package Test::StagedFileProducer;
=head1 NAME
Test::StagedFileProducer -- mtime-based file production engine
=head1 SYNOPSIS
use Test::StagedFileProducer;
my $wherever = '/your/test/directory';
my $producer = Test::StagedFileProducer->new(path => $wherever);
$producer->exclude("$wherever/log", "$wherever/build-stamp");
my $output = "$wherever/file.out";
$producer->add_stage(
products => [$output],
build =>sub {
print encode_utf8("Building $output.\n");
},
skip =>sub {
print encode_utf8("Skipping $output.\n");
}
);
$producer->run(minimum_epoch => time, verbose => 1);
=head1 DESCRIPTION
Provides a way to define and stack file production stages that all
depend on subsets of the same group of files.
After the stages are defined, the processing engine takes an inventory
of all files in a target directory. It excludes some files, like logs,
that should not be considered.
Each stage adds its own products to the list of files to be excluded
before deciding whether to produce them. The decision is based on
relative file modification times, in addition to a systemic rebuilding
threshold. Before rebuilding, each stage asks a lower stage to make
the same determination.
The result is an engine with file production stages that depend on
successively larger sets of files.
=cut
use v5.20;
use warnings;
use utf8;
use Carp;
use Const::Fast;
use File::Find::Rule;
use File::Spec::Functions qw(abs2rel);
use File::stat;
use List::Util qw(min max);
use Path::Tiny;
use Unicode::UTF8 qw(encode_utf8);
use Test::Lintian::Helper qw(rfc822date);
const my $EMPTY => q{};
const my $SPACE => q{ };
=head1 FUNCTIONS
=over 4
=item new(path => PATH)
Create a new instance focused on files in directory PATH.
=cut
sub new {
my ($class, %params) = @_;
my $self = bless {}, $class;
croak encode_utf8('Cannot proceed without a path.')
unless exists $params{path};
$self->{path} = $params{path};
$self->{exclude} = [];
$self->{stages} = [];
return $self;
}
=item exclude(LIST)
Excludes all absolute paths in LIST from all mtime comparisons.
This is especially useful for logs. Calls to Path::Tiny->realpath
are made to ensure the elements are canonical and have a chance
of matching something returned by File::Find::Rule.
=cut
sub exclude {
my ($self, @list) = @_;
push(@{$self->{exclude}}, grep { defined } @list);
return;
}
=item add_stage(HASH)
Add a stage defined by HASH to the processing engine for processing
after stages previously added. HASH can define the following keys:
$HASH{products} => LIST; a list of full-path filenames to be
produced.
$HASH{minimum_epoch} => EPOCH; an integer threshold for maximum age
$HASH{build} => SUB; a sub executed when production is required.
$HASH{skip} => SUB; a sub executed when production is not required.
=cut
sub add_stage {
my ($self, %stage) = @_;
push(@{$self->{stages}}, \%stage);
return;
}
=item run(PARAMETERS)
Runs the defined engine using the given parameters, which are
arranged in a matching list suitable for assignment to a hash.
The following two parameters are currently available:
minimum_epoch => EPOCH; a systemic threshold, in epochs, below
which rebuilding is mandatory for any product.
verbose => BOOLEAN; an option to enable more verbose reporting
=cut
sub run {
my ($self, %params) = @_;
$self->{minimum_epoch} = $params{minimum_epoch} // 0;
$self->{verbose} = $params{verbose} // 0;
# take an mtime inventory of all files in path
$self->{mtimes}
= { map { $_ => path($_)->stat->mtime }
File::Find::Rule->file->in($self->{path}) };
say encode_utf8(
'Found the following file modification times (most recent first):')
if $self->{verbose};
my @ordered= reverse sort { $self->{mtimes}{$a} <=> $self->{mtimes}{$b} }
keys %{$self->{mtimes}};
foreach my $file (@ordered) {
my $relative = abs2rel($file, $self->{path});
say encode_utf8(rfc822date($self->{mtimes}{$file}) . " : $relative")
if $self->{verbose};
}
$self->_process_remaining_stages(@{$self->{exclude}});
return;
}
=item _process_remaining_stages(LIST)
An internal subroutine that is used recursively to execute
the stages. The list passed describes the list of files to
be excluded from subsequent mtime calculations.
Please note that the bulk of the execution takes place
after calling the next lower stage. That is to ensure that
any lower build targets (or products, in our parlance) are
met before the present stage attempts to do its job.
=cut
sub _process_remaining_stages {
my ($self, @exclude) = @_;
if (scalar @{$self->{stages}}) {
# get the next processing stage
my %stage = %{ pop(@{$self->{stages}}) };
# add our products to the list of files excluded
my @products = grep { defined } @{$stage{products}//[]};
push(@exclude, @products);
# pass to next lower stage for potential rebuilding
$self->_process_remaining_stages(@exclude);
# get good paths that will match those of File::Find
@exclude = map { path($_)->realpath } @exclude;
say encode_utf8($EMPTY) if $self->{verbose};
my @relative = sort map { abs2rel($_, $self->{path}) } @products;
say encode_utf8(
'Considering production of: ' . join($SPACE, @relative))
if $self->{verbose};
say encode_utf8('Excluding: '
. join($SPACE, sort map { abs2rel($_, $self->{path}) } @exclude))
if $self->{verbose};
my %relevant = %{$self->{mtimes}};
delete @relevant{@exclude};
# my @ordered= reverse sort { $relevant{$a} <=> $relevant{$b} }
# keys %relevant;
# foreach my $file (@ordered) {
# say encode_utf8(rfc822date($relevant{$file}) . ' : ' . abs2rel($file, $self->{path}))
# if $self->{verbose};
# }
say encode_utf8($EMPTY) if $self->{verbose};
my $file_epoch = (max(values %relevant))//time;
say encode_utf8(
'Input files modified on : '. rfc822date($file_epoch))
if $self->{verbose};
my $systemic_minimum_epoch = $self->{minimum_epoch} // 0;
say encode_utf8('Systemic minimum epoch is : '
. rfc822date($systemic_minimum_epoch))
if $self->{verbose};
my $stage_minimum_epoch = $stage{minimum_epoch} // 0;
say encode_utf8('Stage minimum epoch is : '
. rfc822date($stage_minimum_epoch))
if $self->{verbose};
my $threshold
= max($stage_minimum_epoch, $systemic_minimum_epoch, $file_epoch);
say encode_utf8(
'Rebuild threshold is : '. rfc822date($threshold))
if $self->{verbose};
say encode_utf8($EMPTY) if $self->{verbose};
my $product_epoch
= min(map { -e ? path($_)->stat->mtime : 0 } @products);
if($product_epoch) {
say encode_utf8(
'Products modified on : '. rfc822date($product_epoch))
if $self->{verbose};
} else {
say encode_utf8('At least one product is not present.')
if $self->{verbose};
}
# not producing if times are equal; resolution 1 sec
if ($product_epoch < $threshold) {
say encode_utf8('Producing: ' . join($SPACE, @relative))
if $self->{verbose};
$stage{build}->() if exists $stage{build};
# sometimes the products are not the newest files
path($_)->touch(time) for @products;
} else {
say encode_utf8(
'Skipping production of: ' . join($SPACE, @relative))
if $self->{verbose};
$stage{skip}->() if exists $stage{skip};
}
}
return;
}
=back
=cut
1;
# Local Variables:
# indent-tabs-mode: nil
# cperl-indent-level: 4
# End:
# vim: syntax=perl sw=4 sts=4 sr et
|