File: copyright.t

package info (click to toggle)
libfilter-signatures-perl 0.19-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 236 kB
  • sloc: perl: 1,321; makefile: 2
file content (95 lines) | stat: -rw-r--r-- 2,472 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!perl
use warnings;
use strict;
use File::Find;
use Test::More tests => 1;
use POSIX 'strftime';

my $this_year = strftime '%Y', localtime;

my $last_modified_year = 0;

my $is_checkout = -d '.git';

require './Makefile.PL';
# Loaded from Makefile.PL
our %module = get_module_info();

my @files;
#my $blib = File::Spec->catfile(qw(blib lib));
find(\&wanted, grep { -d } ('lib'));

if( my $exe = $module{EXE_FILES}) {
    push @files, @$exe;
};

sub wanted {
  push @files, $File::Find::name if /\.p(l|m|od)$/;
}

sub collect {
    my( $file ) = @_;
    note $file;
    my $modified_ts;
    if( $is_checkout ) {
        # diag `git log -1 --pretty="format:%ct" "$file"`;
        $modified_ts = `git log -1 --pretty="format:%ct" "$file"`;
    } else {
        $modified_ts = (stat($_))[9];
    }

    my $modified_year;
    if( $modified_ts ) {
        $modified_year = strftime('%Y', localtime($modified_ts));
    } else {
        $modified_year = 1970;
    };

    open my $fh, '<', $file
        or die "Couldn't read $file: $!";
    my @copyright = map {
                        /\bcopyright\b.*?\d{4}-(\d{4})\b/i
                        ? [ $_ => $1 ]
                        : ()
                    }
                    <$fh>;
    my $copyright = 0;
    for (@copyright) {
        $copyright = $_->[1] > $copyright ? $_->[1] : $copyright;
    };

    return {
        file => $file,
        copyright_lines => \@copyright,
        copyright => $copyright,
        modified => $modified_year,
    };
};

my @results;
for my $file (@files) {
    push @results, collect($file);
};

for my $file (@results) {
    $last_modified_year = $last_modified_year < $file->{modified}
                          ? $file->{modified}
                          : $last_modified_year;
};

note "Distribution was last modified in $last_modified_year";

my @out_of_date = grep { $_->{copyright} and $_->{copyright} < $last_modified_year } @results;

if(! is 0+@out_of_date, 0, "All files have a current copyright year ($last_modified_year)") {
    for my $file (@out_of_date) {
        diag sprintf "%s modified %d, but copyright is %d", $file->{file}, $file->{modified}, $file->{copyright};
        diag $_ for map {@$_} @{ $file->{copyright_lines}};
    };
    diag q{To fix (in a rough way, please review) run};
    diag sprintf q{    perl -i -ple 's!(\bcopyright\b.*?\d{4}-)(\d{4})\b!${1}%s!i' %s},
        $this_year,
        join ' ',
        map { $_->{file} } @out_of_date;
};