Debian Codegraph @

xt/copyright.t

raw
1 #!perl
2 use warnings;
3 use strict;
4 use File::Find;
5 use Test::More tests => 1;
6 use POSIX 'strftime';
7
8 my $this_year = strftime '%Y', localtime;
9
10 my $last_modified_year = 0;
11
12 my $is_checkout = -d '.git';
13
14 require './Makefile.PL';
15 # Loaded from Makefile.PL
16 our %module = get_module_info();
17
18 my @files;
19 #my $blib = File::Spec->catfile(qw(blib lib));
20 find(\&wanted, grep { -d } ('lib'));
21
22 if( my $exe = $module{EXE_FILES}) {
23 push @files, @$exe;
24 };
25
26 sub wanted {
27 push @files, $File::Find::name if /\.p(l|m|od)$/;
28 }
29
30 sub collect {
31 my( $file ) = @_;
32 note $file;
33 my $modified_ts;
34 if( $is_checkout ) {
35 # diag `git log -1 --pretty="format:%ct" "$file"`;
36 $modified_ts = `git log -1 --pretty="format:%ct" "$file"`;
37 } else {
38 $modified_ts = (stat($_))[9];
39 }
40
41 my $modified_year;
42 if( $modified_ts ) {
43 $modified_year = strftime('%Y', localtime($modified_ts));
44 } else {
45 $modified_year = 1970;
46 };
47
48 open my $fh, '<', $file
49 or die "Couldn't read $file: $!";
50 my @copyright = map {
51 /\bcopyright\b.*?\d{4}-(\d{4})\b/i
52 ? [ $_ => $1 ]
53 : ()
54 }
55 <$fh>;
56 my $copyright = 0;
57 for (@copyright) {
58 $copyright = $_->[1] > $copyright ? $_->[1] : $copyright;
59 };
60
61 return {
62 file => $file,
63 copyright_lines => \@copyright,
64 copyright => $copyright,
65 modified => $modified_year,
66 };
67 };
68
69 my @results;
70 for my $file (@files) {
71 push @results, collect($file);
72 };
73
74 for my $file (@results) {
75 $last_modified_year = $last_modified_year < $file->{modified}
76 ? $file->{modified}
77 : $last_modified_year;
78 };
79
80 note "Distribution was last modified in $last_modified_year";
81
82 my @out_of_date = grep { $_->{copyright} and $_->{copyright} < $last_modified_year } @results;
83
84 if(! is 0+@out_of_date, 0, "All files have a current copyright year ($last_modified_year)") {
85 for my $file (@out_of_date) {
86 diag sprintf "%s modified %d, but copyright is %d", $file->{file}, $file->{modified}, $file->{copyright};
87 diag $_ for map {@$_} @{ $file->{copyright_lines}};
88 };
89 diag q{To fix (in a rough way, please review) run};
90 diag sprintf q{ perl -i -ple 's!(\bcopyright\b.*?\d{4}-)(\d{4})\b!${1}%s!i' %s},
91 $this_year,
92 join ' ',
93 map { $_->{file} } @out_of_date;
94 };
95