Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- sub macro_expand {
- my ( $this, $ctx, $src, $dest, $tempDest) = @_;
- my $orig='';
- $src = localizePath($src);
- #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.
- my $atomic_rename_enabled = $^O ne 'MSWin32';
- if (!$atomic_rename_enabled) {
- #print "\n ************\n INSIDE THE IF BLOCK..BUT ACTUALLY I SHOULD NOT BE HERE \n **********";
- $tempDest = $dest;
- }
- $tempDest = localizePath($tempDest);
- $dest = localizePath($dest);
- open( SRC, "<$src" ) || die "ERROR: cannot open $src: $!\nStopped";
- if ( -e $tempDest ) {
- unlink($tempDest) || die "Unlink $tempDest: $!\n";
- }
- open( TEMPDEST, ">$tempDest" ) || die "ERROR: cannot write $tempDest: $!\nStopped";
- while (<SRC>) {
- # print "\n AT THE BEGINNING OF THE LOOP";
- while ( $orig =~ /%%([^%]*)%%/ ) {
- my $m = $1;
- # Hack for Raed
- if ( $m =~ /^dm\d+-value$/ ) {
- if ( $^O eq "solaris" || $^O eq "linux" ) {
- $m .= "-UNIX";
- }
- else {
- $m .= "-NT";
- }
- }
- $this->_croak_with_ctx($ctx, "Macro $m from $src is not defined.")
- unless exists $$ctx{$m};
- my $r = $ctx->{$m};
- if (defined $r) {
- $r = $this->get_macro_for_pod($m, $r);
- }
- else {
- $r = '';
- }
- if (ref($r) eq "ARRAY" && $src =~ /\.properties\.in$/m) {
- my $key;
- $_ =~/^(\S+)=/;
- $key = $1;
- $orig="";
- my $i = 1;
- foreach my $value (@$r) {
- $orig .= "${key}.${i}=$value\n";
- $i++;
- }
- }
- else {
- #print STDERR "r is now set to $r\n";
- $orig =~ s/%%([^%]*)%%/$r/;
- }
- }
- if ( $tempDest =~ /\.py$/ ) {
- $orig =~ s/\\/\//g;
- }
- if ( $^O eq "solaris" || $^O eq "linux" ) {
- $orig =~ s/\r//g;
- }
- }
- #1.Write
- print TEMPDEST $orig;
- #2.Flush
- TEMPDEST->flush;
- #if( ! -e $tempDest ){
- #die("\n ************* \n WTF is $tempDest???\n******************\n");
- #}
- #################
- #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.
- #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.
- #TEMPDEST->sync();
- #################
- #3.Renaming the temp file name to the original property file name and that completes the atomic file write steps.
- if ($atomic_rename_enabled) {
- #print "\n ****$tempDest";
- #print "\n ****$dest";
- #print " \n $atomic_rename_enabled ";
- #print " \n RENAMING $tempDest to $dest";
- rename ($tempDest,$dest) or die ("Could not rename $tempDest to $dest: $!");
- #rename ($tempDest,$dest);
- #print " \n ****$tempDest";
- }
- #Closing the file handlers.
- close(TEMPDEST);
- close(SRC);
- if ( ( ( $^O eq "solaris" ) || ( $^O eq "linux" ) )
- && ( $dest =~ /\.sh$/ ) )
- {
- chmod( 0755, $dest );
- }
- }
Add Comment
Please, Sign In to add comment