66import logging
77import re
88import subprocess
9- from pathlib import Path
109
1110import requests
11+ from platformdirs import user_cache_path
1212
1313logger = logging .getLogger (__name__ )
1414
15+ TEST_ASSET_CACHE_DIR = user_cache_path ("PROCESS-regression-tests" , "ukaea" )
16+
1517
1618@dataclasses .dataclass
1719class TrackedMFile :
@@ -28,9 +30,7 @@ def __init__(self) -> None:
2830 self ._hashes = self ._git_commit_hashes ()
2931 self ._tracked_mfiles = self ._get_tracked_mfiles ()
3032
31- def get_reference_mfile (
32- self , scenario_name : str , directory : Path , target_hash : str | None = None
33- ):
33+ def get_reference_mfile (self , scenario_name : str , target_hash : str | None = None ):
3434 """Finds the most recent reference MFile for `<scenario_name>.IN.DAT`
3535 and downloads it to the `directory` with the name `ref.<scenario_name>.MFILE.DAT`.
3636
@@ -50,17 +50,29 @@ def get_reference_mfile(
5050 `None` is returned.
5151 :rtype: Path
5252 """
53- reference_mfile_location = directory / f"ref. { scenario_name } .MFILE.DAT"
53+
5454 for mf in self ._tracked_mfiles :
5555 if (mf .scenario_name == scenario_name and target_hash is None ) or (
5656 mf .scenario_name == scenario_name and target_hash == mf .hash
5757 ):
58- Path (reference_mfile_location ).write_text (
58+ cache_directory = TEST_ASSET_CACHE_DIR / mf .hash
59+ cached_location = cache_directory / f"ref.{ scenario_name } .MFILE.DAT"
60+
61+ if cached_location .exists ():
62+ logger .info (
63+ f"Using cached reference MFile ({ cached_location } ) found for commit { mf .hash } ."
64+ )
65+ return cached_location
66+
67+ cache_directory .mkdir (parents = True , exist_ok = True )
68+ cached_location .write_text (
5969 requests .get (mf .download_link ).content .decode ()
6070 )
6171
62- logger .info (f"Reference MFile found for commit { mf .hash } " )
63- return reference_mfile_location
72+ logger .info (
73+ f"Reference MFile found for commit { mf .hash } . Writing to { cached_location } "
74+ )
75+ return cached_location
6476
6577 return None
6678
0 commit comments