Ignore:
Timestamp:
2006-08-18T09:24:29+12:00 (18 years ago)
Author:
shaoqun
Message:

fixed th bugs on windows

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/gsdl/perllib/downloaders/Z3950Download.pm

    r11783 r12465  
    4040    @Z3950Download::ISA = ('BasDownload');
    4141}
    42 
    43 local (*YAZOUT, *YAZIN);
    4442
    4543my $arguments =
     
    9795    # Must set $self->{'url'}, since GLI use $self->{'url'} to calculate the log file name!
    9896    $self->{'url'} = $self->{'host'}.":".$self->{'port'};
     97
     98     my $os = $ENV{'GSDLOS'};
     99
     100    if ($os !~ /windows/) {
     101    $self->{'yaz'} = "$ENV{'GSDLHOME'}/packages/yaz/yaz-2.1.4/client/yaz-client";
     102    }
     103    else{
     104    $self->{'yaz'} = "$ENV{'GSDLHOME'}/bin/windows/yaz-client";
     105    }
     106   
    99107    return bless $self, $class;
    100108
     
    106114    my ($hashGeneralOptions) = @_;
    107115    my ($strOpen,$strBase,$strFind,$strResponse,$intAmount,$intMaxRecords,$strRecords);
     116
     117    my $url = $self->{'url'};
     118 
    108119    print STDERR "<<Defined Maximum>>\n";
    109120 
    110     my $url = $self->{'url'};
    111121    print STDERR "Opening connection to $url\n";
    112122     
    113     my $childpid = open2(*YAZOUT, *YAZIN, "yaz-client")
    114     or die "can't open pipe to yaz-client: $!";
    115  
    116     $strOpen = &run_command_with_output("open $url"); 
     123    my  $yaz = $self->{'yaz'};
     124 
     125    my $childpid = open2(*YAZOUT, *YAZIN, $yaz)
     126    or (print STDERR "<<Finished>>\n" and die "can't open pipe to yaz-client: $!");
     127    $self->{'YAZOUT'} = *YAZOUT;
     128    $self->{'YAZIN'} = *YAZIN;
     129
     130    $strOpen = $self->open_connection("open $url"); 
     131
     132    if (!$strOpen) {
     133        print STDERR "Cannot connect to $url\n";
     134        print STDERR "<<Finished>>\n"; 
     135    return 0;
     136    }
     137
    117138    print STDERR "Access database: \"$self->{'database'}\"\n";
    118     &run_command_without_output("base $self->{'database'}");
     139    $self->run_command_without_output("base $self->{'database'}");
    119140    print STDERR "Searching for keyword: \"$self->{'find'}\"\n";
    120     $intAmount = &findAmount($self->{'find'});
     141    $intAmount = $self->findAmount($self->{'find'});
    121142
    122143    if($intAmount <= 0)
    123144    {
    124145    ($intAmount == -1)?
    125         print STDERR "Unexpected format, Parsing operation can not be performed\n" :
    126         print STDERR "No Record is found\n";
     146        print STDERR "Something wrong with the arguments,downloading can not be performed\n":
     147        print STDERR "No Record is found\n";
     148    print STDERR "<<Finished>>\n";
    127149    return 0;
    128150    }
    129151    $intMaxRecords = ($self->{'max_records'} > $intAmount)? $intAmount : $self->{'max_records'};
    130152    print STDERR "<<Total number of record(s):$intMaxRecords>>\n";
    131     $strRecords = &getRecords($intMaxRecords);
    132     print STDERR $strRecords;
    133     &saveRecords($self,$strRecords,$hashGeneralOptions->{'cache_dir'},$intMaxRecords);
     153    $strRecords = "Records: $intMaxRecords\n".$self->getRecords($intMaxRecords);
     154   
     155    $self->saveRecords($strRecords,$hashGeneralOptions->{'cache_dir'},$intMaxRecords);
    134156    print STDERR "Closing connection...\n";
     157    print STDERR "<<Finished>>\n";
     158
    135159    close(YAZOUT);
    136160    close(YAZIN);
    137     waitpid($childpid, 0);
    138161    return 1;
    139162}
    140163
     164sub open_connection{
     165  my ($self,$strCommand) =  (@_);
     166 
     167  $self->run_command($strCommand); 
     168
     169  my $out = $self->{'YAZOUT'};
     170
     171  $_ = <$out>;
     172 
     173  return (/Connecting...OK/i)? 1: 0;
     174 
     175}
     176
    141177sub findAmount
    142178{
     179    my ($self) = shift (@_);
    143180    my($strFindTarget) = @_;
    144     my $strResponse = &run_command_with_output("find $strFindTarget");
    145     return ($strResponse =~ m/^Number of hits: (\d+)/m)? $1:-1;   
     181    my $strResponse = $self->run_command_with_output("find $strFindTarget","^Number of hits:");
     182   return ($strResponse =~ m/^Number of hits: (\d+)/m)? $1:-1;   
    146183}
    147184
    148185sub getRecords
    149186{
     187    my ($self) = shift (@_);
    150188    my ($intMaxRecords) = @_;
    151     my ($strShow,$intStartNumber,$strResponse,$strRecords,$intRecordsLeft);
     189    my ($strShow,$intStartNumber,$numRecords,$strResponse,$strRecords,$intRecordsLeft);
    152190
    153191    $intStartNumber = 1;
    154192    $intRecordsLeft = $intMaxRecords;
     193    $numRecords = 0;
     194    $strResponse ="";
     195
    155196    while ($intRecordsLeft > 0)
    156197    {
    157198    if($intRecordsLeft > 50)
    158199    {
    159         print STDERR "<<Done:50>>\n";
     200       
    160201        print STDERR "Yaz is Gathering records: $intStartNumber - ".($intStartNumber+49)."\n";
    161        
     202        $numRecords = 50;
    162203        $strShow = "show $intStartNumber+50";
    163204        $intStartNumber = $intStartNumber + 50;
    164205        $intRecordsLeft = $intRecordsLeft - 50;
     206             
    165207    }
    166208    else
    167209    {
    168         print STDERR "<<Done:".($intRecordsLeft).">>\n";
     210        $numRecords = $intRecordsLeft;
    169211        print STDERR "Yaz is Gathering records: $intStartNumber - ".($intStartNumber+$intRecordsLeft-1)."\n";
    170212        $strShow = "show $intStartNumber+$intRecordsLeft";
    171213        $intRecordsLeft = 0;
     214       
     215           }
     216   
     217    $strResponse .= $self->get($strShow,$numRecords);
     218         
     219    if ($strResponse eq ""){
     220        print STDERR "<<ERROR: failed to get $numRecords records>>\n";
    172221    }
     222    else{
     223        print STDERR "<<Done:$numRecords>>\n";
     224    }
     225    }
     226
     227    return  "$strResponse\n";
    173228   
    174     $strResponse = &run_command_with_output($strShow);
    175        
    176     if($strResponse =~ m/Records: (\d*?)\n(.*?)nextResultSetPosition = (\d*?)\n/s)
    177     {
    178         $strRecords .= $2;
    179     }
    180     }
    181     return $strRecords;
    182229}
    183230
     
    188235    # setup directories
    189236    # Currently only gather the MARC format
    190     my $strFileName = &generateFileName($self,$intMaxRecords);
     237    my $strFileName = $self->generateFileName($intMaxRecords);
     238
     239    $strOutputDir  =~ s/"//g;
     240
    191241    my $strFileURL = "$strOutputDir/$self->{'host'}/$strFileName.marc";
    192     $strFileURL =~ s/:/\//g;
    193 
     242 
    194243    # prepare subdirectory for record (if needed)
    195     my ($strSubDirPath,$unused) = dirFileSplit($strFileURL);
     244    my ($strSubDirPath,$unused) = $self->dirFileSplit($strFileURL);
     245 
    196246    &util::mk_all_dir($strSubDirPath);
    197247
    198248    my $ds = &util::get_dirsep();
    199249    my $strOutputFile = &util::filename_cat($strOutputDir,$self->{'host'},"$strFileName.marc");
    200     $strOutputFile =~ s/:/$ds/g;
    201 
     250   
    202251    print STDERR "Saving records to \"$strOutputFile\"\n";
    203252
    204253    # save record
    205254    open (ZOUT,">$strOutputFile")
    206     || die "Unable to save oai metadata record: $!\n";
     255    || die "Unable to save Z3950 record: $!\n";
    207256    print ZOUT $strRecords;
    208257    close(ZOUT);
    209258}
    210259
     260
    211261sub run_command_with_output
    212262{
     263    my ($self,$strCMD,$strStopRE) =@_;
     264   
     265    $self->run_command($strCMD);
     266   
     267    return $self->get_output($strStopRE);
     268 
     269}
     270
     271sub get{
     272   my ($self,$strShow,$numRecord) = @_; 
     273
     274   $self->run_command($strShow); 
     275   
     276   my $strFullOutput="";
     277   my $count=0;
     278   my $readRecord = 0;
     279   
     280   while (my $strLine = <YAZOUT>)
     281   {
     282   
     283       if ($strLine =~ m/Records: ([\d]*)/i ){
     284       $readRecord = 1;
     285       next; 
     286       }
     287     
     288      return $strFullOutput if ($strLine =~ m/nextResultSetPosition|Not connected/i);
     289       
     290      next if(!$readRecord);
     291     
     292      $strFullOutput .= $strLine;     
     293  }
     294   
     295}
     296
     297sub run_command_without_output
     298{
     299     my ($self) = shift (@_);
    213300    my ($strCMD) = @_;
    214    
    215     return &run_command($strCMD,"^Elapsed:.*\$");
    216 }
    217 
    218 sub run_command_without_output
    219 {
    220     my ($strCMD) = @_;
    221 
    222     &run_command($strCMD);
     301
     302    $self->run_command($strCMD);
    223303}
    224304
    225305sub run_command
    226306{
    227     my ($strCMD,$strStopRE) = @_;
    228    
    229  
    230     print YAZIN "$strCMD\n";
     307    my ($self,$strCMD) = @_;
     308 
     309    my $input = $self->{'YAZIN'};
     310
     311    print $input "$strCMD\n"; 
     312}
     313
     314sub get_output{
     315    my ($self,$strStopRE) = @_; 
     316
    231317    if (!defined $strStopRE){return "";}
    232318    else
    233319    {
    234320    my $strFullOutput;
    235     while (my $strLine = <YAZOUT>)
     321        my $output = $self->{'YAZOUT'};
     322    while (my $strLine = <$output>)
    236323    {
    237         $strFullOutput .= $strLine;
    238         if($strLine =~ m/$strStopRE/){return $strFullOutput;}
     324           $strFullOutput .= $strLine;   
     325       if($strLine =~ m/^$strStopRE|Not connected/i){return $strFullOutput;}
    239326    }
    240327    }
     
    245332    my ($self,$intMaxRecords) = @_;
    246333    my $strFileName = ($self->{'database'})."_".($self->{'find'})."_".($intMaxRecords);
     334 
    247335}
    248336
    249337sub dirFileSplit
    250338{
    251     my ($strFile) = @_;
    252 
    253     my @aryDirs = split("/",$strFile);
     339    my ($self,$strFile) = @_;
     340
     341    my @aryDirs = split("[/\]",$strFile);
     342   
    254343    my $strLocalFile = pop(@aryDirs);
    255344    my $strSubDirs = join("/",@aryDirs);
     
    258347}
    259348
     349sub url_information
     350{
     351   my ($self,$url) = @_;
     352
     353   $url = $self->{'url'} unless defined $url;
     354
     355   my  $yaz =  $self->{'yaz'};
     356   
     357   my $childpid = open2(*YAZOUT, *YAZIN, $yaz)
     358       or die "can't open pipe to yaz-client: $!";
     359 
     360   $self->{'YAZOUT'} = *YAZOUT;
     361   $self->{'YAZIN'} = *YAZIN;
     362
     363   my $strOpen = $self->open_connection("open $url");
     364   
     365    if (!$strOpen) {
     366        print STDERR "Cannot connect to $url\n";
     367        print STDERR "<<Finished>>\n"; 
     368    return 0;
     369    }
     370
     371   
     372   $strOpen = $self->run_command_with_output("open $url","^Options"); 
     373
     374
     375   $strOpen =~ s/Z> //g;
     376   $strOpen =~ s/Elapsed:.*//g;
     377
     378   print STDERR $strOpen;
     379
     380   print STDERR "<<Finished>>\n";
     381
     382   close(YAZOUT);
     383   close(YAZIN);
     384
     385   return 0;
     386
     387}
     388
    260389sub error
    261390{
    262     my ($strFunctionName,$strError) = @_;
     391    my ($self,$strFunctionName,$strError) = @_;
    263392    {
    264393    print STDERR "Error occoured in Z3950Download.pm\n".
Note: See TracChangeset for help on using the changeset viewer.