From bb440128bdf47cbec0789b1e9453ca726ad3dd50 Mon Sep 17 00:00:00 2001 From: Mike Frysinger Date: Tue, 12 Jun 2012 18:07:58 -0400 Subject: [PATCH] repoman: handle trailing newlines better Automatically strip trailing newlines from the ChangeLog, and be better about not adding them in the first place (still not perfect, but getting there). Signed-off-by: Mike Frysinger --- pym/portage/tests/repoman/test_echangelog.py | 9 +++++++++ pym/repoman/utilities.py | 11 ++++++++--- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/pym/portage/tests/repoman/test_echangelog.py b/pym/portage/tests/repoman/test_echangelog.py index 71d6d5ebf..74aa1c54c 100644 --- a/pym/portage/tests/repoman/test_echangelog.py +++ b/pym/portage/tests/repoman/test_echangelog.py @@ -83,6 +83,7 @@ class RepomanEchangelogTestCase(TestCase): UpdateChangeLog(self.pkgdir, self.user, 'test!', self.skel_changelog, self.cat, self.pkg, quiet=True) actual_cl = self._readlines(self.changelog) self.assertEqual(actual_cl[0], self.header_pkg) + self.assertNotEqual(actual_cl[-1], '\n') def testExistingGoodHeader(self): # Test existing ChangeLog (correct values) @@ -99,3 +100,11 @@ class RepomanEchangelogTestCase(TestCase): UpdateChangeLog(self.pkgdir, self.user, 'test!', self.skel_changelog, self.cat, self.pkg, quiet=True) actual_cl = self._readlines(self.changelog) self.assertEqual(actual_cl[0], self.header_pkg) + + def testTrailingNewlines(self): + # Make sure trailing newlines get chomped. + self._writelines(self.changelog, ['#\n', 'foo\n', '\n', 'bar\n', '\n', '\n']) + + UpdateChangeLog(self.pkgdir, self.user, 'test!', self.skel_changelog, self.cat, self.pkg, quiet=True) + actual_cl = self._readlines(self.changelog) + self.assertNotEqual(actual_cl[-1], '\n') diff --git a/pym/repoman/utilities.py b/pym/repoman/utilities.py index 1e07bad4c..013858a6d 100644 --- a/pym/repoman/utilities.py +++ b/pym/repoman/utilities.py @@ -807,7 +807,9 @@ def UpdateChangeLog(pkgdir, user, msg, skel_path, category, package, for line in textwrap.wrap(msg, 80, \ initial_indent=' ', subsequent_indent=' '): clnew_lines.append(_unicode_decode('%s\n' % line)) - clnew_lines.append(_unicode_decode('\n')) + # Don't append a trailing newline if the file is new. + if clold_file is not None: + clnew_lines.append(_unicode_decode('\n')) f = io.open(f, mode='w', encoding=_encodings['repo.content'], errors='backslashreplace') @@ -839,9 +841,12 @@ def UpdateChangeLog(pkgdir, user, msg, skel_path, category, package, # in the unified_diff call below. clold_lines = old_header_lines + clold_lines - for line in clold_file: - f.write(line) + # Trim any trailing newlines. + lines = clold_file.readlines() clold_file.close() + while lines and lines[-1] == '\n': + del lines[-1] + f.writelines(lines) f.close() # show diff -- 2.26.2