From a9b5c22d88e8513a9d3c9a21084d0bb475c31650 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Mon, 27 Jan 2014 09:40:57 -0800 Subject: [PATCH] project: Replace original_year and final_year with years Rather than automatically bumping the final year to the current year, only bump to the last year for which a file was changed. I've kept the 'first-last' syntax, but update_copyright.utils.copyright_string is not the only place you have to edit if you prefer to list them all explicitly: Copyright (C) year-1, year-2, ..., year-n A. U. Thor ... --- update_copyright/project.py | 25 ++++++++++++------------- update_copyright/utils.py | 30 ++++++++++++++++-------------- update_copyright/vcs/__init__.py | 4 ++-- 3 files changed, 30 insertions(+), 29 deletions(-) diff --git a/update_copyright/project.py b/update_copyright/project.py index f587419..03296b9 100644 --- a/update_copyright/project.py +++ b/update_copyright/project.py @@ -185,14 +185,14 @@ class Project (object): _LOG.info('update {}'.format(filename)) contents = _utils.get_contents( filename=filename, unicode=True, encoding=self._encoding) - original_year = self._vcs.original_year(filename=filename) + years = self._vcs.years(filename=filename) authors = self._vcs.authors(filename=filename) new_contents = _utils.update_copyright( - contents=contents, original_year=original_year, authors=authors, + contents=contents, years=years, authors=authors, text=self._copyright, info=self._info(), prefix=('# ', '# ', None), width=self._width, tag=self._copyright_tag) new_contents = _utils.update_copyright( - contents=new_contents, original_year=original_year, + contents=new_contents, years=years, authors=authors, text=self._copyright, info=self._info(), prefix=('/* ', ' * ', ' */'), width=self._width, tag=self._copyright_tag) @@ -215,28 +215,27 @@ class Project (object): return _LOG.info('update pyfile at {}'.format(self._pyfile)) current_year = _time.gmtime()[0] - original_year = self._vcs.original_year() + years = self._vcs.years() authors = self._vcs.authors() lines = [ _utils.copyright_string( - original_year=original_year, final_year=current_year, - authors=authors, text=self._copyright, info=self._info(), - prefix=('# ', '# ', None), width=self._width), + years=years, authors=authors, text=self._copyright, + info=self._info(), prefix=('# ', '# ', None), + width=self._width), '', 'import textwrap as _textwrap', '', '', 'LICENSE = """', _utils.copyright_string( - original_year=original_year, final_year=current_year, - authors=authors, text=self._copyright, info=self._info(), - prefix=('', '', None), width=self._width), + years=years, authors=authors, text=self._copyright, + info=self._info(), prefix=('', '', None), width=self._width), '""".strip()', '', 'def short_license(info, wrap=True, **kwargs):', ' paragraphs = [', ] paragraphs = _utils.copyright_string( - original_year=original_year, final_year=current_year, - authors=authors, text=self._short_copyright, info=self._info(), - author_format_fn=_utils.short_author_formatter, wrap=False, + years=years, authors=authors, text=self._short_copyright, + info=self._info(), author_format_fn=_utils.short_author_formatter, + wrap=False, ).split('\n\n') for p in paragraphs: lines.append(" '{}'.format(**info),".format( diff --git a/update_copyright/utils.py b/update_copyright/utils.py index c3ef002..8ec22ce 100644 --- a/update_copyright/utils.py +++ b/update_copyright/utils.py @@ -22,7 +22,6 @@ import os as _os import os.path as _os_path import sys as _sys import textwrap as _textwrap -import time as _time from . import LOG as _LOG @@ -54,12 +53,12 @@ def short_author_formatter(copyright_year_string, authors): blurb = '{} {}'.format(copyright_year_string, ', '.join(authors)) return [blurb] -def copyright_string(original_year, final_year, authors, text, info={}, +def copyright_string(years, authors, text, info={}, author_format_fn=long_author_formatter, formatter_kwargs={}, prefix=('', '', None), wrap=True, **wrap_kwargs): """ - >>> print(copyright_string(original_year=2005, final_year=2005, + >>> print(copyright_string(years=[2005], ... authors=['A ', 'B '], ... text=['BLURB',], prefix=('# ', '# ', None), ... )) # doctest: +REPORT_UDIFF @@ -67,7 +66,7 @@ def copyright_string(original_year, final_year, authors, text, info={}, # B # # BLURB - >>> print(copyright_string(original_year=2005, final_year=2009, + >>> print(copyright_string(years=[2005, 2009], ... authors=['A ', 'B '], ... text=['BLURB',], prefix=('/* ', ' * ', ' */'), ... )) # doctest: +REPORT_UDIFF @@ -76,7 +75,7 @@ def copyright_string(original_year, final_year, authors, text, info={}, * * BLURB */ - >>> print(copyright_string(original_year=2005, final_year=2009, + >>> print(copyright_string(years=[2005, 2009], ... authors=['A ', 'B '], ... text=['BLURB',] ... )) # doctest: +REPORT_UDIFF @@ -84,7 +83,7 @@ def copyright_string(original_year, final_year, authors, text, info={}, B BLURB - >>> print(copyright_string(original_year=2005, final_year=2005, + >>> print(copyright_string(years=[2005], ... authors=['A ', 'B '], ... text=['This file is part of {program}.',], ... author_format_fn=short_author_formatter, @@ -95,7 +94,7 @@ def copyright_string(original_year, final_year, authors, text, info={}, This file is part of update-copyright. - >>> print(copyright_string(original_year=2005, final_year=2005, + >>> print(copyright_string(years=[2005], ... authors=['A ', 'B '], ... text=[('This file is part of {program}. '*3 ... ).strip(),], @@ -111,9 +110,13 @@ def copyright_string(original_year, final_year, authors, text, info={}, if key not in wrap_kwargs: wrap_kwargs[key] = prefix[1] - if original_year == final_year: - date_range = str(original_year) + if not years: + raise ValueError('empty years argument: {!r}'.format(years)) + elif len(years) == 1: + date_range = str(years[0]) else: + original_year = min(years) + final_year = max(years) date_range = '{}-{}'.format(original_year, final_year) copyright_year_string = 'Copyright (C) {}'.format(date_range) @@ -215,21 +218,20 @@ def update_copyright(contents, prefix=('# ', '# ', None), tag=None, **kwargs): ... bla bla bla ... ''' >>> print(update_copyright( - ... contents, original_year=2008, authors=['Jack', 'Jill'], + ... contents, years=[2008], authors=['Jack', 'Jill'], ... text=['BLURB',], prefix=('# ', '# ', None), tag='--tag--' ... )) # doctest: +ELLIPSIS, +REPORT_UDIFF Some file bla bla - # Copyright (C) 2008-... Jack - # Jill + # Copyright (C) 2008 Jack + # Jill # # BLURB (copyright ends) bla bla bla """ - current_year = _time.gmtime()[0] - string = copyright_string(final_year=current_year, prefix=prefix, **kwargs) + string = copyright_string(prefix=prefix, **kwargs) contents = tag_copyright(contents=contents, prefix=prefix, tag=tag) return contents.replace(tag, string) diff --git a/update_copyright/vcs/__init__.py b/update_copyright/vcs/__init__.py index cd5a367..25aefdf 100644 --- a/update_copyright/vcs/__init__.py +++ b/update_copyright/vcs/__init__.py @@ -41,7 +41,7 @@ class VCSBackend (object): def _years(self, filename=None): raise NotImplementedError() - def original_year(self, filename=None): + def years(self, filename=None): years = self._years(filename=filename) if filename is None: years.update(self._year_hacks.values()) @@ -51,7 +51,7 @@ class VCSBackend (object): if splitpath in self._year_hacks: years.add(self._year_hacks[splitpath]) years = sorted(years) - return years[0] + return years def _authors(self, filename=None): raise NotImplementedError() -- 2.26.2