Embedix-DB

 view release on metacpan or  search on metacpan

bin/edb_populate.pl  view on Meta::CPAN

# to prevent future headaches, and to show I'm disciplined
use strict;

# for benchmarking
use Benchmark;

# for loading ECD data from files
use Embedix::ECD;

# for adding ECD data to a database
use Embedix::DB;

# for command line args
use Getopt::Long;

# for objectified filehandles
use IO::File;

# for the help message
use Pod::Usage;

# for globals
use vars qw($fucked_up $log_fh $err_fh);

$fucked_up = undef;

# header
sub print_header {
    my $format = '%7s|%7s|%7s|%7s| %s' . "\n";
    printf $log_fh ($format, 'bytes', 'usr', 'sys', 'cpu', 'ecd');
    print $log_fh '-' x 78;
    print $log_fh "\n";
}

# stats in beppu-readable form
sub print_entry {
    my $time     = shift;
    my $filename = shift;
    my $comment  = ($fucked_up) 
        ? "$filename (" . substr($fucked_up, 0, 3) . ")" 
        : $filename;
    my $size     = (stat($filename))[7];
    my $format   = '%7s|%7.2f|%7.2f|%7.2f| %s' . "\n";

    if ($fucked_up) {
        print $err_fh "$fucked_up\n";
        $fucked_up = undef;
    }

    printf $log_fh (
        $format, 
        $size, 
        $time->[1], 
        $time->[2],
        $time->[1] + $time->[2],
        $comment
    );
}

# return a closure that will add an ECD to the database
sub updater {
    my %opt = @_;
    my $filename = $opt{filename};
    my $edb      = $opt{edb};

    return sub {
        my $ecd;
        my $size = (stat($filename))[7];
        printf $err_fh ("> %7d $filename\n", $size, $filename);

        eval { $ecd = Embedix::ECD->newFromFile($filename) };
        if ($@) {
            $fucked_up = "ECD failure: $@";
            return;
        }
        unless ($ecd->hasChildren) {
            $fucked_up = "ECD failure: ECD is empty";
            return;
        }

        eval { $edb->updateDistro(ecd => $ecd) };
        if ($@) {
            $fucked_up = "EDB failure: $@";
            return;
        }
    }
}

# return a filehandle
sub fh_for_file {
    my $filename = shift;
    my $fh = IO::File->new("> $filename");
    $fh->autoflush(1);
    return $fh;
}



# XXX the fun begins here XXX



# get options
my %opt;
GetOptions (
    \%opt,
    "help|h",
    "database|d=s",
    "distro=s",
    "board=s",
    "log=s",
    "errorlog=s",
    "sizelimit=i",
);

pod2usage(-verbose => 1, -output => \*STDOUT) if (defined $opt{help});

my $dbname    = $opt{database}  || 'embedix';
my $distro    = $opt{distro}    || 'Embedix 1.2';
my $board     = $opt{board}     || 'generic';
my $sizelimit = $opt{sizelimit} || 0;



( run in 0.897 second using v1.01-cache-2.11-cpan-39bf76dae61 )