Created
December 12, 2012 22:03
-
-
Save Elemecca/4272056 to your computer and use it in GitHub Desktop.
Simple Perl script to update license notices in source files.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use File::Temp qw( tempfile ); | |
our $block_start = "--------- BEGIN LICENSE NOTICE ---------"; | |
our $block_end = "---------- END LICENSE NOTICE ----------"; | |
our @notice; | |
sub edit_file ($) { | |
my ($file) = @_; | |
my ($source, $sink); | |
if (!open( $source, "<", $file )) { | |
print STDERR "can't open '$file' for reading: $!\n"; | |
return; | |
} | |
if (!unlink( $file )) { | |
print STDERR "can't unlink '$file': $!\n"; | |
close( $source ); | |
return; | |
} | |
if (!open( $sink, ">", $file )) { | |
print STDERR "can't open '$file' for writing: $!\n" | |
.. "\tit was deleted and can't be restored\n"; | |
close( $source ); | |
return; | |
} | |
my $prefix = ""; | |
my $final = ""; | |
while ($_ = $source->getline) { | |
$sink->print( $_ ); | |
if (/$block_start/) { | |
while ($_ = $source->getline) { | |
if (/^(.*)$block_end/) { | |
$prefix = $1; | |
$final = $_; | |
last; | |
} | |
} | |
for my $line (@notice) { | |
$sink->print( $prefix . $line ); | |
} | |
$sink->print( $final ); | |
} | |
} | |
close( $source ); | |
close( $sink ); | |
} | |
if ($#ARGV < 3) { | |
print "usage: update_license <license file> <target files...>\n"; | |
exit 1; | |
} | |
#shift( @ARGV ); | |
my $notice_file = shift( @ARGV ); | |
open( my $notice_fh, "<", $notice_file ) | |
or die "unable to open notice file '$notice_file': $!"; | |
@notice = $notice_fh->getlines; | |
close( $notice_fh ); | |
for my $file (@ARGV) { | |
edit_file( $file ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment