Skip to content

Commit

Permalink
Merge pull request #252 from ESMCI/santos/case-context-manager
Browse files Browse the repository at this point in the history
Make `Case` a context manager.
  • Loading branch information
jgfouca authored Jul 14, 2016
2 parents c52b65c + 215987a commit c10289e
Show file tree
Hide file tree
Showing 30 changed files with 309 additions and 294 deletions.
4 changes: 2 additions & 2 deletions cime_config/acme/machines/template.case.run
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ def _main_func(description):
sys.exit(1 if test_results.failed > 0 else 0)

caseroot = parse_command_line(sys.argv, description)
case = Case(caseroot)
success = case_run(case)
with Case(caseroot, read_only=False) as case:
success = case_run(case)

sys.exit(0 if success else 1)

Expand Down
4 changes: 2 additions & 2 deletions cime_config/acme/machines/template.case.test
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ def _main_func(description):
sys.exit(1 if test_results.failed > 0 else 0)

caseroot, testname = parse_command_line(sys.argv, description)
case = Case(caseroot)
success = case_test(case, testname)
with Case(caseroot, read_only=False) as case:
success = case_test(case, testname)

sys.exit(0 if success else 1)

Expand Down
4 changes: 2 additions & 2 deletions cime_config/acme/machines/template.lt_archive
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ def _main_func(description):
sys.exit(1 if test_results.failed > 0 else 0)

caseroot = parse_command_line(sys.argv, description)
case = Case(caseroot)
success = case_lt_archive(case)
with Case(caseroot) as case:
success = case_lt_archive(case)

sys.exit(0 if success else 1)

Expand Down
4 changes: 2 additions & 2 deletions cime_config/acme/machines/template.st_archive
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ def _main_func(description):
sys.exit(1 if test_results.failed > 0 else 0)

caseroot = parse_command_line(sys.argv, description)
case = Case(caseroot)
success = case_st_archive(case)
with Case(caseroot, read_only=False) as case:
success = case_st_archive(case)

sys.exit(0 if success else 1)

Expand Down
4 changes: 2 additions & 2 deletions cime_config/cesm/machines/template.case.run
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ def _main_func(description):
sys.exit(1 if test_results.failed > 0 else 0)

caseroot = parse_command_line(sys.argv, description)
case = Case(caseroot)
success = case_run(case)
with Case(caseroot, read_only=False) as case:
success = case_run(case)

sys.exit(0 if success else 1)

Expand Down
4 changes: 2 additions & 2 deletions cime_config/cesm/machines/template.case.test
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ def _main_func(description):
sys.exit(1 if test_results.failed > 0 else 0)

caseroot, testname = parse_command_line(sys.argv, description)
case = Case(caseroot)
success = case_test(case, testname)
with Case(caseroot, read_only=False) as case:
success = case_test(case, testname)

sys.exit(0 if success else 1)

Expand Down
4 changes: 2 additions & 2 deletions cime_config/cesm/machines/template.lt_archive
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ def _main_func(description):
sys.exit(1 if test_results.failed > 0 else 0)

caseroot = parse_command_line(sys.argv, description)
case = Case(caseroot)
success = case_lt_archive(case)
with Case(caseroot) as case:
success = case_lt_archive(case)

sys.exit(0 if success else 1)

Expand Down
4 changes: 2 additions & 2 deletions cime_config/cesm/machines/template.st_archive
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ def _main_func(description):
sys.exit(1 if test_results.failed > 0 else 0)

caseroot = parse_command_line(sys.argv, description)
case = Case(caseroot)
success = case_st_archive(case)
with Case(caseroot, read_only=False) as case:
success = case_st_archive(case)

sys.exit(0 if success else 1)

Expand Down
18 changes: 8 additions & 10 deletions driver_cpl/cime_config/buildexe
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,14 @@ def _main_func():

logger.info("Building a single executable version of target coupled model")

case = Case(caseroot)

casetools = case.get_value("CASETOOLS")
cimeroot = case.get_value("CIMEROOT")
exeroot = case.get_value("EXEROOT")
gmake = case.get_value("GMAKE")
gmake_j = case.get_value("GMAKE_J")
model = case.get_value("MODEL")

