Skip to content

Commit 424ed6b

Browse files
No public description
PiperOrigin-RevId: 595454485
1 parent 953275b commit 424ed6b

File tree

2 files changed

+63
-4
lines changed

2 files changed

+63
-4
lines changed

orbit/actions/export_saved_model.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,13 @@ def managed_files(self):
108108
`ExportFileManager` instance, sorted in increasing integer order of the
109109
IDs returned by `next_id_fn`.
110110
"""
111-
files = _find_managed_files(self._base_name)
112-
return [
113-
safe_normpath(os.path.join(f, self._subdirectory)) for f in files
114-
]
111+
files = []
112+
for file in _find_managed_files(self._base_name):
113+
# Normalize path and maybe add subdirectory...
114+
file = safe_normpath(os.path.join(file, self._subdirectory))
115+
if tf.io.gfile.exists(file):
116+
files.append(file)
117+
return files
115118

116119
def clean_up(self):
117120
"""Cleans up old files matching `{base_name}-*`.

orbit/actions/export_saved_model_test.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,62 @@ def next_id():
154154
step_folder = os.path.join(directory.full_path, 'basename-1000')
155155
self.assertIn(subdirectory, tf.io.gfile.listdir(step_folder))
156156

157+
def test_export_file_manager_with_suffix_second_cleanup_succeeds(self):
158+
directory = self.create_tempdir()
159+
base_name = os.path.join(directory.full_path, 'basename')
160+
161+
id_num = 0
162+
163+
def next_id():
164+
return id_num
165+
166+
subdirectory = 'sub'
167+
168+
manager = actions.ExportFileManager(
169+
base_name, max_to_keep=2, next_id_fn=next_id, subdirectory=subdirectory
170+
)
171+
id_num = 30
172+
directory.create_file(manager.next_name())
173+
id_num = 200
174+
directory.create_file(manager.next_name())
175+
id_num = 1000
176+
directory.create_file(manager.next_name())
177+
manager.clean_up() # Should delete file with lowest ID.
178+
# Note that the base folder is intact, only the suffix folder is deleted.
179+
self.assertEqual(
180+
_id_sorted_file_base_names(directory.full_path),
181+
['basename-30', 'basename-200', 'basename-1000'],
182+
)
183+
# Verify that the suffix folder has been deleted from the lowest ID
184+
# but not from the others.
185+
self.assertEmpty(
186+
tf.io.gfile.listdir(os.path.join(directory.full_path, 'basename-30'))
187+
)
188+
self.assertNotEmpty(
189+
tf.io.gfile.listdir(os.path.join(directory.full_path, 'basename-200'))
190+
)
191+
self.assertNotEmpty(
192+
tf.io.gfile.listdir(os.path.join(directory.full_path, 'basename-1000'))
193+
)
194+
# Add another ID, run clean_up again and verify that it worked.
195+
id_num = 2000
196+
directory.create_file(manager.next_name())
197+
manager.clean_up() # Should delete file with lowest ID.
198+
# Verify that the suffix folder has been deleted from the two lowest ID
199+
# directories but not from the others.
200+
self.assertEmpty(
201+
tf.io.gfile.listdir(os.path.join(directory.full_path, 'basename-30'))
202+
)
203+
self.assertEmpty(
204+
tf.io.gfile.listdir(os.path.join(directory.full_path, 'basename-200'))
205+
)
206+
self.assertNotEmpty(
207+
tf.io.gfile.listdir(os.path.join(directory.full_path, 'basename-1000'))
208+
)
209+
self.assertNotEmpty(
210+
tf.io.gfile.listdir(os.path.join(directory.full_path, 'basename-2000'))
211+
)
212+
157213
def test_export_file_manager_managed_files(self):
158214
directory = self.create_tempdir()
159215
directory.create_file('basename-5')

0 commit comments

Comments
 (0)