Repository: ESMCI/cime

Issue:
configure tool doesn't honor --output-dir for the .env_mach_specific files
In testing my fix for #3553, I noticed that the standalone configure tool doesn't honor `--output-dir` for the `.env_mach_specific` files: It puts most of the content of these files in the current working directory rather than in the directory specified by --output-dir. The fix is relatively simple. I'll include this in the PR where I fix #3553 .


Discussion:

Resolving patch:
diff --git a/scripts/lib/CIME/BuildTools/configure.py b/scripts/lib/CIME/BuildTools/configure.py
--- a/scripts/lib/CIME/BuildTools/configure.py
+++ b/scripts/lib/CIME/BuildTools/configure.py
@@ -97,7 +97,7 @@ def _generate_env_mach_specific(output_dir, machobj, compiler, mpilib, debug,
         logger.warning("{} already exists, delete to replace".format(ems_path))
         return
 
-    ems_file = EnvMachSpecific(output_dir, unit_testing=unit_testing)
+    ems_file = EnvMachSpecific(output_dir, unit_testing=unit_testing, standalone_configure=True)
     ems_file.populate(machobj)
     ems_file.write()
 
@@ -107,7 +107,7 @@ def _generate_env_mach_specific(output_dir, machobj, compiler, mpilib, debug,
     fake_case = FakeCase(compiler, mpilib, debug, comp_interface)
     ems_file.load_env(fake_case)
     for shell in ('sh', 'csh'):
-        ems_file.make_env_mach_specific_file(shell, fake_case)
+        ems_file.make_env_mach_specific_file(shell, fake_case, output_dir=output_dir)
         shell_path = os.path.join(output_dir, ".env_mach_specific." + shell)
         with open(shell_path, 'a') as shell_file:
             if shell == 'sh':
diff --git a/scripts/lib/CIME/XML/env_mach_specific.py b/scripts/lib/CIME/XML/env_mach_specific.py
--- a/scripts/lib/CIME/XML/env_mach_specific.py
+++ b/scripts/lib/CIME/XML/env_mach_specific.py
@@ -15,14 +15,20 @@
 class EnvMachSpecific(EnvBase):
     # pylint: disable=unused-argument
     def __init__(self, caseroot=None, infile="env_mach_specific.xml",
-                 components=None, unit_testing=False, read_only=False):
+                 components=None, unit_testing=False, read_only=False,
+                 standalone_configure=False):
         """
         initialize an object interface to file env_mach_specific.xml in the case directory
+
+        Notes on some arguments:
+        standalone_configure: logical - whether this is being called from the standalone
+            configure utility, outside of a case
         """
         schema = os.path.join(get_cime_root(), "config", "xml_schemas", "env_mach_specific.xsd")
         EnvBase.__init__(self, caseroot, infile, schema=schema, read_only=read_only)
         self._allowed_mpi_attributes = ("compiler", "mpilib", "threaded", "unit_testing", "queue")
         self._unit_testing = unit_testing
+        self._standalone_configure = standalone_configure
 
     def populate(self, machobj):
         """Add entries to the file using information from a Machines object."""
@@ -144,7 +150,14 @@ def save_all_env_info(self, filename):
             f.write(self.list_modules())
         run_cmd_no_fail("echo -e '\n' && env", arg_stdout=filename)
 
-    def make_env_mach_specific_file(self, shell, case):
+    def make_env_mach_specific_file(self, shell, case, output_dir=''):
+        """Writes .env_mach_specific.sh or .env_mach_specific.csh
+
+        Args:
+        shell: string - 'sh' or 'csh'
+        case: case object
+        output_dir: string - path to output directory (if empty string, uses current directory)
+        """
         module_system = self.get_module_system_type()
         sh_init_cmd = self.get_module_system_init_path(shell)
         sh_mod_cmd = self.get_module_system_cmd_path(shell)
@@ -161,8 +174,12 @@ def make_env_mach_specific_file(self, shell, case):
         if "SOFTENV_LOAD" in os.environ:
             lines.append("source $SOFTENV_LOAD")
 
-        modules_to_load = self._get_modules_for_case(case)
-        envs_to_set = self._get_envs_for_case(case)
+        if self._unit_testing or self._standalone_configure:
+            job = None
+        else:
+            job = case.get_primary_job()
+        modules_to_load = self._get_modules_for_case(case, job=job)
+        envs_to_set = self._get_envs_for_case(case, job=job)
         filename = ".env_mach_specific.{}".format(shell)
         if modules_to_load is not None:
             if module_system == "module":
@@ -180,7 +197,7 @@ def make_env_mach_specific_file(self, shell, case):
                 else:
                     expect(False, "Unknown shell type: '{}'".format(shell))
 
-        with open(filename, "w") as fd:
+        with open(os.path.join(output_dir, filename), "w") as fd:
             fd.write("\n".join(lines))
 
     # Private API


Test patch: