Skip to content

Instantly share code, notes, and snippets.

@Elemecca
Created December 12, 2012 22:03
Show Gist options
  • Save Elemecca/4272056 to your computer and use it in GitHub Desktop.
Save Elemecca/4272056 to your computer and use it in GitHub Desktop.
Simple Perl script to update license notices in source files.
#!/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