From 1e1413717df6ed6809833004bf47088e021ccb46 Mon Sep 17 00:00:00 2001 From: Zac Medico Date: Thu, 20 Oct 2011 23:06:33 -0700 Subject: [PATCH] UpdateChangeLog: split out/test copyright regex This also fixes a case where something like "Copyright 2011 " would be replaced with "Copyright 2011-2011 ". --- pym/portage/tests/repoman/test_simple.py | 23 ++++++++++++++ pym/repoman/utilities.py | 40 +++++++++++++++++------- 2 files changed, 51 insertions(+), 12 deletions(-) diff --git a/pym/portage/tests/repoman/test_simple.py b/pym/portage/tests/repoman/test_simple.py index 83de883e1..9dae0efda 100644 --- a/pym/portage/tests/repoman/test_simple.py +++ b/pym/portage/tests/repoman/test_simple.py @@ -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: diff --git a/pym/repoman/utilities.py b/pym/repoman/utilities.py index 6b4bd5098..eec6fdfee 100644 --- a/pym/repoman/utilities.py +++ b/pym/repoman/utilities.py @@ -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) line = line.replace('', 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() -- 2.26.2