[Bio] / Babel / bin / load_source_data.pl Repository:
ViewVC logotype

View of /Babel/bin/load_source_data.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.11 - (download) (as text) (annotate)
Wed Jul 20 16:41:16 2011 UTC (8 years, 10 months ago) by tharriso
Branch: MAIN
CVS Tags: mgrast_dev_08112011, mgrast_dev_08022011, mobedac_release_05302012, mgrast_version_3_2, mgrast_dev_12152011, mgrast_dev_10262011, mgrast_release_3_1_2, mgrast_release_3_1_1, HEAD
Changes since 1.10: +26 -7 lines
fig_config independent, db server independent, more options

#!/usr/bin/env perl

use strict;
use warnings;

use Babel::lib::Babel;
use Data::Dumper;
use Getopt::Long;

my $usage  = "load_source_data.pl [--verbose] [--dbname NAME] [--dbuser USER] [--dbhost HOST] --download DOWNLOAD_FILE --source SOURCE_FILE\n";
my $dfile  = '';
my $sfile  = '';
my $verb   = '';
my $dbname = $FIG_Config::babel_db;
my $dbuser = $FIG_Config::babel_dbuser;
my $dbhost = $FIG_Config::babel_dbhost;

if ( (@ARGV > 0) && ($ARGV[0] =~ /-h/) ) { print STDERR $usage; exit; }
if ( ! GetOptions('dbname:s'   => \$dbname,
		  'dbuser:s'   => \$dbuser,
		  'dbhost:s'   => \$dbhost,
		  "download=s" => \$dfile,
		  "source=s"   => \$sfile,
		  "verbose!"   => \$verb
		 ) ) {
  print STDERR $usage; exit;
}
unless ($sfile && (-s $sfile) && $dfile && (-s $dfile)) {
  print STDERR $usage; exit;
}

print "Initializing Babel DB\n" if ($verb); 
my ($dbh, $babel);
if ($dbname && $dbuser && $dbhost) {
  $dbh   = DBI->connect("DBI:Pg:dbname=$dbname;host=$dbhost", $dbuser, '');
  $babel = Babel::lib::Babel->new($dbh);
}
else {
  $babel = new Babel::lib::Babel;
  $dbh   = $babel->dbh();
}

my $data  = $babel->source_info_from_file($sfile);
my $down  = $babel->download_info_from_file($dfile);
my $srcs  = $babel->sources();
my $repos = {};

foreach my $name ( keys %$srcs ) {
  if (exists $data->{$name}) {
    my $src  = exists($data->{$name}{source})      ? $data->{$name}{source}      : '';
    my $vers = exists($data->{$name}{version})     ? $data->{$name}{version}     : '';
    my $desc = exists($data->{$name}{description}) ? $data->{$name}{description} : '';
    my $titl = exists($data->{$name}{title})       ? $data->{$name}{title}       : '';
    my $url  = exists($data->{$name}{url})         ? $data->{$name}{url}         : '';
    my $type = exists($data->{$name}{type})        ? $data->{$name}{type}        : '';
    my $link = exists($data->{$name}{link})        ? $data->{$name}{link}        : '';
    my $sql  = qq(UPDATE sources SET source='$src', version='$vers', description='$desc', url='$url', type='$type', title='$titl', link='$link' WHERE name='$name');
    if ($src && $url && $type) {
      $repos->{$src} = 1;
      if ($verb) { print "$sql\n"; }
      my $res = $dbh->do($sql);
      if (! $res) { print STDERR "Error updating sources: " . $dbh->error . "\n"; }
    }
  }
  else {
    print "ERROR: $name\n";
  }
}
foreach my $name ( keys %$repos ) {
  if (exists($down->{$name}) && (scalar(@{$down->{$name}}) > 0)) {
    my (@paths, @files);
    my $date = $down->{$name}[0]{download_date};
    foreach my $d ( @{$down->{$name}} ) {
      push @paths, $d->{download_path};
      push @files, $d->{download_file};
    }
    my $path = "{" . join(",", map {qq("$_")} @paths) . "}";
    my $file = "{" . join(",", map {qq("$_")} @files) . "}";
    my $sql  = qq(UPDATE sources SET download_path='$path', download_file='$file', download_date='$date' WHERE source='$name');

    if ($verb) { print "$sql\n"; }
    my $res = $dbh->do($sql);
    if (! $res) { print STDERR "Error updating sources: " . $dbh->error . "\n"; }
  }
}


MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3