33import configparser
44import os
55import pickle
6+ import warnings
67
78from compass .mpas_cores import get_mpas_cores
89from compass .config import add_config , ensure_absolute_paths
1112
1213
1314def setup_cases (tests = None , numbers = None , config_file = None , machine = None ,
14- work_dir = None , baseline_dir = None , mpas_model_path = None ):
15+ work_dir = None , baseline_dir = None , mpas_model_path = None ,
16+ cached = None ):
1517 """
1618 Set up one or more test cases
1719
@@ -20,8 +22,10 @@ def setup_cases(tests=None, numbers=None, config_file=None, machine=None,
2022 tests : list of str, optional
2123 Relative paths for a test cases to set up
2224
23- numbers : list of int, optional
24- Case numbers to setup, as listed from ``compass list``
25+ numbers : list of str, optional
26+ Case numbers to setup, as listed from ``compass list``, optionally with
27+ a suffix ``c`` to indicate that all steps in that test case should be
28+ cached
2529
2630 config_file : str, optional
2731 Configuration file with custom options for setting up and running test
@@ -41,6 +45,10 @@ def setup_cases(tests=None, numbers=None, config_file=None, machine=None,
4145 The relative or absolute path to the root of a branch where the MPAS
4246 model has been built
4347
48+ cached : list of list of str, optional
49+ For each test in ``tests``, which steps (if any) should be cached,
50+ or "_all" if all steps should be cached
51+
4452 Returns
4553 -------
4654 test_cases : dict of compass.TestCase
@@ -56,6 +64,14 @@ def setup_cases(tests=None, numbers=None, config_file=None, machine=None,
5664 if tests is None and numbers is None :
5765 raise ValueError ('At least one of tests or numbers is needed.' )
5866
67+ if cached is not None :
68+ if tests is None :
69+ warnings .warn ('Ignoring "cached" argument becasue "tests" was '
70+ 'not provided' )
71+ elif len (cached ) != len (tests ):
72+ raise ValueError ('A list of cached steps must be provided for '
73+ 'each test in "tests"' )
74+
5975 if work_dir is None :
6076 work_dir = os .getcwd ()
6177
@@ -71,18 +87,36 @@ def setup_cases(tests=None, numbers=None, config_file=None, machine=None,
7187 if numbers is not None :
7288 keys = list (all_test_cases )
7389 for number in numbers :
90+ cache_all = False
91+ if number .endswith ('c' ):
92+ cache_all = True
93+ number = int (number [:- 1 ])
94+ else :
95+ number = int (number )
96+
7497 if number >= len (keys ):
7598 raise ValueError ('test number {} is out of range. There are '
7699 'only {} tests.' .format (number , len (keys )))
77100 path = keys [number ]
78- test_cases [path ] = all_test_cases [path ]
101+ test_case = all_test_cases [path ]
102+ if cache_all :
103+ for step in test_case .steps .values ():
104+ step .cached = True
105+ test_cases [path ] = test_case
79106
80107 if tests is not None :
81- for path in tests :
108+ for index , path in enumerate ( tests ) :
82109 if path not in all_test_cases :
83110 raise ValueError ('Test case with path {} is not in '
84111 'test_cases' .format (path ))
85- test_cases [path ] = all_test_cases [path ]
112+ test_case = all_test_cases [path ]
113+ if cached is not None :
114+ step_names = cached [index ]
115+ if len (step_names ) > 0 and step_names [0 ] == '_all' :
116+ step_names = list (test_case .steps .keys ())
117+ for step_name in step_names :
118+ test_case .steps [step_name ].cached = True
119+ test_cases [path ] = test_case
86120
87121 # get the MPAS core of the first test case. We'll assume all tests are
88122 # for this core
@@ -133,6 +167,10 @@ def setup_case(path, test_case, config_file, machine, work_dir, baseline_dir,
133167 """
134168
135169 print (' {}' .format (path ))
170+ cached_steps = [step .name for step in test_case .steps .values () if step .cached ]
171+ if len (cached_steps ) > 0 :
172+ cached_steps = ' ' .join (cached_steps )
173+ print (f' steps with cached outputs: { cached_steps } ' )
136174
137175 config = configparser .ConfigParser (
138176 interpolation = configparser .ExtendedInterpolation ())
@@ -253,10 +291,12 @@ def main():
253291 help = "Relative path for a test case to set up" ,
254292 metavar = "PATH" )
255293 parser .add_argument ("-n" , "--case_number" , nargs = '+' , dest = "case_num" ,
256- type = int ,
294+ type = str ,
257295 help = "Case number(s) to setup, as listed from "
258296 "'compass list'. Can be a space-separated"
259- "list of case numbers." , metavar = "NUM" )
297+ "list of case numbers. A suffix 'c' indicates"
298+ "that all steps in the test should use cached"
299+ "outputs." , metavar = "NUM" )
260300 parser .add_argument ("-f" , "--config_file" , dest = "config_file" ,
261301 help = "Configuration file for test case setup" ,
262302 metavar = "FILE" )
@@ -274,13 +314,21 @@ def main():
274314 help = "The path to the build of the MPAS model for the "
275315 "core." ,
276316 metavar = "PATH" )
317+ parser .add_argument ("--cached" , dest = "cached" , nargs = '+' ,
318+ help = "A list of steps in the test case supplied with"
319+ "--test that should use cached outputs, or "
320+ "'_all' if all steps should be cached" ,
321+ metavar = "STEP" )
277322
278323 args = parser .parse_args (sys .argv [2 :])
324+ cached = None
279325 if args .test is None :
280326 tests = None
281327 else :
282328 tests = [args .test ]
329+ if args .cached is not None :
330+ cached = [args .cached ]
283331 setup_cases (tests = tests , numbers = args .case_num ,
284332 config_file = args .config_file , machine = args .machine ,
285333 work_dir = args .work_dir , baseline_dir = args .baseline_dir ,
286- mpas_model_path = args .mpas_model )
334+ mpas_model_path = args .mpas_model , cached = cached )
0 commit comments