-
Notifications
You must be signed in to change notification settings - Fork 4
/
addlicense.pl
492 lines (408 loc) · 12.4 KB
/
addlicense.pl
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
#!/usr/bin/perl -w
#
# Copyright (c) 2002 Steve Slaven, All Rights Reserved.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of
# the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA 02111-1307 USA
#
# Adds a license header to a file, unless it detects one already present
# can also recognize a few types of files and tries to add the license
# as proper comments
use strict;
use vars qw( %ENV );
use Getopt::Std;
use File::Copy;
my %licenses = (
gpl2 => q{Copyright (c) [%year%] [%author%], All Rights Reserved.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of
the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston,
MA 02111-1307 USA},
perl => q{Copyright (c) [%year%] [%author%]. All rights reserved.
This program is free software and is provided "as is" without
express or implied warranty. You can redistribute it and/or
modify it under the same terms as Perl itself.},
mpl => q{Copyright (c) [%year%] [%author%]. All rights reserved.
The contents of this file are subject to the Mozilla Public
License Version 1.1 (the "License"); you may not use this file
except in compliance with the License. You may obtain a copy of
the License at http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS
IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
implied. See the License for the specific language governing
rights and limitations under the License.},
consultant => q{Copyright (c) [%year%] [%author%]. All rights reserved.
The contents of this file are provided under a nontransferrable,
nonexclusive license. This license may be terminated at any
time without reason, notice, or consent, at which time all
copies of the file must be removed including but not limited
to copies in production use, on backup media, or in print. This
license shall be terminated in the event the Licensee fails to
comply to the terms of this license.
This file may not be reproduced in part or in whole without the
express written consent of the copyright holder, including but
not limited to distribution to affiliates, internal developers,
3rd parties, or published in print or electronically.
SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THOSE
RELATING TO MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
LIMITATION OF LIABILITY: LICENSOR SHALL NOT BE LIABLE FOR ANY
LOSS OF PROFITS, LOSS OF USE, LOSS OF DATA, INTERRUPTIONS
OF BUSINESS, NOR FOR INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL
DAMAGES OF ANY KIND WHETHER UNDER THIS AGREEMENT OR OTHERWISE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
No rights are granted to the Licensee that are not explicitly
stated in this license unless contained in a supplemental
written contract signed by both parties.} );
# Regex to detect if a license is in a file
my %detect = (
gpl2 => 'GNU General',
perl => 'same terms as Perl itself',
mpl => 'Mozilla Public',
consultant => 'under a nontransferrable,'
);
# Subs to commentify for languanges
my %commentify = (
perl => \&comment_hash,
sh => \&comment_hash,
c => \&comment_c,
cpp => \&comment_cpp,
text => \&comment_text
);
# Subs to handle inserting
my %insertify = (
perl => \&insert_line2,
sh => \&insert_line2,
c => \&insert_line1,
cpp => \&insert_line1,
text => \&insert_line1,
);
# Output of 'file' command to autodetect file types
my %autodetect = (
perl => 'perl script',
text => '(text|data)',
c => 'C program text',
cpp => 'C++ program text',
sh => 'shell script'
);
my %extensions = (
pm => 'perl',
pl => 'perl',
c => 'c',
cpp => 'cpp',
sh => 'sh'
);
my %o;
# Handle defaults
$o{ l } = 'gpl2';
$o{ s } = 3;
$o{ a } = $ENV{ USER };
# Load rc
my %kwswitch = (
author => 'a',
license => 'l',
language => 'L',
space => 's',
force => 'f'
);
if( open( IN, "$ENV{HOME}/.add_licenserc" ) ) {
my $line;
my( $kw, $val );
my( @splitup );
while( $line = <IN> ) {
chomp( $line );
$line =~ s/#.*$//;
$line =~ s/^\s*//;
( $kw, $val ) = $line =~ /^(\S+)\s*=\s*(.*)/;
$val =~ s/\s+$// if $val;
if( $kw ) {
if( $kwswitch{ $kw } ) {
$o{ $kwswitch{ $kw } } = $val;
}else{
# Maybe it's a 'license' key?
if( $kw eq 'newlicense' ) {
# Yep, read in the license
@splitup = split( /\t/, $val );
$splitup[ 2 ] = '' unless $splitup[ 2 ];
print "Adding license: $splitup[0]\n";
print " from $splitup[1]\n";
print " detected by '$splitup[2]'\n";
$splitup[ 1 ] =~ s/^~/$ENV{HOME}/;
open( NEWLICENSE, $splitup[1] ) ||
warn( "Unable to open '$splitup[1]': $!" );
$licenses{ $splitup[ 0 ] } =
join( '', <NEWLICENSE> );
close( NEWLICENSE );
$detect{ $splitup[ 0 ] } =
$splitup[ 2 ];
}else{
warn( "Unknown keyword: '$kw'" );
}
}
}
}
close( IN );
}
# Get cmdline
getopts( 'hfL:l:s:a:', \%o );
# Load substitutions
my %subs = (
year => ( localtime() )[ 5 ] + 1900,
author => $o{ a }
);
# Force help unless files
$o{ h } = 1 unless $ARGV[ 0 ];
die( usage() ) if $o{ h };
# Handle unknown
die( "Unknown license '$o{l}'" ) unless defined( $licenses{ $o{ l } } );
# Check if already present and detect languages
my @files;
my( $file, $lang, $lic, $force );
for $file ( @ARGV ) {
# Handle lang/lic parse
$lang = $lic = $force = undef;
if( $file =~ /^!/ ) {
$file =~ s/^.//;
$force = 1;
}
if( $file =~ /:/ ) {
( $lic, $file ) = $file =~ /^(\S+?):(.*)/;
if( $lic =~ /,/ ) {
# Grab language
( $lic, $lang ) = split( /,/, $lic );
}
}
push( @files, {
file => $file,
lang => $lang,
lic => $lic || $o{ l },
force => $force || $o{ f }
} );
# Check if file has thing
if( -f $file ) {
if( open( IN, $file ) ) {
if( ! defined( $licenses{ $files[ -1 ] -> { lic } } ) ) {
warn( sprintf( "Unknown license: '%s'",
$files[ -1 ] -> { lic } ) );
@files = remove_file( $file, @files );
}elsif( grep( /\Q$detect{ $files[ -1 ] -> { lic } }\E/,
<IN> ) &&
( ! $files[ -1 ] -> { force } ) ) {
warn( sprintf( "'%s' already has license '%s'",
$file,
$files[ -1 ] -> { lic } ) );
@files = remove_file( $file, @files );
}else{
# Handle lang detect if needed
$files[ -1 ] -> { lang } = $o{ L }
if $o{ L };
if( ! $files[ -1 ] -> { lang } ) {
$files[ -1 ] -> { lang } =
detect_lang( $files[ -1 ] ->
{ file } );
}
# Verify known language
if( ! $commentify{
$files[ -1 ] -> { lang } } ) {
warn( sprintf( "Unknown languange '%s'",
$files[ -1 ] -> { lang } ) );
@files = remove_file( $file, @files );
}
}
close( IN );
}else{
warn( "Unable to open '$file': $!" );
@files = remove_file( $file, @files );
}
}else{
print "File '$_' does not exist\n";
@files = remove_file( $file, @files );
}
}
die( "No files to process" ) unless scalar( @files ) > 0;
# Make commented
my $insert;
for( @files ) {
# Notify the user
printf( 'Updating: "%s" as "%s" with "%s"',
$_ -> { file },
$_ -> { lang },
$_ -> { lic } );
print "\n";
# Get initial commented license
$insert = &{ $commentify{ $_ -> { lang } } }( $_ -> { lic } );
# Do subs
$insert =~ s/\[%(.*?)%\]/$subs{$1}/ge;
# Handle insertion
$file = $_ -> { file };
copy( $file, "$file.tmp" );
&{ $insertify{ $_ -> { lang } } }( "$file.tmp", $file, $insert );
unlink( "$file.tmp" );
}
# Commenting subs
sub comment_hash {
# Makes it perl commentish, or shell
return( comment_generic( "#", shift() ) );
}
sub comment_cpp {
return( comment_generic( "//", shift() ) );
}
sub comment_text {
return( comment_generic( "", shift() ) );
}
sub comment_c {
return( "/*\n" .
comment_generic( " *", shift() ) .
"*/\n" );
}
sub comment_generic {
# Takes a preceding comment char, and commentifies the license
my $char = shift;
my $license = shift;
my $ret;
for( split( /\n/, get_license( $license ), -1 ) ) {
$ret .= $char . " " x $o{ s } . $_ . "\n";
}
return( $ret );
}
sub get_license {
return( "\n" . $licenses{ shift() } . "\n" );
}
# Insertification
sub insert_line1 {
# For c/cpp/text/etc where license starts at line 1
my $input = shift;
my $output = shift;
my $insert = shift;
local *OUT;
local *IN;
open( IN, $input ) || die( "Couldn't open '$input': $!" );
open( OUT, ">$output" ) || die( "Couldn't open '$output': $!" );
print OUT $insert;
print OUT <IN>;
close( OUT );
close( IN );
}
sub insert_line2 {
# Used for shell scripts, etc, where license should be
# right after the first line
my $input = shift;
my $output = shift;
my $insert = shift;
local *OUT;
local *IN;
open( IN, $input ) || die( "Couldn't open '$input': $!" );
open( OUT, ">$output" ) || die( "Couldn't open '$output': $!" );
print OUT scalar( <IN> );
print OUT $insert;
print OUT <IN>;
close( OUT );
close( IN );
}
sub usage {
my $usage = qq{
add_license v1.30
Adds license notification to a source file
Author: Steve Slaven - http://hoopajoo.net
Usage: $0 [-hf] [-s whitespace] [-l default license] [-L force language]
[!license,language:]file [ file ... ]
-h This help
-f Force insertion even if it looks like the license is
already in place for all files
-s Number of spaces between comments and start of license
-l Default license when not explicitly specified
-L Force language, all files will be assumed to be this type
! Force insertion for this file only
lang Language to use when commentifying the license (see below)
license License to include (see below)
add_license does rudementory checks to see if the license is already
in place, and if not, will modify the file to include the requested
license.
Configuration options can also be placed in a HOME/.add_licenserc
in the format:
keyword = value
With the following keywords equal to the following switches:
author => 'a'
license => 'l'
space => 's'
New licenses can be added with the newlicense keyword, followed
by the name, filename, and detect string which MUST BE seperated
by TABS.
So a simple RC might contain:
-- begin --
# Set common defaults here
author = Steve Slaven
space = 2
license = gpl2
newlicense = my_license ~/licenses/my_license Licensed under MY LICENSE
-- end --
The last part of the my_license thing is used to detect if the
license has already been added. In the license file special
keyword sequences can be used to input special data, such as date
or author. They are enclosed in \[\%keyword\%\] sequences.
author The author as defined in the RC file
year Current 4 digit year
};
$usage .= "Licenses:\n";
for( keys( %licenses ) ) {
$usage .= " $_\n";
}
$usage .= "\nLanguages:\n";
for( keys( %commentify ) ) {
$usage .= " $_\n";
}
# Print current opts
$usage .= "\nCurrent Options:\n";
$usage .= join( "\n", map { " $_ => $o{$_}" } keys( %o ) );
$usage .= "\n\nCurrent Subs (keyword seqs):\n";
$usage .= join( "\n", map { " $_ => $subs{$_}" } keys( %subs ) );
$usage .= "\n";
return( $usage );
}
# Returns all but $1
sub remove_file {
my $file = shift;
my @files;
for( @_ ) {
if( $_ -> { file } ne $file ) {
push( @files, $_ );
}
}
return( @files );
}
# Tries to detect language type
sub detect_lang {
my $file = shift;
# Uses 'file'
my $is = `file "$file"`;
for( keys( %autodetect ) ) {
if( $is =~ /\Q$autodetect{$_}\E/ ) {
return( $_ );
}
}
# Try by extension
my $ext;
( $ext ) = $file =~ /\.([a-zA-Z0-9]+)$/;
return( $extensions{ $ext } ) if $extensions{ $ext };
return( 'unknown' );
}