Blame SOURCES/subscription-manager-1.10.14-8-to-subscription-manager-1.10.14-9.patch

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