| 1 | package CPAN::Audit::FreshnessCheck; | ||
| 2 | |||
| 3 | use v5.10; | ||
| 4 | |||
| 5 | our $VERSION = '1.002'; | ||
| 6 | |||
| 7 | =head1 NAME | ||
| 8 | |||
| 9 | CPAN::Audit::Freshness - check freshness of CPAN::Audit::DB | ||
| 10 | |||
| 11 | =head1 SYNOPSIS | ||
| 12 | |||
| 13 | use CPAN::Audit::Freshness; | ||
| 14 | |||
| 15 | # from the command-line, with default threshold | ||
| 16 | % perl -MCPAN::Audit::Freshness cpan-audit | ||
| 17 | |||
| 18 | # from the command-line, with specified threshold of 5 days | ||
| 19 | % perl -MCPAN::Audit::Freshness=5 cpan-audit | ||
| 20 | % env CPAN_AUDIT_FRESH_DAYS=30 perl -MCPAN::Audit::Freshness cpan-audit | ||
| 21 | |||
| 22 | =head1 DESCRIPTION | ||
| 23 | |||
| 24 | When loaded, this module outputs a warning if it thinks the version | ||
| 25 | of L<CPANSA::DB> is too old. It does this by comparing the version | ||
| 26 | of that module, which is date-based, with the current time. The default | ||
| 27 | threshold is 30 days, although you can set the value of C<CPAN_AUDIT_FRESH_DAYS>. | ||
| 28 | |||
| 29 | This previously checked the deprecated L<CPAN::Audit::DB> module, but | ||
| 30 | there is no version of that module that is fresh where L<CPANSA::DB> | ||
| 31 | isn't present. | ||
| 32 | |||
| 33 | There is no other functionality for this module. | ||
| 34 | |||
| 35 | =head1 LICENSE | ||
| 36 | |||
| 37 | This library is under the Artistic License 2.0. | ||
| 38 | |||
| 39 | =head1 AUTHOR | ||
| 40 | |||
| 41 | Copyright © 2022-2026, brian d foy, I<< <briandfoy@pobox.com> >> | ||
| 42 | |||
| 43 | =cut | ||
| 44 | |||
| 45 | sub import { | ||
| 46 | my( $class, $days ) = @_; | ||
| 47 | |||
| 48 | require Time::Piece; | ||
| 49 | require CPANSA::DB; | ||
| 50 | |||
| 51 | my $template = '%Y%m%d'; | ||
| 52 | my( $year, $month, $day ) = (localtime)[5,4,3]; | ||
| 53 | $year += 1900; $month += 1; | ||
| 54 | |||
| 55 | my $now_string = sprintf "%4d%02d%02d", $year, $month, $day; | ||
| 56 | my $now_moment = Time::Piece->strptime( $now_string, $template ); | ||
| 57 | |||
| 58 | my $db_time = CPANSA::DB->VERSION =~ s/\..*//r; | ||
| 59 | my $db_moment = Time::Piece->strptime( $db_time, $template ); | ||
| 60 | |||
| 61 | my $duration = int( ($now_moment - $db_moment) / 86_400 ); | ||
| 62 | warn "Database is $duration days old. Check for updates with `cpan -D CPANSA::DB`\n" | ||
| 63 | if $duration >= threshold($days); | ||
| 64 | } | ||
| 65 | |||
| 66 | sub default_threshold { 30 } | ||
| 67 | |||
| 68 | sub threshold { $_[0] // $ENV{CPAN_AUDIT_FRESH_DAYS} // default_threshold() } | ||
| 69 | |||
| 70 | __PACKAGE__; |