os.environ["PIO_VERSION"] = str(case.get_value("PIO_VERSION"))
with Case(caseroot) as case:
casetools = case.get_value("CASETOOLS")
cimeroot = case.get_value("CIMEROOT")
exeroot = case.get_value("EXEROOT")
gmake = case.get_value("GMAKE")
gmake_j = case.get_value("GMAKE_J")
model = case.get_value("MODEL")
os.environ["PIO_VERSION"] = str(case.get_value("PIO_VERSION"))

with open('Filepath', 'w') as out:
out.write(os.path.join(caseroot, "SourceMods", "src.drv") + "\n")
Expand Down
70 changes: 34 additions & 36 deletions driver_cpl/cime_config/buildnml
Original file line number Diff line number Diff line change
Expand Up @@ -23,42 +23,40 @@ def _main_func():

caseroot = parse_input(sys.argv)

case = Case(caseroot)

cimeroot = case.get_value("CIMEROOT")
caseroot = case.get_value("CASEROOT")
casebuild = case.get_value("CASEBUILD")
rundir = case.get_value("RUNDIR")
grid = case.get_value("GRID")
atm_grid = case.get_value("ATM_GRID")
lnd_grid = case.get_value("LND_GRID")
ocn_grid = case.get_value("OCN_GRID")
rof_grid = case.get_value("ROF_GRID")
wav_grid = case.get_value("WAV_GRID")
comp_atm = case.get_value("COMP_ATM")
pio_version = case.get_value("PIO_VERSION")

confdir = os.path.join(casebuild, "cplconf")
if not os.path.isdir(confdir):
os.makedirs(confdir)

# create cplconf/namelist

infile_text = ""
if comp_atm == 'cam':
# *** FIXME - this is confusing!!!***
# cam is actually changing the driver namelist settings
cam_config_opts = case.get_value("CAM_CONFIG_OPTS")
if "adiabatic" in cam_config_opts:
infile_text = "atm_adiabatic = .true."
if "ideal" in cam_config_opts:
infile_text = "atm_ideal_phys = .true."
if "aquaplanet" in cam_config_opts:
infile_text = "aqua_planet = .true. \n aqua_planet_sst = 1"

user_nl_file = os.path.join(caseroot, "user_nl_cpl")
namelist_infile = os.path.join(confdir, "namelist")
create_namelist_infile(case, user_nl_file, namelist_infile, infile_text)
with Case(caseroot) as case:
cimeroot = case.get_value("CIMEROOT")
caseroot = case.get_value("CASEROOT")
casebuild = case.get_value("CASEBUILD")
rundir = case.get_value("RUNDIR")
grid = case.get_value("GRID")
atm_grid = case.get_value("ATM_GRID")
lnd_grid = case.get_value("LND_GRID")
ocn_grid = case.get_value("OCN_GRID")
rof_grid = case.get_value("ROF_GRID")
wav_grid = case.get_value("WAV_GRID")
comp_atm = case.get_value("COMP_ATM")
pio_version = case.get_value("PIO_VERSION")

confdir = os.path.join(casebuild, "cplconf")
if not os.path.isdir(confdir):
os.makedirs(confdir)

# create cplconf/namelist
infile_text = ""
if comp_atm == 'cam':
# *** FIXME - this is confusing!!!***
# cam is actually changing the driver namelist settings
cam_config_opts = case.get_value("CAM_CONFIG_OPTS")
if "adiabatic" in cam_config_opts:
infile_text = "atm_adiabatic = .true."
if "ideal" in cam_config_opts:
infile_text = "atm_ideal_phys = .true."
if "aquaplanet" in cam_config_opts:
infile_text = "aqua_planet = .true. \n aqua_planet_sst = 1"

user_nl_file = os.path.join(caseroot, "user_nl_cpl")
namelist_infile = os.path.join(confdir, "namelist")
create_namelist_infile(case, user_nl_file, namelist_infile, infile_text)

# call build-namelist

Expand Down
47 changes: 24 additions & 23 deletions scripts/Tools/case.build
Original file line number Diff line number Diff line change
Expand Up @@ -91,29 +91,30 @@ def _main_func(description):
caseroot, sharedlib_only, model_only, cleanlist = parse_command_line(sys.argv, description)
logging.info("calling build.case_build with caseroot=%s" %caseroot)

