IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Feb 29, 2008, 2:35:40 PM (18 years ago)
Author:
bills
Message:

converted --add to work with data base based DataStore

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/DataStoreServer/scripts/dsreg

    r16690 r16761  
    11#!/usr/bin/env perl
    22#
    3 # Mock-up datastore registration
     3# DataStore fileset registration
    44#
    55# Does not enforce validity of metadata items.
     
    77
    88use strict;
     9use warnings;
    910
    1011use Getopt::Long qw( GetOptions );
    1112use Pod::Usage qw( pod2usage );
    1213use Digest::MD5::File qw( file_md5_hex );
     14use DBI;
     15use PS::IPP::Metadata::Config;
     16use PS::IPP::Metadata::Stats;
     17use PS::IPP::Metadata::List qw( parse_md_list );
     18
     19use PS::IPP::Config qw($PS_EXIT_SUCCESS
     20                       $PS_EXIT_UNKNOWN_ERROR
     21                       $PS_EXIT_SYS_ERROR
     22                       $PS_EXIT_CONFIG_ERROR
     23                       $PS_EXIT_PROG_ERROR
     24                       $PS_EXIT_DATA_ERROR
     25                       $PS_EXIT_TIMEOUT_ERROR
     26                       metadataLookupStr
     27                       metadataLookupBool
     28                       caturi
     29                       );
    1330
    1431my $add;
    1532my $del;
    1633
    17 my $ds_dir = './dsroot';
     34#my $ds_dir = './dsroot';
     35my $ds_dir = '/var/www/html/ds/dsroot';
    1836my $product;
    1937my $fileset;
    2038
     39### XXX: these aren't currently used
    2140### fill in some bogus values here
    2241my $type = 'OBJECT';
     
    3453#
    3554
     55# XXX: need to make argument parsing file type specific
    3656GetOptions(
    3757    'add'               => \$add,
     
    4464    'etime|ex|e=f'      => \$etime,
    4565    'filter|f=s'        => \$filter,
    46         'date=s'                        => \$date,
     66        'date=s'        => \$date,
    4767    'airmass|a=f'       => \$airmass,
    4868) or pod2usage(2);
    4969
    50 my $err;
     70my $err = "";
    5171
    5272$err .= "Must specify a product and a fileset.\n"
    53     unless $#ARGV == 1;
     73    unless @ARGV == 2;
    5474
    5575# will exit if neither or both -add and -del were specified
    56 $err .= "Must specify either -add or -del.\n"
     76$err .= "Must specify either --add or --del.\n"
    5777    unless $add xor $del;
    5878
    5979$err .= "Datastore not found at '$ds_dir'.\n"
    60     if stat("$ds_dir/index.txt") == undef;
     80    if stat("$ds_dir/index.txt") eq undef;
    6181
    6282# bail if any of the above args were improper
     
    6787
    6888show_usage("Invalid product '$product'.")
    69     unless stat("$ds_dir/$product/index.txt") != undef;
     89    unless defined stat("$ds_dir/$product/index.txt");
    7090
    7191$fileset = shift;
     
    7898umask 0011;
    7999
     100my $ipprc =  PS::IPP::Config->new(); # IPP Configuration
     101my $siteConfig = $ipprc->{_siteConfig};
     102
     103my $dbserver = metadataLookupStr($siteConfig, 'DBSERVER');
     104my $dbname   = metadataLookupStr($siteConfig, 'DBNAME');
     105my $dbuser   = metadataLookupStr($siteConfig, 'DBUSER');
     106my $dbpass   = metadataLookupStr($siteConfig, 'DBPASSWORD');
     107exit ($PS_EXIT_CONFIG_ERROR) unless defined $dbserver and $dbname and $dbuser and $dbpass;
     108
     109my $dsn = "DBI:mysql:host=$dbserver;database=$dbname";
     110
     111my $dbh = DBI->connect($dsn, $dbuser, $dbpass) or die "Cannot connect to server\n";
     112
    80113
    81114#
     
    83116#
    84117if ($del) {
     118
     119    die("delete not implemented yet");
     120
    85121    if (stat("$ds_dir/$product/$fileset/index.txt") == undef) {
    86122        print "Fileset '$fileset' doesn't exist under $product.\n";
     
    178214
    179215#
    180 # add fileset
     216# addding a new fileset
    181217#
    182218else {
    183     unless (stat("$ds_dir/$product/$fileset/index.txt") == undef) {
     219    my $stmt = $dbh->prepare("SELECT prod_id FROM dsProduct WHERE prod_name = \'$product\'");
     220    $stmt->execute();
     221    my $row = $stmt->fetchrow_hashref();
     222    my $prod_id = $row->{prod_id};
     223
     224    if (!$prod_id) {
     225        die("product $product not found");
     226    }
     227    my $count = $dbh->do("SELECT fileset_id FROM dsFileset WHERE fileset_name = '$fileset'" .
     228                        " AND prod_id = $prod_id");
     229
     230    if ($count != 0E0) {
    184231        print "Fileset '$fileset' already exists under $product.\n";
    185         exit(4);
    186     }
    187 
    188    
    189         # Create the fileset index
    190 
    191     open(IO, ">", "$ds_dir/$product/$fileset/index.txt")
    192         or die("Failed to open '$ds_dir/$product/$fileset/index.txt' for writing");
    193     print IO "# fileID      |bytes   |md5sum                          |type|chipname\n";
    194    
    195     #print "-- Send file list with lines of the form 'fileID type chipname' "
    196     #    ."followed by EOF.\n";
    197 
     232        exit 1;
     233    }
    198234
    199235    # Read file data from STDIN
     
    201237    my $lineno = 0;
    202238
     239    my @files;
    203240    while (<STDIN>) {
    204241        $lineno++;
    205242       
    206         unless (/([A-Za-z0-9-_.]+)\s+([A-Za-z0-9-_.]+)\s+([A-Za-z0-9-_.]+)/) {
    207             print "Line $lineno: ignored malformed input line.\n";
    208             next;
    209         }
    210 
    211         # $1 = file id
    212         # $2 = type
    213         # $3 = chip
    214 
    215         my @finfo = stat("$ds_dir/$product/$fileset/$1");
     243        my $filename;
     244        my $filetype;
     245        my $type_col_0;
     246        if (/([A-Za-z0-9-_.]+)\s+([A-Za-z0-9-_.]+)\s+([A-Za-z0-9-_.]+)/) {
     247            $filename = $1;
     248            $filetype = $2;
     249            $type_col_0 = $3;
     250        } else {
     251            unless (/([A-Za-z0-9-_.]+)\s+([A-Za-z0-9-_.]+)\s+/) {
     252                print STDERR "Line $lineno: ignored malformed input line.\n";
     253                next;
     254            }
     255            $filename = $1;
     256            $filetype = $2;
     257        }
     258
     259        my @finfo = stat("$ds_dir/$product/$fileset/$filename");
    216260
    217261        unless (@finfo) {
    218             print "Line $lineno: referenced file "
    219                 ."($ds_dir/$product/$fileset/$1) does not exist.\n";
    220             next;
     262            die ("Line $lineno: referenced file "
     263                ."($ds_dir/$product/$fileset/$1) does not exist.\n");
    221264        }
    222265       
    223266        # Get MD5 sum
    224         my $md5 = file_md5_hex("$ds_dir/$product/$fileset/$1");
    225 
    226         # Write line to the fileset index
    227         print IO "$1|$finfo[7]|$md5|$2|$3\n";
    228     }
    229 
    230     close(IO);
    231    
     267        my $md5 = file_md5_hex("$ds_dir/$product/$fileset/$filename");
     268
     269        my $hashref = {
     270            'file' => $filename,
     271            'bytes' => $finfo[7],
     272            'md5sum'    => $md5,
     273            'type'      => $filetype,
     274            'type_col_0'=> $type_col_0
     275        };
     276
     277        push @files, $hashref;
     278    }
     279
     280
     281    if (@files == 0) {
     282        die("empty filelist");
     283    }
     284
     285    eval {
     286        $dbh->{RaiseError} = 1; # raise exception if error occurs
     287        $dbh->{PrintError} = 0;
     288        $dbh->{AutoCommit} = 0;
     289
     290        $count = $dbh->do("INSERT into dsFileset" .
     291                " (prod_id, fileset_name, reg_time, type)" .
     292                " VALUES($prod_id, \'$fileset\', UTC_TIMESTAMP(), 'PSRESULTS')");
     293        if ($count == 0E0) {
     294            die("failed to insert $fileset");
     295        }
     296
     297        my $fileset_id = $dbh->last_insert_id(undef, undef, undef, undef);
     298
     299        # print STDERR "Inserted fileset_id $fileset_id\n";
     300
     301        foreach my $ref (@files) {
     302            my $query;
     303            if ($ref->{type_col_0}) {
     304                $query = "INSERT INTO dsFile" .
     305                    " (fileset_id, file_id, file_name, bytes, md5sum, type, type_col_0)" .
     306                    " VALUES($fileset_id, 0, \'$ref->{file}\', $ref->{bytes}," .
     307                             " \'$ref->{md5sum}\', \'$ref->{type}\', \'$ref->{type_col_0}\')";
     308            } else {
     309                $query = "INSERT INTO dsFile" .
     310                    " (fileset_id, file_id, file_name, bytes, md5sum, type)" .
     311                    " VALUES($fileset_id, 0, \'$ref->{file}\', $ref->{bytes}," .
     312                             " \'$ref->{md5sum}\', \'$ref->{type}\')";
     313            }
     314            $count = $dbh->do($query);
     315
     316            if ($count == 0E0) {
     317                $dbh->rollback();
     318                die("failed to insert $ref->{file} into $fileset");
     319            }
     320        }
     321
     322        $count = $dbh->do("UPDATE dsProduct SET last_update = UTC_TIMESTAMP(), last_fs = \'$fileset\'"
     323            . " WHERE prod_id = $prod_id");
     324        if ($count == 0E0) {
     325            $dbh->rollback();
     326            die("failed to update dsProduct $prod_id");
     327        }
     328
     329        $dbh->commit();
     330    };
     331    if ($@) { # an error occured
     332        print STDERR "transactionfailed, rolling back error was:\n$@\n";
     333        eval {$dbh->rollback();};
     334        exit 1;
     335    }
    232336       
    233     # Add reference to the new fileset to the product index
    234 
    235         $date = get_iso8601() if not defined($date);
    236 
    237         #FIXME this should probably be inserted in order!
    238 
    239     my $indexstr =
    240         "$fileset|$date|$type|$ra  $dec $equinox|$etime|$filter|$airmass";
    241 
    242     #open(IO, ">>", "$ds_dir/$product/index.txt")
    243     #    or die("Failed to open $ds_dir/$product/index.txt\n");
    244     open(IO, ">>", "$ds_dir/$product/index.lst")
    245         or die("Failed to open $ds_dir/$product/index.lst\n");
    246     print IO $indexstr, "\n";
    247     close(IO);
    248 
    249 
    250      # Update the root listing to reflect the new most-recent fileset
    251 
    252     open(IO, "$ds_dir/index.txt")
    253         or die("Failed to open '$ds_dir/index.txt' for reading\n");
    254 
    255     my $buf;
    256 
    257     while (<IO>) {
    258         my ($prod, $oldrecent, $oldtime, $extra) = split(/\|/, $_, 4);
    259         $prod =~ s/^\s+|\s+$//;
    260        
    261         if ($prod ne $product) {
    262             $buf .= $_;
    263             next;
    264         }
    265 
    266         $buf .= "$prod|$fileset|$date|$extra";
    267     }
    268 
    269     close(IO);
    270 
    271 
    272     # Write the new index
    273 
    274     open(IO, ">", "$ds_dir/index.txt")
    275         or die("Failed to open '$ds_dir/index.txt' for writing\n");
    276        
    277     print IO $buf;
    278     close(IO);
    279    
    280337    print "Added $fileset to $product.\n";
     338
     339    exit 0;
    281340}
    282341
     
    290349    pod2usage(
    291350        -msg =>
    292 "usage: $tmp[$#tmp] [-add|-del] product fileset [options]
     351"usage: $tmp[$#tmp] [--add|--del] product fileset [options]
    293352
    294353Commands:
    295354
    296     -del        Remove a fileset.
    297     -add        Add a new fileset with the given metadata options.
     355    --del        Remove a fileset.
     356    --add        Add a new fileset with the given metadata options.
    298357                File information will be read per-line from STDIN, in the form:
    299358                    fileID type chipname
     
    301360Options:
    302361   
    303     -dsdir      Specify the path to the Datastore (defaults to CWD).
     362    --dsdir      Specify the path to the Datastore (defaults to CWD).
    304363   
    305364 (The following metadata is optional because this is just a mock-up.)
    306365
    307     -date       ISO8601 date string. (File timestamp used if not specified.)
    308     -type       Fileset type.
    309     -ra         Telescope pointing RA.
    310     -dec        Telescope pointing Dec.
    311     -equinox    Equinox.
    312     -etime      Exposure time.
    313     -filter     Filter used.
    314     -airmass    Air mass.
     366    --date       ISO8601 date string. (File timestamp used if not specified.)
     367    --type       Fileset type.
     368    --ra         Telescope pointing RA.
     369    --dec        Telescope pointing Dec.
     370    --equinox    Equinox.
     371    --etime      Exposure time.
     372    --filter     Filter used.
     373    --airmass    Air mass.
    315374   
    316375$str",
Note: See TracChangeset for help on using the changeset viewer.