CPAN-SQLite

 view release on metacpan or  search on metacpan

lib/CPAN/SQLite/META.pm  view on Meta::CPAN

  my ($self, $mods) = @_;
  my $class     = 'CPAN::Distribution';
  my $cpan_meta = $self->{cpan_meta};
  my %containsmods;
  if ($mods and (ref($mods) eq 'ARRAY')) {
    %containsmods = map { $_->{mod_name} => 1 } @$mods;
  }
  my $d = $cpan_meta->instance($class => $global_id);
  return $d->{CONTAINSMODS} = \%containsmods;
}

sub reload {
  my ($self, %args) = @_;

  my $time         = $args{'time'} || time;
  my $force        = $args{force};
  my $db_name      = $CPAN::SQLite::db_name;
  my $db           = File::Spec->catfile($CPAN::Config->{cpan_home}, $db_name);
  my $journal_file = $db . '-journal';
  if (-e $journal_file) {
    $CPAN::FrontEnd->mywarn('Database locked - cannot update.');
    return;
  }
  my @args = ($^X, '-MCPAN::SQLite::META=setup,update,check', '-e');
  if (-e $db && -s _) {
    my $mtime_db    = (stat(_))[9];
    my $time_string = gmtime_string($mtime_db);
    $CPAN::FrontEnd->myprint("Database was generated on $time_string\n");

    # Check for status, force update if it fails
    if (system(@args, 'check')) {
      $force = 1;
      $CPAN::FrontEnd->myprint("Database file requires reindexing\n");
    }

    unless ($force) {
      return if (($time - $mtime_db) < $CPAN::Config->{index_expire} * 86400);
    }
    $CPAN::FrontEnd->myprint('Updating database file ... ');
    push @args, q{update};
  } else {
    unlink($db) if -e _;
    $CPAN::FrontEnd->myprint('Creating database file ... ');
    push @args, q{setup};
  }
  if ($CPAN::SQLite::DBI::dbh) {
    $CPAN::SQLite::DBI::dbh->disconnect();
    $CPAN::SQLite::DBI::dbh = undef;
  }
  system(@args) == 0 or die qq{system @args failed: $?};
  $CPAN::FrontEnd->myprint("Done!\n");
  return 1;
}

sub setup {
  my $obj = CPAN::SQLite->new(setup => 1);
  $obj->index() or die qq{CPAN::SQLite setup failed};
  return;
}

sub update {
  my $obj = CPAN::SQLite->new();
  $obj->index() or die qq{CPAN::SQLite update failed};
  return;
}

sub check {
  my $obj = CPAN::SQLite->new();
  my $db  = File::Spec->catfile($obj->{'db_dir'}, $obj->{'db_name'});
  my $dbh = DBI->connect("DBI:SQLite:$db", '', '', { 'RaiseError' => 0, 'PrintError' => 0, 'AutoCommit' => 1 });
  if (my $sth = $dbh->prepare('SELECT status FROM info WHERE status = 1')) {
    if ($sth->execute()) {
      if ($sth->fetchrow_arrayref()) {
        exit 0;    # status = 1
      } else {
        exit 1;    # status <> 1, need reindexing
      }
    } else {

      # Something's wrong, will be safer to reinitialize
      $dbh->disconnect();
      undef $dbh;
      setup();
      update();
    }
  } else {

    # Probably old version of DB or no DB at all, run setup and update
    $dbh->disconnect();
    undef $dbh;
    setup();
    update();
  }
  return;
}

sub gmtime_string {
  my $time = shift;
  return unless $time;
  my @a = gmtime($time);
  my $string =
    sprintf("%s, %02d %s %d %02d:%02d:%02d GMT", $days[$a[6]], $a[3], $months[$a[4]], $a[5] + 1900, $a[2], $a[1], $a[0]);
  return $string;
}

sub extract_distinfo {
  my ($self, $pathname) = @_;
  unless ($pathname =~ m{^\w/\w\w/}) {
    $pathname =~ s{^(\w)(\w)(.*)}{$1/$1$2/$1$2$3};
  }
  my $d        = CPAN::DistnameInfo->new($pathname);
  my $dist     = $d->dist;
  my $download = download($d->cpanid, $d->filename);
  return ($dist and $download) ? ($dist, $download) : undef;
}

1;

=head1 NAME

CPAN::SQLite::META - helper module for CPAN.pm integration



( run in 1.160 second using v1.01-cache-2.11-cpan-e1769b4cff6 )