]> git.vpit.fr Git - perl/modules/CPANPLUS-Dist-Gentoo.git/commitdiff
Check the portage tree timestamp at initialization time
authorVincent Pit <vince@profvince.com>
Fri, 15 Jun 2012 10:21:46 +0000 (12:21 +0200)
committerVincent Pit <vince@profvince.com>
Fri, 15 Jun 2012 10:21:46 +0000 (12:21 +0200)
lib/CPANPLUS/Dist/Gentoo.pm
lib/CPANPLUS/Dist/Gentoo/Maps.pm
samples/gengentooisms

index fb54d76303c0e2d8444697a792f05c0bed49fa76..ad4d10828603d628508d14cb035c0d4948672d78 100644 (file)
@@ -8,6 +8,7 @@ use List::Util qw<reduce>;
 use File::Copy ();
 use File::Path ();
 use File::Spec;
 use File::Copy ();
 use File::Path ();
 use File::Spec;
+use POSIX      ();
 
 use IPC::Cmd          ();
 use Parse::CPAN::Meta ();
 
 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;
 
  $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;
 }
 
  return $format_available = 1;
 }
 
index 957450dd6621206e832dde99186d5705b4223eae..c564c4ba7fea79c0a8b3f7e1527b74d0714db9ea 100644 (file)
@@ -3,6 +3,9 @@ package CPANPLUS::Dist::Gentoo::Maps;
 use strict;
 use warnings;
 
 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.
 =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;
 }
 
  return join '.', map int, @parts;
 }
 
+=head2 C<get_portage_timestamp $portage>
+
+Get the numerical timestamp associated with the portage tree located at C<$portage>.
+Requires L<POSIX::strptime>, and returns C<undef> 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<TIMESTAMP>
+
+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<CPANPLUS::Dist::Gentoo>.
 =head1 SEE ALSO
 
 L<CPANPLUS::Dist::Gentoo>.
index 03253e053397a87edec037b6624ca2fb9cda5bb8..24238e3b0ae6402528f1d6d80c1d7ae912d8a7ca 100755 (executable)
@@ -43,15 +43,7 @@ sub p {
  printf $fmt, @args;
 }
 
  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;
 
 {
  my $ua;
@@ -287,7 +279,7 @@ my $already_parsed = 0;
 
 if (-e STATE_FILE) {
  my $state = Storable::retrieve(STATE_FILE);
 
 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] };
   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);
 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 {
    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>) {
 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;
   printf $dst "%s%s %s\n", $_, (' ' x ($max - length)), $name_mismatch{$_}
                                                    for sort keys %name_mismatch;
   last SRC;
+ } else {
+  print $dst $_;
  }
 }
 
  }
 }