gdb/copyright.py - gdb

Global variables defined

Functions defined

Source code

  1. #! /usr/bin/env python

  2. # Copyright (C) 2011-2015 Free Software Foundation, Inc.
  3. #
  4. # This file is part of GDB.
  5. #
  6. # This program is free software; you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation; either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program.  If not, see <http://www.gnu.org/licenses/>.

  18. """copyright.py

  19. This script updates the list of years in the copyright notices in
  20. most files maintained by the GDB project.

  21. Usage: cd src/gdb && python copyright.py

  22. Always review the output of this script before committing it!
  23. A useful command to review the output is:
  24.     % filterdiff -x \*.c -x \*.cc -x \*.h -x \*.exp updates.diff
  25. This removes the bulk of the changes which are most likely to be correct.
  26. """

  27. import datetime
  28. import os
  29. import os.path
  30. import subprocess


  31. def get_update_list():
  32.     """Return the list of files to update.

  33.     Assumes that the current working directory when called is the root
  34.     of the GDB source tree (NOT the gdb/ subdirectory!).  The names of
  35.     the files are relative to that root directory.
  36.     """
  37.     result = []
  38.     for gdb_dir in ('gdb', 'sim', 'include/gdb'):
  39.         for root, dirs, files in os.walk(gdb_dir, topdown=True):
  40.             for dirname in dirs:
  41.                 reldirname = "%s/%s" % (root, dirname)
  42.                 if (dirname in EXCLUDE_ALL_LIST
  43.                     or reldirname in EXCLUDE_LIST
  44.                     or reldirname in NOT_FSF_LIST
  45.                     or reldirname in BY_HAND):
  46.                     # Prune this directory from our search list.
  47.                     dirs.remove(dirname)
  48.             for filename in files:
  49.                 relpath = "%s/%s" % (root, filename)
  50.                 if (filename in EXCLUDE_ALL_LIST
  51.                     or relpath in EXCLUDE_LIST
  52.                     or relpath in NOT_FSF_LIST
  53.                     or relpath in BY_HAND):
  54.                     # Ignore this file.
  55.                     pass
  56.                 else:
  57.                     result.append(relpath)
  58.     return result


  59. def update_files(update_list):
  60.     """Update the copyright header of the files in the given list.

  61.     We use gnulib's update-copyright script for that.
  62.     """
  63.     # We want to use year intervals in the copyright notices, and
  64.     # all years should be collapsed to one single year interval,
  65.     # even if there are "holes" in the list of years found in the
  66.     # original copyright notice (OK'ed by the FSF, case [gnu.org #719834]).
  67.     os.environ['UPDATE_COPYRIGHT_USE_INTERVALS'] = '2'

  68.     # Perform the update, and save the output in a string.
  69.     update_cmd = ['bash', 'gdb/gnulib/import/extra/update-copyright']
  70.     update_cmd += update_list

  71.     p = subprocess.Popen(update_cmd, stdout=subprocess.PIPE,
  72.                          stderr=subprocess.STDOUT)
  73.     update_out = p.communicate()[0]

  74.     # Process the output.  Typically, a lot of files do not have
  75.     # a copyright notice :-(.  The update-copyright script prints
  76.     # a well defined warning when it did not find the copyright notice.
  77.     # For each of those, do a sanity check and see if they may in fact
  78.     # have one.  For the files that are found not to have one, we filter
  79.     # the line out from the output, since there is nothing more to do,
  80.     # short of looking at each file and seeing which notice is appropriate.
  81.     # Too much work! (~4,000 files listed as of 2012-01-03).
  82.     update_out = update_out.splitlines()
  83.     warning_string = ': warning: copyright statement not found'
  84.     warning_len = len(warning_string)

  85.     for line in update_out:
  86.         if line.endswith('\n'):
  87.             line = line[:-1]
  88.         if line.endswith(warning_string):
  89.             filename = line[:-warning_len]
  90.             if may_have_copyright_notice(filename):
  91.                 print line
  92.         else:
  93.             # Unrecognized file format. !?!
  94.             print "*** " + line


  95. def may_have_copyright_notice(filename):
  96.     """Check that the given file does not seem to have a copyright notice.

  97.     The filename is relative to the root directory.
  98.     This function assumes that the current working directory is that root
  99.     directory.

  100.     The algorigthm is fairly crude, meaning that it might return
  101.     some false positives.  I do not think it will return any false
  102.     negatives...  We might improve this function to handle more
  103.     complex cases later...
  104.     """
  105.     # For now, it may have a copyright notice if we find the word
  106.     # "Copyright" at the (reasonable) start of the given file, say
  107.     # 50 lines...
  108.     MAX_LINES = 50

  109.     fd = open(filename)

  110.     lineno = 1
  111.     for line in fd:
  112.         if 'Copyright' in line:
  113.             return True
  114.         lineno += 1
  115.         if lineno > 50:
  116.             return False
  117.     return False


  118. def main ():
  119.     """The main subprogram."""
  120.     if not os.path.isfile("gnulib/import/extra/update-copyright"):
  121.         print "Error: This script must be called from the gdb directory."
  122.     root_dir = os.path.dirname(os.getcwd())
  123.     os.chdir(root_dir)

  124.     update_list = get_update_list()
  125.     update_files (update_list)

  126.     # Remind the user that some files need to be updated by HAND...
  127.     if BY_HAND:
  128.         print
  129.         print "\033[31mREMINDER: The following files must be updated by hand." \
  130.               "\033[0m"
  131.         for filename in BY_HAND + MULTIPLE_COPYRIGHT_HEADERS:
  132.             print "  ", filename

  133. ############################################################################
  134. #
  135. # Some constants, placed at the end because they take up a lot of room.
  136. # The actual value of these constants is not significant to the understanding
  137. # of the script.
  138. #
  139. ############################################################################

  140. # Files which should not be modified, either because they are
  141. # generated, non-FSF, or otherwise special (e.g. license text,
  142. # or test cases which must be sensitive to line numbering).
  143. #
  144. # Filenames are relative to the root directory.
  145. EXCLUDE_LIST = (
  146.     'gdb/nat/glibc_thread_db.h',
  147.     'gdb/CONTRIBUTE',
  148.     'gdb/gnulib/import'
  149. )

  150. # Files which should not be modified, either because they are
  151. # generated, non-FSF, or otherwise special (e.g. license text,
  152. # or test cases which must be sensitive to line numbering).
  153. #
  154. # Matches any file or directory name anywhere.  Use with caution.
  155. # This is mostly for files that can be found in multiple directories.
  156. # Eg: We want all files named COPYING to be left untouched.

  157. EXCLUDE_ALL_LIST = (
  158.     "COPYING", "COPYING.LIB", "CVS", "configure", "copying.c",
  159.     "fdl.texi", "gpl.texi", "aclocal.m4",
  160. )

  161. # The list of files to update by hand.
  162. BY_HAND = (
  163.     # These files are sensitive to line numbering.
  164.     "gdb/testsuite/gdb.base/step-line.inp",
  165.     "gdb/testsuite/gdb.base/step-line.c",
  166. )

  167. # Files containing multiple copyright headers.  This script is only
  168. # fixing the first one it finds, so we need to finish the update
  169. # by hand.
  170. MULTIPLE_COPYRIGHT_HEADERS = (
  171.     "gdb/doc/gdb.texinfo",
  172.     "gdb/doc/refcard.tex",
  173.     "gdb/gdbarch.sh",
  174. )

  175. # The list of file which have a copyright, but not head by the FSF.
  176. # Filenames are relative to the root directory.
  177. NOT_FSF_LIST = (
  178.     "gdb/exc_request.defs",
  179.     "gdb/gdbtk",
  180.     "gdb/testsuite/gdb.gdbtk/",
  181.     "sim/arm/armemu.h", "sim/arm/armos.c", "sim/arm/gdbhost.c",
  182.     "sim/arm/dbg_hif.h", "sim/arm/dbg_conf.h", "sim/arm/communicate.h",
  183.     "sim/arm/armos.h", "sim/arm/armcopro.c", "sim/arm/armemu.c",
  184.     "sim/arm/kid.c", "sim/arm/thumbemu.c", "sim/arm/armdefs.h",
  185.     "sim/arm/armopts.h", "sim/arm/dbg_cp.h", "sim/arm/dbg_rdi.h",
  186.     "sim/arm/parent.c", "sim/arm/armsupp.c", "sim/arm/armrdi.c",
  187.     "sim/arm/bag.c", "sim/arm/armvirt.c", "sim/arm/main.c", "sim/arm/bag.h",
  188.     "sim/arm/communicate.c", "sim/arm/gdbhost.h", "sim/arm/armfpe.h",
  189.     "sim/arm/arminit.c",
  190.     "sim/common/cgen-fpu.c", "sim/common/cgen-fpu.h",
  191.     "sim/common/cgen-accfp.c",
  192.     "sim/erc32/sis.h", "sim/erc32/erc32.c", "sim/erc32/func.c",
  193.     "sim/erc32/float.c", "sim/erc32/interf.c", "sim/erc32/sis.c",
  194.     "sim/erc32/exec.c",
  195.     "sim/mips/m16run.c", "sim/mips/sim-main.c",
  196.     "sim/moxie/moxie-gdb.dts",
  197.     # Not a single file in sim/ppc/ appears to be copyright FSF :-(.
  198.     "sim/ppc/filter.h", "sim/ppc/gen-support.h", "sim/ppc/ld-insn.h",
  199.     "sim/ppc/hw_sem.c", "sim/ppc/hw_disk.c", "sim/ppc/idecode_branch.h",
  200.     "sim/ppc/sim-endian.h", "sim/ppc/table.c", "sim/ppc/hw_core.c",
  201.     "sim/ppc/gen-support.c", "sim/ppc/gen-semantics.h", "sim/ppc/cpu.h",
  202.     "sim/ppc/sim_callbacks.h", "sim/ppc/RUN", "sim/ppc/Makefile.in",
  203.     "sim/ppc/emul_chirp.c", "sim/ppc/hw_nvram.c", "sim/ppc/dc-test.01",
  204.     "sim/ppc/hw_phb.c", "sim/ppc/hw_eeprom.c", "sim/ppc/bits.h",
  205.     "sim/ppc/hw_vm.c", "sim/ppc/cap.h", "sim/ppc/os_emul.h",
  206.     "sim/ppc/options.h", "sim/ppc/gen-idecode.c", "sim/ppc/filter.c",
  207.     "sim/ppc/corefile-n.h", "sim/ppc/std-config.h", "sim/ppc/ld-decode.h",
  208.     "sim/ppc/filter_filename.h", "sim/ppc/hw_shm.c",
  209.     "sim/ppc/pk_disklabel.c", "sim/ppc/dc-simple", "sim/ppc/misc.h",
  210.     "sim/ppc/device_table.h", "sim/ppc/ld-insn.c", "sim/ppc/inline.c",
  211.     "sim/ppc/emul_bugapi.h", "sim/ppc/hw_cpu.h", "sim/ppc/debug.h",
  212.     "sim/ppc/hw_ide.c", "sim/ppc/debug.c", "sim/ppc/gen-itable.h",
  213.     "sim/ppc/interrupts.c", "sim/ppc/hw_glue.c", "sim/ppc/emul_unix.c",
  214.     "sim/ppc/sim_calls.c", "sim/ppc/dc-complex", "sim/ppc/ld-cache.c",
  215.     "sim/ppc/registers.h", "sim/ppc/dc-test.02", "sim/ppc/options.c",
  216.     "sim/ppc/igen.h", "sim/ppc/registers.c", "sim/ppc/device.h",
  217.     "sim/ppc/emul_chirp.h", "sim/ppc/hw_register.c", "sim/ppc/hw_init.c",
  218.     "sim/ppc/sim-endian-n.h", "sim/ppc/filter_filename.c",
  219.     "sim/ppc/bits.c", "sim/ppc/idecode_fields.h", "sim/ppc/hw_memory.c",
  220.     "sim/ppc/misc.c", "sim/ppc/double.c", "sim/ppc/psim.h",
  221.     "sim/ppc/hw_trace.c", "sim/ppc/emul_netbsd.h", "sim/ppc/psim.c",
  222.     "sim/ppc/ppc-instructions", "sim/ppc/tree.h", "sim/ppc/README",
  223.     "sim/ppc/gen-icache.h", "sim/ppc/gen-model.h", "sim/ppc/ld-cache.h",
  224.     "sim/ppc/mon.c", "sim/ppc/corefile.h", "sim/ppc/vm.c",
  225.     "sim/ppc/INSTALL", "sim/ppc/gen-model.c", "sim/ppc/hw_cpu.c",
  226.     "sim/ppc/corefile.c", "sim/ppc/hw_opic.c", "sim/ppc/gen-icache.c",
  227.     "sim/ppc/events.h", "sim/ppc/os_emul.c", "sim/ppc/emul_generic.c",
  228.     "sim/ppc/main.c", "sim/ppc/hw_com.c", "sim/ppc/gen-semantics.c",
  229.     "sim/ppc/emul_bugapi.c", "sim/ppc/device.c", "sim/ppc/emul_generic.h",
  230.     "sim/ppc/tree.c", "sim/ppc/mon.h", "sim/ppc/interrupts.h",
  231.     "sim/ppc/cap.c", "sim/ppc/cpu.c", "sim/ppc/hw_phb.h",
  232.     "sim/ppc/device_table.c", "sim/ppc/lf.c", "sim/ppc/lf.c",
  233.     "sim/ppc/dc-stupid", "sim/ppc/hw_pal.c", "sim/ppc/ppc-spr-table",
  234.     "sim/ppc/emul_unix.h", "sim/ppc/words.h", "sim/ppc/basics.h",
  235.     "sim/ppc/hw_htab.c", "sim/ppc/lf.h", "sim/ppc/ld-decode.c",
  236.     "sim/ppc/sim-endian.c", "sim/ppc/gen-itable.c",
  237.     "sim/ppc/idecode_expression.h", "sim/ppc/table.h", "sim/ppc/dgen.c",
  238.     "sim/ppc/events.c", "sim/ppc/gen-idecode.h", "sim/ppc/emul_netbsd.c",
  239.     "sim/ppc/igen.c", "sim/ppc/vm_n.h", "sim/ppc/vm.h",
  240.     "sim/ppc/hw_iobus.c", "sim/ppc/inline.h",
  241.     "sim/testsuite/sim/bfin/s21.s", "sim/testsuite/sim/mips/mips32-dsp2.s",
  242. )

  243. if __name__ == "__main__":
  244.     main()