UpdateChangeLog: split out/test copyright regex
authorZac Medico <zmedico@gentoo.org>
Fri, 21 Oct 2011 06:06:33 +0000 (23:06 -0700)
committerZac Medico <zmedico@gentoo.org>
Fri, 21 Oct 2011 06:06:33 +0000 (23:06 -0700)
This also fixes a case where something like "Copyright 2011 " would be
replaced with "Copyright 2011-2011 ".

pym/portage/tests/repoman/test_simple.py
pym/repoman/utilities.py

index 83de883e1e53ccb8835f4597360f3b6696bc518e..9dae0efda034f48eb58e8c8fdf680c9dc312a48b 100644 (file)
@@ -14,9 +14,32 @@ from portage.process import find_binary
 from portage.tests import TestCase
 from portage.tests.resolver.ResolverPlayground import ResolverPlayground
 from portage.util import ensure_dirs
+from repoman.utilities import _update_copyright_year
 
 class SimpleRepomanTestCase(TestCase):
 
+       def testCopyrightUpdate(self):
+               test_cases = (
+                       (
+                               '2011',
+                               '# Copyright 1999-2008 Gentoo Foundation; Distributed under the GPL v2',
+                               '# Copyright 1999-2011 Gentoo Foundation; Distributed under the GPL v2',
+                       ),
+                       (
+                               '2011',
+                               '# Copyright 1999 Gentoo Foundation; Distributed under the GPL v2',
+                               '# Copyright 1999-2011 Gentoo Foundation; Distributed under the GPL v2',
+                       ),
+                       (
+                               '1999',
+                               '# Copyright 1999 Gentoo Foundation; Distributed under the GPL v2',
+                               '# Copyright 1999 Gentoo Foundation; Distributed under the GPL v2',
+                       ),
+               )
+
+               for year, before, after in test_cases:
+                       self.assertEqual(_update_copyright_year(year, before), after)
+
        def _must_skip(self):
                xmllint = find_binary("xmllint")
                if not xmllint:
index 6b4bd5098059a3d4cd27ac4f2a78d6b0af47badb..eec6fdfeec0c10e89a7b97f3e6472dbeffdc77fe 100644 (file)
@@ -524,6 +524,31 @@ def FindVCS():
 
        return outvcs
 
+_copyright_re1 = re.compile(r'^(# Copyright \d\d\d\d)-\d\d\d\d ')
+_copyright_re2 = re.compile(r'^(# Copyright )(\d\d\d\d) ')
+
+
+class _copyright_repl(object):
+       __slots__ = ('year',)
+       def __init__(self, year):
+               self.year = year
+       def __call__(self, matchobj):
+               if matchobj.group(2) == self.year:
+                       return matchobj.group(0)
+               else:
+                       return '%s%s-%s ' % \
+                               (matchobj.group(1), matchobj.group(2), self.year)
+
+def _update_copyright_year(year, line):
+       """
+       These two regexes are taken from echangelog
+       update_copyright(), except that we don't hardcode
+       1999 here (in order to be more generic).
+       """
+       line = _copyright_re1.sub(r'\1-%s ' % year, line)
+       line = _copyright_re2.sub(_copyright_repl(year), line)
+       return line
+
 def update_copyright(fn_path, year, pretend):
        """
        Check file for a Copyright statement, and update its year.  The
@@ -549,13 +574,7 @@ def update_copyright(fn_path, year, pretend):
                        new_header.append(line)
                        break
 
-               # These two regexes are taken from echangelog
-               # update_copyright(), except that we don't hardcode
-               # 1999 here (in order to be more generic).
-               line = re.sub(r'^(# Copyright \d+) ',
-                       r'\1-%s ' % year, line)
-               line = re.sub(r'^(# Copyright \d\d\d\d)-\d\d\d\d',
-                       r'\1-%s' % year, line)
+               line = _update_copyright_year(year, line)
                new_header.append(line)
 
        difflines = 0
@@ -671,9 +690,7 @@ def UpdateChangeLog(pkgdir, user, msg, skel_path, category, package,
                                        clold_lines.append(line)
                                        break
                                old_header_lines.append(line)
-                               header_lines.append(
-                                       re.sub(r'^(# Copyright \d\d\d\d)-\d\d\d\d ',
-                                       r'\1-%s ' % year, line))
+                               header_lines.append(_update_copyright_year(year, line))
                                if not line_strip:
                                        break
 
@@ -685,8 +702,7 @@ def UpdateChangeLog(pkgdir, user, msg, skel_path, category, package,
                                        break
                                line = line.replace('<CATEGORY>', category)
                                line = line.replace('<PACKAGE_NAME>', package)
-                               line = re.sub(r'^(# Copyright \d\d\d\d)-\d\d\d\d ',
-                                       r'\1-%s ' % year, line)
+                               line = _update_copyright_year(year, line)
                                header_lines.append(line)
                        header_lines.append(_unicode_decode('\n'))
                        clskel_file.close()