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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
|
#!/usr/bin/perl
# IBM_PROLOG_BEGIN_TAG
# This is an automatically generated prolog.
#
# $Source: src/build/tools/addCopyright.pl $
#
# OpenPOWER HostBoot Project
#
# Contributors Listed Below - COPYRIGHT 2011,2014
# [+] International Business Machines Corp.
# [+] Google Inc.
#
#
# Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied. See the License for the specific language governing
# permissions and limitations under the License.
#
# IBM_PROLOG_END_TAG
#################################### ABOUT ####################################
# Forked from: #
# Author: Mark Jerde (mjerde@us.ibm.com) #
# Date: Fri Mar 19 17:40:32 2010 UTC #
# #
# addCopyright.pl will automatically insert appropriate copyright statements #
# in source files following the IBM copyright guidelines (and templates) #
# referenced below : #
# FSP ClearCase Architecture #
# Version 1.9 #
# 10/12/2010 #
# Editor: Alan Hlava #
# #
# Section 3.14.1 of the above doc has templates for different files #
# #
# NOTE: FSP uses the phrase "OCO Source materials" in their copyright #
# block, which is classified as 'p1' . We will use the same #
# classification here. #
# NOTE: to list all files in src EXCEPT the build dir, run: #
# make clean # remove autogenerated files #
# find src -path 'src/build' -prune -o ! -type d -print | tr '\n' ' ' #
# #
# addCopyright.pl does not support piping, but you can send these #
# to a file, add "addCopyright.pl update" to the beginning of the line, #
# and run the file to update all #
###############################################################################
use strict;
use warnings;
use POSIX;
use Getopt::Long;
use File::Basename;
use lib dirname (__FILE__);
#------------------------------------------------------------------------------
# Project-specific settings.
#------------------------------------------------------------------------------
my $ReleaseYear = `date +%Y`;
chomp( $ReleaseYear );
$ReleaseYear = $ENV{'DATE_OVERRIDE'} if defined $ENV{'DATE_OVERRIDE'};
my $copyrightSymbol = "";
# my $copyrightSymbol = "(C)"; # Uncomment if unable to use character.
# set by environment variable in project env.bash
my $projectName = $ENV{'PROJECT_NAME'};
my $copyrightStr = "Contributors Listed Below - COPYRIGHT";
## note that these use single ticks so that the escape chars are NOT evaluated yet.
my $OLD_DELIMITER_END = 'IBM_PROLOG_END';
my $DELIMITER_END = 'IBM_PROLOG_END_TAG';
my $DELIMITER_BEGIN = 'IBM_PROLOG_BEGIN_TAG';
my $SOURCE_BEGIN_TAG = "\$Source:";
my $SOURCE_END_TAG = "\$";
# Desired License, set by environment variable in project env.bash
my $LicenseFile = $ENV{'LICENSE'};
#------------------------------------------------------------------------------
# Contributer info
#------------------------------------------------------------------------------
# Constants for company's copyright
# When adding a new company add constant here and to %fileContributorsCompany
use constant IBM => 'International Business Machines Corp.';
use constant GOOGLE => 'Google Inc.';
# Create mapping for git contrubitors to companies
my %fileContributorsCompany = (
"ibm.com" => IBM,
"ozlabs.org" => IBM,
"google.com" => GOOGLE,
"Google Shared Technology" => GOOGLE,
);
#------------------------------------------------------------------------------
# Constants
#------------------------------------------------------------------------------
use constant RC_INVALID_PARAMETERS => 1;
use constant RC_OLD_COPYRIGHT_BLOCK => 2;
use constant RC_NO_COPYRIGHT_BLOCK => 3;
use constant RC_INVALID_SOURCE_LINE => 4;
use constant RC_INVALID_YEAR => 5;
use constant RC_OLD_DELIMITER_END => 6;
use constant RC_PROJECT_DOES_NOT_MATCH => 7;
use constant RC_BAD_CONTRIBUTORS_BLOCK => 8;
use constant RC_NO_COPYRIGHT_STRING => 9;
use constant RC_INVALID_FILETYPE => 10;
#------------------------------------------------------------------------------
# Global Vars
#------------------------------------------------------------------------------
my $opt_help = 0;
my $opt_debug = 0;
my $operation = "";
my $opt_logfile = "";
my $DelimiterBegin = "";
my $CopyrightBlock = "";
my $DelimiterEnd = "";
my $CopyRightString = "";
my $TempFile = "";
my @Files = ();
my $rc = 0;
# NOTE: $OLD_DELIMITER_END is a subset of $DELIMITER_END so must match
# $DELIMITER_END first in order to return the entire string.
my $g_end_del_re = "($DELIMITER_END|$OLD_DELIMITER_END)";
my $g_prolog_re = "($DELIMITER_BEGIN)((.|\n)+?)$g_end_del_re";
#------------------------------------------------------------------------------
# Forward Declaration
#------------------------------------------------------------------------------
sub validate( $ );
sub update( $$ );
sub extractCopyrightBlock( $ );
sub checkCopyrightblock( $$ );
sub createYearString( $ );
sub removeCopyrightBlock( $$ );
sub addEmptyCopyrightBlock( $$$ );
sub fillinEmptyCopyrightBlock( $$ );
sub getFileContributors( $ );
#######################################################################
# Main
#######################################################################
if (scalar(@ARGV) < 2)
{
## needs at least one filename and an operation as a parameter
usage();
}
my @SaveArgV = @ARGV;
#------------------------------------------------------------------------------
# Parse optional input arguments
#------------------------------------------------------------------------------
GetOptions( "help|?" => \$opt_help,
"validate" => sub { $operation="validate"; },
"update" => sub { $operation="update"; },
"log-failed-files=s" => \$opt_logfile,
"debug" => \$opt_debug,
);
## scan through remaining args and store all files in @Files
## check for old-type parms, just in case
foreach ( @ARGV )
{
## print $_;
if ( m/^debug$/ ) { $opt_debug = 1; next; }
if ( m/^update$/ ) { $operation = $_; next; }
if ( m/^validate$/ ) { $operation = $_; next; }
push @Files, $_ ;
}
if ( $opt_debug )
{
print STDERR __LINE__, " : ---- DEBUG -----\n";
print STDERR "help = $opt_help\n";
print STDERR "debug = $opt_debug\n";
print STDERR "operation = $operation\n";
print STDERR "log-failed-files = $opt_logfile\n";
## dump files specified
print STDERR "Files:\n";
print STDERR join( ' ', @Files ), "\n";
print STDERR "ReleaseYear = $ReleaseYear\n";
print "\n";
}
if ( $operation eq "" )
{
print STDOUT "No operation specified\n";
usage();
exit RC_INVALID_PARAMETERS;
}
if ( ( $opt_logfile ne "" )
## && ( $operation eq "validate" )
)
{
my $logdate = `date +%Y-%m-%d:%H%M`;
chomp $logdate;
open( LOGFH, "> $opt_logfile" ) or die "ERROR $?: Failed to open $opt_logfile: $!";
print LOGFH "## logfile generated $logdate from command line:\n";
print LOGFH $0, " ", join( ' ', @SaveArgV );
print LOGFH "\nFAILING files:\n";
}
########################################################################
## MAIN
########################################################################
# Loop through all files and process.
foreach ( @Files )
{
## clear global vars
$DelimiterBegin = "";
$CopyrightBlock = "";
$DelimiterEnd = "";
$CopyRightString = "";
$rc = 0;
## get filetype
my $filetype = filetype($_);
print STDOUT "File $_: Type $filetype\n";
## set Temporary file name.
$TempFile = "$_.gitCPYWRT";
if ( $opt_debug ) { print STDERR __LINE__, ": Temporary file name = $TempFile\n"; }
##
## Special case is this file, just return 0 and add copyright manually.
##
if ( m/addCopyright\.pl/ )
{
print STDOUT "---------------------------------------------------------\n";
print STDOUT "Skipping special case file: $_\n";
print STDOUT " Please add the copyright prolog manually.\n";
print STDOUT "---------------------------------------------------------\n";
next;
}
##
## Gerrit submissions can include deleted files, just warn and continue
if ( ! -e $_ )
{
print STDOUT "---------------------------------------------------------\n";
print STDOUT "Skipping deleted file: $_\n";
print STDOUT "---------------------------------------------------------\n";
next;
}
##
## Unknown files are valid, but should generate a warning.
if ("Unknown" eq $filetype)
{
print STDOUT "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n";
print STDOUT "WARNING:: File $_ :Unknown Filetype: $filetype\n";
print STDOUT " Skipping this file and continuing.\n";
print STDOUT " Please add the copyright prolog manually.\n";
print STDOUT "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n";
next;
}
##
## text files are valid, but should generate a warning.
if (("txt" eq $filetype) || "Initfile" eq $filetype)
{
print STDOUT "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n";
print STDOUT "WARNING:: File $_ : Filetype: $filetype\n";
print STDOUT " Skipping this file and continuing.\n";
print STDOUT " If needed, Please add the copyright \n";
print STDOUT " prolog manually.\n";
print STDOUT "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n";
next;
}
## extract the existing copyright block
( $DelimiterBegin, $CopyrightBlock, $DelimiterEnd ) = extractCopyrightBlock( $_ );
##
## validate the file.
## if $logfile exists, print failing filename to $logfile and
## keep going.
## Otherwise, exit with $rc
##
if ( $operation =~ m/validate/i )
{
$rc = validate( $_ );
if ( $rc == RC_OLD_DELIMITER_END )
{
## special case, let it go for now
print STDOUT "$_ has old prolog end '$OLD_DELIMITER_END', please fix\n";
}
elsif ( $rc )
{
print STDOUT "$_ FAILED copyright validation: $rc \n";
if ( $opt_logfile ne "" )
{
print LOGFH "$_ # FAILED $rc \n";
}
else
{
exit $rc;
}
}
## continue to next file
next;
} ## endif validate
##
## update
##
if ($operation =~ m/update/i)
{
$rc = update( $_, $filetype );
if ( $rc )
{
print STDOUT "$_ FAILED copyright update: $rc \n";
exit $rc;
}
## continue to next file
next;
} ## endif update
} # end foreach
if ( $opt_logfile ne "" )
{
close( LOGFH );
}
#########################################################################
## Subroutines
#########################################################################
#######################################
## usage: print usage and quit
#######################################
sub usage
{
print STDOUT "Usage: addCopyright.pl { update | validate } \n";
print STDOUT " [ --log-failed-files ]\n";
print STDOUT " [ --debug ] \n";
print STDOUT " file1 file2 ...\n";
}
#######################################
## validate the file
## param[in] $filename to validate
## returns 0 success, nonzero failure
## See constants above for values of failure
#######################################
sub validate( $ )
{
my ( $filename ) = @_;
my $rc = 0;
if ( $CopyrightBlock eq "" )
{
print STDOUT "WARNING: No copyright block.\n";
return RC_NO_COPYRIGHT_BLOCK;
}
$rc = checkCopyrightBlock( $CopyrightBlock, $filename );
# good file
return $rc;
}
##
## @sub update the copyright block.
##
## @param[in] filename
## @param[in] filetype
##
## @return success or failure (currently only return success)
##
sub update( $$ )
{
my ( $filename, $filetype ) = @_;
my $olddelimiter = 0;
my $localrc = 0;
$localrc = validate( $filename );
if ( $localrc != 0 )
{
print STDOUT "Copyright Block check returned $localrc , fixing...\n";
if ( $localrc != RC_NO_COPYRIGHT_BLOCK )
{
if ( $opt_debug) { print STDERR __LINE__, ": remove old copyright block...\n"; }
removeCopyrightBlock( $filename, $filetype );
}
if ($opt_debug) { print STDERR __LINE__, ": Add empty copyright block...\n"; }
addEmptyCopyrightBlock( $filename, $filetype, $localrc );
if ( $opt_debug ) { print STDERR __LINE__, ": fill in new copyright block...\n"; }
fillinEmptyCopyrightBlock( $filename, $filetype );
}
## return OK by default.
return 0;
}
#####################################
## Analyze file and return a text string of the file type
#####################################
sub filetype
{
my $filename = shift;
my $fileinfo = `file $filename | sed 's/^.*: //'`;
chomp $fileinfo;
# Sorted by anticipated frequency of occurrence.
if ( $filename =~ m/\.xml$/i )
# Added XML file to the top of the list because some comments in
# an XML file cause older versions of 'file' to incorrectly return
# "ASCII C++ program text" even though the file is obviously XML.
# Specifically we are seeing "<!-- // ..." cause this trouble.
{
return "xml"
}
if ( $filename =~ m/\.txt$/i )
{
return "txt"
}
if ( $filename =~ m/\.initfile$/i )
{
return "Initfile"
}
if ( ( $filename =~ m/\.[cht]$/i )
||( $filename =~ m/\.[cht]\+\+$/i )
||( $filename =~ m/\.[cht]pp$/i )
||( $filename =~ m/\.inl$/ ) # inline C functions
||( $filename =~ m/\.y$/ ) # yacc
||( $filename =~ m/\.lex$/ ) # flex
||( $fileinfo =~ m/c program text/i )
||( $fileinfo =~ m/c\+\+ program text/i )
||( $fileinfo =~ m/c source/i )
||( $fileinfo =~ m/c\+\+ source/i )
)
{
return "C";
}
if ( ( $filename =~ m/\.pl$/ )
||( $filename =~ m/\.perl$/ )
||( $filename =~ m/\.pm$/ )
||( $fileinfo =~ m/perl.*script.*text executable/i) )
{
return "Perl";
}
if ($filename =~ m/\.s$/i)
{
return "Assembly";
}
if (($filename =~ m/Makefile$/i) or
($filename =~ m/\.mk$/i))
{
return "Makefile";
}
if ( $filename =~ m/\.am$/i )
{
return "Automake";
}
if ( ($filename =~ m/configure\.ac$/i)
||($filename =~ m/Makefile\.in$/i) )
{
return "Autoconf";
}
if ( ( $filename =~ m/\.[kc]{0,1}sh$/i )
||( $filename =~ m/\.bash$/i )
||( $fileinfo =~ m/shell script/i )
||( $fileinfo =~ m/^a \/bin\/[af]sh( -x|) *script text( executable|)$/ )
||( $fileinfo eq "Bourne shell script text")
||( $fileinfo eq "Bourne shell script text executable")
||( $fileinfo eq "Bourne-Again shell script text")
||( $fileinfo eq "Bourne-Again shell script text executable") )
{
return "Shellscript";
}
if ( $filename =~ m/\.py$/ )
{
return "Python";
}
if ( $filename =~ m/\.tcl$/ )
{
return "Tcl";
}
if ( $filename =~ m/\.x$/ )
{
return "RPC";
}
if ( ($filename =~ m/^commitinfo$/)
||($filename =~ m/^checkoutlist$/)
||($filename =~ m/^loginfo$/) )
{
return "CVS";
}
if ( $filename =~ m/\.emx$/ )
{
# Used by Rational Software Architect. modelling file.
return "EmxFile";
}
if ( $filename =~ m/\.mof$/ )
{
return "MofFile";
}
if ( $filename =~ m/\.ld$/ )
{
return "LinkerScript";
}
if ( $filename =~ m/\.rule$/i )
{
return "PrdRuleFile"
}
if ( ( $filename =~ m/\.emx$/i )
||( $filename =~ m/\.odt$/i )
||( $filename =~ m/\.gitignore$/i )
||( $filename =~ m/\.conf$/i )
||( $filename =~ m/\.lidhdr$/i )
||( $filename =~ m/\.vpdinfo$/i )
||( $filename =~ m/\.pdf$/i )
)
{
# Known, but we can't deal with it so call it unknown.
return "Unknown";
}
if ( -f $filename )
{
my $type = `grep "\\\$Filetype:.*\\\$" $filename`;
if ( $type =~ m/\$Filetype:([^\$]*)\$/ )
{
$type = $1;
}
$type =~ s/^\s*//;
$type =~ s/\s*$//;
my %knownTypes = qw/Assembly Assembly Automake Automake Autoconf Autoconf C C CVS CVS EmxFile EmxFile LinkerScript LinkerScript Makefile Makefile MofFile MofFile Perl Perl PrdRuleFile PrdRuleFile Python Python RPC RPC Shellscript Shellscript Tcl Tcl/;
return $type if defined($knownTypes{$type});
}
{ # Other random files containing non-printable characters.
my $file = `cat $filename`;
if ( $file =~ m/([^\x20-\x7E\s])/ )
{
return "Unknown";
}
}
return "Unknown";
}
########################################################################
## extractCopyrightBlock
##
## param[in] $infile - filename
##
## param[out] returns block or ""
########################################################################
sub extractCopyrightBlock( $ )
{
my ( $infile ) = shift;
my $data = "";
open( FH, "< $infile") or die "ERROR $? : can't open $infile : $!";
read( FH, $data, -s FH ) or die "ERROR $? : reading $infile: $!";
close FH;
## print $data;
# Extract the prolog into beginning delimiter, block data, and end delimiter
my @prolog = ( '', '', '' );
@prolog = ($1, $2, $4) if ( $data =~ m/$g_prolog_re/ );
## As long as we're here extract the copyright string within the block
## and save it to a global var
$CopyRightString = $1 if ( $prolog[1] =~ /(^.*$copyrightStr.*$)/m );
if ( $opt_debug )
{
print STDERR __LINE__, ": DELIMITER_BEGIN: >${prolog[0]}<\n";
print STDERR __LINE__, ": DELIMITER_END: >${prolog[2]}<\n";
print STDERR __LINE__, ": Extracted Block: \n>>>>>${prolog[1]}<<<<<\n";
print STDERR __LINE__, ": CopyRightString: >$CopyRightString<\n";
}
return @prolog;
}
#######################################
## Check Copyright Block
##
## @param[in] - Copyright block
## @parma[in] - filename
##
## @return 0 if success, nonzero otherwise
#######################################
sub checkCopyrightBlock( $$ )
{
my ( $block, $filename ) = @_;
my $goodsourcelineflag = 1; ## init to TRUE
my $yearstring = createYearString( $filename );
my $goodyearflag = 1; ## init to TRUE
## Check for old "$IBMCopyrightBlock" signature
if ( $block =~ m/\$IBMCopyrightBlock:.*\$/s )
{
print STDOUT "WARNING: Old copyright block\n";
return RC_OLD_COPYRIGHT_BLOCK;
}
if ( $DelimiterEnd eq $OLD_DELIMITER_END )
{
print STDOUT "WARNING: Old Prolog Tag End\n";
return RC_OLD_DELIMITER_END;
}
## If no $CopyRightString was extracted above, fail with that
if ( ( !defined( $CopyRightString ) )
|| ( $CopyRightString eq "" )
)
{
print STDOUT "WARNING: can't find copyright string\n";
return RC_NO_COPYRIGHT_STRING;
}
## Check to see if this matches the project it lives in
if ( !( $block =~ m/$projectName Project/ ) )
{
print STDOUT "WARNING: Copyright block does not reference project $projectName\n";
return RC_PROJECT_DOES_NOT_MATCH;
}
## split into lines and check for specific things
## $Source: src/usr/initservice/istepdispatcher/istepdispatcher.H $
my %blockFileContributorsHash = ();
my $numBlockEntries = 0;
for ( split /^/, $block )
{
chomp( $_ );
## verify that the $Source: tag has the correct filename
if ( m/.*$SOURCE_BEGIN_TAG.*\$/ )
{
if ( $opt_debug ) { print STDERR __LINE__, ": $SOURCE_BEGIN_TAG : >$_<\n"; }
$goodsourcelineflag = grep /$filename/, $_ ;
if ( !$goodsourcelineflag )
{
print STDOUT "WARNING: invalid filename in tag: $_\n";
print STDOUT " Expected: $filename\n";
## FAIL, bad sourceline
}
}
## check for the correct year string
if ( m/$copyrightStr/ )
{
$goodyearflag = grep /$copyrightStr.* $yearstring/, $_;
if ( !$goodyearflag )
{
print STDOUT "WARNING: outdated copyright year: $_\n";
print STDOUT " Expected: $yearstring\n";
## FAIL, bad yearstring
}
}
# Grab file contributors in block
if ( m/\[\+\]/ )
{
# remove everything through [+]
$_ =~ s/[^\]]*\]//;
# remove closing comment string depening on language
if (( filetype($filename) eq "C") ||
( filetype($filename) eq "LinkerScript"))
{
# remove */
$_ =~ s/\*\///;
}
elsif ( filetype($filename) eq "xml" )
{
# remove -->
$_ =~ s/\-\-\>//;
}
# remove trailing and leading whitespace
$_ =~ s/^\s+|\s+$//g;
# add contributor to hash
$blockFileContributorsHash{$_} = 1;
$numBlockEntries++;
}
} ## endfor
# Get file contributors from git log
my %fileContributors = getFileContributors( $filename );
# Make sure no extra or missing contributors by checking if the current
# contributor block matches the git log history
if ( (scalar keys %fileContributors) !=
($numBlockEntries) )
{
print STDOUT "WARNING: Extra or missing file contributors\n";
return RC_BAD_CONTRIBUTORS_BLOCK;
}
# Check if block's contibutors matches every contributor in git log
# file history
while ( my ($key, $value) = each(%fileContributors) )
{
# Block does not match file contributors
if ( !exists($blockFileContributorsHash{$key}) )
{
print STDOUT "WARNING: File contributors section not correct\n";
return RC_BAD_CONTRIBUTORS_BLOCK;
}
}
if ( !$goodsourcelineflag )
{
return RC_INVALID_SOURCE_LINE;
}
if ( !$goodyearflag )
{
return RC_INVALID_YEAR;
}
return 0;
}
sub createYearString( $ )
{
my ( $filename ) = @_;
my $yearstr = "";
my @years = ();
## Analyse the CopyRightString for begin and end years - this is for old
## files that are checked in from FSP. In this case the earliest
## year will be before it was checked into git . We have to construct
## a yearstring based on the earliest year.
if ( $CopyRightString =~ m/$copyrightStr/ )
{
@years = ( $CopyRightString =~ /([0-9]{4})/g );
}
push @years, $ReleaseYear; # Add the current year.
##
## Make a call to git to find the earliest commit date of the file
## new files will not have a log, so the "git log" call above will
# return nothing.
## Push any year we find onto the @years array
my $cmd = "git log -- $filename | grep Date: | tail --line=1";
## print "run $cmd\n";
my @logstrings = split( " ", `$cmd` );
if ( $? ) { die "ERROR $? : Could not run $cmd $!\n"; }
if ( scalar(@logstrings) >= 5 )
{
push @years, $logstrings[5] ;
}
## sort and remove duplicates by loading it into a hash
my %temphash;
@temphash{@years} = ();
my @outyears = sort keys %temphash;
if ( $opt_debug )
{ print STDERR __LINE__, ": years: ", join( ',', @outyears ), "\n"; }
## lowest year, which may be the only one.
$yearstr = $outyears[0] ;
## if there is more than one index then also output the highest one.
if ( $#outyears > 0 )
{
# A '-' is preferred but CMVC uses ',' so using ','.
$yearstr .= ",$outyears[$#outyears]";
}
return $yearstr;
}
###################################
## Helper function for removeCopyrightBlock()
###################################
sub removeProlog($$$)
{
my ( $data, $begin_re, $end_re ) = @_;
$data =~ s/(\n?)(.*?$begin_re.*$g_prolog_re(.|\n)*?$end_re.*?\n)/$1/;
return $data;
}
###################################
## remove old Copyright Block in preparation for making a new one.
## makes up a debug file named "<filename>.remove"
###################################
sub removeCopyrightBlock( $$ )
{
my ( $filename, $filetype ) = @_ ;
my $data = "" ;
## Modify file in place with temp file Perl Cookbook 7.8
my $savedbgfile = "$filename.remove";
system( "cp -p $filename $TempFile" ); ## preserve file permissions
open( INPUT, "< $filename" ) or die " $? can't open $filename: $!" ;
read( INPUT, $data, -s INPUT ) or die "ERROR $? : reading $filename: $!";
close( INPUT ) or die " $? can't close $filename: $!" ;
open( OUTPUT, "> $TempFile" ) or die " $? can't open $TempFile: $!" ;
select( OUTPUT ); ## new default filehandle for print
## preprocess to get rid of OLD_DELIMITER_END
$data =~ s/$OLD_DELIMITER_END(\s+?)/$DELIMITER_END$1/;
if ( "C" eq $filetype )
{
## pre-process this for /* */ comments
$data = removeProlog( $data, '\/\*', '\*\/' );
## Now apply filter for // comments
$data = removeProlog( $data, '\/\/', '' );
}
elsif ( ("RPC" eq $filetype) or
("LinkerScript" eq $filetype)
)
{
$data = removeProlog( $data, '\/\*', '\*\/' );
}
elsif ( $filetype eq "xml" )
{
$data = removeProlog( $data, '<!--', '-->' );
}
elsif ( "Assembly" eq $filetype )
{
$data = removeProlog( $data, '\#', '' );
}
elsif ( ("Autoconf" eq $filetype) or
("Automake" eq $filetype) or
("CVS" eq $filetype) or
("Makefile" eq $filetype) or
("Perl" eq $filetype) or
("PrdRuleFile" eq $filetype) or
("Python" eq $filetype) or
("Shellscript" eq $filetype) or
("Tcl" eq $filetype)
)
{
# Don't wipe the the '#!' line at the top.
$data = removeProlog( $data, '\#', '' );
}
else
{
print STDOUT "ERROR: Don't know how to remove old block from $filetype file.\n";
close OUTPUT;
return RC_INVALID_FILETYPE;
}
print OUTPUT $data;
## finish up the files
close( OUTPUT ) or die " $? can't close $TempFile: $!" ;
rename( $filename, "$savedbgfile" ) or die " $? can't rename $filename: $!" ;
rename( $TempFile, $filename ) or die " $? can't rename $TempFile: $!" ;
if ( !$opt_debug )
{
## leave the files around for debug
unlink( $savedbgfile ) or die " $? can't delete $savedbgfile: $!";
}
}
###################################
## Add an empty copyright block to the file, for example (C/C++ files):
##
## // IBM_PROLOG_BEGIN_TAG IBM_PROLOG_END_TAG
##
## - The block will be filled-in in the next step.
##
## @param[in] - filename to modify
## @param[in] - filetype
## @param[in] - returncode from validate
##
## @return none
##
## - Makes up a debug file called "<filename>.empty"
##################################
sub addEmptyCopyrightBlock( $$$ )
{
my ( $filename, $filetype, $validaterc ) = @_;
my $line;
## Modify file in place with temp file Perl Cookbook 7.8
my $savedbgfile = "$filename.empty";
system( "cp -p $filename $TempFile" ) ; ## preserve permissions
open( INPUT, "< $filename" ) or die " $? can't open $filename: $!" ;
open( OUTPUT, "> $TempFile" ) or die " $? can't open $TempFile: $!" ;
select( OUTPUT ); ## new default filehandle for print
if ("Assembly" eq $filetype)
{
print OUTPUT "# $DELIMITER_BEGIN $DELIMITER_END\n";
}
elsif ( ("Makefile" eq $filetype) or
("PrdRuleFile" eq $filetype) )
{
print OUTPUT "# $DELIMITER_BEGIN $DELIMITER_END\n";
}
elsif (("Autoconf" eq $filetype) or
("Automake" eq $filetype) or
("CVS" eq $filetype) or
("Perl" eq $filetype) or
("Python" eq $filetype) or
("Shellscript" eq $filetype) or
("Tcl" eq $filetype))
{
## All files with a "shebang" at the beginning
$line = <INPUT>;
# Keep the '#!' line at the top.
## The following says : if the first line is a "shebang" line
## (e.g. "#!/usr/bin/perl"), then print it _before_ the copyright
## block, otherwise (the unless line), print it _after_ the copyright
## block.
if ($line =~ m/^#!/)
{
print OUTPUT $line;
}
print OUTPUT "# $DELIMITER_BEGIN $DELIMITER_END\n";
unless ($line =~ m/^#!/)
{
print OUTPUT $line;
}
}
elsif ( "C" eq $filetype )
{
print OUTPUT "/* $DELIMITER_BEGIN $DELIMITER_END */\n";
}
elsif ( ("RPC" eq $filetype) or
("LinkerScript" eq $filetype)
)
{
# ld stubbornly refuses to use modern comment lines.
print OUTPUT "/* $DELIMITER_BEGIN $DELIMITER_END */\n";
}
elsif ("MofFile" eq $filetype)
{
print OUTPUT "// $DELIMITER_BEGIN $DELIMITER_END\n";
}
elsif ("xml" eq $filetype )
{
print OUTPUT "<!-- $DELIMITER_BEGIN $DELIMITER_END -->\n";
}
else
{
print STDOUT "ERROR: Can\'t handle filetype: $filetype\n";
close( INPUT ) or die " $? can't close $filename: $!" ;
close( OUTPUT ) or die " $? can't close $TempFile: $!" ;
## leave $Tempfile around for debug
return RC_INVALID_FILETYPE;
}
# Copy rest of file
while (defined($line = <INPUT>))
{
print OUTPUT $line;
}
## finish up the files
close( INPUT ) or die " $? can't close $filename: $!" ;
close( OUTPUT ) or die " $? can't close $TempFile: $!" ;
rename( $filename, "$savedbgfile" ) or die " $? can't rename $filename: $!" ;
rename( $TempFile, $filename ) or die " $? can't rename $TempFile: $!" ;
if ( !$opt_debug )
{
## leave the files around for debug
unlink( $savedbgfile ) or die " $? can't delete $savedbgfile: $!";
}
}
############################################
## Helper functions for fillinEmptyCopyrightBlock()
############################################
sub addPrologComments($$$)
{
my ( $data, $begin, $end ) = @_;
my @lines = split( /\n/, $data );
$data = '';
for my $line ( @lines )
{
# If there is an block comment end tag, fill the end of the line with
# spaces.
if ( $end )
{
my $max_line_len = 70;
my $len = length($line);
if ( $len < $max_line_len )
{
my $fill = ' ' x ($max_line_len - $len);
$line .= $fill;
}
}
# NOTE: CMVC prologs with inline comments will have a single trailing
# space at the end of the line. This is undesirable for most
# developers so it will not be added.
if ( $line =~ m/$DELIMITER_BEGIN/ )
{
$line = "$line $end" if ( $end );
$line = "$line\n";
}
elsif ( $line =~ m/$DELIMITER_END/ )
{
$line = "$begin $line";
}
else
{
if ( not $end and not $line )
{
# Compensate for blank lines with no end delimeter.
$line = "$begin\n";
}
else
{
$line = "$begin $line";
$line = "$line $end" if ( $end );
$line = "$line\n";
}
}
$data .= $line;
}
return $data;
}
############################################
## fill in the empty copyright block
## Copyright guidelines from:
## FSP ClearCase Architecture
## Version 1.9
## 10/12/2010
## Editor: Alan Hlava
##
## Section 3.14.1 has templates for different files
##
############################################
sub fillinEmptyCopyrightBlock( $$ )
{
my ( $filename, $filetype ) = @_;
my $copyrightYear = createYearString( $filename );
# Get copyright contributors based on hash so no duplicates
my %fileContributors = getFileContributors( $filename );
my $copyright_Contributors = "";
foreach my $key (sort keys %fileContributors)
{
$copyright_Contributors .= "[+] ".$key."\n";
}
## Get desired license
my $LicenseContent = "";
open(LICENSE, "< $LicenseFile") or die " $? can't open $LicenseFile: $!" ;
while (my $line = <LICENSE>)
{
my $evalLine = eval "qq($line)";
$LicenseContent .= $evalLine;
}
close(LICENSE);
## define the final copyright block template here.
my $IBMCopyrightBlock = <<EOF;
$DELIMITER_BEGIN
$LicenseContent
$DELIMITER_END
EOF
## Modify file in place with temp file Perl Cookbook 7.8
my $savedbgfile = "$filename.fillin";
system( "cp -p $filename $TempFile" ); ## preserve file permissions
open( INPUT, "< $filename" ) or die " $? can't open $filename: $!" ;
open( OUTPUT, "> $TempFile" ) or die " $? can't open $TempFile: $!" ;
select( OUTPUT ); ## new default filehandle for print
my $newline;
my $lines = "";
while ( defined($newline = <INPUT>) ) { $lines .= $newline; }
if ("Assembly" eq $filetype)
{
$IBMCopyrightBlock = addPrologComments($IBMCopyrightBlock, '#', '');
}
elsif ( ("Makefile" eq $filetype) or
("PrdRuleFile" eq $filetype) )
{
$IBMCopyrightBlock = addPrologComments($IBMCopyrightBlock, '#', '');
}
elsif (("Autoconf" eq $filetype) or
("Automake" eq $filetype) or
("CVS" eq $filetype) or
("Perl" eq $filetype) or
("Python" eq $filetype) or
("Shellscript" eq $filetype) or
("Tcl" eq $filetype))
{
## all files with a "shebang"
$IBMCopyrightBlock = addPrologComments($IBMCopyrightBlock, '#', '');
}
elsif ( "C" eq $filetype )
{
## lex files are classified as C, but do not recognize '//' comments
$IBMCopyrightBlock = addPrologComments($IBMCopyrightBlock, '/*', '*/');
}
elsif ( ("RPC" eq $filetype) or
("LinkerScript" eq $filetype)
)
{
$IBMCopyrightBlock = addPrologComments($IBMCopyrightBlock, '/*', '*/');
}
elsif ("EmxFile" eq $filetype)
{
# Not yet formatted correctly for EmxFile needs, but should coexist.
$IBMCopyrightBlock = "$DELIMITER_BEGIN IBM Confidential OCO Source Materials (C) Copyright IBM Corp. $copyrightYear The source code for this program is not published or otherwise divested of its trade secrets, irrespective of what has been deposited with the U.S. Copyright Office. $DELIMITER_END";
}
elsif ("MofFile" eq $filetype)
{
$IBMCopyrightBlock = addPrologComments($IBMCopyrightBlock, '//', '');
}
elsif ( "xml" eq $filetype)
{
$IBMCopyrightBlock = addPrologComments($IBMCopyrightBlock, '<!--', '-->');
}
else
{
print STDOUT "ERROR: Can\'t handle filetype: $filetype\n";
return RC_INVALID_FILETYPE;
}
# Replace existing block with the current content.
$lines =~ s/$DELIMITER_BEGIN[^\$]*$DELIMITER_END/$IBMCopyrightBlock/s;
print OUTPUT $lines;
## finish up the files
close( INPUT ) or die " $? can't close $filename: $!" ;
close( OUTPUT ) or die " $? can't close $TempFile: $!" ;
rename( $filename, "$savedbgfile" ) or die " $? can't rename $filename: $!" ;
rename( $TempFile, $filename ) or die " $? can't rename $TempFile: $!" ;
if ( !$opt_debug )
{
## leave the files around for debug
unlink( $savedbgfile ) or die " $? can't delete $savedbgfile: $!";
}
}
#######################################
## Gets file contirbutors based on git log of a file
##
## @parma[in] filename
##
## @return hash of contributors (key,value) => (name/company, 1)
#######################################
sub getFileContributors( $ )
{
my ( $filename ) = @_;
# Create a "set like" hash for file contributors to handle duplicates
# so key is the only important information
my %fileContributors = ();
# Check file for company Origin
my @gitDomain = `git log -- $filename | grep Origin: | sort | uniq`;
foreach my $origin (@gitDomain)
{
chomp($origin);
# Remove all characters through word "Origin:"
$origin =~ s/[^:]*\://;
# Remove white space after colon
$origin =~ s/^\s+//;
if (exists($fileContributorsCompany{$origin}))
{
# Add company info for copyright contribution
$fileContributors{$fileContributorsCompany{$origin}} = 1;
}
}
# Check file for all contributors
my @gitAuthors = `git log --pretty="%aN <%aE>" -- $filename | sort | uniq`;
# Add current commiter, add < > around the email to follow the same format
# as the above array
my $curAuthorEmail = `git config user.email`;
chomp($curAuthorEmail);
my $curGitAuthor = "<".$curAuthorEmail.">";
push(@gitAuthors, $curGitAuthor);
foreach my $contributor (@gitAuthors)
{
my $companyExists = 0;
chomp($contributor);
# Grab company domain out of contributor's email
my $domain = substr ($contributor, index($contributor, '@') + 1, -1);
# Due to multiple prefixes for IBM like us, in, linux, etc will try
# removing characters up to each period (.) until correct domain
# address found
my @domainSections = split(/\./,$domain);
for (my $i = 0; $i < @domainSections; $i++)
{
if (exists($fileContributorsCompany{$domain}))
{
$companyExists = 1;
last;
}
# Remove all characters upto & including the first period (.) seen
$domain =~ s/[^.]*\.//;
}
#Check if contributor's company exists
if ($companyExists)
{
# Add company info for copyright contribution
$fileContributors{$fileContributorsCompany{$domain}} = 1;
}
else
{
my $name = substr ($contributor, 0, index($contributor, '<') -1);
if($name)
{
# Add individual info for copyright contribution
$fileContributors{$name} = 1;
}
else
{
die("Cannot find name of contributor in git commit");
}
}
}
return %fileContributors;
}
|