Commit | Line | Data |
---|---|---|
e9bdf92c JB |
1 | #! /usr/bin/env python |
2 | ||
0b302171 | 3 | # Copyright (C) 2011-2012 Free Software Foundation, Inc. |
8ba098ad JB |
4 | # |
5 | # This file is part of GDB. | |
6 | # | |
7 | # This program is free software; you can redistribute it and/or modify | |
8 | # it under the terms of the GNU General Public License as published by | |
9 | # the Free Software Foundation; either version 3 of the License, or | |
10 | # (at your option) any later version. | |
11 | # | |
12 | # This program is distributed in the hope that it will be useful, | |
13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | # GNU General Public License for more details. | |
16 | # | |
17 | # You should have received a copy of the GNU General Public License | |
18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | ||
e9bdf92c JB |
20 | """copyright.py |
21 | ||
8ba098ad JB |
22 | This script updates the list of years in the copyright notices in |
23 | most files maintained by the GDB project. | |
24 | ||
25 | Usage: cd src/gdb && python copyright.py | |
e9bdf92c | 26 | |
8ba098ad JB |
27 | Always review the output of this script before committing it! |
28 | A useful command to review the output is: | |
29 | % filterdiff -x \*.c -x \*.cc -x \*.h -x \*.exp updates.diff | |
30 | This removes the bulk of the changes which are most likely to be correct. | |
e9bdf92c JB |
31 | """ |
32 | ||
33 | import datetime | |
e9bdf92c JB |
34 | import os |
35 | import os.path | |
8ba098ad JB |
36 | import subprocess |
37 | ||
38 | # A list of prefixes that start a multi-line comment. These prefixes | |
39 | # should not be repeatead when wraping long lines. | |
40 | MULTILINE_COMMENT_PREFIXES = ( | |
41 | '/*', # C/C++ | |
42 | '<!--', # XML | |
43 | '{', # Pascal | |
44 | ) | |
e9bdf92c | 45 | |
8ba098ad JB |
46 | |
47 | def get_update_list(): | |
48 | """Return the list of files to update. | |
49 | ||
50 | Assumes that the current working directory when called is the root | |
51 | of the GDB source tree (NOT the gdb/ subdirectory!). The names of | |
52 | the files are relative to that root directory. | |
e9bdf92c | 53 | """ |
8ba098ad JB |
54 | result = [] |
55 | for gdb_dir in ('gdb', 'sim', 'include/gdb'): | |
56 | for root, dirs, files in os.walk(gdb_dir, topdown=True): | |
57 | for dirname in dirs: | |
58 | reldirname = "%s/%s" % (root, dirname) | |
59 | if (dirname in EXCLUDE_ALL_LIST | |
60 | or reldirname in EXCLUDE_LIST | |
61 | or reldirname in NOT_FSF_LIST | |
62 | or reldirname in BY_HAND): | |
63 | # Prune this directory from our search list. | |
64 | dirs.remove(dirname) | |
65 | for filename in files: | |
66 | relpath = "%s/%s" % (root, filename) | |
67 | if (filename in EXCLUDE_ALL_LIST | |
68 | or relpath in EXCLUDE_LIST | |
69 | or relpath in NOT_FSF_LIST | |
70 | or relpath in BY_HAND): | |
71 | # Ignore this file. | |
72 | pass | |
73 | else: | |
74 | result.append(relpath) | |
75 | return result | |
76 | ||
77 | ||
78 | def update_files(update_list): | |
79 | """Update the copyright header of the files in the given list. | |
80 | ||
81 | We use gnulib's update-copyright script for that. | |
82 | """ | |
83 | # Tell the update-copyright script that we do not want it to | |
84 | # repeat the prefixes in MULTILINE_COMMENT_PREFIXES. | |
85 | os.environ['MULTILINE_COMMENT_PREFIXES'] = \ | |
86 | '\n'.join(MULTILINE_COMMENT_PREFIXES) | |
87 | # We want to use year intervals in the copyright notices. | |
88 | os.environ['UPDATE_COPYRIGHT_USE_INTERVALS'] = '1' | |
89 | ||
90 | # Perform the update, and save the output in a string. | |
91 | update_cmd = ['bash', 'gdb/gnulib/extra/update-copyright'] + update_list | |
92 | p = subprocess.Popen(update_cmd, stdout=subprocess.PIPE, | |
93 | stderr=subprocess.STDOUT) | |
94 | update_out = p.communicate()[0] | |
95 | ||
96 | # Process the output. Typically, a lot of files do not have | |
97 | # a copyright notice :-(. The update-copyright script prints | |
98 | # a well defined warning when it did not find the copyright notice. | |
99 | # For each of those, do a sanity check and see if they may in fact | |
100 | # have one. For the files that are found not to have one, we filter | |
101 | # the line out from the output, since there is nothing more to do, | |
102 | # short of looking at each file and seeing which notice is appropriate. | |
103 | # Too much work! (~4,000 files listed as of 2012-01-03). | |
104 | update_out = update_out.splitlines() | |
105 | warning_string = ': warning: copyright statement not found' | |
106 | warning_len = len(warning_string) | |
107 | ||
108 | for line in update_out: | |
109 | if line.endswith('\n'): | |
110 | line = line[:-1] | |
111 | if line.endswith(warning_string): | |
112 | filename = line[:-warning_len] | |
113 | if may_have_copyright_notice(filename): | |
114 | print line | |
115 | else: | |
116 | # Unrecognized file format. !?! | |
117 | print "*** " + line | |
118 | ||
119 | ||
120 | def may_have_copyright_notice(filename): | |
121 | """Check that the given file does not seem to have a copyright notice. | |
122 | ||
123 | The filename is relative to the root directory. | |
124 | This function assumes that the current working directory is that root | |
125 | directory. | |
e9bdf92c | 126 | |
8ba098ad JB |
127 | The algorigthm is fairly crude, meaning that it might return |
128 | some false positives. I do not think it will return any false | |
129 | negatives... We might improve this function to handle more | |
130 | complex cases later... | |
131 | """ | |
132 | # For now, it may have a copyright notice if we find the word | |
133 | # "Copyright" at the (reasonable) start of the given file, say | |
134 | # 50 lines... | |
135 | MAX_LINES = 50 | |
136 | ||
137 | fd = open(filename) | |
138 | ||
139 | lineno = 1 | |
140 | for line in fd: | |
141 | if 'Copyright' in line: | |
142 | return True | |
143 | lineno += 1 | |
144 | if lineno > 50: | |
145 | return False | |
146 | return False | |
147 | ||
148 | ||
149 | def main (): | |
150 | """The main subprogram.""" | |
151 | if not os.path.isfile("gnulib/extra/update-copyright"): | |
152 | print "Error: This script must be called from the gdb directory." | |
153 | root_dir = os.path.dirname(os.getcwd()) | |
154 | os.chdir(root_dir) | |
155 | ||
156 | update_list = get_update_list() | |
157 | update_files (update_list) | |
158 | ||
159 | # Remind the user that some files need to be updated by HAND... | |
160 | if BY_HAND: | |
161 | ||
162 | print "\033[31mREMINDER: The following files must be updated by hand." \ | |
163 | "\033[0m" | |
164 | for filename in BY_HAND: | |
165 | print " ", filename | |
166 | ||
167 | ############################################################################ | |
168 | # | |
169 | # Some constants, placed at the end because they take up a lot of room. | |
170 | # The actual value of these constants is not significant to the understanding | |
171 | # of the script. | |
172 | # | |
173 | ############################################################################ | |
e9bdf92c | 174 | |
8ba098ad JB |
175 | # Files which should not be modified, either because they are |
176 | # generated, non-FSF, or otherwise special (e.g. license text, | |
177 | # or test cases which must be sensitive to line numbering). | |
178 | # | |
179 | # Filenames are relative to the root directory. | |
180 | EXCLUDE_LIST = ( | |
181 | 'gdb/gdbarch.c', 'gdb/gdbarch.h', | |
182 | 'gdb/gnulib' | |
183 | ) | |
e9bdf92c JB |
184 | |
185 | # Files which should not be modified, either because they are | |
186 | # generated, non-FSF, or otherwise special (e.g. license text, | |
187 | # or test cases which must be sensitive to line numbering). | |
8ba098ad JB |
188 | # |
189 | # Matches any file or directory name anywhere. Use with caution. | |
190 | # This is mostly for files that can be found in multiple directories. | |
191 | # Eg: We want all files named COPYING to be left untouched. | |
192 | ||
193 | EXCLUDE_ALL_LIST = ( | |
194 | "COPYING", "COPYING.LIB", "CVS", "configure", "copying.c", | |
195 | "fdl.texi", "gpl.texi", "aclocal.m4", | |
196 | ) | |
197 | ||
198 | # The list of files to update by hand. | |
199 | BY_HAND = ( | |
200 | # These files are sensitive to line numbering. | |
201 | "gdb/testsuite/gdb.base/step-line.inp", | |
202 | "gdb/testsuite/gdb.base/step-line.c", | |
203 | ) | |
204 | ||
205 | # The list of file which have a copyright, but not head by the FSF. | |
206 | # Filenames are relative to the root directory. | |
207 | NOT_FSF_LIST = ( | |
208 | "gdb/exc_request.defs", | |
209 | "gdb/osf-share", | |
210 | "gdb/gdbtk", | |
211 | "gdb/testsuite/gdb.gdbtk/", | |
212 | "sim/arm/armemu.h", "sim/arm/armos.c", "sim/arm/gdbhost.c", | |
213 | "sim/arm/dbg_hif.h", "sim/arm/dbg_conf.h", "sim/arm/communicate.h", | |
214 | "sim/arm/armos.h", "sim/arm/armcopro.c", "sim/arm/armemu.c", | |
215 | "sim/arm/kid.c", "sim/arm/thumbemu.c", "sim/arm/armdefs.h", | |
216 | "sim/arm/armopts.h", "sim/arm/dbg_cp.h", "sim/arm/dbg_rdi.h", | |
217 | "sim/arm/parent.c", "sim/arm/armsupp.c", "sim/arm/armrdi.c", | |
218 | "sim/arm/bag.c", "sim/arm/armvirt.c", "sim/arm/main.c", "sim/arm/bag.h", | |
219 | "sim/arm/communicate.c", "sim/arm/gdbhost.h", "sim/arm/armfpe.h", | |
220 | "sim/arm/arminit.c", | |
221 | "sim/common/cgen-fpu.c", "sim/common/cgen-fpu.h", "sim/common/cgen-fpu.h", | |
222 | "sim/common/cgen-accfp.c", "sim/common/sim-fpu.c", | |
223 | "sim/erc32/sis.h", "sim/erc32/erc32.c", "sim/erc32/func.c", | |
224 | "sim/erc32/float.c", "sim/erc32/interf.c", "sim/erc32/sis.c", | |
225 | "sim/erc32/exec.c", | |
226 | "sim/mips/m16run.c", "sim/mips/sim-main.c", | |
227 | "sim/mn10300/sim-main.h", | |
228 | "sim/moxie/moxie-gdb.dts", | |
229 | # Not a single file in sim/ppc/ appears to be copyright FSF :-(. | |
230 | "sim/ppc/filter.h", "sim/ppc/gen-support.h", "sim/ppc/ld-insn.h", | |
231 | "sim/ppc/hw_sem.c", "sim/ppc/hw_disk.c", "sim/ppc/idecode_branch.h", | |
232 | "sim/ppc/sim-endian.h", "sim/ppc/table.c", "sim/ppc/hw_core.c", | |
233 | "sim/ppc/gen-support.c", "sim/ppc/gen-semantics.h", "sim/ppc/cpu.h", | |
234 | "sim/ppc/sim_callbacks.h", "sim/ppc/RUN", "sim/ppc/Makefile.in", | |
235 | "sim/ppc/emul_chirp.c", "sim/ppc/hw_nvram.c", "sim/ppc/dc-test.01", | |
236 | "sim/ppc/hw_phb.c", "sim/ppc/hw_eeprom.c", "sim/ppc/bits.h", | |
237 | "sim/ppc/hw_vm.c", "sim/ppc/cap.h", "sim/ppc/os_emul.h", | |
238 | "sim/ppc/options.h", "sim/ppc/gen-idecode.c", "sim/ppc/filter.c", | |
239 | "sim/ppc/corefile-n.h", "sim/ppc/std-config.h", "sim/ppc/ld-decode.h", | |
240 | "sim/ppc/filter_filename.h", "sim/ppc/hw_shm.c", | |
241 | "sim/ppc/pk_disklabel.c", "sim/ppc/dc-simple", "sim/ppc/misc.h", | |
242 | "sim/ppc/device_table.h", "sim/ppc/ld-insn.c", "sim/ppc/inline.c", | |
243 | "sim/ppc/emul_bugapi.h", "sim/ppc/hw_cpu.h", "sim/ppc/debug.h", | |
244 | "sim/ppc/hw_ide.c", "sim/ppc/debug.c", "sim/ppc/gen-itable.h", | |
245 | "sim/ppc/interrupts.c", "sim/ppc/hw_glue.c", "sim/ppc/emul_unix.c", | |
246 | "sim/ppc/sim_calls.c", "sim/ppc/dc-complex", "sim/ppc/ld-cache.c", | |
247 | "sim/ppc/registers.h", "sim/ppc/dc-test.02", "sim/ppc/options.c", | |
248 | "sim/ppc/igen.h", "sim/ppc/registers.c", "sim/ppc/device.h", | |
249 | "sim/ppc/emul_chirp.h", "sim/ppc/hw_register.c", "sim/ppc/hw_init.c", | |
250 | "sim/ppc/sim-endian-n.h", "sim/ppc/filter_filename.c", | |
251 | "sim/ppc/bits.c", "sim/ppc/idecode_fields.h", "sim/ppc/hw_memory.c", | |
252 | "sim/ppc/misc.c", "sim/ppc/double.c", "sim/ppc/psim.h", | |
253 | "sim/ppc/hw_trace.c", "sim/ppc/emul_netbsd.h", "sim/ppc/psim.c", | |
254 | "sim/ppc/ppc-instructions", "sim/ppc/tree.h", "sim/ppc/README", | |
255 | "sim/ppc/gen-icache.h", "sim/ppc/gen-model.h", "sim/ppc/ld-cache.h", | |
256 | "sim/ppc/mon.c", "sim/ppc/corefile.h", "sim/ppc/vm.c", | |
257 | "sim/ppc/INSTALL", "sim/ppc/gen-model.c", "sim/ppc/hw_cpu.c", | |
258 | "sim/ppc/corefile.c", "sim/ppc/hw_opic.c", "sim/ppc/gen-icache.c", | |
259 | "sim/ppc/events.h", "sim/ppc/os_emul.c", "sim/ppc/emul_generic.c", | |
260 | "sim/ppc/main.c", "sim/ppc/hw_com.c", "sim/ppc/gen-semantics.c", | |
261 | "sim/ppc/emul_bugapi.c", "sim/ppc/device.c", "sim/ppc/emul_generic.h", | |
262 | "sim/ppc/tree.c", "sim/ppc/mon.h", "sim/ppc/interrupts.h", | |
263 | "sim/ppc/cap.c", "sim/ppc/cpu.c", "sim/ppc/hw_phb.h", | |
264 | "sim/ppc/device_table.c", "sim/ppc/lf.c", "sim/ppc/lf.c", | |
265 | "sim/ppc/dc-stupid", "sim/ppc/hw_pal.c", "sim/ppc/ppc-spr-table", | |
266 | "sim/ppc/emul_unix.h", "sim/ppc/words.h", "sim/ppc/basics.h", | |
267 | "sim/ppc/hw_htab.c", "sim/ppc/lf.h", "sim/ppc/ld-decode.c", | |
268 | "sim/ppc/sim-endian.c", "sim/ppc/gen-itable.c", | |
269 | "sim/ppc/idecode_expression.h", "sim/ppc/table.h", "sim/ppc/dgen.c", | |
270 | "sim/ppc/events.c", "sim/ppc/gen-idecode.h", "sim/ppc/emul_netbsd.c", | |
271 | "sim/ppc/igen.c", "sim/ppc/vm_n.h", "sim/ppc/vm.h", | |
272 | "sim/ppc/hw_iobus.c", "sim/ppc/inline.h", | |
273 | "sim/testsuite/sim/bfin/s21.s", "sim/testsuite/sim/mips/mips32-dsp2.s", | |
274 | ) | |
e9bdf92c JB |
275 | |
276 | if __name__ == "__main__": | |
8ba098ad | 277 | main() |
e9bdf92c | 278 |