summaryrefslogtreecommitdiffstats
path: root/src/build/tools/addCopyright.pl
blob: b8f713a66382812083304567f574ca47efa13852 (plain)
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
#!/usr/bin/perl
#  IBM_PROLOG_BEGIN_TAG
#  This is an automatically generated prolog.
#
#  $Source: src/build/tools/addCopyright.pl $
#
#  IBM CONFIDENTIAL
#
#  COPYRIGHT International Business Machines Corp. 2011
#
#  p1
#
#  Object Code Only (OCO) source materials
#  Licensed Internal Code Source Materials
#  IBM HostBoot Licensed Internal Code
#
#  The source code for this program is not published or other-
#  wise divested of its trade secrets, irrespective of what has
#  been deposited with the U.S. Copyright Office.
#
#  Origin: 30
#
#  IBM_PROLOG_END

# Forked from:
# Author: Mark Jerde (mjerde@us.ibm.com)
# Date: Fri Mar 19 17:40:32 2010 UTC
#
# CVS Information:
# $Revision: 1.55 $
# $Date: 2011/01/10 19:23:05 $
# $RCSfile: addCopyright.pl,v $

use strict;
use warnings;

#################################### ABOUT ####################################
# 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 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                                        #
###############################################################################

# Project-specific settings.
#
## my $releaseYear = "2011";
my $releaseYear = `date +%Y`;
chomp( $releaseYear );
my $copyrightSymbol = "";
# my $copyrightSymbol = "(C)";  # Uncomment if unable to use  character.

my  $projectName            =   "HostBoot";
my  $DELIMITER_END          =   "IBM_PROLOG_END";
my  $DELIMITER_BEGIN        =   "IBM_PROLOG_BEGIN_TAG";
my  $DELIMITER_WARNING_TEXT =   "This is an automatically generated prolog.";
my  $PVALUE                 =   "p1";       ## my best guess

my  $PVALUE_TEXT            =
"Object Code Only (OCO) source materials
  Licensed Internal Code Source Materials
  IBM $projectName Licensed Internal Code

  The source code for this program is not published or other-
  wise divested of its trade secrets, irrespective of what has
  been deposited with the U.S. Copyright Office.";
my  $ORIGIN                 =   "30";


# End Project-specific settings

#######################################################################
# Main
#######################################################################

if (scalar(@ARGV) < 2)
{
    usage();
}

##
my $operation = shift;
if (not ($operation =~ m/update/i) and
    not ($operation =~ m/validate/i))
{
    print "Operation update or validate is required\n";
    usage();
}

##  check for debug mode
my  $debug  =   shift( @ARGV );
# print   "$debug\n";
if ( $debug =~ m/debug/i )
{
    print   STDERR  "debug mode\n";
    $debug  =   1;
}
else
{
    print   STDERR "normal mode\n";
    unshift( @ARGV , $debug );
    $debug  =   0;
}

# Read files and process.
while (defined($_ = shift))
{
    my $filetype = filetype($_);
    print "File $_: Type $filetype\n";



    ##
    ##  validate
    ##      Continue for unknown filetype
    ##      Exit 2 if has old copyright block
    ##      Exit 3 if no copyright block
    ##
    if ( $operation =~  m/validate/i )
    {

        if  ( ! -e $_ )
        {
            print "---------------------------------------------------------\n";
            print "Skipping deleted file: $_\n";
            print "---------------------------------------------------------\n";
            next;
        }

        if  ("Unknown" eq $filetype)
        {
            print "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n";
            print "WARNING:: File $_ :Unknown Filetype: $filetype\n";
            print "         Skipping this file and continuing.\n";
            print "         Please add the copyright prologue manually.\n";
            print "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n";
            ## exit 1;
            next;
        }
        if ( hasoldcopyrightblock( $_, $filetype )    )
        {
            print "ERROR File $_: has old copyright block, please fix\n";
            exit 2;
        }

        if ( ! hascopyrightblock( $_, $filetype )  )
        {
            print "ERROR File $_: Missing Copyright notice!!!\n";
            exit 3;
        }

        #   good file
        next;
    }

    ##
    ##  update
    ##  Continue with a warning for unknown filetype -
    ##
    if ($operation =~ m/update/i)
    {
        if  ("Unknown" eq $filetype)
        {
            print "!!!!! WARNING: File $_ :Unknown Filetype: $_\n";
            next;
        }
        ##  do a check for the old  "$IBMCopyright:  $"  block
        if ( hasoldcopyrightblock ( $_, $filetype )  )
        {
            print "ERROR File $_: has old copyright block, please fix\n";
            exit 2;
        }

        if ( ! hascopyrightblock( $_, $filetype ) )
        {
            if ($debug) { print "Add empty copyright block\n";    }
            addEmptyCopyrightBlock( $_, $filetype );
            ##  check that the first part worked
            if ( ! hascopyrightblock( $_, $filetype ) )
            {
                print STDERR "Failed to create Copyright block, please fix\n";
                exit 2;
            }

            fillInEmptyCopyrightBlock( $_, $filetype );
        }
    }

}   #   endwhile



#########################################################################
##  Subroutines
#########################################################################

#######################################
##  usage:  print usage and quit
#######################################
sub usage
{
    print "Usage: addCopyright.pl { update | validate } [ debug ] file1 ...\n\n";
    exit 1;
}

#####################################
##  log unknown file type - unfinished
#####################################
sub logUnknownFiletype
{
    my $filename = shift;
    my $fileinfo = shift;

    # TODO
    print "Unknown filetype: $filename $fileinfo\n";
}

#####################################
##  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/\.[cht]$/i )
       ||( $filename =~ m/\.[cht]\+\+$/i )
       ||( $filename =~ m/\.[cht]pp$/i )
       ||( $fileinfo =~ m/c program text/i )
       ||( $fileinfo =~ m/c\+\+ program text/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/\.xml$/i )
    {
        return "xml"
    }
    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 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";
        }
    }
    logUnknownFiletype($filename,$fileinfo);
    return "Unknown";
}

######################################
##  Figure out the first copyright year for the file
##      return as a string
######################################
sub copyrightFirstYearForFile
{
    my $file = shift;
    my $fileContent = shift;
    my $year = $releaseYear;
    my @logstrings;
    # TODO find multiple copyright statements and pick the earliest one
    # Search for earlier years from prior release.
    if ($fileContent =~ m/Copyright IBM Corp. (\d{4})/)
    {
        $year = $1 if ($1 < $year);
    }

    my  $cmd = "git log -- $file | grep Date: | tail --line=1";
    ## print "run $cmd\n";
    @logstrings = split( " ", `$cmd` );

    #   new files will not have a log, so the "git log" call above will
    #       return nothing.  In this case, just return the old calculated
    #       value.
    if ( (scalar(@logstrings) < 5)  or ($year lt $logstrings[5]) )
    {
        return $year;
    }
    else
    {
        return $logstrings[5];
    }
}

########################################
##  Figure out the last copyright year for file
##      Return as a string
########################################
sub copyrightLastYearForFile
{
    return $releaseYear;
}



#######################################
##  Check for old $IBMCopyrightBlock signature
##      return 1 if it's there, otherwise 0
#######################################
sub hasoldcopyrightblock
{
    my ( $filename, $filetype ) =   @_;
    my $file = `head -180 $filename`;       # Must appear in first 180 lines.

    if ( $file =~ m/\$IBMCopyrightBlock:.*\$/s )
    {
        return  1;
    }

    return  0;
}

#######################################
##  Check for new IBM_PROLOG_BEGIN_TAG signature
##      return 1 if it's there, otherwise 0
#######################################
sub hascopyrightblock
{
    my ( $filename, $filetype ) =   @_;
    my $file = `head -180 $filename`;       # Must appear in first 180 lines.

    #print $filename;
    #print $file;
    #print   $DELIMITER_BEGIN;
    #print   $DELIMITER_END;


    if ( $file =~ m/$DELIMITER_BEGIN.*$DELIMITER_END/s )
    {
        return  1;
    }

    return  0;
}
###################################
##  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.
##  - Makes up a temporary file called "<filename>.cvsCPYRT"
##################################
sub addEmptyCopyrightBlock
{
    my ( $filename, $filetype)  =   @_;
    my $line;
    open(INPUT, $_) or exit;
    system "touch $_.cvsCPYRT";
    open(OUTPUT, ">$_.cvsCPYRT") or exit;

    if ("Assembly" eq $filetype)
    {
        ## print OUTPUT ";// \$IBMCopyrightBlock: \$\n";
        print OUTPUT "#  $DELIMITER_BEGIN $DELIMITER_END\n";
    }
    elsif (("Autoconf" eq $filetype) or
        ("Automake" eq $filetype) or
        ("CVS" eq $filetype) or
        ("Makefile" eq $filetype) or
        ("Perl" eq $filetype) or
        ("Python" eq $filetype) or
        ("Shellscript" eq $filetype) or
        ("Tcl" eq $filetype))
    {
        $line = <INPUT>;
        # Keep the '#!' line at the top.
        if ($line =~ m/^#!/)
        {
            print OUTPUT $line;
        }
        ## print OUTPUT "# \$IBMCopyrightBlock: \$\n";
        print OUTPUT "# $DELIMITER_BEGIN $DELIMITER_END\n";
        unless ($line =~ m/^#!/)
        {
            print OUTPUT $line;
        }
    }
    elsif ( ("C" eq $filetype)
           )
    {
        ##print OUTPUT " * \$IBMCopyrightBlock: \$\n";
        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 */\n";
    }
    elsif ("MofFile" eq $filetype)
    {
        ##print OUTPUT "// \$IBMCopyrightBlock: \$\n";
        print OUTPUT "// $DELIMITER_BEGIN $DELIMITER_END\n";
    }
    elsif ("xml" eq $filetype )
    {
        print   OUTPUT "<!-- $DELIMITER_BEGIN $DELIMITER_END -->\n";
    }
    else
    {
        print   STDERR  "Can\'t handle filetype:  $filetype\n";
        exit    1;
    }

    # Copy rest of file
    while (defined($line = <INPUT>))
    {
        print OUTPUT $line;
    }
    close OUTPUT;
    close INPUT;
    system "cat $_.cvsCPYRT > $_";
    system "rm -f $_.cvsCPYRT";
    system "touch $_";

}

############################################
##  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 $file = `cat $_`;
    my $firstYear = copyrightFirstYearForFile($_, $file);
    my $lastYear = copyrightLastYearForFile($_);
    my $copyrightYear = $firstYear;
    $copyrightYear .= " - $lastYear" unless ( "$firstYear" eq "$lastYear" );
    ##  define the final copyright block template here.
    my $IBMCopyrightBlock =
" $DELIMITER_BEGIN
  $DELIMITER_WARNING_TEXT

  \$Source: $filename \$

  IBM CONFIDENTIAL

  COPYRIGHT International Business Machines Corp. $copyrightYear

  $PVALUE

  $PVALUE_TEXT

  Origin: $ORIGIN

  $DELIMITER_END";


    if (($file =~ m/$DELIMITER_BEGIN*.$DELIMITER_END/s ) and
        ("Unknown" ne $filetype))
    {
        open(INPUT, $_ ) or exit;
        system "touch $_.cvsCPYRT";
        open(OUTPUT, ">$_.cvsCPYRT") or exit;

        my $newline;
        my $lines = "";
        while ( defined($newline = <INPUT>) ) { $lines .= $newline; }

        if ("Assembly" eq $filetype)
        {
            ## assembly
            $IBMCopyrightBlock =~ s/\n/\n# /sg;
        }
        if (("Autoconf" eq $filetype) or
            ("Automake" eq $filetype) or
            ("CVS" eq $filetype) or
            ("Makefile" eq $filetype) or
            ("Perl" eq $filetype) or
            ("Python" eq $filetype) or
            ("Shellscript" eq $filetype) or
            ("Tcl" eq $filetype))
        {
            $IBMCopyrightBlock =~ s/\n/\n#/sg;
        }
        if (("C" eq $filetype)
           )
        {
            # Use C++ style comments
            $IBMCopyrightBlock =~ s|\n|\n//|sg;
        }
        if (("RPC" eq $filetype) or
            ("LinkerScript" eq $filetype)
           )
        {
            $IBMCopyrightBlock =~ s|\n|\n *|sg;
        }
        if ("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";
        }
        if ("MofFile" eq $filetype)
        {
            $IBMCopyrightBlock =~ s|\n|\n//|sg;
        }
        if ( "xml" eq $filetype)
        {
            $IBMCopyrightBlock =~ s|\n|\n   |sg;
        }

        # Replace existing block with the current content.
        # $lines =~ s/\$IBMCopyrightBlock:[^\$]*\$/$IBMCopyrightBlock/s;
        $lines =~ s/$DELIMITER_BEGIN[^\$]*$DELIMITER_END/$IBMCopyrightBlock/s;

        print OUTPUT $lines;
        close OUTPUT;
        close INPUT;

        system "cat $_.cvsCPYRT > $_";
        system "rm -f $_.cvsCPYRT";
        system "touch $_";
    }
    else
    {
        print "Cannot find empty copyright block\n";
        exit 1;
    }
}
OpenPOWER on IntegriCloud