Guest User

Untitled

a guest
Jun 23rd, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 3.42 KB | None | 0 0
  1. sub macro_expand {
  2.     my ( $this, $ctx, $src, $dest, $tempDest) = @_;
  3.     my $orig='';
  4.     $src  = localizePath($src);
  5.    
  6.     #We are doing atomic file writes here to avoid any partial expansion of values into property files or in other words to avoid partially written property files.That is why we have a temporary file handler.
  7.    
  8.     my $atomic_rename_enabled = $^O ne 'MSWin32';
  9.     if (!$atomic_rename_enabled) {
  10.     #print "\n ************\n INSIDE THE IF BLOCK..BUT ACTUALLY I SHOULD NOT BE HERE \n **********";
  11.         $tempDest = $dest;
  12.     }
  13.  
  14.     $tempDest = localizePath($tempDest);
  15.     $dest = localizePath($dest);
  16.  
  17.     open( SRC, "<$src" ) || die "ERROR: cannot open $src: $!\nStopped";
  18.     if ( -e $tempDest ) {
  19.         unlink($tempDest) || die "Unlink $tempDest: $!\n";
  20.     }
  21.    
  22.     open( TEMPDEST, ">$tempDest" ) || die "ERROR: cannot write $tempDest: $!\nStopped";
  23.    
  24.     while (<SRC>) {
  25.        # print "\n AT THE BEGINNING OF THE LOOP";
  26.          while ( $orig =~ /%%([^%]*)%%/ ) {
  27.             my $m = $1;
  28.  
  29.             #  Hack for Raed
  30.             if ( $m =~ /^dm\d+-value$/ ) {
  31.                 if ( $^O eq "solaris" || $^O eq "linux" ) {
  32.                     $m .= "-UNIX";
  33.                 }
  34.                 else {
  35.                     $m .= "-NT";
  36.                 }
  37.             }
  38.  
  39.  
  40.             $this->_croak_with_ctx($ctx, "Macro $m from $src is not defined.")
  41.                 unless exists $$ctx{$m};
  42.  
  43.             my $r = $ctx->{$m};
  44.  
  45.             if (defined $r) {
  46.                 $r = $this->get_macro_for_pod($m, $r);
  47.             }
  48.             else {
  49.                 $r = '';
  50.             }
  51.             if (ref($r) eq "ARRAY" && $src =~ /\.properties\.in$/m) {
  52.                 my $key;
  53.                 $_ =~/^(\S+)=/;
  54.                 $key = $1;
  55.                 $orig="";
  56.                 my $i = 1;
  57.                 foreach my $value (@$r) {
  58.                 $orig .= "${key}.${i}=$value\n";
  59.                 $i++;
  60.                 }
  61.             }
  62.             else {
  63.                 #print STDERR "r is now set to $r\n";
  64.                 $orig =~ s/%%([^%]*)%%/$r/;
  65.             }
  66.         }
  67.         if ( $tempDest =~ /\.py$/ ) {
  68.         $orig =~ s/\\/\//g;
  69.         }  
  70.         if ( $^O eq "solaris" || $^O eq "linux" ) {
  71.             $orig =~ s/\r//g;
  72.         }
  73.     }
  74.     #1.Write
  75.     print TEMPDEST $orig;
  76.     #2.Flush
  77.     TEMPDEST->flush;
  78.     #if( ! -e $tempDest ){
  79.     #die("\n ************* \n WTF is $tempDest???\n******************\n");
  80.     #}
  81.     #################
  82.     #Avoiding sync to disk because it makes the install painfully slower. Sync is intended to avoid partially written files in the event of OS crashing while the write is happening.
  83.     #The chances of this happening is very very low. So we are skipping the sync to disk step in the atomic file write to keep the install faster.
  84.     #TEMPDEST->sync();
  85.     #################
  86.     #3.Renaming the temp file name to the original property file name and that completes the atomic file write steps.
  87.     if ($atomic_rename_enabled) {
  88.     #print "\n ****$tempDest";
  89.     #print "\n ****$dest";
  90.     #print " \n $atomic_rename_enabled ";
  91.     #print " \n RENAMING $tempDest to $dest";
  92.     rename ($tempDest,$dest) or die ("Could not rename $tempDest to $dest: $!");
  93.     #rename ($tempDest,$dest);
  94.     #print " \n  ****$tempDest";
  95.     }
  96.     #Closing the file handlers.
  97.     close(TEMPDEST);
  98.     close(SRC);
  99.     if (   ( ( $^O eq "solaris" ) || ( $^O eq "linux" ) )
  100.         && ( $dest =~ /\.sh$/ ) )
  101.     {
  102.         chmod( 0755, $dest );
  103.     }
  104. }
Add Comment
Please, Sign In to add comment