-
Notifications
You must be signed in to change notification settings - Fork 3
/
update_copyright
executable file
·206 lines (164 loc) · 5.36 KB
/
update_copyright
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/usr/bin/env perl
#
# update_copyright - add missing years to the Genome Research Ltd copyright
# notice. This script only updates from the last written
# date it can find so as not to rewrite history.
#
# Prints out a list of files where the appropriate copyright
# notice is not found.
#
# Author: Andrew Whitwham <[email protected]>
#
# Copyright (C) 2019-2021 Genome Research Ltd.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
#
#
#
# Usage: run in git repository. Prints out a list of problem files
# it cannot handle.
use strict;
use warnings;
use File::Copy;
my $cmd = 'git ls-tree --full-tree -r --name-only HEAD';
open my $gh, '-|', $cmd or die "Cannot run \"$cmd\"\n";
while ((my $filename = <$gh>)) {
chomp $filename;
# file names to ignore
if ($filename =~ /\.(s|b|cr)am|\.out|\.expected|\.yml|\.reg|\.m4|\.vcf|\.f(a|q)|\.bed|\.gz|\.gff|\.csi|^\./) {
next;
}
my $log = "git log --stat -w $filename";
open my $sh, '-|', $log or die "Cannot run \"$log\"\n";
my %year;
my $date;
my $ignore = 0;
# These are commits in samtools we choose not to use for copyright updates.
my $exluded = '4363c1f8|59e644fc|62c34584|70062453|74a3e0a7|904f1f56|d779e259|fcb8af08';
while ((my $statline = <$sh>)) {
if ($statline =~ /^commit\s+($exluded)/) {
$ignore = 1;
}
if ($statline =~ /^Author:\s+John Marshall/) {
# does a lot of work, but not GRL copyright
$ignore = 1;
}
if ($statline =~ /^Date:/) {
if ($statline =~ /\w{3}\s\w{3}\s\d+\s\d{2}:\d{2}:\d{2}\s(\d{4})\s/) {
$date = $1;
}
}
if ($date) {
if ($statline =~ /^\s+$filename\s+\|\s+(\d+)/) {
if (!$ignore) {
$year{$date} += $1;
} else {
$ignore = 0;
}
}
}
}
my @active;
for my $y (sort keys %year) {
if ($year{$y} > 10) {
push @active, $y;
}
}
close $sh;
if (scalar @active) {
my $workname = "$filename.working";
my $changed = 0;
open my $inf, '<', $filename or die "Cannot open $filename\n";
open my $out, '>', $workname or die "Cannot open $workname\n";
while ((my $inline = <$inf>)) {
if ($inline =~ /(C|c)opyright \((c|C)\)[\s0-9\-,]*Genome\s+Research (Ltd|Limited)/) {
if ($inline =~ /\A.*(\d{4})/s) {
my $last = $1;
my $mod_dates = make_dates($last, @active);
$inline =~ s/$last/$mod_dates/;
# This is fairly horrible but it does work
if ($inline =~ /(\d{4})-\d{4}-(\d{4})/) {
my $beg = $1;
my $end = $2;
$inline =~ s/\d{4}-\d{4}-\d{4}/${beg}-$end/;
}
$changed = 1;
}
}
print $out $inline;
}
close $inf;
close $out;
if ($changed) {
my $result = copy($workname, $filename);
if (!$result) {
print "Error copying $workname to $filename.\n";
} else {
unlink $workname;
}
} else {
unlink $workname;
print "$filename\n";
}
}
}
close $gh;
# Layout the years, either with a dash for contiguous years
# e.g. 2011-2013 or comma separated otherwise e.g. 2011, 2013, 2015.
sub make_dates {
my ($in_date, @act) = @_;
my $ad = 0;
my $out_str;
my $fst;
my $lst;
$fst = $lst = $in_date;
for my $y (@act) {
if (!$fst) {
$fst = $y;
$lst = $y;
next;
} elsif ($y <= $fst) {
next;
}
if ($y - $lst > 1) {
if ($ad) {
$out_str .= ", ";
}
if ($fst == $lst) {
$out_str .= $fst;
} else {
$out_str .= "${fst}-${lst}";
}
$fst = $y;
$lst = $y;
$ad = 1;
} else {
$lst = $y;
}
}
if ($ad) {
$out_str .= ", ";
}
if ($fst == $lst) {
$out_str .= "$fst";
} else {
$out_str .= "${fst}-${lst}";
}
return $out_str;
}