annotate contrib/update-copyright.py @ 131:84e7813d76e9

gcc-8.2
author mir3636
date Thu, 25 Oct 2018 07:37:49 +0900
parents 04ced10e8804
children 1830386684a0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 #!/usr/bin/python
kono
parents:
diff changeset
2 #
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
3 # Copyright (C) 2013-2018 Free Software Foundation, Inc.
111
kono
parents:
diff changeset
4 #
kono
parents:
diff changeset
5 # This script is free software; you can redistribute it and/or modify
kono
parents:
diff changeset
6 # it under the terms of the GNU General Public License as published by
kono
parents:
diff changeset
7 # the Free Software Foundation; either version 3, or (at your option)
kono
parents:
diff changeset
8 # any later version.
kono
parents:
diff changeset
9
kono
parents:
diff changeset
10 # This script adjusts the copyright notices at the top of source files
kono
parents:
diff changeset
11 # so that they have the form:
kono
parents:
diff changeset
12 #
kono
parents:
diff changeset
13 # Copyright XXXX-YYYY Free Software Foundation, Inc.
kono
parents:
diff changeset
14 #
kono
parents:
diff changeset
15 # It doesn't change code that is known to be maintained elsewhere or
kono
parents:
diff changeset
16 # that carries a non-FSF copyright.
kono
parents:
diff changeset
17 #
kono
parents:
diff changeset
18 # The script also doesn't change testsuite files, except those in
kono
parents:
diff changeset
19 # libstdc++-v3. This is because libstdc++-v3 has a conformance testsuite,
kono
parents:
diff changeset
20 # while most tests in other directories are just things that failed at some
kono
parents:
diff changeset
21 # point in the past.
kono
parents:
diff changeset
22 #
kono
parents:
diff changeset
23 # Pass --this-year to the script if you want it to add the current year
kono
parents:
diff changeset
24 # to all applicable notices. Pass --quilt if you are using quilt and
kono
parents:
diff changeset
25 # want files to be added to the quilt before being changed.
kono
parents:
diff changeset
26 #
kono
parents:
diff changeset
27 # By default the script will update all directories for which the
kono
parents:
diff changeset
28 # output has been vetted. You can instead pass the names of individual
kono
parents:
diff changeset
29 # directories, including those that haven't been approved. So:
kono
parents:
diff changeset
30 #
kono
parents:
diff changeset
31 # update-copyright.py --this-year
kono
parents:
diff changeset
32 #
kono
parents:
diff changeset
33 # is the command that would be used at the beginning of a year to update
kono
parents:
diff changeset
34 # all copyright notices (and possibly at other times to check whether
kono
parents:
diff changeset
35 # new files have been added with old years). On the other hand:
kono
parents:
diff changeset
36 #
kono
parents:
diff changeset
37 # update-copyright.py --this-year libitm
kono
parents:
diff changeset
38 #
kono
parents:
diff changeset
39 # would run the script on just libitm/.
kono
parents:
diff changeset
40 #
kono
parents:
diff changeset
41 # Note that things like --version output strings must be updated before
kono
parents:
diff changeset
42 # this script is run. There's already a separate procedure for that.
kono
parents:
diff changeset
43
kono
parents:
diff changeset
44 import os
kono
parents:
diff changeset
45 import re
kono
parents:
diff changeset
46 import sys
kono
parents:
diff changeset
47 import time
kono
parents:
diff changeset
48 import subprocess
kono
parents:
diff changeset
49
kono
parents:
diff changeset
50 class Errors:
kono
parents:
diff changeset
51 def __init__ (self):
kono
parents:
diff changeset
52 self.num_errors = 0
kono
parents:
diff changeset
53
kono
parents:
diff changeset
54 def report (self, filename, string):
kono
parents:
diff changeset
55 if filename:
kono
parents:
diff changeset
56 string = filename + ': ' + string
kono
parents:
diff changeset
57 sys.stderr.write (string + '\n')
kono
parents:
diff changeset
58 self.num_errors += 1
kono
parents:
diff changeset
59
kono
parents:
diff changeset
60 def ok (self):
kono
parents:
diff changeset
61 return self.num_errors == 0
kono
parents:
diff changeset
62
kono
parents:
diff changeset
63 class GenericFilter:
kono
parents:
diff changeset
64 def __init__ (self):
kono
parents:
diff changeset
65 self.skip_files = set()
kono
parents:
diff changeset
66 self.skip_dirs = set()
kono
parents:
diff changeset
67 self.skip_extensions = set()
kono
parents:
diff changeset
68 self.fossilised_files = set()
kono
parents:
diff changeset
69 self.own_files = set()
kono
parents:
diff changeset
70
kono
parents:
diff changeset
71 self.skip_files |= set ([
kono
parents:
diff changeset
72 # Skip licence files.
kono
parents:
diff changeset
73 'COPYING',
kono
parents:
diff changeset
74 'COPYING.LIB',
kono
parents:
diff changeset
75 'COPYING3',
kono
parents:
diff changeset
76 'COPYING3.LIB',
kono
parents:
diff changeset
77 'LICENSE',
kono
parents:
diff changeset
78 'fdl.texi',
kono
parents:
diff changeset
79 'gpl_v3.texi',
kono
parents:
diff changeset
80 'fdl-1.3.xml',
kono
parents:
diff changeset
81 'gpl-3.0.xml',
kono
parents:
diff changeset
82
kono
parents:
diff changeset
83 # Skip auto- and libtool-related files
kono
parents:
diff changeset
84 'aclocal.m4',
kono
parents:
diff changeset
85 'compile',
kono
parents:
diff changeset
86 'config.guess',
kono
parents:
diff changeset
87 'config.sub',
kono
parents:
diff changeset
88 'depcomp',
kono
parents:
diff changeset
89 'install-sh',
kono
parents:
diff changeset
90 'libtool.m4',
kono
parents:
diff changeset
91 'ltmain.sh',
kono
parents:
diff changeset
92 'ltoptions.m4',
kono
parents:
diff changeset
93 'ltsugar.m4',
kono
parents:
diff changeset
94 'ltversion.m4',
kono
parents:
diff changeset
95 'lt~obsolete.m4',
kono
parents:
diff changeset
96 'missing',
kono
parents:
diff changeset
97 'mkdep',
kono
parents:
diff changeset
98 'mkinstalldirs',
kono
parents:
diff changeset
99 'move-if-change',
kono
parents:
diff changeset
100 'shlibpath.m4',
kono
parents:
diff changeset
101 'symlink-tree',
kono
parents:
diff changeset
102 'ylwrap',
kono
parents:
diff changeset
103
kono
parents:
diff changeset
104 # Skip FSF mission statement, etc.
kono
parents:
diff changeset
105 'gnu.texi',
kono
parents:
diff changeset
106 'funding.texi',
kono
parents:
diff changeset
107 'appendix_free.xml',
kono
parents:
diff changeset
108
kono
parents:
diff changeset
109 # Skip imported texinfo files.
kono
parents:
diff changeset
110 'texinfo.tex',
kono
parents:
diff changeset
111 ])
kono
parents:
diff changeset
112
kono
parents:
diff changeset
113
kono
parents:
diff changeset
114 def get_line_filter (self, dir, filename):
kono
parents:
diff changeset
115 if filename.startswith ('ChangeLog'):
kono
parents:
diff changeset
116 # Ignore references to copyright in changelog entries.
kono
parents:
diff changeset
117 return re.compile ('\t')
kono
parents:
diff changeset
118
kono
parents:
diff changeset
119 return None
kono
parents:
diff changeset
120
kono
parents:
diff changeset
121 def skip_file (self, dir, filename):
kono
parents:
diff changeset
122 if filename in self.skip_files:
kono
parents:
diff changeset
123 return True
kono
parents:
diff changeset
124
kono
parents:
diff changeset
125 (base, extension) = os.path.splitext (os.path.join (dir, filename))
kono
parents:
diff changeset
126 if extension in self.skip_extensions:
kono
parents:
diff changeset
127 return True
kono
parents:
diff changeset
128
kono
parents:
diff changeset
129 if extension == '.in':
kono
parents:
diff changeset
130 # Skip .in files produced by automake.
kono
parents:
diff changeset
131 if os.path.exists (base + '.am'):
kono
parents:
diff changeset
132 return True
kono
parents:
diff changeset
133
kono
parents:
diff changeset
134 # Skip files produced by autogen
kono
parents:
diff changeset
135 if (os.path.exists (base + '.def')
kono
parents:
diff changeset
136 and os.path.exists (base + '.tpl')):
kono
parents:
diff changeset
137 return True
kono
parents:
diff changeset
138
kono
parents:
diff changeset
139 # Skip configure files produced by autoconf
kono
parents:
diff changeset
140 if filename == 'configure':
kono
parents:
diff changeset
141 if os.path.exists (base + '.ac'):
kono
parents:
diff changeset
142 return True
kono
parents:
diff changeset
143 if os.path.exists (base + '.in'):
kono
parents:
diff changeset
144 return True
kono
parents:
diff changeset
145
kono
parents:
diff changeset
146 return False
kono
parents:
diff changeset
147
kono
parents:
diff changeset
148 def skip_dir (self, dir, subdir):
kono
parents:
diff changeset
149 return subdir in self.skip_dirs
kono
parents:
diff changeset
150
kono
parents:
diff changeset
151 def is_fossilised_file (self, dir, filename):
kono
parents:
diff changeset
152 if filename in self.fossilised_files:
kono
parents:
diff changeset
153 return True
kono
parents:
diff changeset
154 # Only touch current current ChangeLogs.
kono
parents:
diff changeset
155 if filename != 'ChangeLog' and filename.find ('ChangeLog') >= 0:
kono
parents:
diff changeset
156 return True
kono
parents:
diff changeset
157 return False
kono
parents:
diff changeset
158
kono
parents:
diff changeset
159 def by_package_author (self, dir, filename):
kono
parents:
diff changeset
160 return filename in self.own_files
kono
parents:
diff changeset
161
kono
parents:
diff changeset
162 class Copyright:
kono
parents:
diff changeset
163 def __init__ (self, errors):
kono
parents:
diff changeset
164 self.errors = errors
kono
parents:
diff changeset
165
kono
parents:
diff changeset
166 # Characters in a range of years. Include '.' for typos.
kono
parents:
diff changeset
167 ranges = '[0-9](?:[-0-9.,\s]|\s+and\s+)*[0-9]'
kono
parents:
diff changeset
168
kono
parents:
diff changeset
169 # Non-whitespace characters in a copyright holder's name.
kono
parents:
diff changeset
170 name = '[\w.,-]'
kono
parents:
diff changeset
171
kono
parents:
diff changeset
172 # Matches one year.
kono
parents:
diff changeset
173 self.year_re = re.compile ('[0-9]+')
kono
parents:
diff changeset
174
kono
parents:
diff changeset
175 # Matches part of a year or copyright holder.
kono
parents:
diff changeset
176 self.continuation_re = re.compile (ranges + '|' + name)
kono
parents:
diff changeset
177
kono
parents:
diff changeset
178 # Matches a full copyright notice:
kono
parents:
diff changeset
179 self.copyright_re = re.compile (
kono
parents:
diff changeset
180 # 1: 'Copyright (C)', etc.
kono
parents:
diff changeset
181 '([Cc]opyright'
kono
parents:
diff changeset
182 '|[Cc]opyright\s+\([Cc]\)'
kono
parents:
diff changeset
183 '|[Cc]opyright\s+%s'
kono
parents:
diff changeset
184 '|[Cc]opyright\s+©'
kono
parents:
diff changeset
185 '|[Cc]opyright\s+@copyright{}'
kono
parents:
diff changeset
186 '|copyright = u\''
kono
parents:
diff changeset
187 '|@set\s+copyright[\w-]+)'
kono
parents:
diff changeset
188
kono
parents:
diff changeset
189 # 2: the years. Include the whitespace in the year, so that
kono
parents:
diff changeset
190 # we can remove any excess.
kono
parents:
diff changeset
191 '(\s*(?:' + ranges + ',?'
kono
parents:
diff changeset
192 '|@value\{[^{}]*\})\s*)'
kono
parents:
diff changeset
193
kono
parents:
diff changeset
194 # 3: 'by ', if used
kono
parents:
diff changeset
195 '(by\s+)?'
kono
parents:
diff changeset
196
kono
parents:
diff changeset
197 # 4: the copyright holder. Don't allow multiple consecutive
kono
parents:
diff changeset
198 # spaces, so that right-margin gloss doesn't get caught
kono
parents:
diff changeset
199 # (e.g. gnat_ugn.texi).
kono
parents:
diff changeset
200 '(' + name + '(?:\s?' + name + ')*)?')
kono
parents:
diff changeset
201
kono
parents:
diff changeset
202 # A regexp for notices that might have slipped by. Just matching
kono
parents:
diff changeset
203 # 'copyright' is too noisy, and 'copyright.*[0-9]' falls foul of
kono
parents:
diff changeset
204 # HTML header markers, so check for 'copyright' and two digits.
kono
parents:
diff changeset
205 self.other_copyright_re = re.compile ('copyright.*[0-9][0-9]',
kono
parents:
diff changeset
206 re.IGNORECASE)
kono
parents:
diff changeset
207 self.comment_re = re.compile('#+|[*]+|;+|%+|//+|@c |dnl ')
kono
parents:
diff changeset
208 self.holders = { '@copying': '@copying' }
kono
parents:
diff changeset
209 self.holder_prefixes = set()
kono
parents:
diff changeset
210
kono
parents:
diff changeset
211 # True to 'quilt add' files before changing them.
kono
parents:
diff changeset
212 self.use_quilt = False
kono
parents:
diff changeset
213
kono
parents:
diff changeset
214 # If set, force all notices to include this year.
kono
parents:
diff changeset
215 self.max_year = None
kono
parents:
diff changeset
216
kono
parents:
diff changeset
217 # Goes after the year(s). Could be ', '.
kono
parents:
diff changeset
218 self.separator = ' '
kono
parents:
diff changeset
219
kono
parents:
diff changeset
220 def add_package_author (self, holder, canon_form = None):
kono
parents:
diff changeset
221 if not canon_form:
kono
parents:
diff changeset
222 canon_form = holder
kono
parents:
diff changeset
223 self.holders[holder] = canon_form
kono
parents:
diff changeset
224 index = holder.find (' ')
kono
parents:
diff changeset
225 while index >= 0:
kono
parents:
diff changeset
226 self.holder_prefixes.add (holder[:index])
kono
parents:
diff changeset
227 index = holder.find (' ', index + 1)
kono
parents:
diff changeset
228
kono
parents:
diff changeset
229 def add_external_author (self, holder):
kono
parents:
diff changeset
230 self.holders[holder] = None
kono
parents:
diff changeset
231
kono
parents:
diff changeset
232 class BadYear():
kono
parents:
diff changeset
233 def __init__ (self, year):
kono
parents:
diff changeset
234 self.year = year
kono
parents:
diff changeset
235
kono
parents:
diff changeset
236 def __str__ (self):
kono
parents:
diff changeset
237 return 'unrecognised year: ' + self.year
kono
parents:
diff changeset
238
kono
parents:
diff changeset
239 def parse_year (self, string):
kono
parents:
diff changeset
240 year = int (string)
kono
parents:
diff changeset
241 if len (string) == 2:
kono
parents:
diff changeset
242 if year > 70:
kono
parents:
diff changeset
243 return year + 1900
kono
parents:
diff changeset
244 elif len (string) == 4:
kono
parents:
diff changeset
245 return year
kono
parents:
diff changeset
246 raise self.BadYear (string)
kono
parents:
diff changeset
247
kono
parents:
diff changeset
248 def year_range (self, years):
kono
parents:
diff changeset
249 year_list = [self.parse_year (year)
kono
parents:
diff changeset
250 for year in self.year_re.findall (years)]
kono
parents:
diff changeset
251 assert len (year_list) > 0
kono
parents:
diff changeset
252 return (min (year_list), max (year_list))
kono
parents:
diff changeset
253
kono
parents:
diff changeset
254 def set_use_quilt (self, use_quilt):
kono
parents:
diff changeset
255 self.use_quilt = use_quilt
kono
parents:
diff changeset
256
kono
parents:
diff changeset
257 def include_year (self, year):
kono
parents:
diff changeset
258 assert not self.max_year
kono
parents:
diff changeset
259 self.max_year = year
kono
parents:
diff changeset
260
kono
parents:
diff changeset
261 def canonicalise_years (self, dir, filename, filter, years):
kono
parents:
diff changeset
262 # Leave texinfo variables alone.
kono
parents:
diff changeset
263 if years.startswith ('@value'):
kono
parents:
diff changeset
264 return years
kono
parents:
diff changeset
265
kono
parents:
diff changeset
266 (min_year, max_year) = self.year_range (years)
kono
parents:
diff changeset
267
kono
parents:
diff changeset
268 # Update the upper bound, if enabled.
kono
parents:
diff changeset
269 if self.max_year and not filter.is_fossilised_file (dir, filename):
kono
parents:
diff changeset
270 max_year = max (max_year, self.max_year)
kono
parents:
diff changeset
271
kono
parents:
diff changeset
272 # Use a range.
kono
parents:
diff changeset
273 if min_year == max_year:
kono
parents:
diff changeset
274 return '%d' % min_year
kono
parents:
diff changeset
275 else:
kono
parents:
diff changeset
276 return '%d-%d' % (min_year, max_year)
kono
parents:
diff changeset
277
kono
parents:
diff changeset
278 def strip_continuation (self, line):
kono
parents:
diff changeset
279 line = line.lstrip()
kono
parents:
diff changeset
280 match = self.comment_re.match (line)
kono
parents:
diff changeset
281 if match:
kono
parents:
diff changeset
282 line = line[match.end():].lstrip()
kono
parents:
diff changeset
283 return line
kono
parents:
diff changeset
284
kono
parents:
diff changeset
285 def is_complete (self, match):
kono
parents:
diff changeset
286 holder = match.group (4)
kono
parents:
diff changeset
287 return (holder
kono
parents:
diff changeset
288 and (holder not in self.holder_prefixes
kono
parents:
diff changeset
289 or holder in self.holders))
kono
parents:
diff changeset
290
kono
parents:
diff changeset
291 def update_copyright (self, dir, filename, filter, file, line, match):
kono
parents:
diff changeset
292 orig_line = line
kono
parents:
diff changeset
293 next_line = None
kono
parents:
diff changeset
294 pathname = os.path.join (dir, filename)
kono
parents:
diff changeset
295
kono
parents:
diff changeset
296 intro = match.group (1)
kono
parents:
diff changeset
297 if intro.startswith ('@set'):
kono
parents:
diff changeset
298 # Texinfo year variables should always be on one line
kono
parents:
diff changeset
299 after_years = line[match.end (2):].strip()
kono
parents:
diff changeset
300 if after_years != '':
kono
parents:
diff changeset
301 self.errors.report (pathname,
kono
parents:
diff changeset
302 'trailing characters in @set: '
kono
parents:
diff changeset
303 + after_years)
kono
parents:
diff changeset
304 return (False, orig_line, next_line)
kono
parents:
diff changeset
305 else:
kono
parents:
diff changeset
306 # If it looks like the copyright is incomplete, add the next line.
kono
parents:
diff changeset
307 while not self.is_complete (match):
kono
parents:
diff changeset
308 try:
kono
parents:
diff changeset
309 next_line = file.next()
kono
parents:
diff changeset
310 except StopIteration:
kono
parents:
diff changeset
311 break
kono
parents:
diff changeset
312
kono
parents:
diff changeset
313 # If the next line doesn't look like a proper continuation,
kono
parents:
diff changeset
314 # assume that what we've got is complete.
kono
parents:
diff changeset
315 continuation = self.strip_continuation (next_line)
kono
parents:
diff changeset
316 if not self.continuation_re.match (continuation):
kono
parents:
diff changeset
317 break
kono
parents:
diff changeset
318
kono
parents:
diff changeset
319 # Merge the lines for matching purposes.
kono
parents:
diff changeset
320 orig_line += next_line
kono
parents:
diff changeset
321 line = line.rstrip() + ' ' + continuation
kono
parents:
diff changeset
322 next_line = None
kono
parents:
diff changeset
323
kono
parents:
diff changeset
324 # Rematch with the longer line, at the original position.
kono
parents:
diff changeset
325 match = self.copyright_re.match (line, match.start())
kono
parents:
diff changeset
326 assert match
kono
parents:
diff changeset
327
kono
parents:
diff changeset
328 holder = match.group (4)
kono
parents:
diff changeset
329
kono
parents:
diff changeset
330 # Use the filter to test cases where markup is getting in the way.
kono
parents:
diff changeset
331 if filter.by_package_author (dir, filename):
kono
parents:
diff changeset
332 assert holder not in self.holders
kono
parents:
diff changeset
333
kono
parents:
diff changeset
334 elif not holder:
kono
parents:
diff changeset
335 self.errors.report (pathname, 'missing copyright holder')
kono
parents:
diff changeset
336 return (False, orig_line, next_line)
kono
parents:
diff changeset
337
kono
parents:
diff changeset
338 elif holder not in self.holders:
kono
parents:
diff changeset
339 self.errors.report (pathname,
kono
parents:
diff changeset
340 'unrecognised copyright holder: ' + holder)
kono
parents:
diff changeset
341 return (False, orig_line, next_line)
kono
parents:
diff changeset
342
kono
parents:
diff changeset
343 else:
kono
parents:
diff changeset
344 # See whether the copyright is associated with the package
kono
parents:
diff changeset
345 # author.
kono
parents:
diff changeset
346 canon_form = self.holders[holder]
kono
parents:
diff changeset
347 if not canon_form:
kono
parents:
diff changeset
348 return (False, orig_line, next_line)
kono
parents:
diff changeset
349
kono
parents:
diff changeset
350 # Make sure the author is given in a consistent way.
kono
parents:
diff changeset
351 line = (line[:match.start (4)]
kono
parents:
diff changeset
352 + canon_form
kono
parents:
diff changeset
353 + line[match.end (4):])
kono
parents:
diff changeset
354
kono
parents:
diff changeset
355 # Remove any 'by'
kono
parents:
diff changeset
356 line = line[:match.start (3)] + line[match.end (3):]
kono
parents:
diff changeset
357
kono
parents:
diff changeset
358 # Update the copyright years.
kono
parents:
diff changeset
359 years = match.group (2).strip()
kono
parents:
diff changeset
360 try:
kono
parents:
diff changeset
361 canon_form = self.canonicalise_years (dir, filename, filter, years)
kono
parents:
diff changeset
362 except self.BadYear as e:
kono
parents:
diff changeset
363 self.errors.report (pathname, str (e))
kono
parents:
diff changeset
364 return (False, orig_line, next_line)
kono
parents:
diff changeset
365
kono
parents:
diff changeset
366 line = (line[:match.start (2)]
kono
parents:
diff changeset
367 + ('' if intro.startswith ('copyright = ') else ' ')
kono
parents:
diff changeset
368 + canon_form + self.separator
kono
parents:
diff changeset
369 + line[match.end (2):])
kono
parents:
diff changeset
370
kono
parents:
diff changeset
371 # Use the standard (C) form.
kono
parents:
diff changeset
372 if intro.endswith ('right'):
kono
parents:
diff changeset
373 intro += ' (C)'
kono
parents:
diff changeset
374 elif intro.endswith ('(c)'):
kono
parents:
diff changeset
375 intro = intro[:-3] + '(C)'
kono
parents:
diff changeset
376 line = line[:match.start (1)] + intro + line[match.end (1):]
kono
parents:
diff changeset
377
kono
parents:
diff changeset
378 # Strip trailing whitespace
kono
parents:
diff changeset
379 line = line.rstrip() + '\n'
kono
parents:
diff changeset
380
kono
parents:
diff changeset
381 return (line != orig_line, line, next_line)
kono
parents:
diff changeset
382
kono
parents:
diff changeset
383 def process_file (self, dir, filename, filter):
kono
parents:
diff changeset
384 pathname = os.path.join (dir, filename)
kono
parents:
diff changeset
385 if filename.endswith ('.tmp'):
kono
parents:
diff changeset
386 # Looks like something we tried to create before.
kono
parents:
diff changeset
387 try:
kono
parents:
diff changeset
388 os.remove (pathname)
kono
parents:
diff changeset
389 except OSError:
kono
parents:
diff changeset
390 pass
kono
parents:
diff changeset
391 return
kono
parents:
diff changeset
392
kono
parents:
diff changeset
393 lines = []
kono
parents:
diff changeset
394 changed = False
kono
parents:
diff changeset
395 line_filter = filter.get_line_filter (dir, filename)
kono
parents:
diff changeset
396 mode = None
kono
parents:
diff changeset
397 with open (pathname, 'r') as file:
kono
parents:
diff changeset
398 prev = None
kono
parents:
diff changeset
399 mode = os.fstat (file.fileno()).st_mode
kono
parents:
diff changeset
400 for line in file:
kono
parents:
diff changeset
401 while line:
kono
parents:
diff changeset
402 next_line = None
kono
parents:
diff changeset
403 # Leave filtered-out lines alone.
kono
parents:
diff changeset
404 if not (line_filter and line_filter.match (line)):
kono
parents:
diff changeset
405 match = self.copyright_re.search (line)
kono
parents:
diff changeset
406 if match:
kono
parents:
diff changeset
407 res = self.update_copyright (dir, filename, filter,
kono
parents:
diff changeset
408 file, line, match)
kono
parents:
diff changeset
409 (this_changed, line, next_line) = res
kono
parents:
diff changeset
410 changed = changed or this_changed
kono
parents:
diff changeset
411
kono
parents:
diff changeset
412 # Check for copyright lines that might have slipped by.
kono
parents:
diff changeset
413 elif self.other_copyright_re.search (line):
kono
parents:
diff changeset
414 self.errors.report (pathname,
kono
parents:
diff changeset
415 'unrecognised copyright: %s'
kono
parents:
diff changeset
416 % line.strip())
kono
parents:
diff changeset
417 lines.append (line)
kono
parents:
diff changeset
418 line = next_line
kono
parents:
diff changeset
419
kono
parents:
diff changeset
420 # If something changed, write the new file out.
kono
parents:
diff changeset
421 if changed and self.errors.ok():
kono
parents:
diff changeset
422 tmp_pathname = pathname + '.tmp'
kono
parents:
diff changeset
423 with open (tmp_pathname, 'w') as file:
kono
parents:
diff changeset
424 for line in lines:
kono
parents:
diff changeset
425 file.write (line)
kono
parents:
diff changeset
426 os.fchmod (file.fileno(), mode)
kono
parents:
diff changeset
427 if self.use_quilt:
kono
parents:
diff changeset
428 subprocess.call (['quilt', 'add', pathname])
kono
parents:
diff changeset
429 os.rename (tmp_pathname, pathname)
kono
parents:
diff changeset
430
kono
parents:
diff changeset
431 def process_tree (self, tree, filter):
kono
parents:
diff changeset
432 for (dir, subdirs, filenames) in os.walk (tree):
kono
parents:
diff changeset
433 # Don't recurse through directories that should be skipped.
kono
parents:
diff changeset
434 for i in xrange (len (subdirs) - 1, -1, -1):
kono
parents:
diff changeset
435 if filter.skip_dir (dir, subdirs[i]):
kono
parents:
diff changeset
436 del subdirs[i]
kono
parents:
diff changeset
437
kono
parents:
diff changeset
438 # Handle the files in this directory.
kono
parents:
diff changeset
439 for filename in filenames:
kono
parents:
diff changeset
440 if filter.skip_file (dir, filename):
kono
parents:
diff changeset
441 sys.stdout.write ('Skipping %s\n'
kono
parents:
diff changeset
442 % os.path.join (dir, filename))
kono
parents:
diff changeset
443 else:
kono
parents:
diff changeset
444 self.process_file (dir, filename, filter)
kono
parents:
diff changeset
445
kono
parents:
diff changeset
446 class CmdLine:
kono
parents:
diff changeset
447 def __init__ (self, copyright = Copyright):
kono
parents:
diff changeset
448 self.errors = Errors()
kono
parents:
diff changeset
449 self.copyright = copyright (self.errors)
kono
parents:
diff changeset
450 self.dirs = []
kono
parents:
diff changeset
451 self.default_dirs = []
kono
parents:
diff changeset
452 self.chosen_dirs = []
kono
parents:
diff changeset
453 self.option_handlers = dict()
kono
parents:
diff changeset
454 self.option_help = []
kono
parents:
diff changeset
455
kono
parents:
diff changeset
456 self.add_option ('--help', 'Print this help', self.o_help)
kono
parents:
diff changeset
457 self.add_option ('--quilt', '"quilt add" files before changing them',
kono
parents:
diff changeset
458 self.o_quilt)
kono
parents:
diff changeset
459 self.add_option ('--this-year', 'Add the current year to every notice',
kono
parents:
diff changeset
460 self.o_this_year)
kono
parents:
diff changeset
461
kono
parents:
diff changeset
462 def add_option (self, name, help, handler):
kono
parents:
diff changeset
463 self.option_help.append ((name, help))
kono
parents:
diff changeset
464 self.option_handlers[name] = handler
kono
parents:
diff changeset
465
kono
parents:
diff changeset
466 def add_dir (self, dir, filter = GenericFilter()):
kono
parents:
diff changeset
467 self.dirs.append ((dir, filter))
kono
parents:
diff changeset
468
kono
parents:
diff changeset
469 def o_help (self, option = None):
kono
parents:
diff changeset
470 sys.stdout.write ('Usage: %s [options] dir1 dir2...\n\n'
kono
parents:
diff changeset
471 'Options:\n' % sys.argv[0])
kono
parents:
diff changeset
472 format = '%-15s %s\n'
kono
parents:
diff changeset
473 for (what, help) in self.option_help:
kono
parents:
diff changeset
474 sys.stdout.write (format % (what, help))
kono
parents:
diff changeset
475 sys.stdout.write ('\nDirectories:\n')
kono
parents:
diff changeset
476
kono
parents:
diff changeset
477 format = '%-25s'
kono
parents:
diff changeset
478 i = 0
kono
parents:
diff changeset
479 for (dir, filter) in self.dirs:
kono
parents:
diff changeset
480 i += 1
kono
parents:
diff changeset
481 if i % 3 == 0 or i == len (self.dirs):
kono
parents:
diff changeset
482 sys.stdout.write (dir + '\n')
kono
parents:
diff changeset
483 else:
kono
parents:
diff changeset
484 sys.stdout.write (format % dir)
kono
parents:
diff changeset
485 sys.exit (0)
kono
parents:
diff changeset
486
kono
parents:
diff changeset
487 def o_quilt (self, option):
kono
parents:
diff changeset
488 self.copyright.set_use_quilt (True)
kono
parents:
diff changeset
489
kono
parents:
diff changeset
490 def o_this_year (self, option):
kono
parents:
diff changeset
491 self.copyright.include_year (time.localtime().tm_year)
kono
parents:
diff changeset
492
kono
parents:
diff changeset
493 def main (self):
kono
parents:
diff changeset
494 for arg in sys.argv[1:]:
kono
parents:
diff changeset
495 if arg[:1] != '-':
kono
parents:
diff changeset
496 self.chosen_dirs.append (arg)
kono
parents:
diff changeset
497 elif arg in self.option_handlers:
kono
parents:
diff changeset
498 self.option_handlers[arg] (arg)
kono
parents:
diff changeset
499 else:
kono
parents:
diff changeset
500 self.errors.report (None, 'unrecognised option: ' + arg)
kono
parents:
diff changeset
501 if self.errors.ok():
kono
parents:
diff changeset
502 if len (self.chosen_dirs) == 0:
kono
parents:
diff changeset
503 self.chosen_dirs = self.default_dirs
kono
parents:
diff changeset
504 if len (self.chosen_dirs) == 0:
kono
parents:
diff changeset
505 self.o_help()
kono
parents:
diff changeset
506 else:
kono
parents:
diff changeset
507 for chosen_dir in self.chosen_dirs:
kono
parents:
diff changeset
508 canon_dir = os.path.join (chosen_dir, '')
kono
parents:
diff changeset
509 count = 0
kono
parents:
diff changeset
510 for (dir, filter) in self.dirs:
kono
parents:
diff changeset
511 if (dir + os.sep).startswith (canon_dir):
kono
parents:
diff changeset
512 count += 1
kono
parents:
diff changeset
513 self.copyright.process_tree (dir, filter)
kono
parents:
diff changeset
514 if count == 0:
kono
parents:
diff changeset
515 self.errors.report (None, 'unrecognised directory: '
kono
parents:
diff changeset
516 + chosen_dir)
kono
parents:
diff changeset
517 sys.exit (0 if self.errors.ok() else 1)
kono
parents:
diff changeset
518
kono
parents:
diff changeset
519 #----------------------------------------------------------------------------
kono
parents:
diff changeset
520
kono
parents:
diff changeset
521 class TopLevelFilter (GenericFilter):
kono
parents:
diff changeset
522 def skip_dir (self, dir, subdir):
kono
parents:
diff changeset
523 return True
kono
parents:
diff changeset
524
kono
parents:
diff changeset
525 class ConfigFilter (GenericFilter):
kono
parents:
diff changeset
526 def __init__ (self):
kono
parents:
diff changeset
527 GenericFilter.__init__ (self)
kono
parents:
diff changeset
528
kono
parents:
diff changeset
529 def skip_file (self, dir, filename):
kono
parents:
diff changeset
530 if filename.endswith ('.m4'):
kono
parents:
diff changeset
531 pathname = os.path.join (dir, filename)
kono
parents:
diff changeset
532 with open (pathname) as file:
kono
parents:
diff changeset
533 # Skip files imported from gettext.
kono
parents:
diff changeset
534 if file.readline().find ('gettext-') >= 0:
kono
parents:
diff changeset
535 return True
kono
parents:
diff changeset
536 return GenericFilter.skip_file (self, dir, filename)
kono
parents:
diff changeset
537
kono
parents:
diff changeset
538 class GCCFilter (GenericFilter):
kono
parents:
diff changeset
539 def __init__ (self):
kono
parents:
diff changeset
540 GenericFilter.__init__ (self)
kono
parents:
diff changeset
541
kono
parents:
diff changeset
542 self.skip_files |= set ([
kono
parents:
diff changeset
543 # Not part of GCC
kono
parents:
diff changeset
544 'math-68881.h',
kono
parents:
diff changeset
545 ])
kono
parents:
diff changeset
546
kono
parents:
diff changeset
547 self.skip_dirs |= set ([
kono
parents:
diff changeset
548 # Better not create a merge nightmare for the GNAT folks.
kono
parents:
diff changeset
549 'ada',
kono
parents:
diff changeset
550
kono
parents:
diff changeset
551 # Handled separately.
kono
parents:
diff changeset
552 'testsuite',
kono
parents:
diff changeset
553 ])
kono
parents:
diff changeset
554
kono
parents:
diff changeset
555 self.skip_extensions |= set ([
kono
parents:
diff changeset
556 # Maintained by the translation project.
kono
parents:
diff changeset
557 '.po',
kono
parents:
diff changeset
558
kono
parents:
diff changeset
559 # Automatically-generated.
kono
parents:
diff changeset
560 '.pot',
kono
parents:
diff changeset
561 ])
kono
parents:
diff changeset
562
kono
parents:
diff changeset
563 self.fossilised_files |= set ([
kono
parents:
diff changeset
564 # Old news won't be updated.
kono
parents:
diff changeset
565 'ONEWS',
kono
parents:
diff changeset
566 ])
kono
parents:
diff changeset
567
kono
parents:
diff changeset
568 class TestsuiteFilter (GenericFilter):
kono
parents:
diff changeset
569 def __init__ (self):
kono
parents:
diff changeset
570 GenericFilter.__init__ (self)
kono
parents:
diff changeset
571
kono
parents:
diff changeset
572 self.skip_extensions |= set ([
kono
parents:
diff changeset
573 # Don't change the tests, which could be woend by anyone.
kono
parents:
diff changeset
574 '.c',
kono
parents:
diff changeset
575 '.C',
kono
parents:
diff changeset
576 '.cc',
kono
parents:
diff changeset
577 '.h',
kono
parents:
diff changeset
578 '.hs',
kono
parents:
diff changeset
579 '.f',
kono
parents:
diff changeset
580 '.f90',
kono
parents:
diff changeset
581 '.go',
kono
parents:
diff changeset
582 '.inc',
kono
parents:
diff changeset
583 '.java',
kono
parents:
diff changeset
584 ])
kono
parents:
diff changeset
585
kono
parents:
diff changeset
586 def skip_file (self, dir, filename):
kono
parents:
diff changeset
587 # g++.niklas/README contains historical copyright information
kono
parents:
diff changeset
588 # and isn't updated.
kono
parents:
diff changeset
589 if filename == 'README' and os.path.basename (dir) == 'g++.niklas':
kono
parents:
diff changeset
590 return True
kono
parents:
diff changeset
591 # Similarly params/README.
kono
parents:
diff changeset
592 if filename == 'README' and os.path.basename (dir) == 'params':
kono
parents:
diff changeset
593 return True
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
594 if filename == 'pdt_5.f03' and os.path.basename (dir) == 'gfortran.dg':
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
595 return True
111
kono
parents:
diff changeset
596 return GenericFilter.skip_file (self, dir, filename)
kono
parents:
diff changeset
597
kono
parents:
diff changeset
598 class LibCppFilter (GenericFilter):
kono
parents:
diff changeset
599 def __init__ (self):
kono
parents:
diff changeset
600 GenericFilter.__init__ (self)
kono
parents:
diff changeset
601
kono
parents:
diff changeset
602 self.skip_extensions |= set ([
kono
parents:
diff changeset
603 # Maintained by the translation project.
kono
parents:
diff changeset
604 '.po',
kono
parents:
diff changeset
605
kono
parents:
diff changeset
606 # Automatically-generated.
kono
parents:
diff changeset
607 '.pot',
kono
parents:
diff changeset
608 ])
kono
parents:
diff changeset
609
kono
parents:
diff changeset
610 class LibGCCFilter (GenericFilter):
kono
parents:
diff changeset
611 def __init__ (self):
kono
parents:
diff changeset
612 GenericFilter.__init__ (self)
kono
parents:
diff changeset
613
kono
parents:
diff changeset
614 self.skip_dirs |= set ([
kono
parents:
diff changeset
615 # Imported from GLIBC.
kono
parents:
diff changeset
616 'soft-fp',
kono
parents:
diff changeset
617 ])
kono
parents:
diff changeset
618
kono
parents:
diff changeset
619 class LibStdCxxFilter (GenericFilter):
kono
parents:
diff changeset
620 def __init__ (self):
kono
parents:
diff changeset
621 GenericFilter.__init__ (self)
kono
parents:
diff changeset
622
kono
parents:
diff changeset
623 self.skip_files |= set ([
kono
parents:
diff changeset
624 # Contains no copyright of its own, but quotes the GPL.
kono
parents:
diff changeset
625 'intro.xml',
kono
parents:
diff changeset
626 ])
kono
parents:
diff changeset
627
kono
parents:
diff changeset
628 self.skip_dirs |= set ([
kono
parents:
diff changeset
629 # Contains automatically-generated sources.
kono
parents:
diff changeset
630 'html',
kono
parents:
diff changeset
631
kono
parents:
diff changeset
632 # The testsuite data files shouldn't be changed.
kono
parents:
diff changeset
633 'data',
kono
parents:
diff changeset
634
kono
parents:
diff changeset
635 # Contains imported images
kono
parents:
diff changeset
636 'images',
kono
parents:
diff changeset
637 ])
kono
parents:
diff changeset
638
kono
parents:
diff changeset
639 self.own_files |= set ([
kono
parents:
diff changeset
640 # Contains markup around the copyright owner.
kono
parents:
diff changeset
641 'spine.xml',
kono
parents:
diff changeset
642 ])
kono
parents:
diff changeset
643
kono
parents:
diff changeset
644 def get_line_filter (self, dir, filename):
kono
parents:
diff changeset
645 if filename == 'boost_concept_check.h':
kono
parents:
diff changeset
646 return re.compile ('// \(C\) Copyright Jeremy Siek')
kono
parents:
diff changeset
647 return GenericFilter.get_line_filter (self, dir, filename)
kono
parents:
diff changeset
648
kono
parents:
diff changeset
649 class GCCCopyright (Copyright):
kono
parents:
diff changeset
650 def __init__ (self, errors):
kono
parents:
diff changeset
651 Copyright.__init__ (self, errors)
kono
parents:
diff changeset
652
kono
parents:
diff changeset
653 canon_fsf = 'Free Software Foundation, Inc.'
kono
parents:
diff changeset
654 self.add_package_author ('Free Software Foundation', canon_fsf)
kono
parents:
diff changeset
655 self.add_package_author ('Free Software Foundation.', canon_fsf)
kono
parents:
diff changeset
656 self.add_package_author ('Free Software Foundation Inc.', canon_fsf)
kono
parents:
diff changeset
657 self.add_package_author ('Free Software Foundation, Inc', canon_fsf)
kono
parents:
diff changeset
658 self.add_package_author ('Free Software Foundation, Inc.', canon_fsf)
kono
parents:
diff changeset
659 self.add_package_author ('The Free Software Foundation', canon_fsf)
kono
parents:
diff changeset
660 self.add_package_author ('The Free Software Foundation, Inc.', canon_fsf)
kono
parents:
diff changeset
661 self.add_package_author ('Software Foundation, Inc.', canon_fsf)
kono
parents:
diff changeset
662
kono
parents:
diff changeset
663 self.add_external_author ('ARM')
kono
parents:
diff changeset
664 self.add_external_author ('AdaCore')
kono
parents:
diff changeset
665 self.add_external_author ('Ami Tavory and Vladimir Dreizin, IBM-HRL.')
kono
parents:
diff changeset
666 self.add_external_author ('Cavium Networks.')
kono
parents:
diff changeset
667 self.add_external_author ('Faraday Technology Corp.')
kono
parents:
diff changeset
668 self.add_external_author ('Florida State University')
kono
parents:
diff changeset
669 self.add_external_author ('Greg Colvin and Beman Dawes.')
kono
parents:
diff changeset
670 self.add_external_author ('Hewlett-Packard Company')
kono
parents:
diff changeset
671 self.add_external_author ('Intel Corporation')
kono
parents:
diff changeset
672 self.add_external_author ('Information Technology Industry Council.')
kono
parents:
diff changeset
673 self.add_external_author ('James Theiler, Brian Gough')
kono
parents:
diff changeset
674 self.add_external_author ('Makoto Matsumoto and Takuji Nishimura,')
kono
parents:
diff changeset
675 self.add_external_author ('National Research Council of Canada.')
kono
parents:
diff changeset
676 self.add_external_author ('NVIDIA Corporation')
kono
parents:
diff changeset
677 self.add_external_author ('Peter Dimov and Multi Media Ltd.')
kono
parents:
diff changeset
678 self.add_external_author ('Peter Dimov')
kono
parents:
diff changeset
679 self.add_external_author ('Pipeline Associates, Inc.')
kono
parents:
diff changeset
680 self.add_external_author ('Regents of the University of California.')
kono
parents:
diff changeset
681 self.add_external_author ('Silicon Graphics Computer Systems, Inc.')
kono
parents:
diff changeset
682 self.add_external_author ('Silicon Graphics')
kono
parents:
diff changeset
683 self.add_external_author ('Stephen L. Moshier')
kono
parents:
diff changeset
684 self.add_external_author ('Sun Microsystems, Inc. All rights reserved.')
kono
parents:
diff changeset
685 self.add_external_author ('The Go Authors. All rights reserved.')
kono
parents:
diff changeset
686 self.add_external_author ('The Go Authors. All rights reserved.')
kono
parents:
diff changeset
687 self.add_external_author ('The Go Authors.')
kono
parents:
diff changeset
688 self.add_external_author ('The Regents of the University of California.')
kono
parents:
diff changeset
689 self.add_external_author ('Unicode, Inc.')
kono
parents:
diff changeset
690 self.add_external_author ('University of Toronto.')
kono
parents:
diff changeset
691
kono
parents:
diff changeset
692 class GCCCmdLine (CmdLine):
kono
parents:
diff changeset
693 def __init__ (self):
kono
parents:
diff changeset
694 CmdLine.__init__ (self, GCCCopyright)
kono
parents:
diff changeset
695
kono
parents:
diff changeset
696 self.add_dir ('.', TopLevelFilter())
kono
parents:
diff changeset
697 # boehm-gc is imported from upstream.
kono
parents:
diff changeset
698 self.add_dir ('config', ConfigFilter())
kono
parents:
diff changeset
699 # contrib isn't really part of GCC.
kono
parents:
diff changeset
700 self.add_dir ('fixincludes')
kono
parents:
diff changeset
701 self.add_dir ('gcc', GCCFilter())
kono
parents:
diff changeset
702 self.add_dir (os.path.join ('gcc', 'testsuite'), TestsuiteFilter())
kono
parents:
diff changeset
703 self.add_dir ('gnattools')
kono
parents:
diff changeset
704 self.add_dir ('gotools')
kono
parents:
diff changeset
705 self.add_dir ('include')
kono
parents:
diff changeset
706 # intl is imported from upstream.
kono
parents:
diff changeset
707 self.add_dir ('libada')
kono
parents:
diff changeset
708 self.add_dir ('libatomic')
kono
parents:
diff changeset
709 self.add_dir ('libbacktrace')
kono
parents:
diff changeset
710 self.add_dir ('libcc1')
kono
parents:
diff changeset
711 self.add_dir ('libcpp', LibCppFilter())
kono
parents:
diff changeset
712 self.add_dir ('libdecnumber')
kono
parents:
diff changeset
713 # libffi is imported from upstream.
kono
parents:
diff changeset
714 self.add_dir ('libgcc', LibGCCFilter())
kono
parents:
diff changeset
715 self.add_dir ('libgfortran')
kono
parents:
diff changeset
716 # libgo is imported from upstream.
kono
parents:
diff changeset
717 self.add_dir ('libgomp')
kono
parents:
diff changeset
718 self.add_dir ('libhsail-rt')
kono
parents:
diff changeset
719 self.add_dir ('libiberty')
kono
parents:
diff changeset
720 self.add_dir ('libitm')
kono
parents:
diff changeset
721 self.add_dir ('libobjc')
kono
parents:
diff changeset
722 # liboffloadmic is imported from upstream.
kono
parents:
diff changeset
723 self.add_dir ('libquadmath')
kono
parents:
diff changeset
724 # libsanitizer is imported from upstream.
kono
parents:
diff changeset
725 self.add_dir ('libssp')
kono
parents:
diff changeset
726 self.add_dir ('libstdc++-v3', LibStdCxxFilter())
kono
parents:
diff changeset
727 self.add_dir ('libvtv')
kono
parents:
diff changeset
728 self.add_dir ('lto-plugin')
kono
parents:
diff changeset
729 # maintainer-scripts maintainer-scripts
kono
parents:
diff changeset
730 # zlib is imported from upstream.
kono
parents:
diff changeset
731
kono
parents:
diff changeset
732 self.default_dirs = [
kono
parents:
diff changeset
733 'gcc',
kono
parents:
diff changeset
734 'include',
kono
parents:
diff changeset
735 'libada',
kono
parents:
diff changeset
736 'libatomic',
kono
parents:
diff changeset
737 'libbacktrace',
kono
parents:
diff changeset
738 'libcc1',
kono
parents:
diff changeset
739 'libcpp',
kono
parents:
diff changeset
740 'libdecnumber',
kono
parents:
diff changeset
741 'libgcc',
kono
parents:
diff changeset
742 'libgfortran',
kono
parents:
diff changeset
743 'libgomp',
kono
parents:
diff changeset
744 'libhsail-rt',
kono
parents:
diff changeset
745 'libiberty',
kono
parents:
diff changeset
746 'libitm',
kono
parents:
diff changeset
747 'libobjc',
kono
parents:
diff changeset
748 'libssp',
kono
parents:
diff changeset
749 'libstdc++-v3',
kono
parents:
diff changeset
750 'libvtv',
kono
parents:
diff changeset
751 'lto-plugin',
kono
parents:
diff changeset
752 ]
kono
parents:
diff changeset
753
kono
parents:
diff changeset
754 GCCCmdLine().main()