3 # Copyright (C) 2010 W. Trevor King <wking@drexel.edu>
5 """Automatically update copyright boilerplate.
7 This script is adapted from one written for `Bugs Everywhere`_
10 .. _Bugs Everywhere: http://bugseverywhere.org/
11 .. _Hooke: http://code.google.com/p/hooke/
25 'project': 'CalibCant',
29 # Break "copyright" into "copy" and "right" to avoid matching the
32 This file is part of %(project)s.
34 %(project)s is free software: you can redistribute it and/or
35 modify it under the terms of the GNU Lesser General Public
36 License as published by the Free Software Foundation, either
37 version 3 of the License, or (at your option) any later version.
39 %(project)s is distributed in the hope that it will be useful,
40 but WITHOUT ANY WARRANTY; without even the implied warranty of
41 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
42 GNU Lesser General Public License for more details.
44 You should have received a copy of the GNU Lesser General Public
45 License along with %(project)s. If not, see
46 <http://www.gnu.org/licenses/>.
49 COPY_RIGHT_TAG='-xyz-COPY' + '-RIGHT-zyx-' # unlikely to occur in the wild :p
51 # Convert author names to canonical forms.
52 # ALIASES[<canonical name>] = <list of aliases>
55 # 'John Doe <jdoe@a.com>':
56 # ['John Doe', 'jdoe', 'J. Doe <j@doe.net>'],
58 # Git-based projects are encouraged to use .mailmap instead of
59 # ALIASES. See git-shortlog(1) for details.
62 # List of paths that should not be scanned for copyright updates.
63 # IGNORED_PATHS = ['./.git/']
64 IGNORED_PATHS = ['./.git/', './.be/', './build', './dist/',
65 './calibcant.egg-info/']
66 # List of files that should not be scanned for copyright updates.
67 # IGNORED_FILES = ['COPYING']
68 IGNORED_FILES = ['COPYING']
70 # Work around missing author holes in the VCS history.
71 # AUTHOR_HACKS[<path tuple>] = [<missing authors]
72 # for example, if John Doe contributed to module.py but wasn't listed
73 # in the VCS history of that file:
75 # ('path', 'to', 'module.py'):['John Doe'],
79 # Work around missing year holes in the VCS history.
80 # YEAR_HACKS[<path tuple>] = <original year>
81 # for example, if module.py was published in 2008 but the VCS history
82 # only goes back to 2010:
84 # ('path', 'to', 'module.py'):2008,
88 # Helpers for VCS-specific commands
91 """Recursively split a path into elements.
96 >>> splitpath(os.path.join('a', 'b', 'c'))
98 >>> splitpath(os.path.join('.', 'a', 'b', 'c'))
101 path = os.path.normpath(path)
104 dirname,basename = os.path.split(path)
105 elements.insert(0,basename)
106 if dirname in ['', '.']:
109 return tuple(elements)
111 # VCS-specific commands
113 if PROJECT_INFO['vcs'] == 'Git':
117 _MSWINDOWS = sys.platform == 'win32'
118 _POSIX = not _MSWINDOWS
120 def invoke(args, stdin=None, stdout=subprocess.PIPE, stderr=subprocess.PIPE, expect=(0,)):
122 expect should be a tuple of allowed exit codes.
126 q = subprocess.Popen(args, stdin=subprocess.PIPE,
127 stdout=stdout, stderr=stderr)
129 assert _MSWINDOWS == True, 'invalid platform'
130 # win32 don't have os.execvp() so run the command in a shell
131 q = subprocess.Popen(args, stdin=subprocess.PIPE,
132 stdout=stdout, stderr=stderr, shell=True)
134 raise ValueError([args, e])
135 stdout,stderr = q.communicate(input=stdin)
137 if status not in expect:
138 raise ValueError([args, status, stdout, stderr])
139 return status, stdout, stderr
142 status,stdout,stderr = invoke(['git'] + list(args))
143 return stdout.rstrip('\n')
145 def original_year(filename, year_hacks=YEAR_HACKS):
146 # shortdate filter: YEAR-MONTH-DAY
147 output = git_cmd('log', '--follow',
148 '--format=format:%ad', # Author date
149 '--date=short', # YYYY-MM-DD
151 years = [int(line.split('-', 1)[0]) for line in output.splitlines()]
152 if splitpath(filename) in year_hacks:
153 years.append(year_hacks[splitpath(filename)])
157 def authors(filename, author_hacks=AUTHOR_HACKS):
158 output = git_cmd('log', '--follow', '--format=format:%aN <%aE>',
159 filename) # Author name <author email>
160 ret = list(set(output.splitlines()))
161 if splitpath(filename) in author_hacks:
162 ret.extend(author_hacks[splitpath(filename)])
165 def authors_list(author_hacks=AUTHOR_HACKS):
166 output = git_cmd('log', '--format=format:%aN <%aE>')
167 ret = list(set(output.splitlines()))
168 for path,authors in author_hacks.items():
172 def is_versioned(filename):
173 output = git_cmd('log', '--follow', filename)
178 elif PROJECT_INFO['vcs'] == 'Mercurial':
180 def mercurial_cmd(*args):
184 tmp_stdout = StringIO.StringIO()
185 tmp_stderr = StringIO.StringIO()
186 sys.stdout = tmp_stdout
187 sys.stderr = tmp_stderr
189 mercurial.dispatch.dispatch(list(args))
194 return (tmp_stdout.getvalue().rstrip('\n'),
195 tmp_stderr.getvalue().rstrip('\n'))
197 def original_year(filename, year_hacks=YEAR_HACKS):
198 # shortdate filter: YEAR-MONTH-DAY
199 output,error = mercurial_cmd('log', '--follow',
200 '--template', '{date|shortdate}\n',
202 years = [int(line.split('-', 1)[0]) for line in output.splitlines()]
203 if splitpath(filename) in year_hacks:
204 years.append(year_hacks[splitpath(filename)])
208 def authors(filename, author_hacks=AUTHOR_HACKS):
209 output,error = mercurial_cmd('log', '-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', '--follow',
218 '--template', '{author}\n')
219 ret = list(set(output.splitlines()))
220 for path,authors in author_hacks.items():
224 def is_versioned(filename):
225 output,error = mercurial_cmd('log', '--follow', filename)
230 elif PROJECT_INFO['vcs'] == 'Bazaar':
234 raise NotImplementedError('Unrecognized VCS: %(vcs)s' % PROJECT_INFO)
236 # General utility commands
238 def _strip_email(*args):
239 """Remove email addresses from a series of names.
244 >>> _strip_email('J Doe <jdoe@a.com>')
246 >>> _strip_email('J Doe <jdoe@a.com>', 'JJJ Smith <jjjs@a.com>')
247 ['J Doe', 'JJJ Smith']
250 for i,arg in enumerate(args):
253 author,addr = email.utils.parseaddr(arg)
257 def _reverse_aliases(aliases):
258 """Reverse an `aliases` dict.
260 Input: key: canonical name, value: list of aliases
261 Output: key: alias, value: canonical name
267 ... 'J Doe <jdoe@a.com>':['Johnny <jdoe@b.edu>', 'J'],
268 ... 'JJJ Smith <jjjs@a.com>':['Jingly <jjjs@b.edu>'],
269 ... None:['Anonymous <a@a.com>'],
271 >>> r = _reverse_aliases(aliases)
272 >>> for item in sorted(r.items()):
274 ('Anonymous <a@a.com>', None)
275 ('J', 'J Doe <jdoe@a.com>')
276 ('Jingly <jjjs@b.edu>', 'JJJ Smith <jjjs@a.com>')
277 ('Johnny <jdoe@b.edu>', 'J Doe <jdoe@a.com>')
280 for canonical_name,_aliases in aliases.items():
281 for alias in _aliases:
282 output[alias] = canonical_name
285 def _replace_aliases(authors, with_email=True, aliases=None):
286 """Consolidate and sort `authors`.
288 Make the replacements listed in the `aliases` dict (key: canonical
289 name, value: list of aliases). If `aliases` is ``None``, default
293 ... 'J Doe <jdoe@a.com>':['Johnny <jdoe@b.edu>'],
294 ... 'JJJ Smith <jjjs@a.com>':['Jingly <jjjs@b.edu>'],
295 ... None:['Anonymous <a@a.com>'],
297 >>> _replace_aliases(['JJJ Smith <jjjs@a.com>', 'Johnny <jdoe@b.edu>',
298 ... 'Jingly <jjjs@b.edu>', 'Anonymous <a@a.com>'],
299 ... with_email=True, aliases=aliases)
300 ['J Doe <jdoe@a.com>', 'JJJ Smith <jjjs@a.com>']
301 >>> _replace_aliases(['JJJ Smith', 'Johnny', 'Jingly', 'Anonymous'],
302 ... with_email=False, aliases=aliases)
303 ['J Doe', 'JJJ Smith']
304 >>> _replace_aliases(['JJJ Smith <jjjs@a.com>', 'Johnny <jdoe@b.edu>',
305 ... 'Jingly <jjjs@b.edu>', 'J Doe <jdoe@a.com>'],
306 ... with_email=True, aliases=aliases)
307 ['J Doe <jdoe@a.com>', 'JJJ Smith <jjjs@a.com>']
311 if with_email == False:
312 aliases = dict([(_strip_email(author)[0], _strip_email(*_aliases))
313 for author,_aliases in aliases.items()])
314 rev_aliases = _reverse_aliases(aliases)
315 for i,author in enumerate(authors):
316 if author in rev_aliases:
317 authors[i] = rev_aliases[author]
318 authors = sorted(list(set(authors)))
323 def _copyright_string(original_year, final_year, authors, prefix=''):
325 >>> print _copyright_string(original_year=2005,
327 ... authors=['A <a@a.com>', 'B <b@b.edu>'],
329 ... ) # doctest: +ELLIPSIS
330 # Copyright (C) 2005 A <a@a.com>
334 >>> print _copyright_string(original_year=2005,
336 ... authors=['A <a@a.com>', 'B <b@b.edu>']
337 ... ) # doctest: +ELLIPSIS
338 Copyright (C) 2005-2009 A <a@a.com>
343 if original_year == final_year:
344 date_range = '%s' % original_year
346 date_range = '%s-%s' % (original_year, final_year)
347 lines = ['Copyright (C) %s %s' % (date_range, authors[0])]
348 for author in authors[1:]:
349 lines.append(' '*(len('Copyright (C) ')+len(date_range)+1) +
352 lines.extend((COPY_RIGHT_TEXT % PROJECT_INFO).splitlines())
353 for i,line in enumerate(lines):
354 lines[i] = (prefix + line).rstrip()
355 return '\n'.join(lines)
357 def _tag_copyright(contents):
359 >>> contents = '''Some file
361 ... # Copyright (copyright begins)
362 ... # (copyright continues)
367 >>> print _tag_copyright(contents).replace('COPY-RIGHT', 'CR')
377 for line in contents.splitlines():
378 if incopy == False and line.startswith('# Copyright'):
380 lines.append(COPY_RIGHT_TAG)
381 elif incopy == True and not line.startswith('#'):
384 lines.append(line.rstrip('\n'))
385 return '\n'.join(lines)+'\n'
387 def _update_copyright(contents, original_year, authors):
389 >>> contents = '''Some file
391 ... # Copyright (copyright begins)
392 ... # (copyright continues)
397 >>> print _update_copyright(contents, 2008, ['Jack', 'Jill']
398 ... ) # doctest: +ELLIPSIS, +REPORT_UDIFF
401 # Copyright (C) 2008-... Jack
409 current_year = time.gmtime()[0]
410 copyright_string = _copyright_string(
411 original_year, current_year, authors, prefix='# ')
412 contents = _tag_copyright(contents)
413 return contents.replace(COPY_RIGHT_TAG, copyright_string)
415 def ignored_file(filename, ignored_paths=None, ignored_files=None,
416 check_disk=True, check_vcs=True):
418 >>> ignored_paths = ['./a/', './b/']
419 >>> ignored_files = ['x', 'y']
420 >>> ignored_file('./a/z', ignored_paths, ignored_files, False, False)
422 >>> ignored_file('./ab/z', ignored_paths, ignored_files, False, False)
424 >>> ignored_file('./ab/x', ignored_paths, ignored_files, False, False)
426 >>> ignored_file('./ab/xy', ignored_paths, ignored_files, False, False)
428 >>> ignored_file('./z', ignored_paths, ignored_files, False, False)
431 if ignored_paths == None:
432 ignored_paths = IGNORED_PATHS
433 if ignored_files == None:
434 ignored_files = IGNORED_FILES
435 if check_disk == True and os.path.isfile(filename) == False:
437 for path in ignored_paths:
438 if filename.startswith(path):
440 if os.path.basename(filename) in ignored_files:
442 if check_vcs == True and is_versioned(filename) == False:
446 def _set_contents(filename, contents, original_contents=None, dry_run=False,
448 if original_contents == None and os.path.isfile(filename):
449 f = open(filename, 'r')
450 original_contents = f.read()
453 print "checking %s ... " % filename,
454 if contents != original_contents:
456 if original_contents == None:
460 if verbose > 1 and original_contents != None:
462 difflib.unified_diff(
463 original_contents.splitlines(), contents.splitlines(),
464 fromfile=os.path.normpath(os.path.join('a', filename)),
465 tofile=os.path.normpath(os.path.join('b', filename)),
468 f = file(filename, 'w')
476 def update_authors(authors_fn=authors_list, dry_run=False, verbose=0):
477 authors = authors_fn()
478 authors = _replace_aliases(authors, with_email=True, aliases=ALIASES)
479 new_contents = '%s was written by:\n%s\n' % (
480 PROJECT_INFO['project'],
483 _set_contents('AUTHORS', new_contents, dry_run=dry_run, verbose=verbose)
485 def update_file(filename, original_year_fn=original_year, authors_fn=authors,
486 dry_run=False, verbose=0):
487 f = file(filename, 'r')
491 original_year = original_year_fn(filename)
492 authors = authors_fn(filename)
493 authors = _replace_aliases(authors, with_email=True, aliases=ALIASES)
495 new_contents = _update_copyright(contents, original_year, authors)
496 _set_contents(filename, contents=new_contents, original_contents=contents,
497 dry_run=dry_run, verbose=verbose)
499 def update_files(files=None, dry_run=False, verbose=0):
500 if files == None or len(files) == 0:
502 for dirpath,dirnames,filenames in os.walk('.'):
503 for filename in filenames:
504 files.append(os.path.join(dirpath, filename))
506 for filename in files:
507 if ignored_file(filename) == True:
509 update_file(filename, dry_run=dry_run, verbose=verbose)
515 if __name__ == '__main__':
519 usage = """%%prog [options] [file ...]
521 Update copyright information in source code with information from
522 the %(vcs)s repository. Run from the %(project)s repository root.
524 Replaces every line starting with '^# Copyright' and continuing with
525 '^#' with an auto-generated copyright blurb. If you want to add
526 #-commented material after a copyright blurb, please insert a blank
527 line between the blurb and your comment, so the next run of
528 ``update_copyright.py`` doesn't clobber your comment.
530 If no files are given, a list of files to update is generated
533 p = optparse.OptionParser(usage)
534 p.add_option('--test', dest='test', default=False,
535 action='store_true', help='Run internal tests and exit')
536 p.add_option('--dry-run', dest='dry_run', default=False,
537 action='store_true', help="Don't make any changes")
538 p.add_option('-v', '--verbose', dest='verbose', default=0,
539 action='count', help='Increment verbosity')
540 options,args = p.parse_args()
542 if options.test == True:
546 update_authors(dry_run=options.dry_run, verbose=options.verbose)
547 update_files(files=args, dry_run=options.dry_run, verbose=options.verbose)
549 # LocalWords: difflib