IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 33618


Ignore:
Timestamp:
Mar 28, 2012, 3:24:00 PM (14 years ago)
Author:
bills
Message:

Third draft. Reworked order of checks to be more efficient
and the logic to make it easier to follow. It's quite a labyrinth

File:
1 edited

Legend:

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

    r33602 r33618  
    2121use File::Temp qw( tempfile );
    2222use File::Basename qw( basename dirname );
     23use File::Copy;
    2324use PS::IPP::Metadata::Config;
    2425use PS::IPP::Metadata::List qw( parse_md_list );
     
    3334my $missing_tools;
    3435my $magicdstool   = can_run('magicdstool') or (warn "Can't find magicdstool" and $missing_tools = 1);
    35 my $isdestreaked = can_run('isdestreaked') or (warn "Can't find isdestreaked" and $missing_tools = 1);
    36 my $ppConfigDump = can_run('ppConfigDump') or (warn "Can't find ppConfigDump" and $missing_tools = 1);
     36my $chiptool   = can_run('chiptool') or (warn "Can't find chiptool" and $missing_tools = 1);
     37my $checkfits   = can_run('checkfits') or (warn "Can't find checkfits" and $missing_tools = 1);
     38my $ftable   = can_run('ftable') or (warn "Can't find ftable" and $missing_tools = 1);
     39my $censorObjects   = can_run('censorObjects') or (warn "Can't find censorObjects" and $missing_tools = 1);
    3740if ($missing_tools) {
    3841    warn("Can't find required tools.");
     
    4144
    4245# Parse the command-line arguments
    43 my ($magic_ds_id, $camera, $streaks, $stage, $stage_id, $component, $uri, $path_base);
     46my ($magic_ds_id, $camera, $streaks, $cam_id, $uri, $path_base);
    4447my ($outroot, $release, $bytes, $md5sum);
    4548my ($dbname, $save_temps, $verbose, $no_update, $no_op, $logfile);
     
    4750GetOptions(
    4851           'magic_ds_id=s'  => \$magic_ds_id,# Magic destreak run identifier
    49            'camera=s'       => \$camera,     # camera for evaluating file rules
    50            'stage=s'        => \$stage,      # raw, chip, warp, or diff
    51            'stage_id=s'     => \$stage_id,   # exp_id, chip_id, warp_id, or diff_id
     52           'camera=s'       => \$camera,  # path_base of the input
     53           'cam_id=s'       => \$cam_id,
    5254           'path_base=s'    => \$path_base,  # path_base of the input
    5355           'dbname=s'       => \$dbname,     # Database name
     
    5961
    6062pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;
    61 pod2usage( -msg => "Required options: --magic_ds_id --camera --stage --stage_id --component --path_base",
     63pod2usage( -msg => "Required options: --magic_ds_id --camera --cam_id --path_base",
    6264           -exitval => 3) unless
    6365    defined $magic_ds_id and
    6466    defined $camera and
    65     defined $stage and
    66     defined $stage_id and
     67    defined $cam_id and
    6768    defined $path_base;
    6869
    69 $component = "exposure";
    70 
    7170if ($camera ne 'GPC1') {
    72     my_die("camera must be GPC1 not $camera ",  $magic_ds_id, $component, $PS_EXIT_PROG_ERROR);
    73 }
    74 
    75 if ($stage ne 'camera') {
    76     my_die("stage must be camera",  $magic_ds_id, $component, $PS_EXIT_PROG_ERROR);
    77 }
    78 
    79 my $ipprc = PS::IPP::Config->new( $camera ) or my_die( "Unable to set up", $magic_ds_id, $component, $PS_EXIT_CONFIG_ERROR ); # IPP configuration
    80 
    81 $ipprc->redirect_to_logfile($logfile) or my_die( "Unable to redirect output", $magic_ds_id, $component, $PS_EXIT_SYS_ERROR ) if $logfile;
     71    my_die("camera must be GPC1 not $camera ",  $magic_ds_id, $PS_EXIT_PROG_ERROR);
     72}
     73
     74my $ipprc = PS::IPP::Config->new( $camera ) or my_die( "Unable to set up", $magic_ds_id, $PS_EXIT_CONFIG_ERROR ); # IPP configuration
     75
     76$ipprc->redirect_to_logfile($logfile) or my_die( "Unable to redirect output", $magic_ds_id, $PS_EXIT_SYS_ERROR ) if $logfile;
    8277
    8378my $nebulousServer = $ENV{NEB_SERVER};
    84 &my_die("cannot find NEB_SERVER in environment", $magic_ds_id, $component, $PS_EXIT_CONFIG_ERROR) if !$nebulousServer;
     79&my_die("cannot find NEB_SERVER in environment", $magic_ds_id, $PS_EXIT_CONFIG_ERROR) if !$nebulousServer;
    8580
    8681my $nebulous = eval { Nebulous::Client->new( proxy => $nebulousServer ); };
    8782if ($@ or not defined $nebulous) {
    88     &my_die ("Unable to create a Nebulous::Client object with proxy $nebulousServer", $magic_ds_id, $component, $PS_EXIT_CONFIG_ERROR);
     83    &my_die ("Unable to create a Nebulous::Client object with proxy $nebulousServer", $magic_ds_id, $PS_EXIT_CONFIG_ERROR);
    8984}
    9085
     
    9590my $nebulousInput = inNebulous($dirname);
    9691
    97 &my_die ("$dirname is not in nebulous", $magic_ds_id, $component, $PS_EXIT_CONFIG_ERROR) unless $nebulousInput;
     92&my_die ("$dirname is not in nebulous", $magic_ds_id, $PS_EXIT_CONFIG_ERROR) unless $nebulousInput;
    9893
    9994my $backup_path_base = $dirname   . "/SR_" . "$basename";
     
    10398my $bastrom = $ipprc->filename("PSASTRO.OUTPUT", $backup_path_base);
    10499
    105 print "swap $bastrom and $astrom\n";
    106100restore_file($astrom, $bastrom, 1) or
    107         &my_die("failed to restore smf file", $magic_ds_id, $component, $PS_EXIT_CONFIG_ERROR);
     101        &my_die("failed to restore smf file", $magic_ds_id, $PS_EXIT_CONFIG_ERROR);
     102
     103# Now restore the mask files
    108104
    109105my @otas = qw(XY01 XY02 XY03 XY04 XY05 XY06 XY10 XY11 XY12 XY13 XY14 XY15 XY16 XY17 XY20 XY21 XY22 XY23 XY24 XY25 XY26 XY27 XY30 XY31 XY32 XY33 XY34 XY35 XY36 XY37 XY40 XY41 XY42 XY43 XY44 XY45 XY46 XY47 XY50 XY51 XY52 XY53 XY54 XY55 XY56 XY57 XY60 XY61 XY62 XY63 XY64 XY65 XY66 XY67 XY71 XY72 XY73 XY74 XY75 XY76);
    110106
    111 # go fix the mask files
    112107foreach my $class_id (@otas) {
    113108    my $mask  = "$path_base.$class_id.mk.fits";
    114109    my $bmask = "$backup_path_base.$class_id.mk.fits";
    115110    my $rmask = "$recovery_path_base.$class_id.mk.fits";
    116     restore_file($mask, $bmask, 0);
    117 #  or        &my_die("failed to restore $mask", $magic_ds_id, $component, $PS_EXIT_CONFIG_ERROR);
     111    if (!restore_file($mask, $bmask, 0)) {
     112        # no good mask file. Mark any chips that need this mask file to be useful as bad
     113        # XXX: what about chipRuns with multiple camRuns can't we preserve those?
     114        my $command = "$chiptool -dropprocessedimfile -cam_id $cam_id -class_id $class_id -set_quality 45";
     115        $command   .= " -dbname $dbname" if defined $dbname;
     116        unless ($no_update) {
     117            my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     118                run(command => $command, verbose => $verbose);
     119            unless ($success) {
     120                $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     121                &my_die("Unable to perform magicdstool -addresult: $error_code", $magic_ds_id,
     122                    $error_code);
     123            }
     124        } else {
     125            print "Skipping command: $command\n";
     126        }
     127    }
    118128}
    119129
    120130{
    121     # this sets the camRun's magicked value back to zero
     131    # this sets the camRun's magicked value back to zero and deletes the magicDSFile
     132    # row from the database
    122133    my $command = "$magicdstool -revertdestreakedfile -i_am_sure";
    123134    $command   .= " -state goto_restored";
    124135    $command   .= " -magic_ds_id $magic_ds_id";
    125     $command   .= " -component $component";
     136    $command   .= " -component exposure";
    126137    $command   .= " -dbname $dbname" if defined $dbname;
    127138
     
    132143        unless ($success) {
    133144            $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
    134             &my_die("Unable to perform magicdstool -addresult: $error_code", $magic_ds_id, $component,
     145            &my_die("Unable to perform magicdstool -addresult: $error_code", $magic_ds_id,
    135146                $error_code);
    136147        }
     
    139150    }
    140151}
     152{
     153    # this sets the magicDSRun.state to restored
     154    my $command = "$magicdstool -updaterun";
     155    $command   .= " -set_state restored";
     156    $command   .= " -magic_ds_id $magic_ds_id";
     157    $command   .= " -dbname $dbname" if defined $dbname;
     158
     159    # Add the processed file to the database
     160    unless ($no_update) {
     161        my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     162            run(command => $command, verbose => $verbose);
     163        unless ($success) {
     164            $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     165            &my_die("Unable to perform magicdstool -addresult: $error_code", $magic_ds_id,
     166                $error_code);
     167        }
     168    } else {
     169        print "Skipping command: $command\n";
     170    }
     171}
    141172exit 0;
    142173
    143174### Pau.
     175
    144176sub restore_file {
    145177    my $original_url = shift;
     
    147179    my $smf = shift;
    148180
    149     my $original = check_file($original_url, 1);
     181    print "\nStarting restore for $original_url\n";
     182
     183    # It is the backup that we expect to want to use, check it first.
     184    my $backup = check_file($backup_url, 1, $smf);
     185
     186    # Is backup a good file whether censored or not?
     187    my $backup_ok = ($backup->{so_exists} and $backup->{num_instances});
     188   
     189    # Check original, skip full check if backup is ok and uncensored since the backup
     190    # is the one that we want to keep.
     191    my $original = check_file($original_url, (!$backup_ok or $backup->{censored}), $smf);
    150192
    151193    my $swap_urls = 0;
     
    153195    my $replicate = 0;
    154196
    155     if ($original->{so_exists}) {
    156         if ($original->{num_instances} == 0 or $original->{censored}) {
    157             # expected case: censored original
    158             # check the status of the backup
    159             my $backup = check_file($backup_url, 1);
    160             if ($backup->{so_exists}) {
    161                 if (!$backup->{censored}) {
    162                     # expected case backup is uncensored
    163                     if ($backup->{inconsistent}) {
    164                         if (!repair_file($backup), $smf) {
    165                             print STDERR "failed to repair uncensored backup file $backup_url\n";
    166                             return 0;
    167                         }
    168                     }
    169                     # All systems go
     197    if ($backup_ok and !$backup->{censored}) {
     198        #
     199        # Normal case. A good uncensored backup file is available
     200        #
     201        if ($backup->{inconsistent}) {
     202            # At least one of the instances was bad. Repair.
     203            repair_file($backup, $smf) or
     204                &my_die( "failed to repair $backup_url", $magic_ds_id, $PS_EXIT_CONFIG_ERROR);
     205        }
     206           
     207        $replicate = $backup->{num_instances} < 2;
     208
     209        if ($original->{so_exists}) {
     210            #
     211            # Normal case.
     212            #
     213            $swap_urls = 1;
     214            $kill_censored = 1;
     215        } else {
     216            # no file at original url rename the backup
     217            # note: rename_file prints a message and dies on failure
     218            rename_file($backup_url, $original_url);
     219        }
     220    } else {
     221        # Something is wrong with the backup file, either not ok or censored
     222        # check the original
     223        my $original_ok = ($original->{so_exists} and $original->{num_instances});
     224        if ($original_ok) {
     225            if ($original->{inconsistent}) {
     226                repair_file($original, $smf) or
     227                    &my_die( "failed to repair $original_url", $magic_ds_id, $PS_EXIT_CONFIG_ERROR);
     228            }
     229            if (!$original->{censored}) {
     230                # Note this is where we end up if this file has already been restored
     231                print "Keeping uncensored file at original url: $original_url\n";
     232            } else {
     233                print "No uncensored backup. Keeping censored file at original url: $original_url\n";
     234            }
     235            $kill_censored = $backup->{so_exists};
     236            $replicate = $original->{num_instances} < 2;
     237        }  else {
     238            # Original is bad. Is the backup at least a good file. If so swap it back in even if it is censored.
     239            if ($backup_ok) {
     240                print "No file found at $original_url\n";
     241                print "Swapping in      $backup_url\n";
     242                if ($backup->{inconsistent}) {
     243                    repair_file($backup, $smf) or
     244                        &my_die( "failed to repair $backup_url", $magic_ds_id, $PS_EXIT_CONFIG_ERROR);
     245                }
     246                $replicate = $backup->{num_instances} < 2;
     247                if ($original->{so_exists}) {
    170248                    $swap_urls = 1;
    171249                    $kill_censored = 1;
    172                     $replicate = $backup->{num_instances} < 2;
    173250                } else {
    174                     # user intervention required
    175                     print STDERR "both original and backup url point to censored file names\n";
    176                     my $name = $original->{instances}->[0];
    177                     print STDERR "    original: $name\n";
    178                     $name = $backup->{instances}->[0];
    179                     print STDERR "    backup:   $name\n";
    180                     return 0;
     251                    # note: rename_file prints a message and dies on failure
     252                    rename_file($backup_url, $original_url);
    181253                }
    182254            } else {
    183                 print STDERR "no backup file exists: $backup_url\n";
     255                # we're out of luck
     256                print "No good file for $original_url found\n";
    184257                return 0;
    185258            }
    186         } else {
    187             # original exists but does not appear to be censored. Just have a quick look at the backup
    188             my $backup = check_file($backup_url, 0);
    189             if ($backup->{so_exists} and $backup->{num_instances} and !$backup->{censored}) {
    190                 # user intervention required
    191                 print STDERR "both original and backup url point to un-censored file names\n";
    192                 my $name = $original->{instances}->[0];
    193                 print STDERR "    original: $name\n";
    194                 $name = $backup->{instances}->[0];
    195                 print STDERR "    backup:   $name\n";
    196                 return 0;
    197             }
    198             if ($original->{inconsistent} or scalar @{$file->{bad_instances}} > 0) {
    199                 if (!repair_file($original, $smf)) {
    200                     print STDERR "failed to repair uncensored original file $original_url\n";
    201                     return 0;
    202                 }
    203             }
    204             $kill_censored = 1;
    205             $replicate = $original->{num_instances} < 2;
    206         }
    207     } else {
    208         # no original storage object exists
    209         # We could check for a backup and rename it but this should be a very unusual case
    210         print STDERR "No storage object found for $original_url\n";
    211         return 0;
    212     }
     259        }
     260    }
     261
     262    # Ok Now we know what we need to do so do it
    213263
    214264    if (!$no_op) {
     
    217267                print "failed to swap $backup_url\n";
    218268                print "            to $original_url\n";
    219                 &my_die( "aborting", $magic_ds_id, $component, $PS_EXIT_CONFIG_ERROR);
     269                &my_die( "aborting", $magic_ds_id, $PS_EXIT_CONFIG_ERROR);
    220270            } else {
    221271                print "restored $original_url\n";
     
    227277        }
    228278
    229         if (!$replicate) {
     279        if ($replicate) {
    230280            $ipprc->replicate_file($original_url)
    231                 or &my_die( "failed to replicate $original_url", $magic_ds_id, $component, $PS_EXIT_CONFIG_ERROR);
     281                or &my_die( "failed to replicate $original_url", $magic_ds_id, $PS_EXIT_CONFIG_ERROR);
    232282        }
    233283    } else {
     
    244294    my $url = shift;
    245295    my $full_check = shift;
     296    my $smf = shift;
    246297
    247298    my %file;
     
    260311        my $stat = $nebulous->stat($url);
    261312
    262         &my_die("nebulous stat failed for $url", $magic_ds_id, $component, $PS_EXIT_CONFIG_ERROR) unless $stat;
     313        &my_die("nebulous stat failed for $url", $magic_ds_id, $PS_EXIT_CONFIG_ERROR) unless $stat;
    263314
    264315        $file{total_instances} = $stat->[7];
     
    270321            };
    271322            if (!$instances or !scalar @$instances) {
     323                print "No instances found for $url\n";
    272324                return \%file;
    273325            }
    274326
    275             my $first_inst;
    276             my $first_size = 0;
    277             my $first_md5 = "";
    278             my $first_censored ;
    279             my $available_instances = 0;
     327            my $good_inst;
     328            my $good_size = 0;
     329            my $good_md5 = "";
     330            my $good_censored ;
    280331            foreach my $inst (@$instances) {
    281332                $inst =~ s/file\:\/\///g;
    282333
    283334                if ( ! -e $inst) {
    284                     print STDERR "instance file $inst does not exist\n";
     335                    print "instance file $inst does not exist\n";
    285336                    push @{$file{bad_instances}}, $inst;
    286337                    next;
     
    288339                my $this_size = -s $inst;
    289340                if (!$this_size) {
    290                     print STDERR "instance file $inst has zero size\n";
     341                    print "instance file $inst has zero size\n";
    291342                    push @{$file{bad_instances}}, $inst;
    292343                    next;
    293344                }
    294                 $available_instances++;
    295                 push @{$file{instances}}, $inst;
    296                 my $this_md5 = $full_check ? file_md5_hex($inst) : "";
    297                 if (!defined $first_inst) {
    298                     $first_inst = $inst;
    299                     $first_size = $this_size;
    300                     $first_md5 = $this_md5;
    301                     $first_censored = ($inst =~ /SR_/);
    302                     # unless full_check all we really care about is the censored status
     345
     346                my $this_censored = ($inst =~ /SR_/);
     347
     348                my $this_md5 = "";
     349                if ($full_check) {
     350                    $this_md5 = file_md5_hex($inst);
     351                    if ($smf) {
     352                        my $command = "$ftable -list  $inst | grep psf | wc";
     353                        my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     354                                run(command => $command, verbose => $verbose);
     355
     356                        my $good = 0;
     357                        if ($success) {
     358                            my ($nchips) = split " ", join "", @$stdout_buf;
     359                            $nchips += 0;
     360                            if ($nchips and $nchips == 60) {
     361                                $good = 1;
     362                            } else {
     363                                print "$inst has less than 60 psf extensions: $nchips checking with censorObjects\n";
     364                                my $command = "$censorObjects -checkinputonly -file $inst";
     365                                my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     366                                    run(command => $command, verbose => $verbose);
     367
     368                                if ($success) {
     369                                    $good = 1;
     370                                } else {
     371                                    print "$command failed with error_code: $error_code\n",
     372                                }
     373                            }
     374                        } else {
     375                            print "$command failed with error_code: $error_code\n",
     376                        }
     377                        if (!$good) {
     378                            push @{$file{bad_instances}}, $inst;
     379                            next;
     380                        }
     381                    } else {
     382                        my $command = "$checkfits $inst";
     383                        my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     384                                run(command => $command, verbose => $verbose);
     385                        unless ($success) {
     386                            print "$command failed with error_code: $error_code\n",
     387                            push @{$file{bad_instances}}, $inst;
     388                            next;
     389                        }
     390                    }
     391                }
     392                if (!defined $good_inst) {
     393                    $good_inst = $inst;
     394                    $good_size = $this_size;
     395                    $good_md5 = $this_md5;
     396                    $good_censored = $this_censored;
     397
     398                    push @{$file{instances}}, $inst;
     399
     400                    # unless we're asked for a full all really care about is the censored status
    303401                    last if !$full_check;
    304                 } elsif ($this_size != $first_size) {
    305                     print STDERR "size of $inst $this_size is not equal to size of $first_inst: $first_size\n";
     402                } elsif ($this_size != $good_size) {
     403                    print "size of $inst $this_size is not equal to size of $good_inst: $good_size\n";
    306404                    $file{inconsistent} = 1;
    307                 } elsif ($this_md5 ne $first_md5) {
    308                     print STDERR "md5 of $inst $this_md5 is not equal to size of $first_inst: $first_md5\n";
     405                    push @{$file{bad_instances}}, $inst;
     406                } elsif ($this_md5 ne $good_md5) {
     407                    print "md5 of $inst $this_md5 is not equal to size of $good_inst: $good_md5\n";
    309408                    $file{inconsistent} = 1;
    310                 }
    311             }
    312             $file{num_instances} = $available_instances;
    313             $file{censored} = $first_censored;
    314             $file{size} = $first_size;
    315             $file{md5} = $first_md5;
    316         }
     409                    push @{$file{bad_instances}}, $inst;
     410                } else {
     411                    # this matches the good instance
     412                    push @{$file{instances}}, $inst;
     413                }
     414            }
     415            $file{num_instances} = scalar @{$file{instances}};
     416            $file{size} = $good_size;
     417            $file{md5} = $good_md5;
     418            if ($good_inst) {
     419                $file{censored} = $good_censored;
     420            } else {
     421                # no good instances see if there was a bad one and check whether its url
     422                # looks like a censored one
     423                if (scalar @{$file{bad_instances}}) {
     424                    my $bad = ${$file{bad_instances}}->[0];
     425                    $file{censored} = ($bad =~ /SR_/);
     426                    print "no good instances found setting censored based on $bad\n";
     427                }
     428            }
     429        }
     430    } else {
     431        print "No storage object found for $url \n";
    317432    }
    318433
     
    320435}
    321436
     437sub rename_file {
     438    if ($no_op) {
     439        return 1;
     440    }
     441
     442    my $original_url = shift;
     443    my $backup_url = shift;
     444
     445    # rename the backup as the orig
     446    print "No storage object for original url: $original_url\n";
     447    print "Replacing with file at            : $backup_url\n";
     448
     449    my $moved;
     450    eval {
     451        $moved = nebulous->move($backup_url, $original_url);
     452    };
     453    if ($@ or not $moved) {
     454        print "Failed to move $backup_url to $original_url\n";
     455        &my_die( "aborting", $magic_ds_id, $PS_EXIT_CONFIG_ERROR);
     456    }
     457}
     458
    322459sub repair_file {
     460    if ($no_op) {
     461        return 1;
     462    }
     463
    323464    my $file = shift;
    324465    my $smf = shift;
    325     if ($file->{num_instances} < 2) {
    326         print STDERR "cannot repair file with only one instance\n";
     466
     467    if ($file->{num_instances} < 1) {
     468        print "cannot repair file with only no good instances\n";
    327469        return 0;
    328470    }
    329     my $good_inst;
    330     my $good_size;
    331     my $good_md5;
    332     # try and find a "good instance"
    333     my @good;
    334     my $i;
    335     for ($i = 0; $i < scalar @{$file->{instances}}; $i++; {
    336         # assume the worst
    337         $good[$i] = 0;
    338         if (! -e $inst ) {
    339             next;
    340         }
    341         my $size = -s $inst;
    342         if (! $size ) {
    343             next;
    344         }
    345         my $md5 = file_md5_hex($inst);
    346         if ($good_inst) {
    347             if (($size == $good_size) and ($md5 eq $good_md5)) {
    348                 $good[$i] = 1;
    349             }
    350             next;
    351         }
    352            
    353         if (!$smf) {
    354             my $command = "$checkfits $inst";
    355             my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    356                 run(command => $command, verbose => $verbose);
    357             if ($success) {
    358                 if (!$good_inst) {
    359                     $good_inst = $inst;
    360                     $good_$md5 = $md5;
    361                     $good_size = $size;
    362                 }
    363                 $good[$i] = 1;
    364             }
    365         } else {
    366             # Need to write this program
    367             my $command = "$checksmf $inst";
    368             my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    369                 run(command => $command, verbose => $verbose);
    370             if ($success) {
    371                 if (!$good_inst) {
    372                     $good_inst = $inst;
    373                     $good_$md5 = $md5;
    374                     $good_size = $size;
    375                 }   
    376                 $good[$i] = 1;
    377             }
    378         }
    379     }
    380 
    381     if (!$good_inst) {
    382         print STDERR "unable to find any good instances of $file->{url}\n";
    383         return 0;
    384     }
    385 
    386     my $num_good = 0;
    387     for (my $j = 0; $j <= $i; $j++) {
    388         $num_good++ if $good[$j];
    389     }
    390     for (my $j = 0; $j <= @{$file->{bad_instances}}; $j++) {
    391         $good[$i++] = 0;
     471
     472    # file wouldn't get into instances array unless we decided that it was good in check_file
     473    my $good_inst = $file->{instances}->[0];
     474
     475    my $fixed = 0;
     476    foreach my $bad_inst (@{$file->{bad_instances}}) {
     477        print "Attempting to repair $bad_inst\n";
     478
     479        my $parent_directory = dirname($bad_inst);
     480        if (!-d $parent_directory) {
     481            if (! mkdir $parent_directory) {
     482                print "failed to mkdir parent directory $parent_directory\n";
     483                $fixed = 0;
     484                last;
     485            }
     486            if (!copy $good_inst, $bad_inst) {
     487                print "failed to copy $good_inst to $bad_inst\n";
     488            }
     489            $fixed = 1;
     490            push @{$file->{instances}}, $bad_inst;
     491        }
    392492    }
    393493       
    394 
    395     return 0;
     494    return $fixed;
    396495}
    397496
     
    422521    my $msg = shift;            # Warning message on die
    423522    my $magic_ds_id = shift;    # Magic DS identifier
    424     my $component = shift;      # class_id or skycell_id
    425523    my $exit_code = shift;      # Exit code to add
    426524
    427525    carp($msg);
    428 
    429     $component = "exposure";
    430526
    431527    $exit_code = $PS_EXIT_PROG_ERROR unless defined $exit_code;
Note: See TracChangeset for help on using the changeset viewer.