Changeset 99


Ignore:
Timestamp:
12/05/05 05:29:06 (19 years ago)
Author:
thauvin
Message:
  • perform check separatly, add callback possibility
Location:
trunk/soft/ObsData
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/soft/ObsData/ObsData/Repository.pm

    • Property cvs2svn:cvs-rev changed from 1.36 to 1.37
    r98 r99  
    1212our $CVSID = q$Id$; 
    1313our $CVSREV = (q$Revision$ =~ /^Revision: (.*) $/)[0]; 
    14  
    15 my %default_do_if_change = ( 
    16     archive_new         => 1, 
    17     archive_time        => 0, 
    18     archive_size        => 0, 
    19     archive_configmtime => 0, 
    20  
    21     data_new            => 1, 
    22     data_configmtime    => 0, 
    23 ); 
    24  
    25 sub default_do_if_change { 
    26     return %default_do_if_change; 
    27 } 
    2814 
    2915=head1 METHODS 
     
    5339        # -default => '.', # Is this a good idea 
    5440    ); 
    55      
     41 
    5642    bless($or, $class); 
    5743 
    58     $or->set_do_if_change; 
    5944    $or; 
    6045} 
     
    8469} 
    8570 
    86 sub set_do_if_change { 
    87     my ($self, %settings) = @_; 
    88      
    89     foreach (keys %default_do_if_change) { 
    90         $self->{do_if_change}{$_} = 
    91             defined($settings{$_}) ? $settings{$_} : $default_do_if_change{$_}; 
    92     } 
    93 } 
    94  
    9571package ObsData::Repository::dir; 
    9672 
     
    141117            archivefile => $file, 
    142118        ); 
    143         $orda->get_info; 
    144         if(!$orda->file_need_parse) { 
     119        if(!$orda->do_continue('do_archive')) { 
    145120            $self->loging(0, 
    146121                "Archive %s/%s allready parsed, skipping", 
     
    166141    $or->{dir} or die "No dir given"; 
    167142    $or->{archivefile} or die "No archivefile given"; 
     143 
     144    # $or->{interative_callback} 
     145    $or->{_cache_checks} = {}; 
    168146     
    169147    $or->loging(0, 
     
    175153    bless($or, $class); 
    176154} 
     155 
     156my $checks = { 
     157    archive_exists => sub { 
     158        my ($self) = @_; 
     159        my $res; 
     160        if($self->{status}->SectionExists($self->{archivefile})) { 
     161            $res = 1; 
     162        } else { 
     163            $res = 0; 
     164        } 
     165        return( 
     166            $res,  
     167            $self->loging(0, 
     168                "%s/%s is%s new archive", 
     169                $self->{dir}, 
     170                $self->{archivefile}, 
     171                $res ? " not" : "", 
     172            ) 
     173        );       
     174    }, 
     175 
     176    archive_size => sub { 
     177        my ($self) = @_; 
     178        my $res; 
     179        if (defined(my $oldsize = $self->{status}->val($self->{archivefile}, 'size')) && 
     180            defined(my $size = (stat("$self->{dir}/$self->{archivefile}"))[7])) { 
     181            my $res = $size <=> $oldsize; 
     182            return $res, $self->loging(0, 
     183                "Size for %s/%s %s", 
     184                $self->{dir}, 
     185                $self->{archivefile}, 
     186                $res == 0 ? 
     187                    sprintf("does not differ (%d)", $size) : 
     188                    sprintf("differ %d => %d", $oldsize, $size) 
     189            ); 
     190        } 
     191    }, 
     192 
     193    archive_mtime => sub { 
     194        my ($self) = @_; 
     195        my $res; 
     196        if (defined(my $oldmtime = $self->{status}->val($self->{archivefile}, 'mtime')) && 
     197            defined(my $mtime = (stat("$self->{dir}/$self->{archivefile}"))[9])) { 
     198            my $res = $mtime <=> $oldmtime; 
     199            return $res, $self->loging(0, 
     200                "Mtime for %s/%s %s", 
     201                $self->{dir}, 
     202                $self->{archivefile}, 
     203                $res == 0 ? 
     204                    sprintf("does not differ (%d)", $mtime) : 
     205                    sprintf("differ %d => %d", $oldmtime, $mtime) 
     206            ); 
     207        } 
     208    }, 
     209 
     210    dest_exists => sub { 
     211        my ($self, $datafile, $dest) = @_; 
     212        if (-e $dest) { 
     213            return 1, "$dest exists"; 
     214        } else { 
     215            return 0, "$dest don't exists"; 
     216        } 
     217    }, 
     218}; 
    177219 
    178220#sub DESTROY { 
     
    182224#} 
    183225 
    184 sub get_info { 
    185     my ($self) = @_; 
    186  
    187     $self->{filestats} = [ stat("$self->{dir}/$self->{archivefile}") ]; 
    188  
    189     if (! @{$self->{filestats}}) { 
    190         $self->loging(4, 
    191             "Cannot get info of %s", 
    192             "$self->{dir}/$self->{archivefile}" 
    193         ); 
    194         return 0; 
    195     } 
    196  
    197     if ($self->{status}->SectionExists($self->{archivefile})) { 
    198         if (my $oldmtime = $self->{status}->val($self->{archivefile}, 'mtime')) { 
    199             if ($oldmtime == $self->{filestats}[9]) { 
    200  
    201             } elsif ($oldmtime > $self->{filestats}[9]) { 
    202                 $self->loging(3,  
    203                     "Wahou, %s is older than my reference: %d => %d",  
    204                     $oldmtime, 
    205                     $self->{filestats}[9], 
    206                 ); 
    207             } else { 
    208                 $self->{changes}{archive_time} = $self->loging(0,  
    209                     "%s is more recent %d => %d", 
    210                     $self->{archivefile}, 
    211                     $oldmtime, 
    212                     $self->{filestats}[9], 
    213                 ); 
    214             } 
    215         } 
    216  
    217         if (defined(my $oldsize = $self->{status}->val($self->{archivefile}, 'size'))) { 
    218             if ($oldsize != $self->{filestats}[7]) { 
    219                 $self->{changes}{archive_size} = $self->loging(0,  
    220                     "%s size differ %d => %d", 
    221                     $self->{archivefile}, 
    222                     $oldsize, 
    223                     $self->{filestats}[7], 
    224                 ); 
    225             } 
    226         } 
    227  
    228         if (defined(my $oldconftime = $self->{status}->val($self->{archivefile}, 'configtime'))) { 
    229             if ($self->{obsdata}->config_mtime > $oldconftime) { 
    230                 $self->{changes}{archive_configmtime} = $self->loging(0,  
    231                     "%s config has changed since last processing %d => %d", 
    232                     $self->{archivefile}, 
    233                     $oldconftime, 
    234                     $self->{obsdata}->config_mtime, 
    235                 ); 
    236             } 
    237         } 
     226sub do_check { 
     227    my ($self, $check, @args) = @_; 
     228     
     229    if (!$checks->{$check}) { 
     230        die $self->loging(5, 
     231            "check %s does not exists, exiting now", 
     232            $check 
     233        ); 
     234    } 
     235     
     236    return $checks->{$check}->($self, $check, @args); 
     237} 
     238 
     239sub do_continue { 
     240    my ($self, $why, @args) = @_; 
     241 
     242    if ($self->{interative_callback}) { 
     243        return $self->{interative_callback}->($self, $why, @args); 
    238244    } else { 
    239         $self->{changes}{archive_new} = 
    240             $self->loging(0, "%s is new file", $self->{archivefile}); 
    241     } 
    242     return 1; 
    243 } 
    244  
    245 # TODO Define return code for this function, storing result somewhere to 
    246 # pass to child. 
    247 sub file_need_parse { 
    248     my ($self) = @_; 
    249  
    250     my $need = 0; 
    251     foreach (keys %{$self->{changes}}) { 
    252         if (defined($self->{changes}{$_}) && $self->{do_if_change}{$_}) { 
    253             $self->loging(0, "%s => make parse need", $self->{changes}{$_}); 
    254             $need++; 
    255         } 
    256     } 
    257      
    258     $need 
     245        return $self->default_checks($why, @args); 
     246    } 
     247} 
     248 
     249sub default_checks { 
     250    my ($self, $why, @args) = @_; 
     251     
     252    my ($code, $text); 
     253     
     254    if ($why eq 'do_archive') { 
     255        ($code, $text) = $self->do_check('archive_size'); 
     256        if ($code) { 
     257            $self->loging(3, $text); 
     258        } 
     259 
     260        ($code, $text) = $self->do_check('archive_mtime'); 
     261        if ($code) { 
     262            $self->loging(3, $text); 
     263        } 
     264         
     265        ($code, $text) = $self->do_check('archive_exists'); 
     266        if ($code != 0) { 
     267            $self->loging(0, "%s => skiping", $text); 
     268            return 0; 
     269        } 
     270    } elsif ($why eq 'do_data') { 
     271     
     272    } elsif ($why eq 'overwrite') { 
     273        if ($self->do_check('dest_exists', @args)) { 
     274            return 0; 
     275        } else { 
     276            return 1; 
     277        } 
     278    } 
     279     
     280    1; 
    259281} 
    260282 
     
    289311} 
    290312 
    291 sub parse_archive { 
    292     my ($self) = @_; 
    293  
    294     $self->load_archive or return; 
    295  
    296     foreach my $datafile ($self->{Oarchive}->ls) { 
    297         $self->loging(0, "archive %s: content %s", $self->{archivefile}, $datafile); 
    298         my $data = ObsData::Repository::dir::archive::data->new( 
    299             $self, 
    300             datafile => $datafile, 
    301         ); 
    302         $data->dispatch; 
    303     } 
    304     return 1; 
    305 } 
    306  
    307313sub update_status { 
    308314    my ($self) = @_; 
     
    383389    } 
    384390     
    385     if (-e $destfile) { 
     391    if(!$self->do_continue('overwrite', $datafile, $destfile)) { 
    386392        $self->loging(3, 
    387393            "%s extracted from %s/%s already exists, I won't overwrite it", 
     
    436442 
    437443    foreach my $data ($self->list_archive) { 
    438         if ($self->data_lastcheck($data)) { 
     444        if (!$self->do_continue('do_data', $data)) { 
    439445            $self->loging(0, 
    440446                "%s/%s has allready processed, skipping", 
  • trunk/soft/ObsData/t/OR-03.t

    • Property cvs2svn:cvs-rev changed from 1.6 to 1.7
    r94 r99  
    11# $Id$ 
    22 
    3 use Test::More tests => 10; 
     3use Test::More tests => 13; 
    44use File::Temp qw(tempdir); 
    55 
     
    2323 
    2424{ 
     25    my $callbackwhy; 
    2526    my $or = ObsData::Repository::dir::archive->new( 
    2627        { 
     
    2829            dir => 'testdata/ohp', 
    2930            archivefile => 'ozone34.gz', 
     31            interative_callback => sub { 
     32                my ($self, $why, @args) = @_; 
     33                $callbackwhy = $why; 
     34                if ($why eq 'overwrite') { 
     35                    return 1; 
     36                } else { 
     37                    return $self->default_checks($why, @args); 
     38                } 
     39            }, 
    3040        } 
    3141    ); 
    3242    isa_ok($or, "ObsData::Repository::dir::archive"); 
    3343    ok($or->load_archive, "Can load an archive"); 
     44    ok($or->do_continue('overwrite') eq 1, "Perform do_continue"); 
     45    ok($callbackwhy eq 'overwrite', "arg are properly passed to callback"); 
    3446    ok($or->list_archive, "Can list archive content"); 
    3547    ok($or->get_data_dest('ozone34'), "Can get data destination"); 
     48    ok($or->do_check('archive_exists'), 'Can perform check'); 
    3649    ok($or->extract_data('ozone34', undef, '/dev/null'), "Can extract data"); 
    3750} 
Note: See TracChangeset for help on using the changeset viewer.