Repository: ESMCI/cime

Issue:
cprnc build fails when running suite of tests 
I keep getting failures because more than one test is trying to create the directory at the same time - around line 278 of build.py


Discussion:

Resolving patch:
diff --git a/scripts/lib/CIME/SystemTests/pre.py b/scripts/lib/CIME/SystemTests/pre.py
--- a/scripts/lib/CIME/SystemTests/pre.py
+++ b/scripts/lib/CIME/SystemTests/pre.py
@@ -76,6 +76,7 @@ def run_phase(self): # pylint: disable=arguments-differ
         logger = logging.getLogger(__name__)
         self._activate_case1()
         rundir1 = self._case.get_value("RUNDIR")
+        self._cprnc_exe = self._case.get_value("CCSM_CPRNC")
         self._activate_case2()
         rundir2 = self._case.get_value("RUNDIR")
         compare_ok = True
@@ -113,7 +114,7 @@ def run_phase(self): # pylint: disable=arguments-differ
                 expect((len(restart_files_2) == 1),
                        "Missing case2 restart file, {}", glob_str)
                 rfile2 = restart_files_2[0]
-                ok = cprnc(comp, rfile1, rfile2, self._case, rundir2)[0]
+                ok = cprnc(comp, rfile1, rfile2, self._case, rundir2, cprnc_exe=self._cprnc_exe)[0]
                 logger.warning("CPRNC result for {}: {}".format(os.path.basename(rfile1), "PASS" if (ok == should_match) else "FAIL"))
                 compare_ok = compare_ok and (should_match == ok)
 
diff --git a/scripts/lib/CIME/build.py b/scripts/lib/CIME/build.py
--- a/scripts/lib/CIME/build.py
+++ b/scripts/lib/CIME/build.py
@@ -251,14 +251,19 @@ def _build_libraries(case, exeroot, sharedpath, caseroot, cimeroot, libroot, lid
     if uses_kokkos(case):
         libs.append("kokkos")
 
+    sharedlibroot = os.path.abspath(case.get_value("SHAREDLIBROOT"))
     # Check if we need to build our own cprnc
     if case.get_value("TEST"):
         cprnc_loc = case.get_value("CCSM_CPRNC")
+        full_lib_path = os.path.join(sharedlibroot, compiler, "cprnc")
         if not cprnc_loc or not os.path.exists(cprnc_loc):
-            libs.append("cprnc")
+            case.set_value("CCSM_CPRNC", os.path.join(full_lib_path, "cprnc"))
+            if not os.path.isdir(full_lib_path):
+                os.makedirs(full_lib_path)
+                libs.append("cprnc")
+
 
     logs = []
-    sharedlibroot = os.path.abspath(case.get_value("SHAREDLIBROOT"))
     for lib in libs:
         if buildlist is not None and lib not in buildlist:
             continue
@@ -270,11 +275,11 @@ def _build_libraries(case, exeroot, sharedpath, caseroot, cimeroot, libroot, lid
             full_lib_path = os.path.join(sharedlibroot, sharedpath, "mct", lib)
         elif lib == "cprnc":
             full_lib_path = os.path.join(sharedlibroot, compiler, "cprnc")
-            case.set_value("CCSM_CPRNC", os.path.join(full_lib_path, "cprnc"))
         else:
             full_lib_path = os.path.join(sharedlibroot, sharedpath, lib)
+
         # pio build creates its own directory
-        if (lib != "pio" and not os.path.exists(full_lib_path)):
+        if (lib != "pio" and not os.path.isdir(full_lib_path)):
             os.makedirs(full_lib_path)
 
         file_build = os.path.join(exeroot, "{}.bldlog.{}".format(lib, lid))
diff --git a/scripts/lib/CIME/hist_utils.py b/scripts/lib/CIME/hist_utils.py
--- a/scripts/lib/CIME/hist_utils.py
+++ b/scripts/lib/CIME/hist_utils.py
@@ -302,7 +302,7 @@ def compare_test(case, suffix1, suffix2, ignore_fieldlist_diffs=False):
                           ignore_fieldlist_diffs=ignore_fieldlist_diffs)
 
 def cprnc(model, file1, file2, case, rundir, multiinst_driver_compare=False, outfile_suffix="",
-          ignore_fieldlist_diffs=False):
+          ignore_fieldlist_diffs=False, cprnc_exe=None):
     """
     Run cprnc to compare two individual nc files
 
@@ -322,7 +322,8 @@ def cprnc(model, file1, file2, case, rundir, multiinst_driver_compare=False, out
         where 'comment' is either an empty string or one of the module-level constants
         beginning with CPRNC_ (e.g., CPRNC_FIELDLISTS_DIFFER)
     """
-    cprnc_exe = case.get_value("CCSM_CPRNC")
+    if not cprnc_exe:
+        cprnc_exe = case.get_value("CCSM_CPRNC")
     basename = os.path.basename(file1)
     multiinst_regex = re.compile(r'.*%s[^_]*(_[0-9]{4})[.]h.?[.][^.]+?[.]nc' % model)
     mstr = ''


Test patch: