3 # Copyright (C) 2013-2017 Free Software Foundation, Inc.
5 # This script is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3, or (at your option)
10 # This script adjusts the copyright notices at the top of source files
11 # so that they have the form:
13 # Copyright XXXX-YYYY Free Software Foundation, Inc.
15 # It doesn't change code that is known to be maintained elsewhere or
16 # that carries a non-FSF copyright.
18 # The script also doesn't change testsuite files, except those in
19 # libstdc++-v3. This is because libstdc++-v3 has a conformance testsuite,
20 # while most tests in other directories are just things that failed at some
23 # Pass --this-year to the script if you want it to add the current year
24 # to all applicable notices. Pass --quilt if you are using quilt and
25 # want files to be added to the quilt before being changed.
27 # By default the script will update all directories for which the
28 # output has been vetted. You can instead pass the names of individual
29 # directories, including those that haven't been approved. So:
31 # update-copyright.py --this-year
33 # is the command that would be used at the beginning of a year to update
34 # all copyright notices (and possibly at other times to check whether
35 # new files have been added with old years). On the other hand:
37 # update-copyright.py --this-year libitm
39 # would run the script on just libitm/.
41 # Note that things like --version output strings must be updated before
42 # this script is run. There's already a separate procedure for that.
54 def report (self
, filename
, string
):
56 string
= filename
+ ': ' + string
57 sys
.stderr
.write (string
+ '\n')
61 return self
.num_errors
== 0
65 self
.skip_files
= set()
66 self
.skip_dirs
= set()
67 self
.skip_extensions
= set()
68 self
.fossilised_files
= set()
69 self
.own_files
= set()
71 self
.skip_files |
= set ([
83 # Skip auto- and libtool-related files
104 # Skip FSF mission statement, etc.
109 # Skip imported texinfo files.
114 def get_line_filter (self
, dir, filename
):
115 if filename
.startswith ('ChangeLog'):
116 # Ignore references to copyright in changelog entries.
117 return re
.compile ('\t')
121 def skip_file (self
, dir, filename
):
122 if filename
in self
.skip_files
:
125 (base
, extension
) = os
.path
.splitext (os
.path
.join (dir, filename
))
126 if extension
in self
.skip_extensions
:
129 if extension
== '.in':
130 # Skip .in files produced by automake.
131 if os
.path
.exists (base
+ '.am'):
134 # Skip files produced by autogen
135 if (os
.path
.exists (base
+ '.def')
136 and os
.path
.exists (base
+ '.tpl')):
139 # Skip configure files produced by autoconf
140 if filename
== 'configure':
141 if os
.path
.exists (base
+ '.ac'):
143 if os
.path
.exists (base
+ '.in'):
148 def skip_dir (self
, dir, subdir
):
149 return subdir
in self
.skip_dirs
151 def is_fossilised_file (self
, dir, filename
):
152 if filename
in self
.fossilised_files
:
154 # Only touch current current ChangeLogs.
155 if filename
!= 'ChangeLog' and filename
.find ('ChangeLog') >= 0:
159 def by_package_author (self
, dir, filename
):
160 return filename
in self
.own_files
163 def __init__ (self
, errors
):
166 # Characters in a range of years. Include '.' for typos.
167 ranges
= '[0-9](?:[-0-9.,\s]|\s+and\s+)*[0-9]'
169 # Non-whitespace characters in a copyright holder's name.
173 self
.year_re
= re
.compile ('[0-9]+')
175 # Matches part of a year or copyright holder.
176 self
.continuation_re
= re
.compile (ranges
+ '|' + name
)
178 # Matches a full copyright notice:
179 self
.copyright_re
= re
.compile (
180 # 1: 'Copyright (C)', etc.
182 '|[Cc]opyright\s+\([Cc]\)'
184 '|[Cc]opyright\s+©'
185 '|[Cc]opyright\s+@copyright{}'
187 '|@set\s+copyright[\w-]+)'
189 # 2: the years. Include the whitespace in the year, so that
190 # we can remove any excess.
191 '(\s*(?:' + ranges
+ ',?'
192 '|@value\{[^{}]*\})\s*)'
197 # 4: the copyright holder. Don't allow multiple consecutive
198 # spaces, so that right-margin gloss doesn't get caught
199 # (e.g. gnat_ugn.texi).
200 '(' + name
+ '(?:\s?' + name
+ ')*)?')
202 # A regexp for notices that might have slipped by. Just matching
203 # 'copyright' is too noisy, and 'copyright.*[0-9]' falls foul of
204 # HTML header markers, so check for 'copyright' and two digits.
205 self
.other_copyright_re
= re
.compile ('copyright.*[0-9][0-9]',
207 self
.comment_re
= re
.compile('#+|[*]+|;+|%+|//+|@c |dnl ')
208 self
.holders
= { '@copying': '@copying' }
209 self
.holder_prefixes
= set()
211 # True to 'quilt add' files before changing them.
212 self
.use_quilt
= False
214 # If set, force all notices to include this year.
217 # Goes after the year(s). Could be ', '.
220 def add_package_author (self
, holder
, canon_form
= None):
223 self
.holders
[holder
] = canon_form
224 index
= holder
.find (' ')
226 self
.holder_prefixes
.add (holder
[:index
])
227 index
= holder
.find (' ', index
+ 1)
229 def add_external_author (self
, holder
):
230 self
.holders
[holder
] = None
233 def __init__ (self
, year
):
237 return 'unrecognised year: ' + self
.year
239 def parse_year (self
, string
):
241 if len (string
) == 2:
244 elif len (string
) == 4:
246 raise self
.BadYear (string
)
248 def year_range (self
, years
):
249 year_list
= [self
.parse_year (year
)
250 for year
in self
.year_re
.findall (years
)]
251 assert len (year_list
) > 0
252 return (min (year_list
), max (year_list
))
254 def set_use_quilt (self
, use_quilt
):
255 self
.use_quilt
= use_quilt
257 def include_year (self
, year
):
258 assert not self
.max_year
261 def canonicalise_years (self
, dir, filename
, filter, years
):
262 # Leave texinfo variables alone.
263 if years
.startswith ('@value'):
266 (min_year
, max_year
) = self
.year_range (years
)
268 # Update the upper bound, if enabled.
269 if self
.max_year
and not filter.is_fossilised_file (dir, filename
):
270 max_year
= max (max_year
, self
.max_year
)
273 if min_year
== max_year
:
274 return '%d' % min_year
276 return '%d-%d' % (min_year
, max_year
)
278 def strip_continuation (self
, line
):
280 match
= self
.comment_re
.match (line
)
282 line
= line
[match
.end():].lstrip()
285 def is_complete (self
, match
):
286 holder
= match
.group (4)
288 and (holder
not in self
.holder_prefixes
289 or holder
in self
.holders
))
291 def update_copyright (self
, dir, filename
, filter, file, line
, match
):
294 pathname
= os
.path
.join (dir, filename
)
296 intro
= match
.group (1)
297 if intro
.startswith ('@set'):
298 # Texinfo year variables should always be on one line
299 after_years
= line
[match
.end (2):].strip()
300 if after_years
!= '':
301 self
.errors
.report (pathname
,
302 'trailing characters in @set: '
304 return (False, orig_line
, next_line
)
306 # If it looks like the copyright is incomplete, add the next line.
307 while not self
.is_complete (match
):
309 next_line
= file.next()
310 except StopIteration:
313 # If the next line doesn't look like a proper continuation,
314 # assume that what we've got is complete.
315 continuation
= self
.strip_continuation (next_line
)
316 if not self
.continuation_re
.match (continuation
):
319 # Merge the lines for matching purposes.
320 orig_line
+= next_line
321 line
= line
.rstrip() + ' ' + continuation
324 # Rematch with the longer line, at the original position.
325 match
= self
.copyright_re
.match (line
, match
.start())
328 holder
= match
.group (4)
330 # Use the filter to test cases where markup is getting in the way.
331 if filter.by_package_author (dir, filename
):
332 assert holder
not in self
.holders
335 self
.errors
.report (pathname
, 'missing copyright holder')
336 return (False, orig_line
, next_line
)
338 elif holder
not in self
.holders
:
339 self
.errors
.report (pathname
,
340 'unrecognised copyright holder: ' + holder
)
341 return (False, orig_line
, next_line
)
344 # See whether the copyright is associated with the package
346 canon_form
= self
.holders
[holder
]
348 return (False, orig_line
, next_line
)
350 # Make sure the author is given in a consistent way.
351 line
= (line
[:match
.start (4)]
353 + line
[match
.end (4):])
356 line
= line
[:match
.start (3)] + line
[match
.end (3):]
358 # Update the copyright years.
359 years
= match
.group (2).strip()
361 canon_form
= self
.canonicalise_years (dir, filename
, filter, years
)
362 except self
.BadYear
as e
:
363 self
.errors
.report (pathname
, str (e
))
364 return (False, orig_line
, next_line
)
366 line
= (line
[:match
.start (2)]
367 + ('' if intro
.startswith ('copyright = ') else ' ')
368 + canon_form
+ self
.separator
369 + line
[match
.end (2):])
371 # Use the standard (C) form.
372 if intro
.endswith ('right'):
374 elif intro
.endswith ('(c)'):
375 intro
= intro
[:-3] + '(C)'
376 line
= line
[:match
.start (1)] + intro
+ line
[match
.end (1):]
378 # Strip trailing whitespace
379 line
= line
.rstrip() + '\n'
381 return (line
!= orig_line
, line
, next_line
)
383 def process_file (self
, dir, filename
, filter):
384 pathname
= os
.path
.join (dir, filename
)
385 if filename
.endswith ('.tmp'):
386 # Looks like something we tried to create before.
395 line_filter
= filter.get_line_filter (dir, filename
)
397 with
open (pathname
, 'r') as file:
399 mode
= os
.fstat (file.fileno()).st_mode
403 # Leave filtered-out lines alone.
404 if not (line_filter
and line_filter
.match (line
)):
405 match
= self
.copyright_re
.search (line
)
407 res
= self
.update_copyright (dir, filename
, filter,
409 (this_changed
, line
, next_line
) = res
410 changed
= changed
or this_changed
412 # Check for copyright lines that might have slipped by.
413 elif self
.other_copyright_re
.search (line
):
414 self
.errors
.report (pathname
,
415 'unrecognised copyright: %s'
420 # If something changed, write the new file out.
421 if changed
and self
.errors
.ok():
422 tmp_pathname
= pathname
+ '.tmp'
423 with
open (tmp_pathname
, 'w') as file:
426 os
.fchmod (file.fileno(), mode
)
428 subprocess
.call (['quilt', 'add', pathname
])
429 os
.rename (tmp_pathname
, pathname
)
431 def process_tree (self
, tree
, filter):
432 for (dir, subdirs
, filenames
) in os
.walk (tree
):
433 # Don't recurse through directories that should be skipped.
434 for i
in xrange (len (subdirs
) - 1, -1, -1):
435 if filter.skip_dir (dir, subdirs
[i
]):
438 # Handle the files in this directory.
439 for filename
in filenames
:
440 if filter.skip_file (dir, filename
):
441 sys
.stdout
.write ('Skipping %s\n'
442 % os
.path
.join (dir, filename
))
444 self
.process_file (dir, filename
, filter)
447 def __init__ (self
, copyright
= Copyright
):
448 self
.errors
= Errors()
449 self
.copyright
= copyright (self
.errors
)
451 self
.default_dirs
= []
452 self
.chosen_dirs
= []
453 self
.option_handlers
= dict()
454 self
.option_help
= []
456 self
.add_option ('--help', 'Print this help', self
.o_help
)
457 self
.add_option ('--quilt', '"quilt add" files before changing them',
459 self
.add_option ('--this-year', 'Add the current year to every notice',
462 def add_option (self
, name
, help, handler
):
463 self
.option_help
.append ((name
, help))
464 self
.option_handlers
[name
] = handler
466 def add_dir (self
, dir, filter = GenericFilter()):
467 self
.dirs
.append ((dir, filter))
469 def o_help (self
, option
= None):
470 sys
.stdout
.write ('Usage: %s [options] dir1 dir2...\n\n'
471 'Options:\n' % sys
.argv
[0])
472 format
= '%-15s %s\n'
473 for (what
, help) in self
.option_help
:
474 sys
.stdout
.write (format
% (what
, help))
475 sys
.stdout
.write ('\nDirectories:\n')
479 for (dir, filter) in self
.dirs
:
481 if i
% 3 == 0 or i
== len (self
.dirs
):
482 sys
.stdout
.write (dir + '\n')
484 sys
.stdout
.write (format
% dir)
487 def o_quilt (self
, option
):
488 self
.copyright
.set_use_quilt (True)
490 def o_this_year (self
, option
):
491 self
.copyright
.include_year (time
.localtime().tm_year
)
494 for arg
in sys
.argv
[1:]:
496 self
.chosen_dirs
.append (arg
)
497 elif arg
in self
.option_handlers
:
498 self
.option_handlers
[arg
] (arg
)
500 self
.errors
.report (None, 'unrecognised option: ' + arg
)
502 if len (self
.chosen_dirs
) == 0:
503 self
.chosen_dirs
= self
.default_dirs
504 if len (self
.chosen_dirs
) == 0:
507 for chosen_dir
in self
.chosen_dirs
:
508 canon_dir
= os
.path
.join (chosen_dir
, '')
510 for (dir, filter) in self
.dirs
:
511 if (dir + os
.sep
).startswith (canon_dir
):
513 self
.copyright
.process_tree (dir, filter)
515 self
.errors
.report (None, 'unrecognised directory: '
517 sys
.exit (0 if self
.errors
.ok() else 1)
519 #----------------------------------------------------------------------------
521 class TopLevelFilter (GenericFilter
):
522 def skip_dir (self
, dir, subdir
):
525 class ConfigFilter (GenericFilter
):
527 GenericFilter
.__init
__ (self
)
529 def skip_file (self
, dir, filename
):
530 if filename
.endswith ('.m4'):
531 pathname
= os
.path
.join (dir, filename
)
532 with
open (pathname
) as file:
533 # Skip files imported from gettext.
534 if file.readline().find ('gettext-') >= 0:
536 return GenericFilter
.skip_file (self
, dir, filename
)
538 class GCCFilter (GenericFilter
):
540 GenericFilter
.__init
__ (self
)
542 self
.skip_files |
= set ([
547 self
.skip_dirs |
= set ([
548 # Better not create a merge nightmare for the GNAT folks.
551 # Handled separately.
555 self
.skip_extensions |
= set ([
556 # Maintained by the translation project.
559 # Automatically-generated.
563 self
.fossilised_files |
= set ([
564 # Old news won't be updated.
568 class TestsuiteFilter (GenericFilter
):
570 GenericFilter
.__init
__ (self
)
572 self
.skip_extensions |
= set ([
573 # Don't change the tests, which could be woend by anyone.
586 def skip_file (self
, dir, filename
):
587 # g++.niklas/README contains historical copyright information
589 if filename
== 'README' and os
.path
.basename (dir) == 'g++.niklas':
591 # Similarly params/README.
592 if filename
== 'README' and os
.path
.basename (dir) == 'params':
594 if filename
== 'pdt_5.f03' and os
.path
.basename (dir) == 'gfortran.dg':
596 return GenericFilter
.skip_file (self
, dir, filename
)
598 class LibCppFilter (GenericFilter
):
600 GenericFilter
.__init
__ (self
)
602 self
.skip_extensions |
= set ([
603 # Maintained by the translation project.
606 # Automatically-generated.
610 class LibGCCFilter (GenericFilter
):
612 GenericFilter
.__init
__ (self
)
614 self
.skip_dirs |
= set ([
615 # Imported from GLIBC.
619 class LibStdCxxFilter (GenericFilter
):
621 GenericFilter
.__init
__ (self
)
623 self
.skip_files |
= set ([
624 # Contains no copyright of its own, but quotes the GPL.
628 self
.skip_dirs |
= set ([
629 # Contains automatically-generated sources.
632 # The testsuite data files shouldn't be changed.
635 # Contains imported images
639 self
.own_files |
= set ([
640 # Contains markup around the copyright owner.
644 def get_line_filter (self
, dir, filename
):
645 if filename
== 'boost_concept_check.h':
646 return re
.compile ('// \(C\) Copyright Jeremy Siek')
647 return GenericFilter
.get_line_filter (self
, dir, filename
)
649 class GCCCopyright (Copyright
):
650 def __init__ (self
, errors
):
651 Copyright
.__init
__ (self
, errors
)
653 canon_fsf
= 'Free Software Foundation, Inc.'
654 self
.add_package_author ('Free Software Foundation', canon_fsf
)
655 self
.add_package_author ('Free Software Foundation.', canon_fsf
)
656 self
.add_package_author ('Free Software Foundation Inc.', canon_fsf
)
657 self
.add_package_author ('Free Software Foundation, Inc', canon_fsf
)
658 self
.add_package_author ('Free Software Foundation, Inc.', canon_fsf
)
659 self
.add_package_author ('The Free Software Foundation', canon_fsf
)
660 self
.add_package_author ('The Free Software Foundation, Inc.', canon_fsf
)
661 self
.add_package_author ('Software Foundation, Inc.', canon_fsf
)
663 self
.add_external_author ('ARM')
664 self
.add_external_author ('AdaCore')
665 self
.add_external_author ('Ami Tavory and Vladimir Dreizin, IBM-HRL.')
666 self
.add_external_author ('Cavium Networks.')
667 self
.add_external_author ('Faraday Technology Corp.')
668 self
.add_external_author ('Florida State University')
669 self
.add_external_author ('Greg Colvin and Beman Dawes.')
670 self
.add_external_author ('Hewlett-Packard Company')
671 self
.add_external_author ('Intel Corporation')
672 self
.add_external_author ('Information Technology Industry Council.')
673 self
.add_external_author ('James Theiler, Brian Gough')
674 self
.add_external_author ('Makoto Matsumoto and Takuji Nishimura,')
675 self
.add_external_author ('National Research Council of Canada.')
676 self
.add_external_author ('NVIDIA Corporation')
677 self
.add_external_author ('Peter Dimov and Multi Media Ltd.')
678 self
.add_external_author ('Peter Dimov')
679 self
.add_external_author ('Pipeline Associates, Inc.')
680 self
.add_external_author ('Regents of the University of California.')
681 self
.add_external_author ('Silicon Graphics Computer Systems, Inc.')
682 self
.add_external_author ('Silicon Graphics')
683 self
.add_external_author ('Stephen L. Moshier')
684 self
.add_external_author ('Sun Microsystems, Inc. All rights reserved.')
685 self
.add_external_author ('The Go Authors. All rights reserved.')
686 self
.add_external_author ('The Go Authors. All rights reserved.')
687 self
.add_external_author ('The Go Authors.')
688 self
.add_external_author ('The Regents of the University of California.')
689 self
.add_external_author ('Unicode, Inc.')
690 self
.add_external_author ('University of Toronto.')
692 class GCCCmdLine (CmdLine
):
694 CmdLine
.__init
__ (self
, GCCCopyright
)
696 self
.add_dir ('.', TopLevelFilter())
697 # boehm-gc is imported from upstream.
698 self
.add_dir ('config', ConfigFilter())
699 # contrib isn't really part of GCC.
700 self
.add_dir ('fixincludes')
701 self
.add_dir ('gcc', GCCFilter())
702 self
.add_dir (os
.path
.join ('gcc', 'testsuite'), TestsuiteFilter())
703 self
.add_dir ('gnattools')
704 self
.add_dir ('gotools')
705 self
.add_dir ('include')
706 # intl is imported from upstream.
707 self
.add_dir ('libada')
708 self
.add_dir ('libatomic')
709 self
.add_dir ('libbacktrace')
710 self
.add_dir ('libcc1')
711 # libcilkrts is imported from upstream.
712 self
.add_dir ('libcpp', LibCppFilter())
713 self
.add_dir ('libdecnumber')
714 # libffi is imported from upstream.
715 self
.add_dir ('libgcc', LibGCCFilter())
716 self
.add_dir ('libgfortran')
717 # libgo is imported from upstream.
718 self
.add_dir ('libgomp')
719 self
.add_dir ('libhsail-rt')
720 self
.add_dir ('libiberty')
721 self
.add_dir ('libitm')
722 self
.add_dir ('libobjc')
723 # liboffloadmic is imported from upstream.
724 self
.add_dir ('libquadmath')
725 # libsanitizer is imported from upstream.
726 self
.add_dir ('libssp')
727 self
.add_dir ('libstdc++-v3', LibStdCxxFilter())
728 self
.add_dir ('libvtv')
729 self
.add_dir ('lto-plugin')
730 # maintainer-scripts maintainer-scripts
731 # zlib is imported from upstream.
733 self
.default_dirs
= [