Changeset 24125 for trunk/ippScripts/scripts/receive_file.pl
- Timestamp:
- May 9, 2009, 2:19:48 PM (17 years ago)
- File:
-
- 1 edited
-
trunk/ippScripts/scripts/receive_file.pl (modified) (9 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/ippScripts/scripts/receive_file.pl
r24102 r24125 17 17 use IPC::Cmd 0.36 qw( can_run run ); 18 18 use PS::IPP::Metadata::Config; 19 use PS::IPP::Metadata::List qw( parse_md_list ); 19 20 use PS::IPP::Config 1.01 qw( :standard ); 20 21 use File::Temp qw( tempfile ); 22 use File::Basename qw( basename ); 21 23 use Carp; 22 24 … … 36 38 37 39 # Parse the command-line arguments 38 my ( $file_id, $source, $product, $fileset, $fileset_id, $file, $component, $bytes, $md5sum, $workdir, $d bname, $verbose, $no_update, $save_temps );40 my ( $file_id, $source, $product, $fileset, $fileset_id, $file, $component, $bytes, $md5sum, $workdir, $dirinfo_uri, $dbname, $verbose, $no_update, $save_temps ); 39 41 40 42 GetOptions( … … 49 51 'md5sum=s' => \$md5sum, # md5sum for file from data store 50 52 'workdir=s' => \$workdir, # Working directory for output 53 'dirinfo=s' => \$dirinfo_uri, # file containing the destination directories for this component 51 54 'dbname=s' => \$dbname, # Database name 52 55 'verbose' => \$verbose, # Print to stdout … … 56 59 57 60 pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV; 58 pod2usage( -msg => "Required options: --file_id --source --product --fileset --file -- workdir",61 pod2usage( -msg => "Required options: --file_id --source --product --fileset --file --component --workdir --bytes --md5sum --dirinfo", 59 62 -exitval => $PS_EXIT_CONFIG_ERROR) unless 60 63 defined $file_id and … … 66 69 defined $bytes and 67 70 defined $md5sum and 71 defined $dirinfo_uri and 68 72 defined $workdir; 69 73 70 74 $tempdir .= "/$file_id"; 75 76 &my_die( "dirinfo is NULL for $component", $file_id, $PS_EXIT_CONFIG_ERROR ) 77 if (($dirinfo_uri eq "NULL") and ($component ne "dirinfo")); 71 78 72 79 my $ipprc = PS::IPP::Config->new() or 73 80 &my_die( "Unable to set up", $file_id, $PS_EXIT_CONFIG_ERROR ); # IPP configuration 81 82 my $mdcParser = PS::IPP::Metadata::Config->new; 83 84 74 85 75 86 # Retrieve file … … 87 98 my $mjd_copy = DateTime->now->mjd; # MJD of finishing copy 88 99 100 # figure out which dirinfo file to read 101 my $dirinfo_file_to_read = $component eq "dirinfo" ? $filename : $dirinfo_uri; 102 103 # process it 104 my ($destdir, $components, $dirinfo_lines) = read_dirinfo_file($dirinfo_file_to_read, $file_id); 105 106 # select a directory for the dirinfo and dbinfo files 107 # XXX: perhaps this directory should be set by the script and passed in 108 # rather than computed here. 109 110 my ($day, $month, $year) = (localtime)[3,4,5]; 111 my $datestr = sprintf "%04d%02d%02d", $year+1900, $month + 1, $day; 112 my $dir_for_info_files = caturi($workdir, $datestr, $fileset); 113 89 114 # Deal with file 90 if ($file =~ m|^dbinfo\.\S+\.mdc$|) { 91 # Load into database 92 93 my $target = "$workdir/$file"; # Target destination for file 94 my $fixName = $ipprc->file_create( $target ); # Target for move 95 my $fixFile; 96 97 open $fixFile, ">$fixName" or &my_die( "can't open $fixName\n", $file_id, $PS_EXIT_UNKNOWN_ERROR); 98 99 # Need to fix paths to point to new workdir 100 open my $inFile, $filename or &my_die( "Can't open $filename\n", $file_id, $PS_EXIT_UNKNOWN_ERROR); # Input file 101 my $workdir_old; # Old workdir 102 while (<$inFile>) { 103 # XXX This is a global approach to fixing the path: it should fix anything and everything, but won't 104 # work if there are multiple workdirs in a file and the bits are all mixed up. To cover that case, 105 # we should fix each of the elements (workdir, uri, path_base) separately. 106 if (m|^\s*workdir\s+STR\s+(\S+)|) { 107 $workdir_old = $1; 108 $workdir_old =~ s|\@HOST\@|\\S+|; 109 } 110 if (defined $workdir_old) { 111 s|$workdir_old|$workdir|; 112 } 113 print $fixFile $_; 114 } 115 close($inFile); 116 close($fixFile); 117 #{ 118 my $command = "$receivetool -updatefileset -fileset_id $fileset_id -dbinfo_uri $fixName"; # Command to execute 115 if ($component eq 'dirinfo') { 116 # save the dirinfo file contents into the $workdir 117 118 $dirinfo_uri = caturi($dir_for_info_files, basename($filename)); 119 print "dirinfo_uri: $dirinfo_uri\n" if $verbose; 120 121 my $resolved = $ipprc->file_resolve($dirinfo_uri, 'create'); 122 &my_die( "failed to resolve $dirinfo_uri\n", $file_id, $PS_EXIT_UNKNOWN_ERROR) if !$resolved; 123 124 open OUT, ">$resolved" 125 or &my_die( "failed to open $resolved\n", $file_id, $PS_EXIT_UNKNOWN_ERROR); 126 print OUT @$dirinfo_lines 127 or &my_die( "failed to write $resolved\n", $file_id, $PS_EXIT_UNKNOWN_ERROR); 128 close OUT 129 or &my_die( "failed to close $resolved\n", $file_id, $PS_EXIT_UNKNOWN_ERROR); 130 131 # update the fileset to allow processing of other files 132 my $command = "$receivetool -updatefileset -fileset_id $fileset_id"; 133 $command .= " -set_state new -dirinfo $dirinfo_uri"; 119 134 $command .= " -dbname $dbname" if defined $dbname; 135 120 136 my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) = 121 137 run(command => $command, verbose => $verbose); 122 &my_die( "Unable to set dbinfo_uri for $fileset_id to $fixName\n", $file_id, $PS_EXIT_UNKNOWN_ERROR) unless $success; 123 #} 124 125 } elsif ($file =~ m|.*\.tgz$|) { 138 &my_die( "Unable to update fileset $fileset_id to\n", $file_id, $PS_EXIT_UNKNOWN_ERROR) unless $success; 139 140 } elsif ($component eq "dbinfo") { 141 142 open INFILE, $filename or &my_die( "Can't open $filename\n", $file_id, $PS_EXIT_UNKNOWN_ERROR); 143 144 my @lines = (<INFILE>); 145 146 my $dbinfo = join "", @lines; 147 148 close INFILE; 149 150 my $dbinfo_uri = caturi($dir_for_info_files, basename($filename)); 151 print "dbinfo_uri: $dbinfo_uri\n" if $verbose; 152 153 my $resolved = $ipprc->file_resolve($dbinfo_uri, 'create'); 154 &my_die( "failed to resolve $dbinfo_uri\n", $file_id, $PS_EXIT_UNKNOWN_ERROR) if !$resolved; 155 156 open OUT, ">$resolved" 157 or &my_die( "failed to open $resolved\n", $file_id, $PS_EXIT_UNKNOWN_ERROR); 158 159 # We process the dbinfo file (the exported run from the distribution) line by line 160 # Rather than read it as an mdc and interptet it, we do our substitutions directly 161 # This is much faster. Parsing a mdc file for a chip run takes several seconds. 162 # we are very strict on the formatting 163 164 # first line tells us the run type. From this we get the stage 165 # 166 my $line = $lines[0]; 167 my ($runType, $multi) = split " ", $line; 168 &my_die( "unexpected first line found in $filename: $line\n", $file_id, $PS_EXIT_UNKNOWN_ERROR) 169 if !$runType or ($multi ne 'MULTI'); 170 my $stage; 171 my $comp_name; 172 my $current_component; 173 if ($runType eq 'rawExp') { 174 $stage = 'raw'; 175 $comp_name = 'class_id'; 176 } elsif ($runType eq 'chipRun') { 177 $stage = 'chip'; 178 $comp_name = 'class_id'; 179 } elsif ($runType eq 'camRun') { 180 $stage = 'camera'; 181 $comp_name = 'exposure'; 182 $current_component = $comp_name; 183 } elsif ($runType eq 'fakeRun') { 184 $stage = 'fake'; 185 $comp_name = 'class_id'; 186 } elsif ($runType eq 'warpRun') { 187 $stage = 'warp'; 188 $comp_name = 'skycell_id'; 189 } elsif ($runType eq 'diffRun') { 190 $stage = 'diff'; 191 $comp_name = 'skycell_id'; 192 } elsif ($runType eq 'stackRun') { 193 $stage = 'stack'; 194 $comp_name = 'skycell_id'; 195 } else { 196 &my_die( "unexpected run type line found in $filename: $runType\n", $file_id, $PS_EXIT_UNKNOWN_ERROR); 197 } 198 199 my $new_workdir_value; 200 if ($destdir eq 'none') { 201 $new_workdir_value = "$workdir"; 202 } else { 203 $new_workdir_value = "$workdir/$destdir"; 204 } 205 my $component_dir; 206 if ($current_component) { 207 $component_dir = $components->{$current_component}; 208 } 209 foreach $line (@lines) { 210 my $out_line = $line; 211 212 my ($name, $type, $value) = split " ", $line; 213 # we only edit complete lines 214 if ($name and $type and $value) { 215 my $new_value; 216 if ($name eq $comp_name) { 217 $current_component = $value; 218 $component_dir = $components->{$current_component}; 219 &my_die( "$component_dir is null for $value in $filename: $runType\n", 220 $file_id, $PS_EXIT_UNKNOWN_ERROR) if !$component_dir; 221 } elsif ($name eq 'workdir') { 222 $new_value = $new_workdir_value; 223 } elsif ($name eq 'tess_id') { 224 # for tess_id strip off any directories just keep the basename. 225 # The site configuration will need to map this to a proper location 226 # XXX: Document this 227 $new_value = basename($value); 228 } elsif ((($name eq 'uri') or ($name eq 'path_base')) and ($value ne 'NULL')) { 229 &my_die( "$component_dir is null and we need it for $name", 230 $file_id, $PS_EXIT_PROG_ERROR) if !$component_dir; 231 232 $new_value = caturi($new_workdir_value, $component_dir, basename($value)); 233 } 234 235 if ($new_value) { 236 $out_line = " " . $name . "\t\t" . $type . "\t" . $new_value . "\n"; 237 } 238 } 239 240 print OUT $out_line or &my_die( "failed to write to $resolved\n", $file_id, $PS_EXIT_UNKNOWN_ERROR); 241 } 242 243 close OUT 244 or &my_die( "failed to close $resolved\n", $file_id, $PS_EXIT_UNKNOWN_ERROR); 245 246 # update the fileset to allow processing of other files 247 my $command = "$receivetool -updatefileset -fileset_id $fileset_id"; 248 $command .= " -dbinfo $dbinfo_uri"; 249 $command .= " -dbname $dbname" if defined $dbname; 250 251 my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) = 252 run(command => $command, verbose => $verbose); 253 &my_die( "Unable to update fileset $fileset_id to\n", $file_id, $PS_EXIT_UNKNOWN_ERROR) unless $success; 254 255 256 } elsif ($file =~ m|.*\.tgz$|) { # XXX: perhaps get this off of file type ? 126 257 # Get contents of tarball 127 258 my @files = (); … … 151 282 } 152 283 284 my $component_dir = $components->{$component}; 285 &my_die( "Unable to find component_dir for $component $filename\n", $file_id, $PS_EXIT_UNKNOWN_ERROR) unless $component_dir; 286 287 my $target_dir; 288 if ($destdir eq 'none') { 289 $target_dir = "$workdir"; 290 } else { 291 $target_dir = "$workdir/$destdir"; 292 } 293 $target_dir .= "/$component_dir"; 294 153 295 # Move files into filesystem of choice 154 296 foreach my $file ( @files ) { 155 297 my $from = "$tempdir/$file"; # Source for file 156 my $target = "$ workdir/$file"; # Target destination for file298 my $target = "$target_dir/$file"; # Target destination for file 157 299 my $to = $ipprc->file_create( $target ); # Target for move 158 300 system("mv $from $to") == 0 or &my_die( "Unable to move $file into workdir $workdir: $!\n", $file_id, $PS_EXIT_UNKNOWN_ERROR); … … 162 304 } 163 305 164 unlink $filename or &my_die( "Unable to unlink $filename\n", $file_id, $PS_EXIT_UNKNOWN_ERROR); 306 if (!$save_temps) { 307 unlink $filename or &my_die( "Unable to unlink $filename\n", $file_id, $PS_EXIT_UNKNOWN_ERROR); 308 } 165 309 my $mjd_extract = DateTime->now->mjd; # MJD of finishing extract 166 310 … … 186 330 # Pau. 187 331 332 sub read_dirinfo_file 333 { 334 my $filename = shift; 335 my $file_id = shift; 336 337 open INFILE, $filename or &my_die( "Can't open $filename\n", $file_id, $PS_EXIT_UNKNOWN_ERROR); 338 339 my @lines = (<INFILE>); 340 341 my $dirinfo = join "", @lines; 342 343 close INFILE; 344 345 my $metadata = $mdcParser->parse($dirinfo) or 346 &my_die("Unable to parse metadata config doc", $file_id, $PS_EXIT_UNKNOWN_ERROR); 347 348 my $array = parse_md_list($metadata) or 349 &my_die("Unable to parse metadata list", $file_id, $PS_EXIT_UNKNOWN_ERROR); 350 351 my $dest_hash = $array->[0]; 352 353 my $destdir = $dest_hash->{destdir}; 354 &my_die("destdir not found in $filename", $file_id, $PS_EXIT_UNKNOWN_ERROR) if !$destdir; 355 356 my $components = $array->[1]; 357 358 return ($destdir, $components, \@lines); 359 } 188 360 189 361 sub my_die
Note:
See TracChangeset
for help on using the changeset viewer.
