diff --git a/rel-eng/packages/subscription-manager b/rel-eng/packages/subscription-manager index f9393db..69983e3 100644 --- a/rel-eng/packages/subscription-manager +++ b/rel-eng/packages/subscription-manager @@ -1 +1 @@ -1.10.14-8 ./ +1.10.14-9 ./ diff --git a/src/subscription_manager/repolib.py b/src/subscription_manager/repolib.py index fa0913d..6afdfc8 100644 --- a/src/subscription_manager/repolib.py +++ b/src/subscription_manager/repolib.py @@ -15,7 +15,7 @@ # in this software or its documentation. # -from iniparse import ConfigParser +from iniparse import RawConfigParser as ConfigParser import logging import os import string @@ -542,16 +542,36 @@ class RepoFile(ConfigParser): def read(self): ConfigParser.read(self, self.path) + def _configparsers_equal(self, otherparser): + if set(otherparser.sections()) != set(self.sections()): + return False + + for section in self.sections(): + # Sometimes we end up with ints, but values must be strings to compare + current_items = dict([(str(k), str(v)) for (k, v) in self.items(section)]) + if current_items != dict(otherparser.items(section)): + return False + return True + + def _has_changed(self): + ''' + Check if the version on disk is different from what we have loaded + ''' + on_disk = ConfigParser() + on_disk.read(self.path) + return not self._configparsers_equal(on_disk) + def write(self): if not self.manage_repos: log.debug("Skipping write due to manage_repos setting: %s" % self.path) return - f = open(self.path, 'w') - tidy_writer = TidyWriter(f) - ConfigParser.write(self, tidy_writer) - tidy_writer.close() - f.close() + if self._has_changed(): + f = open(self.path, 'w') + tidy_writer = TidyWriter(f) + ConfigParser.write(self, tidy_writer) + tidy_writer.close() + f.close() def add(self, repo): self.add_section(repo.id) diff --git a/subscription-manager.spec b/subscription-manager.spec index 04a660d..cf294c7 100644 --- a/subscription-manager.spec +++ b/subscription-manager.spec @@ -14,7 +14,7 @@ Name: subscription-manager Version: 1.10.14 -Release: 8%{?dist} +Release: 9%{?dist} Summary: Tools and libraries for subscription and repository management Group: System Environment/Base License: GPLv2 @@ -419,6 +419,9 @@ fi %endif %changelog +* Wed Jun 04 2014 ckozak 1.10.14-9 +- 1104777: Don't rewrite redhat.repo unless it has changed (ckozak@redhat.com) + * Tue May 27 2014 ckozak 1.10.14-8 - 1098891: Update repos, persisting local settings when possible (ckozak@redhat.com) diff --git a/test/test_repolib.py b/test/test_repolib.py index 72505a3..cde217a 100644 --- a/test/test_repolib.py +++ b/test/test_repolib.py @@ -15,6 +15,7 @@ import unittest +from iniparse import RawConfigParser from mock import Mock, patch from StringIO import StringIO @@ -23,7 +24,8 @@ from stubs import StubCertificateDirectory, StubProductCertificate, \ StubProduct, StubEntitlementCertificate, StubContent, \ StubProductDirectory, StubUEP, StubConsumerIdentity import subscription_manager.injection as inj -from subscription_manager.repolib import Repo, UpdateAction, TidyWriter +from subscription_manager.repolib import Repo, UpdateAction, \ + TidyWriter, RepoFile from subscription_manager import repolib @@ -478,3 +480,61 @@ class TidyWriterTests(unittest.TestCase): tidy_writer.close() self.assertEquals("test stuff\n\ntest\n", output.getvalue()) + + +class RepoFileTest(unittest.TestCase): + + @patch("subscription_manager.repolib.RepoFile.create") + @patch("subscription_manager.repolib.TidyWriter") + def test_configparsers_equal(self, tidy_writer, stub_create): + rf = RepoFile() + other = RawConfigParser() + for parser in [rf, other]: + parser.add_section('test') + parser.set('test', 'key', 'val') + self.assertTrue(rf._configparsers_equal(other)) + + @patch("subscription_manager.repolib.RepoFile.create") + @patch("subscription_manager.repolib.TidyWriter") + def test_configparsers_diff_sections(self, tidy_writer, stub_create): + rf = RepoFile() + rf.add_section('new_section') + other = RawConfigParser() + for parser in [rf, other]: + parser.add_section('test') + parser.set('test', 'key', 'val') + self.assertFalse(rf._configparsers_equal(other)) + + @patch("subscription_manager.repolib.RepoFile.create") + @patch("subscription_manager.repolib.TidyWriter") + def test_configparsers_diff_item_val(self, tidy_writer, stub_create): + rf = RepoFile() + other = RawConfigParser() + for parser in [rf, other]: + parser.add_section('test') + parser.set('test', 'key', 'val') + rf.set('test', 'key', 'val2') + self.assertFalse(rf._configparsers_equal(other)) + + @patch("subscription_manager.repolib.RepoFile.create") + @patch("subscription_manager.repolib.TidyWriter") + def test_configparsers_diff_items(self, tidy_writer, stub_create): + rf = RepoFile() + other = RawConfigParser() + for parser in [rf, other]: + parser.add_section('test') + parser.set('test', 'key', 'val') + rf.set('test', 'somekey', 'val') + self.assertFalse(rf._configparsers_equal(other)) + + @patch("subscription_manager.repolib.RepoFile.create") + @patch("subscription_manager.repolib.TidyWriter") + def test_configparsers_equal_int(self, tidy_writer, stub_create): + rf = RepoFile() + other = RawConfigParser() + for parser in [rf, other]: + parser.add_section('test') + parser.set('test', 'key', 'val') + rf.set('test', 'k', 1) + other.set('test', 'k', '1') + self.assertTrue(rf._configparsers_equal(other))