IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
May 9, 2009, 2:19:48 PM (17 years ago)
Author:
bills
Message:

Pass information about the paths from the distribution server to the client.
Use these to edit the database dump

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/ippScripts/scripts/receive_file.pl

    r24102 r24125  
    1717use IPC::Cmd 0.36 qw( can_run run );
    1818use PS::IPP::Metadata::Config;
     19use PS::IPP::Metadata::List qw( parse_md_list );
    1920use PS::IPP::Config 1.01 qw( :standard );
    2021use File::Temp qw( tempfile );
     22use File::Basename qw( basename );
    2123use Carp;
    2224
     
    3638
    3739# Parse the command-line arguments
    38 my ( $file_id, $source, $product, $fileset, $fileset_id, $file, $component, $bytes, $md5sum, $workdir, $dbname, $verbose, $no_update, $save_temps );
     40my ( $file_id, $source, $product, $fileset, $fileset_id, $file, $component, $bytes, $md5sum, $workdir, $dirinfo_uri, $dbname, $verbose, $no_update, $save_temps );
    3941
    4042GetOptions(
     
    4951           'md5sum=s'          => \$md5sum, # md5sum for file from data store
    5052           'workdir=s'         => \$workdir, # Working directory for output
     53           'dirinfo=s'    => \$dirinfo_uri, # file containing the destination directories for this component
    5154           'dbname=s'          => \$dbname,    # Database name
    5255           'verbose'           => \$verbose,   # Print to stdout
     
    5659
    5760pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;
    58 pod2usage( -msg => "Required options: --file_id --source --product --fileset --file --workdir",
     61pod2usage( -msg => "Required options: --file_id --source --product --fileset --file --component --workdir --bytes --md5sum --dirinfo",
    5962           -exitval => $PS_EXIT_CONFIG_ERROR) unless
    6063    defined $file_id and
     
    6669    defined $bytes and
    6770    defined $md5sum and
     71    defined $dirinfo_uri and
    6872    defined $workdir;
    6973
    7074$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"));
    7178
    7279my $ipprc = PS::IPP::Config->new() or
    7380    &my_die( "Unable to set up", $file_id, $PS_EXIT_CONFIG_ERROR ); # IPP configuration
     81
     82my $mdcParser = PS::IPP::Metadata::Config->new;
     83
     84
    7485
    7586# Retrieve file
     
    8798my $mjd_copy = DateTime->now->mjd;   # MJD of finishing copy
    8899
     100# figure out which dirinfo file to read
     101my $dirinfo_file_to_read = $component eq "dirinfo" ? $filename : $dirinfo_uri;
     102
     103# process it
     104my ($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
     110my ($day, $month, $year) = (localtime)[3,4,5];
     111my $datestr = sprintf "%04d%02d%02d", $year+1900, $month + 1, $day;
     112my $dir_for_info_files = caturi($workdir, $datestr, $fileset);
     113
    89114# 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
     115if ($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";
    119134    $command .= " -dbname $dbname" if defined $dbname;
     135
    120136    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    121137        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 ?
    126257    # Get contents of tarball
    127258    my @files = ();
     
    151282    }
    152283
     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
    153295    # Move files into filesystem of choice
    154296    foreach my $file ( @files ) {
    155297        my $from = "$tempdir/$file"; # Source for file
    156         my $target = "$workdir/$file"; # Target destination for file
     298        my $target = "$target_dir/$file"; # Target destination for file
    157299        my $to = $ipprc->file_create( $target ); # Target for move
    158300        system("mv $from $to") == 0 or &my_die( "Unable to move $file into workdir $workdir: $!\n", $file_id, $PS_EXIT_UNKNOWN_ERROR);
     
    162304}
    163305
    164 unlink $filename or &my_die( "Unable to unlink $filename\n", $file_id, $PS_EXIT_UNKNOWN_ERROR);
     306if (!$save_temps) {
     307    unlink $filename or &my_die( "Unable to unlink $filename\n", $file_id, $PS_EXIT_UNKNOWN_ERROR);
     308}
    165309my $mjd_extract = DateTime->now->mjd;   # MJD of finishing extract
    166310
     
    186330# Pau.
    187331
     332sub 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}
    188360
    189361sub my_die
Note: See TracChangeset for help on using the changeset viewer.