public inbox for gentoo-commits@lists.gentoo.org
 help / color / mirror / Atom feed
From: "Mike Gilbert" <floppym@gentoo.org>
To: gentoo-commits@lists.gentoo.org
Subject: [gentoo-commits] proj/portage:master commit in: lib/portage/util/, lib/portage/tests/util/
Date: Tue,  7 Jun 2022 23:48:15 +0000 (UTC)	[thread overview]
Message-ID: <1654645675.c79c8898dc4ccd85829d00e7104b8e69ae8aa7f4.floppym@gentoo> (raw)

commit:     c79c8898dc4ccd85829d00e7104b8e69ae8aa7f4
Author:     David Palao <david.palao <AT> gmail <DOT> com>
AuthorDate: Wed May 25 15:23:20 2022 +0000
Commit:     Mike Gilbert <floppym <AT> gentoo <DOT> org>
CommitDate: Tue Jun  7 23:47:55 2022 +0000
URL:        https://gitweb.gentoo.org/proj/portage.git/commit/?id=c79c8898

refactor(mtimedb): MtimeDB.commit splitted into two methods

- commit itself handles only the logic of when to write to disk
- __write_to_disk performs the actual writing

Signed-off-by: David Palao <david.palao <AT> gmail.com>
Signed-off-by: Mike Gilbert <floppym <AT> gentoo.org>

 lib/portage/tests/util/test_mtimedb.py | 37 +++++++++++++++++++++++++++---
 lib/portage/util/mtimedb.py            | 41 ++++++++++++++++++----------------
 2 files changed, 56 insertions(+), 22 deletions(-)

diff --git a/lib/portage/tests/util/test_mtimedb.py b/lib/portage/tests/util/test_mtimedb.py
index e6ddf5b80..a65a6be91 100644
--- a/lib/portage/tests/util/test_mtimedb.py
+++ b/lib/portage/tests/util/test_mtimedb.py
@@ -222,13 +222,13 @@ class MtimeDBTestCase(TestCase):
         )
         for contents in all_fixtures:
             with patch(
-                'portage.util.mtimedb.open', mock_open(read_data=contents)
+                "portage.util.mtimedb.open", mock_open(read_data=contents)
             ):
                 mtimedb = MtimeDB("/path/to/mtimedb")
             self.assertLessEqual(set(mtimedb.keys()), _MTIMEDBKEYS)
 
     def test_instance_has_default_values(self):
-        with patch('portage.util.mtimedb.open',
+        with patch("portage.util.mtimedb.open",
                    mock_open(read_data=_EMPTY_FILE)):
             mtimedb = MtimeDB("/some/path/mtimedb")
         self.assertEqual(mtimedb["starttime"], 0)
@@ -238,8 +238,39 @@ class MtimeDBTestCase(TestCase):
         self.assertEqual(mtimedb["updates"], {})
 
     def test_instance_has_a_deepcopy_of_clean_data(self):
-        with patch('portage.util.mtimedb.open',
+        with patch("portage.util.mtimedb.open",
                    mock_open(read_data=_ONE_RESUME_LIST_JSON)):
             mtimedb = MtimeDB("/some/path/mtimedb")
         self.assertEqual(dict(mtimedb), dict(mtimedb._clean_data))
         self.assertIsNot(mtimedb, mtimedb._clean_data)
+
+    @patch("portage.util.mtimedb.MtimeDB._MtimeDB__write_to_disk")
+    def test_commit_writes_to_disk_if_needed_and_possible(self, pwrite2disk):
+        with patch("portage.util.mtimedb.open",
+                   mock_open(read_data=_EMPTY_FILE)):
+            mtimedb = MtimeDB("/some/path/mtimedb")
+        mtimedb.commit()
+        pwrite2disk.assert_not_called()
+        mtimedb["updates"]["/long/path/1Q-2021"] = 1739992409
+        d = {}
+        d.update(mtimedb)
+        mtimedb.commit()
+        pwrite2disk.assert_called_once_with(d)
+
+    @patch("portage.util.mtimedb.MtimeDB._MtimeDB__write_to_disk")
+    def test_commit_does_not_write_to_disk_if_no_file(self, pwrite2disk):
+        with patch("portage.util.mtimedb.open",
+                   mock_open(read_data=_EMPTY_FILE)):
+            mtimedb = MtimeDB("/some/path/mtimedb")
+        mtimedb["updates"]["/long/path/1Q-2021"] = 1739992409
+        mtimedb.filename = None
+        mtimedb.commit()
+        pwrite2disk.assert_not_called()
+
+    @patch("portage.util.mtimedb.MtimeDB._MtimeDB__write_to_disk")
+    def test_commit_does_not_write_to_disk_if_no_changes(self, pwrite2disk):
+        with patch("portage.util.mtimedb.open",
+                   mock_open(read_data=_EMPTY_FILE)):
+            mtimedb = MtimeDB("/some/path/mtimedb")
+        mtimedb.commit()
+        pwrite2disk.assert_not_called()

diff --git a/lib/portage/util/mtimedb.py b/lib/portage/util/mtimedb.py
index 3aab0b90b..a6566e3f8 100644
--- a/lib/portage/util/mtimedb.py
+++ b/lib/portage/util/mtimedb.py
@@ -120,24 +120,27 @@ class MtimeDB(dict):
         d.update(self)
         # Only commit if the internal state has changed.
         if d != self._clean_data:
-            d["version"] = str(portage.VERSION)
-            try:
-                f = atomic_ofstream(self.filename, mode="wb")
-            except EnvironmentError:
-                pass
-            else:
-                if self._json_write:
-                    f.write(
-                        _unicode_encode(
-                            json.dumps(d, **self._json_write_opts),
-                            encoding=_encodings["repo.content"],
-                            errors="strict",
-                        )
+            self.__write_to_disk(d)
+
+    def __write_to_disk(self, d):
+        d["version"] = str(portage.VERSION)
+        try:
+            f = atomic_ofstream(self.filename, mode="wb")
+        except EnvironmentError:
+            pass
+        else:
+            if self._json_write:
+                f.write(
+                    _unicode_encode(
+                        json.dumps(d, **self._json_write_opts),
+                        encoding=_encodings["repo.content"],
+                        errors="strict",
                     )
-                else:
-                    pickle.dump(d, f, protocol=2)
-                f.close()
-                apply_secpass_permissions(
-                    self.filename, uid=uid, gid=portage_gid, mode=0o644
                 )
-                self._clean_data = copy.deepcopy(d)
+            else:
+                pickle.dump(d, f, protocol=2)
+            f.close()
+            apply_secpass_permissions(
+                self.filename, uid=uid, gid=portage_gid, mode=0o644
+            )
+            self._clean_data = copy.deepcopy(d)


             reply	other threads:[~2022-06-07 23:48 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-06-07 23:48 Mike Gilbert [this message]
  -- strict thread matches above, loose matches on Subject: below --
2024-02-22 15:36 [gentoo-commits] proj/portage:master commit in: lib/portage/util/, lib/portage/tests/util/ Zac Medico
2022-12-31 13:33 Sam James
2022-06-07 23:48 Mike Gilbert
2022-06-07 23:48 Mike Gilbert
2022-06-07 23:48 Mike Gilbert
2019-01-16  8:33 Zac Medico

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1654645675.c79c8898dc4ccd85829d00e7104b8e69ae8aa7f4.floppym@gentoo \
    --to=floppym@gentoo.org \
    --cc=gentoo-commits@lists.gentoo.org \
    --cc=gentoo-dev@lists.gentoo.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox