Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 4 additions & 11 deletions scripts/lib/CIME/case/preview_namelists.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"""

from CIME.XML.standard_module_setup import *
from CIME.utils import run_sub_or_cmd, safe_copy
from CIME.utils import import_and_run_sub_or_cmd, safe_copy
import time, glob
logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -77,17 +77,10 @@ def create_namelists(self, component=None):
else:
compname = self.get_value("COMP_{}".format(model_str.upper()))
if component is None or component == model_str or compname=="ufsatm":
# first look in the case SourceMods directory
cmd = os.path.join(caseroot, "SourceMods", "src."+compname, "buildnml")
if os.path.isfile(cmd):
logger.warning("\nWARNING: Using local buildnml file {}\n".format(cmd))
else:
# otherwise look in the component config_dir
cmd = os.path.join(config_dir, "buildnml")
expect(os.path.isfile(cmd), "Could not find buildnml file for component {}".format(compname))
cmd = os.path.join(config_dir, "buildnml")
logger.info("Create namelist for component {}".format(compname))
run_sub_or_cmd(cmd, (caseroot), "buildnml",
(self, caseroot, compname), case=self)
import_and_run_sub_or_cmd(cmd, (caseroot), "buildnml",
(self, caseroot, compname), config_dir, compname, case=self)

logger.debug("Finished creating component namelists, component {} models = {}".format(component, models))

Expand Down
22 changes: 22 additions & 0 deletions scripts/lib/CIME/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,28 @@ def file_contains_python_function(filepath, funcname):

return has_function

def import_and_run_sub_or_cmd(cmd, cmdargs, subname, subargs, config_dir, compname,
logfile=None, case=None, from_dir=None, timeout=None):
sys_path_old = sys.path
sys.path.insert(1, config_dir)
try:
mod = importlib.import_module(f"{compname}_cime_py")
getattr(mod, subname)(*subargs)
except (ModuleNotFoundError, AttributeError) as _:
# * ModuleNotFoundError if importlib can not find module,
# * AttributeError if importlib finds the module but
# {subname} is not defined in the module
expect(os.path.isfile(cmd), f"Could not find {subname} file for component {compname}")
run_sub_or_cmd(cmd, cmdargs, subname, subargs, logfile, case, from_dir, timeout)
except Exception:
if logfile:
with open(logfile, "a") as log_fd:
log_fd.write(str(sys.exc_info()[1]))
expect(False, "{} FAILED, cat {}".format(cmd, logfile))
else:
raise
sys.path = sys_path_old

def run_sub_or_cmd(cmd, cmdargs, subname, subargs, logfile=None, case=None,
from_dir=None, timeout=None):
"""
Expand Down