Skip to content

Commit 6e69bba

Browse files
authored
Clean up cache dir at end of expedition (#291)
* clean up cache at end of expedition when difficulty-level is easy * test that cache is deleted when difficulty level is easy
1 parent 41b675b commit 6e69bba

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

src/virtualship/cli/_run.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ def _run(
8383
expedition = _get_expedition(expedition_dir)
8484

8585
# unique id to determine if an expedition has 'changed' since last run (to avoid re-selecting problems when user makes tweaks to schedule to deal with problems encountered)
86-
expedition_id = _unique_id(expedition, expedition_dir)
86+
cache_dir = expedition_dir.joinpath(CACHE)
87+
expedition_id = _unique_id(expedition, cache_dir)
8788

8889
# dedicated problems directory for this expedition
8990
problems_dir = expedition_dir.joinpath(
@@ -234,6 +235,10 @@ def _run(
234235
if os.path.exists(expedition_dir.joinpath(CHECKPOINT)):
235236
os.remove(expedition_dir.joinpath(CHECKPOINT))
236237

238+
# delete cache dir if when --difficulty-level is 'easy' (no useful information to cache in this case, and can interfere with re-runs)
239+
if difficulty_level == "easy" and os.path.exists(cache_dir):
240+
shutil.rmtree(cache_dir)
241+
237242
print("\n------------- END -------------\n")
238243

239244
# end timing
@@ -242,7 +247,7 @@ def _run(
242247
print(f"[TIMER] Expedition completed in {elapsed / 60.0:.2f} minutes.")
243248

244249

245-
def _unique_id(expedition: Expedition, expedition_dir: Path) -> str:
250+
def _unique_id(expedition: Expedition, cache_dir: Path) -> str:
246251
"""
247252
Return a unique id for the expedition (marked by datetime), which can be used to determine whether the expedition has 'changed' since the last run.
248253
@@ -252,7 +257,6 @@ def _unique_id(expedition: Expedition, expedition_dir: Path) -> str:
252257
new_id = datetime.now().strftime("%Y%m%d%H%M%S")
253258
previous_id = None
254259

255-
cache_dir = expedition_dir.joinpath(CACHE)
256260
if not cache_dir.exists():
257261
cache_dir.mkdir()
258262
id_path = cache_dir.joinpath(EXPEDITION_IDENTIFIER)

tests/cli/test_run.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ def execute(self, measurements, out_path):
3333

3434
def test_run(tmp_path, monkeypatch):
3535
"""Testing as if using pre-downloaded, local data."""
36+
difficulty_level = "easy"
37+
3638
expedition_dir = tmp_path / "expedition_dir"
3739
expedition_dir.mkdir()
3840
(expedition_dir / EXPEDITION).write_text(get_example_expedition())
@@ -54,7 +56,7 @@ def test_run(tmp_path, monkeypatch):
5456
fake_data_dir.mkdir()
5557

5658
_run(
57-
expedition_dir, difficulty_level="easy", from_data=fake_data_dir
59+
expedition_dir, difficulty_level=difficulty_level, from_data=fake_data_dir
5860
) # problems turned off here
5961

6062
results_dir = expedition_dir / "results"
@@ -64,3 +66,8 @@ def test_run(tmp_path, monkeypatch):
6466
assert cost_file.exists()
6567
content = cost_file.read_text()
6668
assert "cost:" in content
69+
70+
# check cache dir is deleted at end of expedition when difficulty-level is easy
71+
if difficulty_level == "easy":
72+
cache_dir = expedition_dir / "cache"
73+
assert not cache_dir.exists()

0 commit comments

Comments
 (0)