-
-
Notifications
You must be signed in to change notification settings - Fork 145
/
updatecopyright.d
391 lines (328 loc) · 11 KB
/
updatecopyright.d
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
#!/usr/bin/env rdmd
/**
Update the copyright notices in source files so that they have the form:
---
Copyright XXXX-YYYY by The D Language Foundation, All Rights Reserved
---
It does not change copyright notices of authors that are known to have made
changes under a proprietary license.
Copyright: Copyright (C) 2017-2018 by The D Language Foundation, All Rights Reserved
License: $(WEB boost.org/LICENSE_1_0.txt, Boost License 1.0)
Authors: Iain Buclaw
Example usage:
---
updatecopyright.d --update-year src/dmd
---
*/
module tools.updatecopyright;
int main(string[] args)
{
import std.getopt;
bool updateYear;
bool verbose;
auto opts = getopt(args,
"update-year|y", "Update the current year on every notice", &updateYear,
"verbose|v", "Be more verbose", &verbose);
if (args.length == 1 || opts.helpWanted)
{
defaultGetoptPrinter("usage: updatecopyright [--help|-h] [--update-year|-y] <dir>...",
opts.options);
return 0;
}
Copyright(updateYear, verbose).run(args[1 .. $]);
return 0;
}
//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
struct Copyright
{
import std.algorithm : any, canFind, each, filter, joiner, map;
import std.array : appender, array;
import std.file : DirEntry, SpanMode, dirEntries, remove, rename;
import std.stdio : File, stderr, stdout;
import std.string : endsWith, strip, stripLeft, stripRight;
import std.regex : Regex, matchAll, regex;
// The author to use in copyright notices.
enum author = "The D Language Foundation, All Rights Reserved";
// The standard (C) form.
enum copyright = "(C)";
private:
// True if running in verbose mode.
bool verbose = false;
// True if also updating copyright year.
bool updateYear = false;
// An associative array of known copyright holders.
// Value set to true if the copyright holder is internal.
bool[string] holders;
// Files and directories to ignore during search.
static string[] skipDirs = [
"docs",
"ini",
"test",
"samples",
"vcbuild",
".git",
];
static string[] skipFiles = [
"Jenkinsfile",
"LICENSE.txt",
"VERSION",
".a",
".ddoc",
".deps",
".lst",
".map",
".md",
".o",
".obj",
".sdl",
".sh",
".yml",
];
// Characters in a range of years.
// Include '.' for typos, and '?' for unknown years.
enum rangesStr = `[0-9?](?:[-0-9.,\s]|\s+and\s+)*[0-9]`;
// Non-whitespace characters in a copyright holder's name.
enum nameStr = `[\w.,-]`;
// Matches a full copyright notice:
// - 'Copyright (C)', etc.
// - The years. Includes the whitespace in the year, so that we can
// remove any excess.
// - 'by ', if used
// - The copyright holder.
Regex!char copyrightRe;
// A regexp for notices that might have slipped by.
Regex!char otherCopyrightRe;
// A regexp that matches one year.
Regex!char yearRe;
// Matches part of a year or copyright holder.
Regex!char continuationRe;
Regex!char commentRe;
// Convenience for passing around file/line number information.
struct FileLocation
{
string filename;
size_t linnum;
string toString()
{
import std.format : format;
return "%s(%d)".format(this.filename, this.linnum);
}
}
FileLocation location;
char[] previousLine;
void processFile(string filename)
{
import std.conv : to;
// Looks like something we tried to create before.
if (filename.endsWith(".tmp"))
{
remove(filename);
return;
}
auto file = File(filename, "rb");
auto output = appender!string;
int errors = 0;
bool changed = false;
output.reserve(file.size.to!size_t);
// Reset file location information.
this.location = FileLocation(filename, 0);
this.previousLine = null;
foreach (line; file.byLine)
{
this.location.linnum++;
try
{
changed |= this.processLine(line, output, errors);
}
catch (Exception)
{
if (this.verbose)
stderr.writeln(filename, ": bad input file");
errors++;
break;
}
}
file.close();
// If something changed, write the new file out.
if (changed && !errors)
{
auto tmpfilename = filename ~ ".tmp";
auto tmpfile = File(tmpfilename, "w");
tmpfile.write(output.data);
tmpfile.close();
rename(tmpfilename, filename);
}
}
bool processLine(String, Array)(String line, ref Array output, ref int errors)
{
bool changed = false;
if (this.previousLine)
{
auto continuation = this.stripContinuation(line);
// Merge the lines for matching purposes.
auto mergedLine = this.previousLine.stripRight() ~ `, ` ~ continuation;
auto mergedMatch = mergedLine.matchAll(copyrightRe);
if (!continuation.matchAll(this.continuationRe) ||
!mergedMatch || !this.isComplete(mergedMatch))
{
// If the next line doesn't look like a proper continuation,
// assume that what we've got is complete.
auto match = this.previousLine.matchAll(copyrightRe);
changed |= this.updateCopyright(line, match, errors);
output.put(this.previousLine);
output.put('\n');
}
else
{
line = mergedLine;
}
this.previousLine = null;
}
auto match = line.matchAll(copyrightRe);
if (match)
{
// If it looks like the copyright is incomplete, add the next line.
if (!this.isComplete(match))
{
this.previousLine = line.dup;
return changed;
}
changed |= this.updateCopyright(line, match, errors);
}
else if (line.matchAll(this.otherCopyrightRe))
{
stderr.writeln(this.location, ": unrecognised copyright: ", line.strip);
//errors++; // Only treat this as a warning for now...
}
output.put(line);
output.put('\n');
return changed;
}
String stripContinuation(String)(String line)
{
line = line.stripLeft();
auto match = line.matchAll(this.commentRe);
if (match)
{
auto captures = match.front;
line = captures.post.stripLeft();
}
return line;
}
bool isComplete(Match)(Match match)
{
auto captures = match.front;
return captures.length >= 5 && captures[4] in this.holders;
}
bool updateCopyright(String, Match)(ref String line, Match match, ref int errors)
{
auto captures = match.front;
if (captures.length < 5)
{
stderr.writeln(this.location, ": missing copyright holder");
errors++;
return false;
}
// See if copyright is associated with package author.
// Update the author so as to be consistent everywhere.
auto holder = captures[4];
if (holder !in this.holders)
{
stderr.writeln(this.location, ": unrecognised copyright holder: ", holder);
errors++;
return false;
}
else if (!this.holders[holder])
return false;
// Update the copyright years.
auto years = captures[2].strip;
if (!this.canonicalizeYears(years))
{
stderr.writeln(this.location, ": unrecognised year string: ", years);
errors++;
return false;
}
// Make sure (C) is present.
auto intro = captures[1];
if (intro.endsWith("right"))
intro ~= " " ~ this.copyright;
else if (intro.endsWith("(c)"))
intro = intro[0 .. $ - 3] ~ this.copyright;
// Construct the copyright line, removing any 'by '.
auto newline = captures.pre ~ intro ~ " " ~ years ~ " by " ~ this.author ~ captures.post;
if (line != newline)
{
line = newline;
return true;
}
return false;
}
bool canonicalizeYears(String)(ref String years)
{
import std.conv : to;
import std.datetime : Clock;
auto yearList = years.matchAll(this.yearRe).map!(m => m.front).array;
if (yearList.length > 0)
{
auto minYear = yearList[0];
auto maxYear = yearList[$ - 1];
// Update the upper bound, if enabled.
if (this.updateYear)
maxYear = to!String(Clock.currTime.year);
// Use a range.
if (minYear == maxYear)
years = minYear;
else
years = minYear ~ "-" ~ maxYear;
return true;
}
return false;
}
public:
this(bool updateYear, bool verbose)
{
this.updateYear = updateYear;
this.verbose = verbose;
this.copyrightRe = regex(`([Cc]opyright` ~ `|[Cc]opyright\s+\([Cc]\))` ~
`(\s*(?:` ~ rangesStr ~ `,?)\s*)` ~
`(by\s+)?` ~
`(` ~ nameStr ~ `(?:\s?` ~ nameStr ~ `)*)?`);
this.otherCopyrightRe = regex(`copyright.*[0-9][0-9]`, `i`);
this.yearRe = regex(`[0-9?]+`);
this.continuationRe = regex(rangesStr ~ `|` ~ nameStr);
this.commentRe = regex(`#+|[*]+|;+|//+`);
this.holders = [
"Digital Mars" : true,
"Digital Mars, All Rights Reserved" : true,
"The D Language Foundation, All Rights Reserved" : true,
"The D Language Foundation" : true,
// List of external authors.
"Northwest Software" : false,
"RSA Data Security, Inc. All rights reserved." : false,
"Symantec" : false,
];
}
// Main loop.
void run(string[] args)
{
// Returns true if entry should be skipped for processing.
bool skipPath(DirEntry entry)
{
import std.path : baseName, dirName, pathSplitter;
if (!entry.isFile)
return true;
if (entry.dirName.pathSplitter.filter!(d => this.skipDirs.canFind(d)).any)
return true;
auto basename = entry.baseName;
if (this.skipFiles.canFind!(s => basename.endsWith(s)))
{
if (this.verbose)
stderr.writeln(entry, ": skipping file");
return true;
}
return false;
}
args.map!(arg => arg.dirEntries(SpanMode.depth).filter!(a => !skipPath(a)))
.joiner.each!(f => this.processFile(f));
}
}