5 """Automatically update copyright boilerplate.
7 This script is adapted from one written for `Bugs Everywhere`_.
9 .. _Bugs Everywhere: http://bugseverywhere.org/
22 'project': 'Cookbook',
26 # Break "copyright" into "copy" and "right" to avoid matching the
29 This file is part of %(project)s.
31 %(project)s is free software: you can redistribute it and/or modify it
32 under the terms of the GNU General Public License as published by the
33 Free Software Foundation, either version 3 of the License, or (at your
34 option) any later version.
36 %(project)s is distributed in the hope that it will be useful,
37 but WITHOUT ANY WARRANTY; without even the implied warranty of
38 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
39 GNU General Public License for more details.
41 You should have received a copy of the GNU General Public License
42 along with %(project)s. If not, see <http://www.gnu.org/licenses/>.
45 COPY_RIGHT_TAG='-xyz-COPY' + '-RIGHT-zyx-' # unlikely to occur in the wild :p
47 # Convert author names to canonical forms.
48 # ALIASES[<canonical name>] = <list of aliases>
51 # 'John Doe <jdoe@a.com>':
52 # ['John Doe', 'jdoe', 'J. Doe <j@doe.net>'],
54 # Git-based projects are encouraged to use .mailmap instead of
55 # ALIASES. See git-shortlog(1) for details.
58 # List of paths that should not be scanned for copyright updates.
59 # IGNORED_PATHS = ['./.git/']
60 IGNORED_PATHS = ['./.git/', './recipe/', './cookbook/static/',
61 './cookbook/template/']
62 # List of files that should not be scanned for copyright updates.
63 # IGNORED_FILES = ['COPYING']
64 IGNORED_FILES = ['COPYING']
66 # Work around missing author holes in the VCS history.
67 # AUTHOR_HACKS[<path tuple>] = [<missing authors]
68 # for example, if John Doe contributed to module.py but wasn't listed
69 # in the VCS history of that file:
71 # ('path', 'to', 'module.py'):['John Doe'],
75 # Work around missing year holes in the VCS history.
76 # YEAR_HACKS[<path tuple>] = <original year>
77 # for example, if module.py was published in 2008 but the VCS history
78 # only goes back to 2010:
80 # ('path', 'to', 'module.py'):2008,
84 # Helpers for VCS-specific commands
87 """Recursively split a path into elements.
92 >>> splitpath(os.path.join('a', 'b', 'c'))
94 >>> splitpath(os.path.join('.', 'a', 'b', 'c'))
97 path = os.path.normpath(path)
100 dirname,basename = os.path.split(path)
101 elements.insert(0,basename)
102 if dirname in ['', '.']:
105 return tuple(elements)
107 # VCS-specific commands
109 if PROJECT_INFO['vcs'] == 'Git':
113 _MSWINDOWS = sys.platform == 'win32'
114 _POSIX = not _MSWINDOWS
116 def invoke(args, stdin=None, stdout=subprocess.PIPE, stderr=subprocess.PIPE, expect=(0,)):
118 expect should be a tuple of allowed exit codes.
122 q = subprocess.Popen(args, stdin=subprocess.PIPE,
123 stdout=stdout, stderr=stderr)
125 assert _MSWINDOWS == True, 'invalid platform'
126 # win32 don't have os.execvp() so run the command in a shell
127 q = subprocess.Popen(args, stdin=subprocess.PIPE,
128 stdout=stdout, stderr=stderr, shell=True)
130 raise ValueError([args, e])
131 stdout,stderr = q.communicate(input=stdin)
133 if status not in expect:
134 raise ValueError([args, status, stdout, stderr])
135 return status, stdout, stderr
138 status,stdout,stderr = invoke(['git'] + list(args))
139 return stdout.rstrip('\n')
141 def original_year(filename, year_hacks=YEAR_HACKS):
142 output = git_cmd('log', '--follow',
143 '--format=format:%ad', # Author date
144 '--date=short', # YYYY-MM-DD
146 years = [int(line.split('-', 1)[0]) for line in output.splitlines()]
147 if splitpath(filename) in year_hacks:
148 years.append(year_hacks[splitpath(filename)])
152 def authors(filename, author_hacks=AUTHOR_HACKS):
153 output = git_cmd('log', '--follow', '--format=format:%aN <%aE>',
154 filename) # Author name <author email>
155 ret = list(set(output.splitlines()))
156 if splitpath(filename) in author_hacks:
157 ret.extend(author_hacks[splitpath(filename)])
160 def authors_list(author_hacks=AUTHOR_HACKS):
161 output = git_cmd('log', '--format=format:%aN <%aE>')
162 ret = list(set(output.splitlines()))
163 for path,authors in author_hacks.items():
167 def is_versioned(filename):
168 output = git_cmd('log', '--follow', filename)
173 elif PROJECT_INFO['vcs'] == 'Mercurial':
177 import mercurial.dispatch
179 def mercurial_cmd(*args):
183 tmp_stdout = StringIO.StringIO()
184 tmp_stderr = StringIO.StringIO()
185 sys.stdout = tmp_stdout
186 sys.stderr = tmp_stderr
188 mercurial.dispatch.dispatch(list(args))
193 return (tmp_stdout.getvalue().rstrip('\n'),
194 tmp_stderr.getvalue().rstrip('\n'))
196 def original_year(filename, year_hacks=YEAR_HACKS):
197 # shortdate filter: YEAR-MONTH-DAY
198 output,error = mercurial_cmd('log', '--follow',
199 '--template', '{date|shortdate}\n',
201 years = [int(line.split('-', 1)[0]) for line in output.splitlines()]
202 if splitpath(filename) in year_hacks:
203 years.append(year_hacks[splitpath(filename)])
207 def authors(filename, author_hacks=AUTHOR_HACKS):
208 output,error = mercurial_cmd('log', '--follow',
209 '--template', '{author}\n',
211 ret = list(set(output.splitlines()))
212 if splitpath(filename) in author_hacks:
213 ret.extend(author_hacks[splitpath(filename)])
216 def authors_list(author_hacks=AUTHOR_HACKS):
217 output,error = mercurial_cmd('log', '--template', '{author}\n')
218 ret = list(set(output.splitlines()))
219 for path,authors in author_hacks.items():
223 def is_versioned(filename):
224 output,error = mercurial_cmd('log', '--follow', filename)
229 elif PROJECT_INFO['vcs'] == 'Bazaar':
233 import bzrlib.builtins
236 class LogFormatter (bzrlib.log.LogFormatter):
237 supports_merge_revisions = True
239 supports_deta = False
240 supports_tags = False
241 supports_diff = False
243 def log_revision(self, revision):
244 raise NotImplementedError
246 class YearLogFormatter (LogFormatter):
247 def log_revision(self, revision):
249 time.strftime('%Y', time.gmtime(revision.rev.timestamp))
252 class AuthorLogFormatter (LogFormatter):
253 def log_revision(self, revision):
254 authors = revision.rev.get_apparent_authors()
255 self.to_file.write('\n'.join(authors)+'\n')
257 def original_year(filename, year_hacks=YEAR_HACKS):
258 cmd = bzrlib.builtins.cmd_log()
259 cmd.outf = StringIO.StringIO()
260 cmd.run(file_list=[filename], log_format=YearLogFormatter, levels=0)
261 years = [int(year) for year in set(cmd.outf.getvalue().splitlines())]
262 if splitpath(filename) in year_hacks:
263 years.append(year_hacks[splitpath(filename)])
267 def authors(filename, author_hacks=AUTHOR_HACKS):
268 cmd = bzrlib.builtins.cmd_log()
269 cmd.outf = StringIO.StringIO()
270 cmd.run(file_list=[filename], log_format=AuthorLogFormatter, levels=0)
271 ret = list(set(cmd.outf.getvalue().splitlines()))
272 if splitpath(filename) in author_hacks:
273 ret.extend(author_hacks[splitpath(filename)])
276 def authors_list(author_hacks=AUTHOR_HACKS):
277 cmd = bzrlib.builtins.cmd_log()
278 cmd.outf = StringIO.StringIO()
279 cmd.run(log_format=AuthorLogFormatter, levels=0)
280 output = cmd.outf.getvalue()
281 ret = list(set(cmd.outf.getvalue().splitlines()))
282 for path,authors in author_hacks.items():
286 def is_versioned(filename):
287 cmd = bzrlib.builtins.cmd_log()
288 cmd.outf = StringIO.StringIO()
289 cmd.run(file_list=[filename])
293 raise NotImplementedError('Unrecognized VCS: %(vcs)s' % PROJECT_INFO)
295 # General utility commands
297 def _strip_email(*args):
298 """Remove email addresses from a series of names.
303 >>> _strip_email('J Doe <jdoe@a.com>')
305 >>> _strip_email('J Doe <jdoe@a.com>', 'JJJ Smith <jjjs@a.com>')
306 ['J Doe', 'JJJ Smith']
309 for i,arg in enumerate(args):
312 author,addr = email.utils.parseaddr(arg)
316 def _reverse_aliases(aliases):
317 """Reverse an `aliases` dict.
319 Input: key: canonical name, value: list of aliases
320 Output: key: alias, value: canonical name
326 ... 'J Doe <jdoe@a.com>':['Johnny <jdoe@b.edu>', 'J'],
327 ... 'JJJ Smith <jjjs@a.com>':['Jingly <jjjs@b.edu>'],
328 ... None:['Anonymous <a@a.com>'],
330 >>> r = _reverse_aliases(aliases)
331 >>> for item in sorted(r.items()):
333 ('Anonymous <a@a.com>', None)
334 ('J', 'J Doe <jdoe@a.com>')
335 ('Jingly <jjjs@b.edu>', 'JJJ Smith <jjjs@a.com>')
336 ('Johnny <jdoe@b.edu>', 'J Doe <jdoe@a.com>')
339 for canonical_name,_aliases in aliases.items():
340 for alias in _aliases:
341 output[alias] = canonical_name
344 def _replace_aliases(authors, with_email=True, aliases=None):
345 """Consolidate and sort `authors`.
347 Make the replacements listed in the `aliases` dict (key: canonical
348 name, value: list of aliases). If `aliases` is ``None``, default
352 ... 'J Doe <jdoe@a.com>':['Johnny <jdoe@b.edu>'],
353 ... 'JJJ Smith <jjjs@a.com>':['Jingly <jjjs@b.edu>'],
354 ... None:['Anonymous <a@a.com>'],
356 >>> _replace_aliases(['JJJ Smith <jjjs@a.com>', 'Johnny <jdoe@b.edu>',
357 ... 'Jingly <jjjs@b.edu>', 'Anonymous <a@a.com>'],
358 ... with_email=True, aliases=aliases)
359 ['J Doe <jdoe@a.com>', 'JJJ Smith <jjjs@a.com>']
360 >>> _replace_aliases(['JJJ Smith', 'Johnny', 'Jingly', 'Anonymous'],
361 ... with_email=False, aliases=aliases)
362 ['J Doe', 'JJJ Smith']
363 >>> _replace_aliases(['JJJ Smith <jjjs@a.com>', 'Johnny <jdoe@b.edu>',
364 ... 'Jingly <jjjs@b.edu>', 'J Doe <jdoe@a.com>'],
365 ... with_email=True, aliases=aliases)
366 ['J Doe <jdoe@a.com>', 'JJJ Smith <jjjs@a.com>']
370 if with_email == False:
371 aliases = dict([(_strip_email(author)[0], _strip_email(*_aliases))
372 for author,_aliases in aliases.items()])
373 rev_aliases = _reverse_aliases(aliases)
374 for i,author in enumerate(authors):
375 if author in rev_aliases:
376 authors[i] = rev_aliases[author]
377 authors = sorted(list(set(authors)))
382 def _copyright_string(original_year, final_year, authors, prefix=''):
384 >>> print _copyright_string(original_year=2005,
386 ... authors=['A <a@a.com>', 'B <b@b.edu>'],
388 ... ) # doctest: +ELLIPSIS
389 # Copyright (C) 2005 A <a@a.com>
393 >>> print _copyright_string(original_year=2005,
395 ... authors=['A <a@a.com>', 'B <b@b.edu>']
396 ... ) # doctest: +ELLIPSIS
397 Copyright (C) 2005-2009 A <a@a.com>
402 if original_year == final_year:
403 date_range = '%s' % original_year
405 date_range = '%s-%s' % (original_year, final_year)
406 lines = ['Copyright (C) %s %s' % (date_range, authors[0])]
407 for author in authors[1:]:
408 lines.append(' '*(len('Copyright (C) ')+len(date_range)+1) +
411 lines.extend((COPY_RIGHT_TEXT % PROJECT_INFO).splitlines())
412 for i,line in enumerate(lines):
413 lines[i] = (prefix + line).rstrip()
414 return '\n'.join(lines)
416 def _tag_copyright(contents):
418 >>> contents = '''Some file
420 ... # Copyright (copyright begins)
421 ... # (copyright continues)
426 >>> print _tag_copyright(contents).replace('COPY-RIGHT', 'CR')
436 for line in contents.splitlines():
437 if incopy == False and line.startswith('# Copyright'):
439 lines.append(COPY_RIGHT_TAG)
440 elif incopy == True and not line.startswith('#'):
443 lines.append(line.rstrip('\n'))
444 return '\n'.join(lines)+'\n'
446 def _update_copyright(contents, original_year, authors):
448 >>> contents = '''Some file
450 ... # Copyright (copyright begins)
451 ... # (copyright continues)
456 >>> print _update_copyright(contents, 2008, ['Jack', 'Jill']
457 ... ) # doctest: +ELLIPSIS, +REPORT_UDIFF
460 # Copyright (C) 2008-... Jack
468 current_year = time.gmtime()[0]
469 copyright_string = _copyright_string(
470 original_year, current_year, authors, prefix='# ')
471 contents = _tag_copyright(contents)
472 return contents.replace(COPY_RIGHT_TAG, copyright_string)
474 def ignored_file(filename, ignored_paths=None, ignored_files=None,
475 check_disk=True, check_vcs=True):
477 >>> ignored_paths = ['./a/', './b/']
478 >>> ignored_files = ['x', 'y']
479 >>> ignored_file('./a/z', ignored_paths, ignored_files, False, False)
481 >>> ignored_file('./ab/z', ignored_paths, ignored_files, False, False)
483 >>> ignored_file('./ab/x', ignored_paths, ignored_files, False, False)
485 >>> ignored_file('./ab/xy', ignored_paths, ignored_files, False, False)
487 >>> ignored_file('./z', ignored_paths, ignored_files, False, False)
490 if ignored_paths == None:
491 ignored_paths = IGNORED_PATHS
492 if ignored_files == None:
493 ignored_files = IGNORED_FILES
494 if check_disk == True and os.path.isfile(filename) == False:
496 for path in ignored_paths:
497 if filename.startswith(path):
499 if os.path.basename(filename) in ignored_files:
501 if check_vcs == True and is_versioned(filename) == False:
505 def _set_contents(filename, contents, original_contents=None, dry_run=False,
507 if original_contents == None and os.path.isfile(filename):
508 f = open(filename, 'r')
509 original_contents = f.read()
512 print "checking %s ... " % filename,
513 if contents != original_contents:
515 if original_contents == None:
519 if verbose > 1 and original_contents != None:
521 difflib.unified_diff(
522 original_contents.splitlines(), contents.splitlines(),
523 fromfile=os.path.normpath(os.path.join('a', filename)),
524 tofile=os.path.normpath(os.path.join('b', filename)),
527 f = file(filename, 'w')
535 def update_authors(authors_fn=authors_list, dry_run=False, verbose=0):
536 authors = authors_fn()
537 authors = _replace_aliases(authors, with_email=True, aliases=ALIASES)
538 new_contents = '%s was written by:\n%s\n' % (
539 PROJECT_INFO['project'],
542 _set_contents('AUTHORS', new_contents, dry_run=dry_run, verbose=verbose)
544 def update_file(filename, original_year_fn=original_year, authors_fn=authors,
545 dry_run=False, verbose=0):
546 f = file(filename, 'r')
550 original_year = original_year_fn(filename)
551 authors = authors_fn(filename)
552 authors = _replace_aliases(authors, with_email=True, aliases=ALIASES)
554 new_contents = _update_copyright(contents, original_year, authors)
555 _set_contents(filename, contents=new_contents, original_contents=contents,
556 dry_run=dry_run, verbose=verbose)
558 def update_files(files=None, dry_run=False, verbose=0):
559 if files == None or len(files) == 0:
561 for dirpath,dirnames,filenames in os.walk('.'):
562 for filename in filenames:
563 files.append(os.path.join(dirpath, filename))
565 for filename in files:
566 if ignored_file(filename) == True:
568 update_file(filename, dry_run=dry_run, verbose=verbose)
574 if __name__ == '__main__':
578 usage = """%%prog [options] [file ...]
580 Update copyright information in source code with information from
581 the %(vcs)s repository. Run from the %(project)s repository root.
583 Replaces every line starting with '^# Copyright' and continuing with
584 '^#' with an auto-generated copyright blurb. If you want to add
585 #-commented material after a copyright blurb, please insert a blank
586 line between the blurb and your comment, so the next run of
587 ``update_copyright.py`` doesn't clobber your comment.
589 If no files are given, a list of files to update is generated
592 p = optparse.OptionParser(usage)
593 p.add_option('--test', dest='test', default=False,
594 action='store_true', help='Run internal tests and exit')
595 p.add_option('--dry-run', dest='dry_run', default=False,
596 action='store_true', help="Don't make any changes")
597 p.add_option('-v', '--verbose', dest='verbose', default=0,
598 action='count', help='Increment verbosity')
599 options,args = p.parse_args()
601 if options.test == True:
605 update_authors(dry_run=options.dry_run, verbose=options.verbose)
606 update_files(files=args, dry_run=options.dry_run, verbose=options.verbose)