case = Case(caseroot)
testname = case.get_value('TESTCASE')

if cleanlist:
build.clean(case, cleanlist)
elif(testname is not None):
logging.warn("Building test for %s in directory %s" % (testname, caseroot))
try:
test = globals()[testname](case)
except KeyError:
expect(False, "Could not find a test called '%s'" % testname)
append_status("case.testbuild starting ",
caseroot=caseroot,sfile="CaseStatus")
test.build(sharedlib_only=sharedlib_only, model_only=model_only)
append_status("case.testbuild complete",
caseroot=caseroot,sfile="CaseStatus")
else:
append_status("case.build starting",
caseroot=caseroot,sfile="CaseStatus")
build.case_build(caseroot, case=case, sharedlib_only=sharedlib_only,
model_only=model_only)
append_status("case.build complete",
caseroot=caseroot,sfile="CaseStatus")
with Case(caseroot, read_only=False) as case:
testname = case.get_value('TESTCASE')

if cleanlist:
build.clean(case, cleanlist)
elif(testname is not None):
logging.warn("Building test for %s in directory %s" %
(testname, caseroot))
try:
test = globals()[testname](case)
except KeyError:
expect(False, "Could not find a test called '%s'" % testname)
append_status("case.testbuild starting ",
caseroot=caseroot,sfile="CaseStatus")
test.build(sharedlib_only=sharedlib_only, model_only=model_only)
append_status("case.testbuild complete",
caseroot=caseroot,sfile="CaseStatus")
else:
append_status("case.build starting",
caseroot=caseroot,sfile="CaseStatus")
build.case_build(caseroot, case=case, sharedlib_only=sharedlib_only,
model_only=model_only)
append_status("case.build complete",
caseroot=caseroot,sfile="CaseStatus")

if __name__ == "__main__":
_main_func(__doc__)
5 changes: 2 additions & 3 deletions scripts/Tools/case.setup
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,8 @@ def _main_func(description):
sys.exit(1 if test_results.failed > 0 else 0)

caseroot, clean, test_mode, reset = parse_command_line(sys.argv, description)
case = Case(caseroot)

case_setup(case, clean, test_mode, reset)
with Case(caseroot, read_only=False) as case:
case_setup(case, clean, test_mode, reset)

if __name__ == "__main__":
_main_func(__doc__)
5 changes: 2 additions & 3 deletions scripts/Tools/case.submit
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,8 @@ def _main_func(description):
sys.exit(1 if test_results.failed > 0 else 0)

caseroot, job, no_batch, prereq = parse_command_line(sys.argv, description)
case = Case(caseroot)

submit(case, job=job, no_batch=no_batch, prereq_jobid=prereq)
with Case(caseroot, read_only=False) as case:
submit(case, job=job, no_batch=no_batch, prereq_jobid=prereq)

if __name__ == "__main__":
_main_func(__doc__)
6 changes: 3 additions & 3 deletions scripts/Tools/check_case
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ def _main_func(description):

check_lockedfiles()

case = Case()
preview_namelists(case)
with Case(read_only=False) as case:
preview_namelists(case)
build_complete = case.get_value("BUILD_COMPLETE")

build_complete = case.get_value("BUILD_COMPLETE")
if not build_complete:
expect(False,
"Please rebuild the model interactively by calling case.build")
Expand Down
9 changes: 7 additions & 2 deletions scripts/Tools/check_input_data
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ from standard_script_setup import *
from CIME.utils import expect, get_model, run_cmd
from CIME.XML.machines import Machines
from CIME.check_input_data import check_input_data, SVN_LOCS
from CIME.case import Case

import argparse, doctest, fnmatch

Expand Down Expand Up @@ -64,8 +65,12 @@ def _main_func(description):

svn_loc, input_data_root, data_list_dir, download = parse_command_line(sys.argv, description)

sys.exit(0 if check_input_data(svn_loc=svn_loc, input_data_root=input_data_root,
data_list_dir=data_list_dir, download=download) else 1)
with Case() as case:
sys.exit(0 if check_input_data(case,
svn_loc=svn_loc,
input_data_root=input_data_root,
data_list_dir=data_list_dir,
download=download) else 1)

###############################################################################

Expand Down
Loading

0 comments on commit c10289e

Please sign in to comment.