Repository: ESMCI/cime

Issue:
Extraneous warnings when creating a case: "Machine object has no machine defined"
When creating a case where the machine is not explicitly defined, the warning "Machine object has no machine defined" is sometimes generated numerous times (once on cheyenne, four times on my Mac). From inserting some debugging statements, it looks like this is generated after each attempt to lookup a machine with the new `$ENV` pattern introduced in #4106. I'm not sure this warning actually indicates a problem, but getting these extra warnings when creating a case can be confusing.

I think I understand why this is happening, but I'm not sure what the right solution is: I think the issue is that the `__init__` method in `Machines` calls `probe_machine_name`, which calls `_probe_machine_name_one_guess`, which calls `get_resolved_value` which calls `get_value`, all before `self.set_machine` is called (which is done at the end of the `__init__` method) – and since `self.set_machine` hasn't been called yet, `self.machine_node` is `None`, triggering the warning in `get_value`.



Discussion:
@billsacks Thanks, you're spot on with the analysis. The logic looks fine, and as you pointed out the warning doesn't really indicate a problem. I'm inclined to just change this to a debug statement.
Sorry, I thought that I had fixed this already, but I forgot to commit it.
My change was to change the logger.warning to logger.debug in machines.py
Sounds good, thanks guys.
@billsacks @jedwards4b I think I found another issue with detecting a machine, so I'll bundle these together in a PR.

Resolving patch:
diff --git a/scripts/lib/CIME/XML/machines.py b/scripts/lib/CIME/XML/machines.py
--- a/scripts/lib/CIME/XML/machines.py
+++ b/scripts/lib/CIME/XML/machines.py
@@ -4,7 +4,7 @@
 from CIME.XML.standard_module_setup import *
 from CIME.XML.generic_xml import GenericXML
 from CIME.XML.files import Files
-from CIME.utils import convert_to_unknown_type, get_cime_config, get_all_cime_models
+from CIME.utils import convert_to_unknown_type, get_cime_config
 
 import socket
 
@@ -32,7 +32,7 @@ def __init__(self, infile=None, files=None, machine=None, extra_machines_dir=Non
         self.extra_machines_dir = extra_machines_dir
 
         schema = None
-        supported_models = []
+        checked_files = []
         if files is None:
             files = Files()
         if infile is None:
@@ -43,6 +43,7 @@ def __init__(self, infile=None, files=None, machine=None, extra_machines_dir=Non
         self.machines_dir = os.path.dirname(infile)
 
         GenericXML.__init__(self, infile, schema)
+        checked_files.append(infile)
 
         # Append the contents of $HOME/.cime/config_machines.xml if it exists.
         #
@@ -54,13 +55,17 @@ def __init__(self, infile=None, files=None, machine=None, extra_machines_dir=Non
             os.environ.get("HOME"), ".cime", "config_machines.xml"
         )
         logger.debug("Infile: {}".format(local_infile))
+
         if os.path.exists(local_infile):
             GenericXML.read(self, local_infile, schema)
+            checked_files.append(local_infile)
+
         if extra_machines_dir:
             local_infile = os.path.join(extra_machines_dir, "config_machines.xml")
             logger.debug("Infile: {}".format(local_infile))
             if os.path.exists(local_infile):
                 GenericXML.read(self, local_infile, schema)
+                checked_files.append(local_infile)
 
         if machine is None:
             if "CIME_MACHINE" in os.environ:
@@ -71,26 +76,10 @@ def __init__(self, infile=None, files=None, machine=None, extra_machines_dir=Non
                     machine = cime_config.get("main", "machine")
                 if machine is None:
                     machine = self.probe_machine_name()
-                    if machine is None:
-                        for potential_model in get_all_cime_models():
-                            local_infile = os.path.join(
-                                get_cime_root(),
-                                "config",
-                                potential_model,
-                                "machines",
-                                "config_machines.xml",
-                            )
-                            if local_infile != infile:
-                                GenericXML.read(self, local_infile, schema)
-                                if self.probe_machine_name() is not None:
-                                    supported_models.append(potential_model)
-                                GenericXML.change_file(self, infile, schema)
 
         expect(
             machine is not None,
-            "Could not initialize machine object from {} or {}. This machine is not available for the target CIME_MODEL. The supported CIME_MODELS that can be used are: {}".format(
-                infile, local_infile, supported_models
-            ),
+            f"Could not initialize machine object from {', '.join(checked_files)}. This machine is not available for the target CIME_MODEL.",
         )
         self.set_machine(machine)
 
@@ -165,7 +154,7 @@ def probe_machine_name(self, warn=True):
                 names_not_found_quoted = ["'" + name + "'" for name in names_not_found]
                 names_not_found_str = " or ".join(names_not_found_quoted)
                 if warn:
-                    logger.warning(
+                    logger.debug(
                         "Could not find machine match for {}".format(
                             names_not_found_str
                         )
@@ -253,7 +242,7 @@ def get_value(self, name, attributes=None, resolved=True, subgroup=None):
         Get Value of fields in the config_machines.xml file
         """
         if self.machine_node is None:
-            logger.warning("Machine object has no machine defined")
+            logger.debug("Machine object has no machine defined")
             return None
 
         expect(subgroup is None, "This class does not support subgroups")


Test patch: