Changeset 16761 for trunk/DataStoreServer/scripts
- Timestamp:
- Feb 29, 2008, 2:35:40 PM (18 years ago)
- File:
-
- 1 edited
-
trunk/DataStoreServer/scripts/dsreg (modified) (11 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/DataStoreServer/scripts/dsreg
r16690 r16761 1 1 #!/usr/bin/env perl 2 2 # 3 # Mock-up datastoreregistration3 # DataStore fileset registration 4 4 # 5 5 # Does not enforce validity of metadata items. … … 7 7 8 8 use strict; 9 use warnings; 9 10 10 11 use Getopt::Long qw( GetOptions ); 11 12 use Pod::Usage qw( pod2usage ); 12 13 use Digest::MD5::File qw( file_md5_hex ); 14 use DBI; 15 use PS::IPP::Metadata::Config; 16 use PS::IPP::Metadata::Stats; 17 use PS::IPP::Metadata::List qw( parse_md_list ); 18 19 use 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 ); 13 30 14 31 my $add; 15 32 my $del; 16 33 17 my $ds_dir = './dsroot'; 34 #my $ds_dir = './dsroot'; 35 my $ds_dir = '/var/www/html/ds/dsroot'; 18 36 my $product; 19 37 my $fileset; 20 38 39 ### XXX: these aren't currently used 21 40 ### fill in some bogus values here 22 41 my $type = 'OBJECT'; … … 34 53 # 35 54 55 # XXX: need to make argument parsing file type specific 36 56 GetOptions( 37 57 'add' => \$add, … … 44 64 'etime|ex|e=f' => \$etime, 45 65 'filter|f=s' => \$filter, 46 'date=s' => \$date,66 'date=s' => \$date, 47 67 'airmass|a=f' => \$airmass, 48 68 ) or pod2usage(2); 49 69 50 my $err ;70 my $err = ""; 51 71 52 72 $err .= "Must specify a product and a fileset.\n" 53 unless $#ARGV == 1;73 unless @ARGV == 2; 54 74 55 75 # 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" 57 77 unless $add xor $del; 58 78 59 79 $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; 61 81 62 82 # bail if any of the above args were improper … … 67 87 68 88 show_usage("Invalid product '$product'.") 69 unless stat("$ds_dir/$product/index.txt") != undef;89 unless defined stat("$ds_dir/$product/index.txt"); 70 90 71 91 $fileset = shift; … … 78 98 umask 0011; 79 99 100 my $ipprc = PS::IPP::Config->new(); # IPP Configuration 101 my $siteConfig = $ipprc->{_siteConfig}; 102 103 my $dbserver = metadataLookupStr($siteConfig, 'DBSERVER'); 104 my $dbname = metadataLookupStr($siteConfig, 'DBNAME'); 105 my $dbuser = metadataLookupStr($siteConfig, 'DBUSER'); 106 my $dbpass = metadataLookupStr($siteConfig, 'DBPASSWORD'); 107 exit ($PS_EXIT_CONFIG_ERROR) unless defined $dbserver and $dbname and $dbuser and $dbpass; 108 109 my $dsn = "DBI:mysql:host=$dbserver;database=$dbname"; 110 111 my $dbh = DBI->connect($dsn, $dbuser, $dbpass) or die "Cannot connect to server\n"; 112 80 113 81 114 # … … 83 116 # 84 117 if ($del) { 118 119 die("delete not implemented yet"); 120 85 121 if (stat("$ds_dir/$product/$fileset/index.txt") == undef) { 86 122 print "Fileset '$fileset' doesn't exist under $product.\n"; … … 178 214 179 215 # 180 # add fileset216 # addding a new fileset 181 217 # 182 218 else { 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) { 184 231 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 } 198 234 199 235 # Read file data from STDIN … … 201 237 my $lineno = 0; 202 238 239 my @files; 203 240 while (<STDIN>) { 204 241 $lineno++; 205 242 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"); 216 260 217 261 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"); 221 264 } 222 265 223 266 # 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 } 232 336 233 # Add reference to the new fileset to the product index234 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 fileset251 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 index273 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 280 337 print "Added $fileset to $product.\n"; 338 339 exit 0; 281 340 } 282 341 … … 290 349 pod2usage( 291 350 -msg => 292 "usage: $tmp[$#tmp] [- add|-del] product fileset [options]351 "usage: $tmp[$#tmp] [--add|--del] product fileset [options] 293 352 294 353 Commands: 295 354 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. 298 357 File information will be read per-line from STDIN, in the form: 299 358 fileID type chipname … … 301 360 Options: 302 361 303 - dsdir Specify the path to the Datastore (defaults to CWD).362 --dsdir Specify the path to the Datastore (defaults to CWD). 304 363 305 364 (The following metadata is optional because this is just a mock-up.) 306 365 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. 315 374 316 375 $str",
Note:
See TracChangeset
for help on using the changeset viewer.
