Changeset 16867
- Timestamp:
- Mar 6, 2008, 5:55:53 PM (18 years ago)
- File:
-
- 1 edited
-
trunk/DataStoreServer/scripts/dsreg (modified) (14 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/DataStoreServer/scripts/dsreg
r16765 r16867 1 1 #!/usr/bin/env perl 2 2 # 3 # DataStore fileset registration 4 # 5 # Does not enforce validity of metadata items. 3 # Data Store file set registration and de-registration 6 4 # 7 5 … … 29 27 caturi 30 28 ); 29 my $product; 30 my $fileset; 31 32 my $fstype; 33 my $ps0; 34 my $ps1; 35 my $ps2; 36 my $ps3; 37 my $ps4; 38 my $ps5; 39 my $ps6; 40 my $ps7; 41 42 my $linkfiles; # if set, put links to files in datapath in the Data Store 43 my $copyfiles; # if set, copy files from this directory to the Data Store fileset 44 my $datapath; # source for files required if $linkfiles or $copyfiles 45 46 my $ds_dir; 31 47 32 48 my $add; 33 49 my $del; 34 35 my $ds_dir; 36 my $product; 37 my $fileset; 38 39 ### XXX: these aren't currently used 40 ### fill in some bogus values here 41 my $type = 'OBJECT'; 42 my $ra = '00:00:00.00'; 43 my $dec = '00:00:00.00'; 44 my $equinox = 2000; 45 my $etime = 30.0; 46 my $filter = 'z'; 47 my $airmass = 1.2; 48 49 my $date; 50 my $remove; 51 52 my $verbosity = 1; 53 54 55 50 56 51 57 # … … 53 59 # 54 60 55 # XXX: need to make argument parsing file type specific56 61 GetOptions( 57 'add' => \$add, 58 'del' => \$del, 59 'dsdir|ds=s' => \$ds_dir, 60 'type|t=s' => \$type, 61 'ra|r=s' => \$ra, 62 'dec|d=s' => \$dec, 63 'equinox|eq=f' => \$equinox, 64 'etime|ex|e=f' => \$etime, 65 'filter|f=s' => \$filter, 66 'date=s' => \$date, 67 'airmass|a=f' => \$airmass, 62 'add' => \$add, 63 'del' => \$del, 64 'product=s' => \$product, 65 'fileset=s' => \$fileset, 66 'type=s' => \$fstype, 67 'datapath=s' => \$datapath, 68 'link' => \$linkfiles, 69 'copy' => \$copyfiles, 70 'rm' => \$remove, 71 'ps0=s' => \$ps0, # product specific columns 0 - 7 72 'ps1=s' => \$ps1, 73 'ps2=s' => \$ps2, 74 'ps3=s' => \$ps3, 75 'ps4=s' => \$ps4, 76 'ps5=s' => \$ps5, 77 'ps6=s' => \$ps6, 78 'ps7=s' => \$ps7 68 79 ) or pod2usage(2); 69 80 70 81 my $err = ""; 71 82 72 $err .= "Must specify a product and a fileset.\n" 73 unless @ARGV == 2; 83 $err .= "product and fileset are required\n" unless defined $product and $fileset; 74 84 75 85 # will exit if neither or both -add and -del were specified 76 86 $err .= "Must specify either --add or --del.\n" 77 87 unless $add xor $del; 88 89 if ($add) { 90 if (!$fstype) { 91 $err .= "need to specify fileset type to add\n"; 92 } 93 94 if ($linkfiles and $copyfiles) { 95 $err .= "only one of --link and --copy allowedn"; 96 } 97 if (($linkfiles or $copyfiles) and !$datapath) { 98 $err .= "need to specify datapath with --link or --copy\n"; 99 } 100 } 101 102 103 # 104 # lookup the location of the Data Store 105 # 78 106 79 107 my $ipprc = PS::IPP::Config->new(); # IPP Configuration … … 82 110 $ds_dir = metadataLookupStr($ipprc->{_siteConfig}, 'PSTAMP_DATA_STORE_ROOT'); 83 111 if (!$ds_dir) { 84 die(" data store root not defined");112 die("Data Store root directory not set"); 85 113 } 86 114 } … … 88 116 89 117 if (!stat("$ds_dir/index.txt")) { 90 $err .= "Data store not found at '$ds_dir'.\n"118 $err .= "Data Store not found at '$ds_dir'.\n" 91 119 } 92 120 … … 95 123 if ($err); 96 124 97 $product = shift;98 99 125 show_usage("Invalid product '$product'.") 100 126 unless defined stat("$ds_dir/$product/index.txt"); 101 102 $fileset = shift;103 104 105 #106 # run107 #108 127 109 128 my $dbserver = metadataLookupStr($siteConfig, 'DBSERVER'); … … 117 136 my $dbh = DBI->connect($dsn, $dbuser, $dbpass) or die "Cannot connect to server\n"; 118 137 119 my $index_script_name = "$ds_dir/$product/$fileset/index.txt"; 138 139 140 my $fileset_dir = "$ds_dir/$product/$fileset"; 141 my $index_script_name = "$fileset_dir/index.txt"; 120 142 121 143 if ($del) { … … 143 165 } 144 166 145 # zap the index script 146 unlink("$index_script_name"); 167 if ($remove) { 168 if (system "rm -r $fileset_dir") { 169 die("failed to remove $fileset_dir"); 170 } 171 } else { 172 # zap the index script 173 unlink("$index_script_name"); 174 } 147 175 148 176 my $fileset_id = $fs_row->{fileset_id}; … … 170 198 } else { 171 199 # 172 # add inga new fileset200 # add a new fileset 173 201 # 202 203 # make a string out of the product specifc column values provided 204 my $prodcolstr = make_prodcol_str($ps0, $ps1, $ps2, $ps3, $ps4, $ps5, $ps6, $ps7); 205 174 206 my $stmt = $dbh->prepare("SELECT prod_id FROM dsProduct WHERE prod_name = \'$product\'"); 175 207 $stmt->execute(); … … 198 230 my $filename; 199 231 my $filetype; 200 my $type_col_0; 201 if (/([A-Za-z0-9-_.]+)\s+([A-Za-z0-9-_.]+)\s+([A-Za-z0-9-_.]+)/) { 202 $filename = $1; 203 $filetype = $2; 204 $type_col_0 = $3; 205 } else { 206 unless (/([A-Za-z0-9-_.]+)\s+([A-Za-z0-9-_.]+)\s+/) { 232 my $ts_string = ""; 233 234 chomp; 235 my @fields = split /\|/; 236 if (@fields < 2) { 207 237 print STDERR "Line $lineno: ignored malformed input line.\n"; 208 238 next; 209 } 210 $filename = $1; 211 $filetype = $2; 212 } 213 214 my @finfo = stat("$ds_dir/$product/$fileset/$filename"); 239 } 240 $filename = shift @fields; 241 $filetype = shift @fields; 242 243 # 244 # save any type specific fields provided in the string to be added to the 245 # VALUES list for this file 246 my $nfields = @fields; 247 my $i; 248 for ($i=0; $i < $nfields; $i++) { 249 $ts_string .= ", \'$fields[$i]\'"; 250 } 251 for ($i=$nfields ; $i < 4; $i++) { 252 $ts_string .= ", NULL"; 253 } 254 255 my $path = "$datapath/$filename"; 256 my @finfo = stat("$path"); 215 257 216 258 unless (@finfo) { 217 259 die ("Line $lineno: referenced file " 218 ."($ ds_dir/$product/$fileset/$1)does not exist.\n");260 ."($path does not exist.\n"); 219 261 } 220 262 221 263 # Get MD5 sum 222 my $md5 = file_md5_hex( "$ds_dir/$product/$fileset/$filename");264 my $md5 = file_md5_hex($path); 223 265 224 266 my $hashref = { 225 'file' => $filename,226 'bytes' => $finfo[7],267 'file' => $filename, 268 'bytes' => $finfo[7], 227 269 'md5sum' => $md5, 228 270 'type' => $filetype, 229 't ype_col_0'=> $type_col_0271 'ts_string' => $ts_string 230 272 }; 231 273 232 274 push @files, $hashref; 233 275 } 234 235 276 236 277 if (@files == 0) { 237 278 die("empty filelist"); 238 279 } 239 240 # create the cgi script index file by making a copy of its parent's 280 281 # 282 # Prepare the fileset directory 283 if ($linkfiles or $copyfiles) { 284 ## create the fileset directory 285 if (! -e $fileset_dir) { 286 if (!mkdir $fileset_dir) { 287 die("failed trying to make fileset directory $fileset_dir"); 288 } 289 } 290 eval { 291 ## then copy or symlink the files into place 292 foreach my $ref (@files) { 293 my $src = "$datapath/$ref->{file}"; 294 my $dest = "$fileset_dir/$ref->{file}"; 295 296 if ($linkfiles) { 297 if (! symlink $src, $dest) { 298 die("failed trying to link $src to $dest"); 299 } 300 } else { 301 if (!copy($src, $dest)) { 302 die("copy($src, $dest) failed"); 303 } 304 } 305 } 306 }; 307 if ($@) { # an error occured 308 print STDERR "error preparing the fileset directory: $@\n"; 309 310 if (system "rm -r $fileset_dir") { 311 die("failed to remove $fileset_dir"); 312 } 313 314 exit $PS_EXIT_UNKNOWN_ERROR; 315 } 316 } 317 318 # create the cgi script index.txt by making a copy of its parent's 241 319 if (!copy("$ds_dir/$product/index.txt", $index_script_name)) { 242 die("failed to copy index script to file set");320 die("failed to copy index script to file set"); 243 321 } 244 322 … … 248 326 $dbh->{AutoCommit} = 0; 249 327 250 $count = $dbh->do("INSERT into dsFileset" . 251 " (prod_id, fileset_name, reg_time, type)" . 252 " VALUES($prod_id, \'$fileset\', UTC_TIMESTAMP(), 'PSRESULTS')"); 328 my $qstring = "INSERT into dsFileset" . 329 " (prod_id, fileset_name, reg_time, type," . 330 " prod_col_0, prod_col_1, prod_col_2, prod_col_3, prod_col_4, prod_col_5, " . 331 " prod_col_6, prod_col_7)" . 332 # note prodcolstr begins with a comma 333 " VALUES($prod_id, \'$fileset\', UTC_TIMESTAMP(), \'$fstype\' $prodcolstr)"; 334 $count = $dbh->do($qstring); 253 335 if ($count == 0E0) { 254 336 die("failed to insert $fileset"); … … 260 342 261 343 foreach my $ref (@files) { 262 my $query; 263 if ($ref->{type_col_0}) { 264 $query = "INSERT INTO dsFile" . 265 " (fileset_id, file_id, file_name, bytes, md5sum, type, type_col_0)" . 344 # note: ts_string has a leading comma 345 my $query = "INSERT INTO dsFile" . 346 347 " (fileset_id, file_id, file_name, bytes, md5sum, type, " . 348 " type_col_0, type_col_1, type_col_2, type_col_3)" . 349 266 350 " VALUES($fileset_id, 0, \'$ref->{file}\', $ref->{bytes}," . 267 " \'$ref->{md5sum}\', \'$ref->{type}\', \'$ref->{type_col_0}\')"; 268 } else { 269 $query = "INSERT INTO dsFile" . 270 " (fileset_id, file_id, file_name, bytes, md5sum, type)" . 271 " VALUES($fileset_id, 0, \'$ref->{file}\', $ref->{bytes}," . 272 " \'$ref->{md5sum}\', \'$ref->{type}\')"; 273 } 351 " \'$ref->{md5sum}\', \'$ref->{type}\' $ref->{ts_string} )"; 352 274 353 $count = $dbh->do($query); 275 354 … … 301 380 } 302 381 382 303 383 sub show_usage { 304 384 my $str = shift; … … 310 390 pod2usage( 311 391 -msg => 312 "usage: $tmp[$#tmp] [--add|--del] product fileset[options]392 "usage: $tmp[$#tmp] [--add|--del] --product prod_name --fileset fs_name --type fs_type [options] 313 393 314 394 Commands: 315 395 316 --del Remove a fileset.317 396 --add Add a new fileset with the given metadata options. 318 397 File information will be read per-line from STDIN, in the form: 319 fileID type [chipname] 398 399 fileID|type|ts0|ts1|ts2|ts3 400 320 401 These entries are read until EOF is sent. 402 (The type specific ts[0-3]fields are optional) 403 404 --del Remove a fileset. 405 321 406 Options: 322 323 --dsdir Specify the path to the Datastore 407 --link Link files from datapath to Data Store 408 --copy Copy files from datapath to Data Store 409 --datapath path to source files 410 --ps0 - ps7 Optional product specific data 411 --rm with --del remove the fileset directory from the Data Store 324 412 325 413 $str", 326 414 -exitval => 2 327 415 ); 328 329 if (0) { 330 my $saveString = "(The following metadata is not used yet) 331 332 --date ISO8601 date string. (File timestamp used if not specified.) 333 --type Fileset type. 334 --ra Telescope pointing RA. 335 --dec Telescope pointing Dec. 336 --equinox Equinox. 337 --etime Exposure time. 338 --filter Filter used. 339 --airmass Air mass. "; 340 } 341 } 342 343 # get the current UTC time in iso8601 format 344 # this is used if no time parameter is specified 345 sub get_iso8601() { 346 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = 347 gmtime(time); 348 349 return sprintf("%4d-%02d-%02dT%02d:%02d:%02dZ", 350 $year+1900, $mon+1, $mday, $hour, $min, $sec); 351 } 416 } 417 418 sub make_prodcol_str { 419 420 my @list = @_; 421 my $string = ""; 422 423 foreach my $s (@list) { 424 if ($s) { 425 $string .= ", \'$s\'"; 426 } else { 427 $string .= ", NULL"; 428 } 429 } 430 431 return $string; 432 }
Note:
See TracChangeset
for help on using the changeset viewer.
