Gentoo Archives: gentoo-commits

From: Mike Gilbert <floppym@g.o>
To: gentoo-commits@l.g.o
Subject: [gentoo-commits] proj/portage:master commit in: lib/portage/util/, lib/portage/tests/util/
Date: Tue, 07 Jun 2022 23:48:48
Message-Id: 1654645675.c79c8898dc4ccd85829d00e7104b8e69ae8aa7f4.floppym@gentoo
1 commit: c79c8898dc4ccd85829d00e7104b8e69ae8aa7f4
2 Author: David Palao <david.palao <AT> gmail <DOT> com>
3 AuthorDate: Wed May 25 15:23:20 2022 +0000
4 Commit: Mike Gilbert <floppym <AT> gentoo <DOT> org>
5 CommitDate: Tue Jun 7 23:47:55 2022 +0000
6 URL: https://gitweb.gentoo.org/proj/portage.git/commit/?id=c79c8898
7
8 refactor(mtimedb): MtimeDB.commit splitted into two methods
9
10 - commit itself handles only the logic of when to write to disk
11 - __write_to_disk performs the actual writing
12
13 Signed-off-by: David Palao <david.palao <AT> gmail.com>
14 Signed-off-by: Mike Gilbert <floppym <AT> gentoo.org>
15
16 lib/portage/tests/util/test_mtimedb.py | 37 +++++++++++++++++++++++++++---
17 lib/portage/util/mtimedb.py | 41 ++++++++++++++++++----------------
18 2 files changed, 56 insertions(+), 22 deletions(-)
19
20 diff --git a/lib/portage/tests/util/test_mtimedb.py b/lib/portage/tests/util/test_mtimedb.py
21 index e6ddf5b80..a65a6be91 100644
22 --- a/lib/portage/tests/util/test_mtimedb.py
23 +++ b/lib/portage/tests/util/test_mtimedb.py
24 @@ -222,13 +222,13 @@ class MtimeDBTestCase(TestCase):
25 )
26 for contents in all_fixtures:
27 with patch(
28 - 'portage.util.mtimedb.open', mock_open(read_data=contents)
29 + "portage.util.mtimedb.open", mock_open(read_data=contents)
30 ):
31 mtimedb = MtimeDB("/path/to/mtimedb")
32 self.assertLessEqual(set(mtimedb.keys()), _MTIMEDBKEYS)
33
34 def test_instance_has_default_values(self):
35 - with patch('portage.util.mtimedb.open',
36 + with patch("portage.util.mtimedb.open",
37 mock_open(read_data=_EMPTY_FILE)):
38 mtimedb = MtimeDB("/some/path/mtimedb")
39 self.assertEqual(mtimedb["starttime"], 0)
40 @@ -238,8 +238,39 @@ class MtimeDBTestCase(TestCase):
41 self.assertEqual(mtimedb["updates"], {})
42
43 def test_instance_has_a_deepcopy_of_clean_data(self):
44 - with patch('portage.util.mtimedb.open',
45 + with patch("portage.util.mtimedb.open",
46 mock_open(read_data=_ONE_RESUME_LIST_JSON)):
47 mtimedb = MtimeDB("/some/path/mtimedb")
48 self.assertEqual(dict(mtimedb), dict(mtimedb._clean_data))
49 self.assertIsNot(mtimedb, mtimedb._clean_data)
50 +
51 + @patch("portage.util.mtimedb.MtimeDB._MtimeDB__write_to_disk")
52 + def test_commit_writes_to_disk_if_needed_and_possible(self, pwrite2disk):
53 + with patch("portage.util.mtimedb.open",
54 + mock_open(read_data=_EMPTY_FILE)):
55 + mtimedb = MtimeDB("/some/path/mtimedb")
56 + mtimedb.commit()
57 + pwrite2disk.assert_not_called()
58 + mtimedb["updates"]["/long/path/1Q-2021"] = 1739992409
59 + d = {}
60 + d.update(mtimedb)
61 + mtimedb.commit()
62 + pwrite2disk.assert_called_once_with(d)
63 +
64 + @patch("portage.util.mtimedb.MtimeDB._MtimeDB__write_to_disk")
65 + def test_commit_does_not_write_to_disk_if_no_file(self, pwrite2disk):
66 + with patch("portage.util.mtimedb.open",
67 + mock_open(read_data=_EMPTY_FILE)):
68 + mtimedb = MtimeDB("/some/path/mtimedb")
69 + mtimedb["updates"]["/long/path/1Q-2021"] = 1739992409
70 + mtimedb.filename = None
71 + mtimedb.commit()
72 + pwrite2disk.assert_not_called()
73 +
74 + @patch("portage.util.mtimedb.MtimeDB._MtimeDB__write_to_disk")
75 + def test_commit_does_not_write_to_disk_if_no_changes(self, pwrite2disk):
76 + with patch("portage.util.mtimedb.open",
77 + mock_open(read_data=_EMPTY_FILE)):
78 + mtimedb = MtimeDB("/some/path/mtimedb")
79 + mtimedb.commit()
80 + pwrite2disk.assert_not_called()
81
82 diff --git a/lib/portage/util/mtimedb.py b/lib/portage/util/mtimedb.py
83 index 3aab0b90b..a6566e3f8 100644
84 --- a/lib/portage/util/mtimedb.py
85 +++ b/lib/portage/util/mtimedb.py
86 @@ -120,24 +120,27 @@ class MtimeDB(dict):
87 d.update(self)
88 # Only commit if the internal state has changed.
89 if d != self._clean_data:
90 - d["version"] = str(portage.VERSION)
91 - try:
92 - f = atomic_ofstream(self.filename, mode="wb")
93 - except EnvironmentError:
94 - pass
95 - else:
96 - if self._json_write:
97 - f.write(
98 - _unicode_encode(
99 - json.dumps(d, **self._json_write_opts),
100 - encoding=_encodings["repo.content"],
101 - errors="strict",
102 - )
103 + self.__write_to_disk(d)
104 +
105 + def __write_to_disk(self, d):
106 + d["version"] = str(portage.VERSION)
107 + try:
108 + f = atomic_ofstream(self.filename, mode="wb")
109 + except EnvironmentError:
110 + pass
111 + else:
112 + if self._json_write:
113 + f.write(
114 + _unicode_encode(
115 + json.dumps(d, **self._json_write_opts),
116 + encoding=_encodings["repo.content"],
117 + errors="strict",
118 )
119 - else:
120 - pickle.dump(d, f, protocol=2)
121 - f.close()
122 - apply_secpass_permissions(
123 - self.filename, uid=uid, gid=portage_gid, mode=0o644
124 )
125 - self._clean_data = copy.deepcopy(d)
126 + else:
127 + pickle.dump(d, f, protocol=2)
128 + f.close()
129 + apply_secpass_permissions(
130 + self.filename, uid=uid, gid=portage_gid, mode=0o644
131 + )
132 + self._clean_data = copy.deepcopy(d)