Changeset 26965


Ignore:
Timestamp:
2013-02-26T11:48:07+13:00 (11 years ago)
Author:
jmt12
Message:

Makes use of util library functions (rather than perl built-in file tests) to provide support for HDFS processing (so writing directly to HDFS paths)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • gs2-extensions/parallel-building/trunk/src/perllib/plugouts/GreenstoneXMLPlugout.pm

    r24626 r26965  
    5555
    5656    my $self = new BasePlugout($plugoutlist,$inputargs,$hashArgOptLists);
    57  
     57
    5858    return bless $self, $class;
    5959}
    6060
    61 sub saveas {
    62     my $self = shift (@_);
    63     my ($doc_obj,$doc_dir) = @_;
     61sub saveas
     62{
     63  my $self = shift (@_);
     64  my ($doc_obj,$doc_dir) = @_;
    6465
    65     my $outhandler;
    66     if ($self->{'debug'}) {
    67     $outhandler = STDOUT;
    68     # can we do the xslt and still do debug mode?
    69     }
    70     else {
    71     my $output_dir = $self->get_output_dir();
    72     &util::mk_all_dir ($output_dir) unless -e $output_dir;
    73    
    74     my $working_dir = &util::filename_cat ($output_dir, $doc_dir);   
    75     &util::mk_all_dir ($working_dir) unless -e $working_dir;
    76    
    77     $self->process_assoc_files ($doc_obj, $doc_dir, '');
    78    
    79     $self->process_metafiles_metadata ($doc_obj);
     66  my $outhandler;
     67  if ($self->{'debug'})
     68  {
     69    $outhandler = STDOUT;
     70    # can we do the xslt and still do debug mode?
     71  }
     72  else
     73  {
     74    my $output_dir = $self->get_output_dir();
     75    my $working_dir = &util::filename_cat($output_dir, $doc_dir);
     76    if (!&util::dir_exists($working_dir))
     77    {
     78      &util::mk_all_dir($working_dir);
     79    }
     80    $self->process_assoc_files ($doc_obj, $doc_dir, '');
     81    $self->process_metafiles_metadata ($doc_obj);
     82    my $output_file = &util::filename_cat($working_dir, "doc.xml");
     83    $self->open_xslt_pipe($output_file, $self->{'xslt_file'});
     84    if (defined $self->{'xslt_writer'})
     85    {
     86      $outhandler = $self->{'xslt_writer'};
     87    }
     88    else
     89    {
     90      $outhandler = $self->get_output_handler($output_file);
     91    }
     92  }
    8093
    81     my $output_file = util::filename_cat ($working_dir, "doc.xml");
    82    
    83     $self->open_xslt_pipe($output_file, $self->{'xslt_file'});
    84    
    85    
    86     if (defined $self->{'xslt_writer'}){
    87         $outhandler = $self->{'xslt_writer'};
    88     }
    89     else{
    90         $outhandler = $self->get_output_handler($output_file);
    91     }
     94  binmode($outhandler,":utf8");
     95
     96  $self->output_xml_header($outhandler,"Archive");
     97  my $section_output = &docprint::get_section_xml($doc_obj, $doc_obj->get_top_section());
     98  print $outhandler $section_output;
     99  $self->output_xml_footer($outhandler,"Archive");
     100
     101  if (!$self->{'debug'})
     102  {
     103    if (defined $self->{'xslt_writer'})
     104    {
     105      $self->close_xslt_pipe();
    92106    }
    93    
    94     binmode($outhandler,":utf8");
     107    else
     108    {
     109      close($outhandler);
     110    }
    95111
    96     $self->output_xml_header($outhandler,"Archive");
    97     my $section_output = &docprint::get_section_xml($doc_obj, $doc_obj->get_top_section());
    98     print $outhandler $section_output;
    99     $self->output_xml_footer($outhandler,"Archive");
     112    $self->{'short_doc_file'} = util::filename_cat($doc_dir, "doc.xml");
    100113
    101     if (!$self->{'debug'}) {
    102     if (defined $self->{'xslt_writer'}){     
    103         $self->close_xslt_pipe();
    104     }
    105     else {
    106         close($outhandler);
    107     }
    108    
    109     $self->{'short_doc_file'} = util::filename_cat ($doc_dir, "doc.xml"); 
    110    
    111     $self->store_output_info_reference($doc_obj);
    112     }
     114    $self->store_output_info_reference($doc_obj);
     115  }
     116}
     117# /** saveas() **/
     118
     119# Overriden for HDFS support
     120sub get_output_handler
     121{
     122  my $self = shift (@_);
     123  my ($output_file_name) = @_;
     124  if (&util::isHDFS($output_file_name))
     125  {
     126    open(*OUTPUT, &util::file_openfdcommand($output_file_name, '>'));
     127    return *OUTPUT;
     128  }
     129  else
     130  {
     131    return $self->SUPER::get_output_handler($output_file_name);
     132  }
    113133}
    114134
     135sub get_new_doc_dir
     136{
     137  my $self = shift (@_);
     138  my ($working_info, $working_dir, $OID) = @_;
     139  my $doc_dir = "";
     140  my $doc_dir_rest = $OID;
    115141
     142  #rint "!!GreenstoneXMLPlugout::get_new_doc_dir([working_info]," . $working_dir . "," . $OID . ")\n";
     143
     144  # Remove any \ and / from the OID because we are about to generate a path
     145  # Remove ":" too, as otherwise they get confused with the protocols / drive letters
     146  $doc_dir_rest =~ s/[\:\\\/]//g;
     147  my $doc_dir_num = 0;
     148  my $created_directory = 0;
     149  do
     150  {
     151    if ($doc_dir_num > 0)
     152    {
     153      $doc_dir .= "/";
     154    }
     155#    if ($doc_dir_rest =~ s/^(.{1,3})//)
     156    if ($doc_dir_rest =~ s/^((D|HASH)?.{1,3})//i)
     157    {
     158      $doc_dir .= $1;
     159      $doc_dir_num++;
     160    }
     161    #rint "!! - testing Path: " . $doc_dir . "\n";
     162    $created_directory = &util::mk_all_dir(&util::filename_cat($working_dir, $doc_dir . '.dir'));
     163    #rint "-> result: |" . $created_directory . "|\n";
     164  }
     165  while ($doc_dir_rest ne "" && $doc_dir_num < 32 && !$created_directory);
     166  my $i = 0;
     167  my $doc_dir_base = $doc_dir;
     168  while (!$created_directory && $i < 256)
     169  {
     170    $i++;
     171    $doc_dir = $doc_dir_base . '-' . $i;
     172    #rint "!! - testing Path: " . $doc_dir . "\n";
     173    $created_directory = &util::mk_all_dir(&util::filename_cat($working_dir, $doc_dir . '.dir'));
     174  }
     175  if (!$created_directory)
     176  {
     177    die("Error! Failed to create directory for document: " . $doc_dir_base . "\n");
     178  }
     179  #rint "!! Final Path: " . $doc_dir . ".dir\n";
     180  return $doc_dir . '.dir';
     181}
    116182
    1171831;
Note: See TracChangeset for help on using the changeset viewer.