From: Vincent Pit Date: Fri, 15 Jun 2012 10:21:46 +0000 (+0200) Subject: Check the portage tree timestamp at initialization time X-Git-Tag: v0.12~2 X-Git-Url: http://git.vpit.fr/?p=perl%2Fmodules%2FCPANPLUS-Dist-Gentoo.git;a=commitdiff_plain;h=d5ec73d3500d10154218a91cab682ff97287a21d Check the portage tree timestamp at initialization time --- diff --git a/lib/CPANPLUS/Dist/Gentoo.pm b/lib/CPANPLUS/Dist/Gentoo.pm index fb54d76..ad4d108 100644 --- a/lib/CPANPLUS/Dist/Gentoo.pm +++ b/lib/CPANPLUS/Dist/Gentoo.pm @@ -8,6 +8,7 @@ use List::Util qw; use File::Copy (); use File::Path (); use File::Spec; +use POSIX (); use IPC::Cmd (); use Parse::CPAN::Meta (); @@ -269,6 +270,20 @@ sub format_available { $default_keywords = [ 'x86' ] unless defined $default_keywords; $default_distdir = '/usr/portage/distfiles' unless defined $default_distdir; + my $timestamp = CPANPLUS::Dist::Gentoo::Maps::get_portage_timestamp( + $main_portdir + ); + if (defined $timestamp) { + __PACKAGE__->_notify("Portage tree $main_portdir dates back from UNIX timestamp $timestamp"); + } else { + __PACKAGE__->_notify("Unable to get timestamp for portage tree $main_portdir, using gmtime instead"); + $timestamp = POSIX::mktime(gmtime); + } + if ($timestamp < CPANPLUS::Dist::Gentoo::Maps::TIMESTAMP) { + __PACKAGE__->_abort("Portage tree too old (please run emerge --sync and retry)"); + return $format_available = 0; + } + return $format_available = 1; } diff --git a/lib/CPANPLUS/Dist/Gentoo/Maps.pm b/lib/CPANPLUS/Dist/Gentoo/Maps.pm index 957450d..c564c4b 100644 --- a/lib/CPANPLUS/Dist/Gentoo/Maps.pm +++ b/lib/CPANPLUS/Dist/Gentoo/Maps.pm @@ -3,6 +3,9 @@ package CPANPLUS::Dist::Gentoo::Maps; use strict; use warnings; +use File::Spec; +use POSIX (); + =head1 NAME CPANPLUS::Dist::Gentoo::Maps - Map CPAN distribution names, version numbers and license identifiers to their Gentoo counterparts. @@ -313,6 +316,56 @@ sub perl_version_c2g { return join '.', map int, @parts; } +=head2 C + +Get the numerical timestamp associated with the portage tree located at C<$portage>. +Requires L, and returns C if it is not available. + +=cut + +sub get_portage_timestamp { + my ($portage) = @_; + + { + local $@; + eval { require POSIX::strptime } or return; + } + + my $file = File::Spec->catfile($portage, 'metadata', 'timestamp.chk'); + return unless -e $file; + + my $timestamp = do { + open my $fh, '<', $file or return; + local $/; + <$fh>; + }; + s/^\s*//, s/\s*$// for $timestamp; + + my $shift = 0; + if ($timestamp =~ s/\s+([+-])([0-9]{2})([0-9]{2})$//) { + $shift = ($2 * 60 + $3) * 60; + $shift = -$shift if $1 eq '-'; + } + + my $old_lc_all = POSIX::setlocale(POSIX::LC_ALL()); + POSIX::setlocale(POSIX::LC_ALL(), 'C'); + $timestamp = POSIX::mktime( + POSIX::strptime($timestamp, '%a, %d %b %Y %H:%M:%S') + ); + POSIX::setlocale(POSIX::LC_ALL(), $old_lc_all); + $timestamp += $shift; + + return $timestamp; +} + +=head2 C + +Numerical timestamp associated with the revision of the portage tree that was used for generating the corrections to the natural cpan-to-gentoo mapping listed in this module. + +=cut + +sub TIMESTAMP () { 1339737301 } + =head1 SEE ALSO L. diff --git a/samples/gengentooisms b/samples/gengentooisms index 03253e0..24238e3 100755 --- a/samples/gengentooisms +++ b/samples/gengentooisms @@ -43,15 +43,7 @@ sub p { printf $fmt, @args; } -sub timestamp { - my $tm = File::Spec->catfile(PORTAGE, 'metadata', 'timestamp.chk'); - return unless -e $tm; - open my $fh, '<', $tm; - local $/; - <$fh>; -} - -my $timestamp = timestamp(); +my $timestamp = CPANPLUS::Dist::Gentoo::Maps::get_portage_timestamp(PORTAGE); { my $ua; @@ -287,7 +279,7 @@ my $already_parsed = 0; if (-e STATE_FILE) { my $state = Storable::retrieve(STATE_FILE); - if ($state->[0] eq $timestamp) { + if ($state->[0] == $timestamp) { printf "State retrieved from %s\n", STATE_FILE; @not_on_cpan = @{ $state->[1] }; @unfindable = @{ $state->[2] }; @@ -304,7 +296,7 @@ if (-e STATE_FILE) { unless ($already_parsed) { if (-e DATA_FILE) { my $data = Storable::retrieve(DATA_FILE); - if ($data->[0] eq $timestamp) { + if ($data->[0] == $timestamp) { printf "Data retrieved from %s\n", DATA_FILE; %fetched_uri = %{ $data->[1] }; } else { @@ -383,11 +375,15 @@ open my $dst, '>', TARGET; my $max = max map length, keys %name_mismatch; SRC: while (<$src>) { - print $dst $_; - if (/^__DATA__$/) { + if (/^sub TIMESTAMP/) { + print $dst "sub TIMESTAMP () { $timestamp }\n"; + } elsif (/^__DATA__$/) { + print $dst "__DATA__\n"; printf $dst "%s%s %s\n", $_, (' ' x ($max - length)), $name_mismatch{$_} for sort keys %name_mismatch; last SRC; + } else { + print $dst $_; } }