3 # Copyright (C) 2013-2024 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 # Pass --this-year to the script if you want it to add the current year
19 # to all applicable notices. Pass --quilt if you are using quilt and
20 # want files to be added to the quilt before being changed.
22 # By default the script will update all directories for which the
23 # output has been vetted. You can instead pass the names of individual
24 # directories, including those that haven't been approved. So:
26 # update-copyright.py --this-year
28 # is the command that would be used at the beginning of a year to update
29 # all copyright notices (and possibly at other times to check whether
30 # new files have been added with old years). On the other hand:
32 # update-copyright.py --this-year libiberty
34 # would run the script on just libiberty/.
36 # This script was copied from gcc's contrib/ and modified to suit
37 # binutils. In contrast to the gcc script, this one will update
38 # the testsuite and --version output strings too.
50 def report (self
, filename
, string
):
52 string
= filename
+ ': ' + string
53 sys
.stderr
.write (string
+ '\n')
57 return self
.num_errors
== 0
61 self
.skip_files
= set()
62 self
.skip_dirs
= set()
63 self
.skip_extensions
= set([
67 self
.fossilised_files
= set()
68 self
.own_files
= set()
70 self
.skip_files |
= set ([
84 # Skip auto- and libtool-related files
105 # Skip FSF mission statement, etc.
110 # Skip imported texinfo files.
114 self
.skip_extensions |
= set ([
115 # Maintained by the translation project.
118 # Automatically-generated.
122 self
.skip_dirs |
= set ([
127 def get_line_filter (self
, dir, filename
):
128 if filename
.startswith ('ChangeLog'):
129 # Ignore references to copyright in changelog entries.
130 return re
.compile ('\t')
134 def skip_file (self
, dir, filename
):
135 if filename
in self
.skip_files
:
138 (base
, extension
) = os
.path
.splitext (os
.path
.join (dir, filename
))
139 if extension
in self
.skip_extensions
:
142 if extension
== '.in':
143 # Skip .in files produced by automake.
144 if os
.path
.exists (base
+ '.am'):
147 # Skip files produced by autogen
148 if (os
.path
.exists (base
+ '.def')
149 and os
.path
.exists (base
+ '.tpl')):
152 # Skip configure files produced by autoconf
153 if filename
== 'configure':
154 if os
.path
.exists (base
+ '.ac'):
156 if os
.path
.exists (base
+ '.in'):
161 def skip_dir (self
, dir, subdir
):
162 return subdir
in self
.skip_dirs
164 def is_fossilised_file (self
, dir, filename
):
165 if filename
in self
.fossilised_files
:
167 # Only touch current current ChangeLogs.
168 if filename
!= 'ChangeLog' and filename
.find ('ChangeLog') >= 0:
172 def by_package_author (self
, dir, filename
):
173 return filename
in self
.own_files
176 def __init__ (self
, errors
):
179 # Characters in a range of years. Include '.' for typos.
180 ranges
= '[0-9](?:[-0-9.,\s]|\s+and\s+)*[0-9]'
182 # Non-whitespace characters in a copyright holder's name.
186 self
.year_re
= re
.compile ('[0-9]+')
188 # Matches part of a year or copyright holder.
189 self
.continuation_re
= re
.compile (ranges
+ '|' + name
)
191 # Matches a full copyright notice:
192 self
.copyright_re
= re
.compile (
193 # 1: 'Copyright (C)', etc.
195 '|[Cc]opyright\s+\([Cc]\)'
197 '|[Cc]opyright\s+©'
198 '|[Cc]opyright\s+@copyright{}'
199 '|@set\s+copyright[\w-]+)'
201 # 2: the years. Include the whitespace in the year, so that
202 # we can remove any excess.
203 '(\s*(?:' + ranges
+ ',?'
204 '|@value\{[^{}]*\})\s*)'
209 # 4: the copyright holder. Don't allow multiple consecutive
210 # spaces, so that right-margin gloss doesn't get caught
211 # (e.g. gnat_ugn.texi).
212 '(' + name
+ '(?:\s?' + name
+ ')*)?')
214 # A regexp for notices that might have slipped by. Just matching
215 # 'copyright' is too noisy, and 'copyright.*[0-9]' falls foul of
216 # HTML header markers, so check for 'copyright' and two digits.
217 self
.other_copyright_re
= re
.compile ('(^|[^\._])copyright[^=]*[0-9][0-9]',
219 self
.comment_re
= re
.compile('#+|[*]+|;+|%+|//+|@c |dnl ')
220 self
.holders
= { '@copying': '@copying' }
221 self
.holder_prefixes
= set()
223 # True to 'quilt add' files before changing them.
224 self
.use_quilt
= False
226 # If set, force all notices to include this year.
229 # Goes after the year(s). Could be ', '.
232 def add_package_author (self
, holder
, canon_form
= None):
235 self
.holders
[holder
] = canon_form
236 index
= holder
.find (' ')
238 self
.holder_prefixes
.add (holder
[:index
])
239 index
= holder
.find (' ', index
+ 1)
241 def add_external_author (self
, holder
):
242 self
.holders
[holder
] = None
244 class BadYear (Exception):
245 def __init__ (self
, year
):
249 return 'unrecognised year: ' + self
.year
251 def parse_year (self
, string
):
253 if len (string
) == 2:
256 elif len (string
) == 4:
258 raise self
.BadYear (string
)
260 def year_range (self
, years
):
261 year_list
= [self
.parse_year (year
)
262 for year
in self
.year_re
.findall (years
)]
263 assert len (year_list
) > 0
264 return (min (year_list
), max (year_list
))
266 def set_use_quilt (self
, use_quilt
):
267 self
.use_quilt
= use_quilt
269 def include_year (self
, year
):
270 assert not self
.max_year
273 def canonicalise_years (self
, dir, filename
, filter, years
):
274 # Leave texinfo variables alone.
275 if years
.startswith ('@value'):
278 (min_year
, max_year
) = self
.year_range (years
)
280 # Update the upper bound, if enabled.
281 if self
.max_year
and not filter.is_fossilised_file (dir, filename
):
282 max_year
= max (max_year
, self
.max_year
)
285 if min_year
== max_year
:
286 return '%d' % min_year
288 return '%d-%d' % (min_year
, max_year
)
290 def strip_continuation (self
, line
):
292 match
= self
.comment_re
.match (line
)
294 line
= line
[match
.end():].lstrip()
297 def is_complete (self
, match
):
298 holder
= match
.group (4)
300 and (holder
not in self
.holder_prefixes
301 or holder
in self
.holders
))
303 def update_copyright (self
, dir, filename
, filter, file, line
, match
):
306 pathname
= os
.path
.join (dir, filename
)
308 intro
= match
.group (1)
309 if intro
.startswith ('@set'):
310 # Texinfo year variables should always be on one line
311 after_years
= line
[match
.end (2):].strip()
312 if after_years
!= '':
313 self
.errors
.report (pathname
,
314 'trailing characters in @set: '
316 return (False, orig_line
, next_line
)
318 # If it looks like the copyright is incomplete, add the next line.
319 while not self
.is_complete (match
):
321 next_line
= file.readline()
322 except StopIteration:
325 # If the next line doesn't look like a proper continuation,
326 # assume that what we've got is complete.
327 continuation
= self
.strip_continuation (next_line
)
328 if not self
.continuation_re
.match (continuation
):
331 # Merge the lines for matching purposes.
332 orig_line
+= next_line
333 line
= line
.rstrip() + ' ' + continuation
336 # Rematch with the longer line, at the original position.
337 match
= self
.copyright_re
.match (line
, match
.start())
340 holder
= match
.group (4)
342 # Use the filter to test cases where markup is getting in the way.
343 if filter.by_package_author (dir, filename
):
344 assert holder
not in self
.holders
347 self
.errors
.report (pathname
, 'missing copyright holder')
348 return (False, orig_line
, next_line
)
350 elif holder
not in self
.holders
:
351 self
.errors
.report (pathname
,
352 'unrecognised copyright holder: ' + holder
)
353 return (False, orig_line
, next_line
)
356 # See whether the copyright is associated with the package
358 canon_form
= self
.holders
[holder
]
360 return (False, orig_line
, next_line
)
362 # Make sure the author is given in a consistent way.
363 line
= (line
[:match
.start (4)]
365 + line
[match
.end (4):])
368 line
= line
[:match
.start (3)] + line
[match
.end (3):]
370 # Update the copyright years.
371 years
= match
.group (2).strip()
373 and match
.start(0) > 0 and line
[match
.start(0)-1] == '"'
374 and not filter.is_fossilised_file (dir, filename
)):
375 # A printed copyright date consists of the current year
376 canon_form
= '%d' % self
.max_year
379 canon_form
= self
.canonicalise_years (dir, filename
, filter, years
)
380 except self
.BadYear
as e
:
381 self
.errors
.report (pathname
, str (e
))
382 return (False, orig_line
, next_line
)
384 line
= (line
[:match
.start (2)]
385 + ' ' + canon_form
+ self
.separator
386 + line
[match
.end (2):])
388 # Use the standard (C) form.
389 if intro
.endswith ('right'):
391 elif intro
.endswith ('(c)'):
392 intro
= intro
[:-3] + '(C)'
393 line
= line
[:match
.start (1)] + intro
+ line
[match
.end (1):]
395 # Strip trailing whitespace
396 line
= line
.rstrip() + '\n'
398 return (line
!= orig_line
, line
, next_line
)
400 def guess_encoding (self
, pathname
):
401 for encoding
in ('utf8', 'iso8859'):
403 open(pathname
, 'r', encoding
=encoding
).read()
405 except UnicodeDecodeError:
409 def process_file (self
, dir, filename
, filter):
410 pathname
= os
.path
.join (dir, filename
)
411 if filename
.endswith ('.tmp'):
412 # Looks like something we tried to create before.
421 line_filter
= filter.get_line_filter (dir, filename
)
423 encoding
= self
.guess_encoding(pathname
)
424 with
open (pathname
, 'r', encoding
=encoding
) as file:
426 mode
= os
.fstat (file.fileno()).st_mode
430 # Leave filtered-out lines alone.
431 if not (line_filter
and line_filter
.match (line
)):
432 match
= self
.copyright_re
.search (line
)
434 res
= self
.update_copyright (dir, filename
, filter,
436 (this_changed
, line
, next_line
) = res
437 changed
= changed
or this_changed
439 # Check for copyright lines that might have slipped by.
440 elif self
.other_copyright_re
.search (line
):
441 self
.errors
.report (pathname
,
442 'unrecognised copyright: %s'
447 # If something changed, write the new file out.
448 if changed
and self
.errors
.ok():
449 tmp_pathname
= pathname
+ '.tmp'
450 with
open (tmp_pathname
, 'w', encoding
=encoding
) as file:
453 os
.fchmod (file.fileno(), mode
)
455 subprocess
.call (['quilt', 'add', pathname
])
456 os
.rename (tmp_pathname
, pathname
)
458 def process_tree (self
, tree
, filter):
459 for (dir, subdirs
, filenames
) in os
.walk (tree
):
460 # Don't recurse through directories that should be skipped.
461 for i
in range (len (subdirs
) - 1, -1, -1):
462 if filter.skip_dir (dir, subdirs
[i
]):
465 # Handle the files in this directory.
466 for filename
in filenames
:
467 if filter.skip_file (dir, filename
):
468 sys
.stdout
.write ('Skipping %s\n'
469 % os
.path
.join (dir, filename
))
471 self
.process_file (dir, filename
, filter)
474 def __init__ (self
, copyright
= Copyright
):
475 self
.errors
= Errors()
476 self
.copyright
= copyright (self
.errors
)
478 self
.default_dirs
= []
479 self
.chosen_dirs
= []
480 self
.option_handlers
= dict()
481 self
.option_help
= []
483 self
.add_option ('--help', 'Print this help', self
.o_help
)
484 self
.add_option ('--quilt', '"quilt add" files before changing them',
486 self
.add_option ('--this-year', 'Add the current year to every notice',
489 def add_option (self
, name
, help, handler
):
490 self
.option_help
.append ((name
, help))
491 self
.option_handlers
[name
] = handler
493 def add_dir (self
, dir, filter = GenericFilter()):
494 self
.dirs
.append ((dir, filter))
496 def o_help (self
, option
= None):
497 sys
.stdout
.write ('Usage: %s [options] dir1 dir2...\n\n'
498 'Options:\n' % sys
.argv
[0])
499 format
= '%-15s %s\n'
500 for (what
, help) in self
.option_help
:
501 sys
.stdout
.write (format
% (what
, help))
502 sys
.stdout
.write ('\nDirectories:\n')
506 for (dir, filter) in self
.dirs
:
508 if i
% 3 == 0 or i
== len (self
.dirs
):
509 sys
.stdout
.write (dir + '\n')
511 sys
.stdout
.write (format
% dir)
514 def o_quilt (self
, option
):
515 self
.copyright
.set_use_quilt (True)
517 def o_this_year (self
, option
):
518 self
.copyright
.include_year (time
.localtime().tm_year
)
521 for arg
in sys
.argv
[1:]:
523 self
.chosen_dirs
.append (arg
)
524 elif arg
in self
.option_handlers
:
525 self
.option_handlers
[arg
] (arg
)
527 self
.errors
.report (None, 'unrecognised option: ' + arg
)
529 if len (self
.chosen_dirs
) == 0:
530 self
.chosen_dirs
= self
.default_dirs
531 if len (self
.chosen_dirs
) == 0:
534 for chosen_dir
in self
.chosen_dirs
:
535 canon_dir
= os
.path
.join (chosen_dir
, '')
537 for (dir, filter) in self
.dirs
:
538 if (dir + os
.sep
).startswith (canon_dir
):
540 self
.copyright
.process_tree (dir, filter)
542 self
.errors
.report (None, 'unrecognised directory: '
544 sys
.exit (0 if self
.errors
.ok() else 1)
546 #----------------------------------------------------------------------------
548 class TopLevelFilter (GenericFilter
):
549 def skip_dir (self
, dir, subdir
):
552 class ConfigFilter (GenericFilter
):
554 GenericFilter
.__init
__ (self
)
556 def skip_file (self
, dir, filename
):
557 if filename
.endswith ('.m4'):
558 pathname
= os
.path
.join (dir, filename
)
559 with
open (pathname
) as file:
560 # Skip files imported from gettext.
561 if file.readline().find ('gettext-') >= 0:
563 return GenericFilter
.skip_file (self
, dir, filename
)
565 class LdFilter (GenericFilter
):
567 GenericFilter
.__init
__ (self
)
569 self
.skip_extensions |
= set ([
570 # ld testsuite output match files.
574 class BinutilsCopyright (Copyright
):
575 def __init__ (self
, errors
):
576 Copyright
.__init
__ (self
, errors
)
578 canon_fsf
= 'Free Software Foundation, Inc.'
579 self
.add_package_author ('Free Software Foundation', canon_fsf
)
580 self
.add_package_author ('Free Software Foundation.', canon_fsf
)
581 self
.add_package_author ('Free Software Foundation Inc.', canon_fsf
)
582 self
.add_package_author ('Free Software Foundation, Inc', canon_fsf
)
583 self
.add_package_author ('Free Software Foundation, Inc.', canon_fsf
)
584 self
.add_package_author ('The Free Software Foundation', canon_fsf
)
585 self
.add_package_author ('The Free Software Foundation, Inc.', canon_fsf
)
586 self
.add_package_author ('Software Foundation, Inc.', canon_fsf
)
588 self
.add_external_author ('Carnegie Mellon University')
589 self
.add_external_author ('John D. Polstra.')
590 self
.add_external_author ('Innovative Computing Labs')
591 self
.add_external_author ('Linaro Ltd.')
592 self
.add_external_author ('MIPS Computer Systems, Inc.')
593 self
.add_external_author ('Red Hat Inc.')
594 self
.add_external_author ('Regents of the University of California.')
595 self
.add_external_author ('The Regents of the University of California.')
596 self
.add_external_author ('Third Eye Software, Inc.')
597 self
.add_external_author ('Ulrich Drepper')
598 self
.add_external_author ('Synopsys Inc.')
599 self
.add_external_author ('Alan Woodland')
600 self
.add_external_author ('Diego Elio Petteno')
602 class BinutilsCmdLine (CmdLine
):
604 CmdLine
.__init
__ (self
, BinutilsCopyright
)
606 self
.add_dir ('.', TopLevelFilter())
608 self
.add_dir ('binutils')
609 self
.add_dir ('config', ConfigFilter())
611 self
.add_dir ('elfcpp')
615 self
.add_dir ('gdbserver')
616 self
.add_dir ('gdbsupport')
617 self
.add_dir ('gold')
618 self
.add_dir ('gprof')
619 self
.add_dir ('gprofng')
620 self
.add_dir ('include')
621 self
.add_dir ('ld', LdFilter())
622 self
.add_dir ('libbacktrace')
623 self
.add_dir ('libctf')
624 self
.add_dir ('libdecnumber')
625 self
.add_dir ('libiberty')
626 self
.add_dir ('libsframe')
627 self
.add_dir ('opcodes')
628 self
.add_dir ('readline')
631 self
.default_dirs
= [
648 BinutilsCmdLine().main()