# under the terms of the GPL version 2, or any later version, at your
# option. See the file README and COPYING for more information.
# Copyright 2007 by Don Armstrong <don@donarmstrong.com>.
-# $Id: perl_script 495 2006-08-10 08:02:01Z don $
use warnings;
=head1 NAME
-do_it_all - Call out to each of the search modules to search for each
-of the terms
+ do_it_all - Call out to each of the search modules to search for
+ each of the terms
=head1 SYNOPSIS
use Cwd qw(abs_path);
use IO::File;
use Storable qw(thaw freeze);
+use File::Basename qw(basename);
my %options = (databases => [],
keywords => [],
pod2usage() if $options{help};
pod2usage({verbose=>2}) if $options{man};
+my $base_dir = basename($0);
+
my $ERRORS='';
$ERRORS.="restart-at must be one of get, parse or combine\n" if
if (exists $options{restart_at} and length $options{restart_at}) {
if (lc($options{restart_at}) eq 'get') {
- delete $state{gotten_keywords};
- delete $state{parsed_keywords};
- delete $state{combined_keywords};
+ delete $state{done_keywords}{get};
+ delete $state{done_keywords}{parse};
+ delete $state{done_keywords}{combine};
}
elsif (lc($options{restart_at}) eq 'parse') {
- delete $state{parsed_keywords};
- delete $state{combined_keywords};
+ delete $state{done_keywords}{parse};
+ delete $state{done_keywords}{combine};
}
elsif (lc($options{restart_at}) eq 'combine') {
- delete $state{combined_keywords};
+ delete $state{done_keywords}{combine};
}
}
}
if (not exists $state{done_keywords}{parse}{$database}{$keyword}) {
push @{$actions{parse}{$database}},$keyword;
- delete $state{done_keywords}{combine}{$database}{$keyword} if
+ delete $state{done_keywords}{combine}{$database}{$keyword} if
exists $state{done_keywords}{combine}{$database}{$keyword};
}
if (not exists $state{done_keywords}{combine}{$database}{$keyword}) {
}
}
+if ($actions{combine}) {
+ save_state(\%state);
+ # deal with combining results
+ my @parsed_results = map { my $db = $_;
+ map {
+ "parsed_results_${db}_${_}.txt"
+ } keys %{$state{done_keywords}{parse}{$db}}
+ } keys %{$state{done_keywords}{parse}};
+
+ write_command_to_file('combined_results.txt',
+ "$base_dir/combine_results",
+ @parsed_results,
+ );
+ for my $result (@parsed_results) {
+ s/^parsed_results_//;
+ s/\.txt$//;
+ my ($db,$keyword) = split /_/, $_, 2;
+ $state{done_keywords}{combined}{$db}{$keyword} = 1;
+ }
+ save_state(\%state);
+ ADVISE("Finished; results in $options{results}/combined_results");
+}
+else {
+ ADVISE('Nothing to do. [Perhaps you wanted --restart-at?]');
+}
+
sub handle_action{
my ($state,$database,$queue) = @_;
my $keyword;
my $failed_keywords = ();
while ($keyword = $queue->dequeue) {
# handle the action, baybee
+ if ($state eq 'get') {
+ my $command_fh;
+ open($command_fh,'|-',
+ "get_${database}_results",
+ );
+ print {$command_fh} "$keyword\n";
+ close($command_fh);
+ if ($? != 0) {
+ WARN("get_${database}_results with keyword $keyword failed with error code ".($?>>8));
+ next;
+ }
+ }
+ elsif ($state eq 'parse') {
+ eval {
+ write_command_to_file("parsed_results_${database}_${keyword}.txt",
+ "parse_${database}_results",
+ '--keywords',
+ $keyword,
+ );
+ };
+ if ($@) {
+ WARN("parse_${database}_results failed with $@");
+ next;
+ }
+ }
+ else {
+ die "I don't know how to handle state $state";
+ }
ADVISE("$state results from '$database' for '$keyword'");
push @{$actioned_keywords},$keyword;
}
close $state_fh or die "Unable to close state file: $!";
}
+sub write_command_to_file{
+ my ($file,@command);
+ my $fh = IO::File->new($file,'w') or
+ die "Unable to open $file for writing: $!";
+ my $command_fh;
+ open($command_fh,'-|',
+ @command,
+ ) or die "Unable to execute $command[0] $!";
+ print {$fh} <$command_fh>;
+ close $fh;
+ close $command_fh or die "$command[0] failed with ".($?>>8);
+}
+
sub ADVISE{
print STDOUT map {($_,qq(\n))} @_;