Skip to content

Commit

Permalink
feat: Basic copyright script
Browse files Browse the repository at this point in the history
This script does basic copyright notice checks, and will be
used to add the necessary features in issue ietf-tools#7690.
  • Loading branch information
richsalz committed Jul 20, 2024
1 parent c5ca0ea commit 386db49
Showing 1 changed file with 111 additions and 0 deletions.
111 changes: 111 additions & 0 deletions ietf/check-copyright
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
#!/usr/bin/env perl
# Copyright The IETF Trust 2024. All Rights Reserved.

use strict;
use warnings;
use POSIX qw/strftime/;
use Getopt::Std;

# Getopt settings.
our($opt_h, $opt_c, $opt_l, $opt_m);

# Convenience variables, for the copyright year regexp patterns.
my $this_year = strftime("%Y", localtime);
my $some_year = "[12][0-9][0-9][0-9]";
my $year_range = "(${some_year})(-${some_year})?";
my $copyright = "Copyright The IETF Trust *${year_range}";

sub
usage()
{
my $retcode = pop();

print STDERR "Options:\n";
print STDERR " -h This help message\n";
print STDERR " -c Modify files that should be changed\n";
print STDERR " (Does not do a git commit)\n";
print STDERR " -l List files that need to be changed\n";
print STDERR " -m List files missing copyright\n";
exit $retcode;
}


## Get list of files changed during this year.
sub
collect_files
{
# Get last commit of the of the previous year/
my $FIRST=`git rev-list -1 --before=$this_year-01-01 HEAD`;
chop $FIRST;

# Get every file changed since then, ignoring deleted files.
open(my $FH, "-|", "git diff-tree -r --name-status $FIRST..HEAD")
|| die "Can't open diff-tree, $!";
my @FILES = ();
my @FIELDS;
while ( <$FH> ) {
@FIELDS = split();
next if $FIELDS[0] =~ /D/; # ignore deleted files
next if $FIELDS[1] =~ /\.zip/; # ignore ZIP files
push(@FILES, $FIELDS[1]);
}
close($FH) || die "Can't close diff-tree";
return @FILES;
}

## Process file, notice if copyright is missing or outdated.
sub
process()
{
my $NAME = pop();
my $NEW = "";
my $found = 0;
my $changed = 0;
my $SAVE;

# Read the file, copying to $NEW and changing copyright
# along the way.
open my $FH, '<', $NAME || die "Can't open $NAME, $!";
while ( <$FH> ) {
$SAVE = $_;
if ( /$copyright/io ) {
$found = 1;
$SAVE =~ s|${year_range}|$1-${this_year}|;
$SAVE =~ s|(${some_year})-$1|$1|;
$changed = 1 if $SAVE ne $_;
}
$NEW .= $SAVE;
}
close($FH) || die "Can't close $NAME, $!";

# Copyright missing?
if ( defined $opt_m && ! $found ) {
print "$NAME\n";
return;
}

# Unchanged file?
return if ! $changed;

if ( defined $opt_l ) {
print "$NAME\n";
} else {
# Write the new file
open my $FH, '>', $NAME || die "Can't close-write $NAME, $!";
print $FH $NEW;
close($FH) || die "Can't close-write $NAME, $!";
}
}

## Parse JCL.
getopts('hclm') || &usage(1);
&usage(0) if $opt_h;
if ( defined($opt_m) + defined($opt_l) + defined($opt_c) != 1 ) {
print STDERR "Must have exactly one of m/l/c options\n";
exit 1;
}

## Do the work.
foreach my $F ( &collect_files() ) {
&process($F);
}

0 comments on commit 386db49

Please sign in to comment.