project: Replace original_year and final_year with years
authorW. Trevor King <wking@tremily.us>
Mon, 27 Jan 2014 17:40:57 +0000 (09:40 -0800)
committerW. Trevor King <wking@tremily.us>
Mon, 27 Jan 2014 17:40:57 +0000 (09:40 -0800)
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
update_copyright/utils.py
update_copyright/vcs/__init__.py

index f58741951c4bea4e69c0eac8d435c5af3e781a25..03296b9d600472da6ee0d45ef8f0d85122a4fd44 100644 (file)
@@ -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(
index c3ef002018f05dc1dad26b92a2810f85a6a4377d..8ec22ce750e3b20aa7b249889598b4b70f2f4374 100644 (file)
@@ -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 <a@a.com>', 'B <b@b.edu>'],
     ...                        text=['BLURB',], prefix=('# ', '# ', None),
     ...                        )) # doctest: +REPORT_UDIFF
@@ -67,7 +66,7 @@ def copyright_string(original_year, final_year, authors, text, info={},
     #                    B <b@b.edu>
     #
     # BLURB
-    >>> print(copyright_string(original_year=2005, final_year=2009,
+    >>> print(copyright_string(years=[2005, 2009],
     ...                        authors=['A <a@a.com>', 'B <b@b.edu>'],
     ...                        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 <a@a.com>', 'B <b@b.edu>'],
     ...                        text=['BLURB',]
     ...                        )) # doctest: +REPORT_UDIFF
@@ -84,7 +83,7 @@ def copyright_string(original_year, final_year, authors, text, info={},
                             B <b@b.edu>
     <BLANKLINE>
     BLURB
-    >>> print(copyright_string(original_year=2005, final_year=2005,
+    >>> print(copyright_string(years=[2005],
     ...                        authors=['A <a@a.com>', 'B <b@b.edu>'],
     ...                        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={},
     <BLANKLINE>
     This file is part of
     update-copyright.
-    >>> print(copyright_string(original_year=2005, final_year=2005,
+    >>> print(copyright_string(years=[2005],
     ...                        authors=['A <a@a.com>', 'B <b@b.edu>'],
     ...                        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
     <BLANKLINE>
     """
-    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)
 
index cd5a36746d76e51b76e19189006594c3fb7caf87..25aefdfbe1f305dabf507655f8498b23f634cda0 100644 (file)
@@ -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()