prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  Missing chem_mech files in E3SM CaseDocs after renaming CAM to EAM
  After [renaming CAM to EAM in E3SM](https://github.com/E3SM-Project/E3SM/pull/3845), the following two files are not copied to CaseDocs
  ```
  chem_mech.doc
  chem_mech.in
  ```
  Need to change the 'cam' substring in 'camconf' near the end of cime/scripts/lib/CIME/case/preview_namelists.py. The piece of codes are copied below
  ```
  # Copy over chemistry mechanism docs if they exist
      if (os.path.isdir(os.path.join(casebuild, "camconf"))):
          for file_to_copy in glob.glob(os.path.join(casebuild, "camconf", "*chem_mech*")):
              safe_copy(file_to_copy, docdir)
  ```
  To make it work for both cam and eam, need help to replace the substring 'cam' with the atm COMP_NAME.  Thanks.
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `scripts/lib/CIME/case/preview_namelists.py`
  Content:
  ```
  1 """
  2 API for preview namelist
  3 create_dirs and create_namelists are members of Class case from file case.py
  4 """
  5 
  6 from CIME.XML.standard_module_setup import *
  7 from CIME.utils import run_sub_or_cmd, safe_copy
  8 import time, glob
  9 logger = logging.getLogger(__name__)
  10 
  11 def create_dirs(self):
  12     """
  13     Make necessary directories for case
  14     """
  15     # Get data from XML
  16     exeroot  = self.get_value("EXEROOT")
  17     libroot  = self.get_value("LIBROOT")
  18     incroot  = self.get_value("INCROOT")
  19     rundir   = self.get_value("RUNDIR")
  20     caseroot = self.get_value("CASEROOT")
  21     docdir = os.path.join(caseroot, "CaseDocs")
  22     dirs_to_make = []
  23     models = self.get_values("COMP_CLASSES")
  24     for model in models:
  25         dirname = model.lower()
  26         dirs_to_make.append(os.path.join(exeroot, dirname, "obj"))
  27 
  28     dirs_to_make.extend([exeroot, libroot, incroot, rundir, docdir])
  29 
  30     for dir_to_make in dirs_to_make:
  31         if (not os.path.isdir(dir_to_make) and not os.path.islink(dir_to_make)):
  32             try:
  33                 logger.debug("Making dir '{}'".format(dir_to_make))
  34                 os.makedirs(dir_to_make)
  35             except OSError as e:
  36                 # In a multithreaded situation, we may have lost a race to create this dir.
  37                 # We do not want to crash if that's the case.
  38                 if not os.path.isdir(dir_to_make):
  39                     expect(False, "Could not make directory '{}', error: {}".format(dir_to_make, e))
  40 
  41     # As a convenience write the location of the case directory in the bld and run directories
  42     for dir_ in (exeroot, rundir):
  43         with open(os.path.join(dir_,"CASEROOT"),"w+") as fd:
  44             fd.write(caseroot+"\n")
  45 
  46 def create_namelists(self, component=None):
  47     """
  48     Create component namelists
  49     """
  50     self.flush()
  51 
  52     create_dirs(self)
  53 
  54     casebuild = self.get_value("CASEBUILD")
  55     caseroot = self.get_value("CASEROOT")
  56     rundir = self.get_value("RUNDIR")
  57 
  58     docdir = os.path.join(caseroot, "CaseDocs")
  59 
  60     # Load modules
  61     self.load_env()
  62 
  63     self.stage_refcase()
  64 
  65     # Create namelists - must have cpl last in the list below
  66     # Note - cpl must be last in the loop below so that in generating its namelist,
  67     # it can use xml vars potentially set by other component's buildnml scripts
  68     models = self.get_values("COMP_CLASSES")
  69     models += [models.pop(0)]
  70     for model in models:
  71         model_str = model.lower()
  72         logger.info("  {} {} ".format(time.strftime("%Y-%m-%d %H:%M:%S"),model_str))
  73         config_file = self.get_value("CONFIG_{}_FILE".format(model_str.upper()))
  74         config_dir = os.path.dirname(config_file)
  75         if model_str == "cpl":
  76             compname = "drv"
  77         else:
  78             compname = self.get_value("COMP_{}".format(model_str.upper()))
  79         if component is None or component == model_str or compname=="ufsatm":
  80             # first look in the case SourceMods directory
  81             cmd = os.path.join(caseroot, "SourceMods", "src."+compname, "buildnml")
  82             if os.path.isfile(cmd):
  83                 logger.warning("\nWARNING: Using local buildnml file {}\n".format(cmd))
  84             else:
  85                 # otherwise look in the component config_dir
  86                 cmd = os.path.join(config_dir, "buildnml")
  87             expect(os.path.isfile(cmd), "Could not find buildnml file for component {}".format(compname))
  88             logger.info("Create namelist for component {}".format(compname))
  89             run_sub_or_cmd(cmd, (caseroot), "buildnml",
  90                            (self, caseroot, compname), case=self)
  91 
  92         logger.debug("Finished creating component namelists, component {} models = {}".format(component, models))
  93 
  94     # Save namelists to docdir
  95     if (not os.path.isdir(docdir)):
  96         os.makedirs(docdir)
  97         try:
  98             with open(os.path.join(docdir, "README"), "w") as fd:
  99                 fd.write(" CESM Resolved Namelist Files\n   For documentation only DO NOT MODIFY\n")
  100         except (OSError, IOError) as e:
  101             expect(False, "Failed to write {}/README: {}".format(docdir, e))
  102 
  103     for cpglob in ["*_in_[0-9]*", "*modelio*", "*_in", "nuopc.runconfig",
  104                    "*streams*txt*", "*streams.xml", "*stxt", "*maps.rc", "*cism.config*", "nuopc.runseq"]:
  105         for file_to_copy in glob.glob(os.path.join(rundir, cpglob)):
  106             logger.debug("Copy file from '{}' to '{}'".format(file_to_copy, docdir))
  107             safe_copy(file_to_copy, docdir)
  108 
  109     # Copy over chemistry mechanism docs if they exist
  110     if (os.path.isdir(os.path.join(casebuild, "camconf"))):
  111         for file_to_copy in glob.glob(os.path.join(casebuild, "camconf", "*chem_mech*")):
  112             safe_copy(file_to_copy, docdir)
  113
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/scripts/lib/CIME/case/preview_namelists.py b/scripts/lib/CIME/case/preview_namelists.py
  --- a/scripts/lib/CIME/case/preview_namelists.py
  +++ b/scripts/lib/CIME/case/preview_namelists.py
  @@ -107,6 +107,7 @@
               safe_copy(file_to_copy, docdir)
   
       # Copy over chemistry mechanism docs if they exist
  -    if (os.path.isdir(os.path.join(casebuild, "camconf"))):
  -        for file_to_copy in glob.glob(os.path.join(casebuild, "camconf", "*chem_mech*")):
  +    atmconf = self.get_value("COMP_ATM") + "conf"
  +    if (os.path.isdir(os.path.join(casebuild, atmconf))):
  +        for file_to_copy in glob.glob(os.path.join(casebuild, atmconf, "*chem_mech*")):
               safe_copy(file_to_copy, docdir)
  
verification_info: {"golden_diff": "diff --git a/scripts/lib/CIME/case/preview_namelists.py b/scripts/lib/CIME/case/preview_namelists.py\n--- a/scripts/lib/CIME/case/preview_namelists.py\n+++ b/scripts/lib/CIME/case/preview_namelists.py\n@@ -107,6 +107,7 @@\n             safe_copy(file_to_copy, docdir)\n \n     # Copy over chemistry mechanism docs if they exist\n-    if (os.path.isdir(os.path.join(casebuild, \"camconf\"))):\n-        for file_to_copy in glob.glob(os.path.join(casebuild, \"camconf\", \"*chem_mech*\")):\n+    atmconf = self.get_value(\"COMP_ATM\") + \"conf\"\n+    if (os.path.isdir(os.path.join(casebuild, atmconf))):\n+        for file_to_copy in glob.glob(os.path.join(casebuild, atmconf, \"*chem_mech*\")):\n             safe_copy(file_to_copy, docdir)\n", "issue": "Missing chem_mech files in E3SM CaseDocs after renaming CAM to EAM\nAfter [renaming CAM to EAM in E3SM](https://github.com/E3SM-Project/E3SM/pull/3845), the following two files are not copied to CaseDocs\r\n```\r\nchem_mech.doc\r\nchem_mech.in\r\n```\r\nNeed to change the 'cam' substring in 'camconf' near the end of cime/scripts/lib/CIME/case/preview_namelists.py. The piece of codes are copied below\r\n```\r\n# Copy over chemistry mechanism docs if they exist\r\n    if (os.path.isdir(os.path.join(casebuild, \"camconf\"))):\r\n        for file_to_copy in glob.glob(os.path.join(casebuild, \"camconf\", \"*chem_mech*\")):\r\n            safe_copy(file_to_copy, docdir)\r\n```\r\nTo make it work for both cam and eam, need help to replace the substring 'cam' with the atm COMP_NAME.  Thanks.\n", "before_files": [{"content": "\"\"\"\nAPI for preview namelist\ncreate_dirs and create_namelists are members of Class case from file case.py\n\"\"\"\n\nfrom CIME.XML.standard_module_setup import *\nfrom CIME.utils import run_sub_or_cmd, safe_copy\nimport time, glob\nlogger = logging.getLogger(__name__)\n\ndef create_dirs(self):\n    \"\"\"\n    Make necessary directories for case\n    \"\"\"\n    # Get data from XML\n    exeroot  = self.get_value(\"EXEROOT\")\n    libroot  = self.get_value(\"LIBROOT\")\n    incroot  = self.get_value(\"INCROOT\")\n    rundir   = self.get_value(\"RUNDIR\")\n    caseroot = self.get_value(\"CASEROOT\")\n    docdir = os.path.join(caseroot, \"CaseDocs\")\n    dirs_to_make = []\n    models = self.get_values(\"COMP_CLASSES\")\n    for model in models:\n        dirname = model.lower()\n        dirs_to_make.append(os.path.join(exeroot, dirname, \"obj\"))\n\n    dirs_to_make.extend([exeroot, libroot, incroot, rundir, docdir])\n\n    for dir_to_make in dirs_to_make:\n        if (not os.path.isdir(dir_to_make) and not os.path.islink(dir_to_make)):\n            try:\n                logger.debug(\"Making dir '{}'\".format(dir_to_make))\n                os.makedirs(dir_to_make)\n            except OSError as e:\n                # In a multithreaded situation, we may have lost a race to create this dir.\n                # We do not want to crash if that's the case.\n                if not os.path.isdir(dir_to_make):\n                    expect(False, \"Could not make directory '{}', error: {}\".format(dir_to_make, e))\n\n    # As a convenience write the location of the case directory in the bld and run directories\n    for dir_ in (exeroot, rundir):\n        with open(os.path.join(dir_,\"CASEROOT\"),\"w+\") as fd:\n            fd.write(caseroot+\"\\n\")\n\ndef create_namelists(self, component=None):\n    \"\"\"\n    Create component namelists\n    \"\"\"\n    self.flush()\n\n    create_dirs(self)\n\n    casebuild = self.get_value(\"CASEBUILD\")\n    caseroot = self.get_value(\"CASEROOT\")\n    rundir = self.get_value(\"RUNDIR\")\n\n    docdir = os.path.join(caseroot, \"CaseDocs\")\n\n    # Load modules\n    self.load_env()\n\n    self.stage_refcase()\n\n    # Create namelists - must have cpl last in the list below\n    # Note - cpl must be last in the loop below so that in generating its namelist,\n    # it can use xml vars potentially set by other component's buildnml scripts\n    models = self.get_values(\"COMP_CLASSES\")\n    models += [models.pop(0)]\n    for model in models:\n        model_str = model.lower()\n        logger.info(\"  {} {} \".format(time.strftime(\"%Y-%m-%d %H:%M:%S\"),model_str))\n        config_file = self.get_value(\"CONFIG_{}_FILE\".format(model_str.upper()))\n        config_dir = os.path.dirname(config_file)\n        if model_str == \"cpl\":\n            compname = \"drv\"\n        else:\n            compname = self.get_value(\"COMP_{}\".format(model_str.upper()))\n        if component is None or component == model_str or compname==\"ufsatm\":\n            # first look in the case SourceMods directory\n            cmd = os.path.join(caseroot, \"SourceMods\", \"src.\"+compname, \"buildnml\")\n            if os.path.isfile(cmd):\n                logger.warning(\"\\nWARNING: Using local buildnml file {}\\n\".format(cmd))\n            else:\n                # otherwise look in the component config_dir\n                cmd = os.path.join(config_dir, \"buildnml\")\n            expect(os.path.isfile(cmd), \"Could not find buildnml file for component {}\".format(compname))\n            logger.info(\"Create namelist for component {}\".format(compname))\n            run_sub_or_cmd(cmd, (caseroot), \"buildnml\",\n                           (self, caseroot, compname), case=self)\n\n        logger.debug(\"Finished creating component namelists, component {} models = {}\".format(component, models))\n\n    # Save namelists to docdir\n    if (not os.path.isdir(docdir)):\n        os.makedirs(docdir)\n        try:\n            with open(os.path.join(docdir, \"README\"), \"w\") as fd:\n                fd.write(\" CESM Resolved Namelist Files\\n   For documentation only DO NOT MODIFY\\n\")\n        except (OSError, IOError) as e:\n            expect(False, \"Failed to write {}/README: {}\".format(docdir, e))\n\n    for cpglob in [\"*_in_[0-9]*\", \"*modelio*\", \"*_in\", \"nuopc.runconfig\",\n                   \"*streams*txt*\", \"*streams.xml\", \"*stxt\", \"*maps.rc\", \"*cism.config*\", \"nuopc.runseq\"]:\n        for file_to_copy in glob.glob(os.path.join(rundir, cpglob)):\n            logger.debug(\"Copy file from '{}' to '{}'\".format(file_to_copy, docdir))\n            safe_copy(file_to_copy, docdir)\n\n    # Copy over chemistry mechanism docs if they exist\n    if (os.path.isdir(os.path.join(casebuild, \"camconf\"))):\n        for file_to_copy in glob.glob(os.path.join(casebuild, \"camconf\", \"*chem_mech*\")):\n            safe_copy(file_to_copy, docdir)\n", "path": "scripts/lib/CIME/case/preview_namelists.py"}], "after_files": [{"content": "\"\"\"\nAPI for preview namelist\ncreate_dirs and create_namelists are members of Class case from file case.py\n\"\"\"\n\nfrom CIME.XML.standard_module_setup import *\nfrom CIME.utils import run_sub_or_cmd, safe_copy\nimport time, glob\nlogger = logging.getLogger(__name__)\n\ndef create_dirs(self):\n    \"\"\"\n    Make necessary directories for case\n    \"\"\"\n    # Get data from XML\n    exeroot  = self.get_value(\"EXEROOT\")\n    libroot  = self.get_value(\"LIBROOT\")\n    incroot  = self.get_value(\"INCROOT\")\n    rundir   = self.get_value(\"RUNDIR\")\n    caseroot = self.get_value(\"CASEROOT\")\n    docdir = os.path.join(caseroot, \"CaseDocs\")\n    dirs_to_make = []\n    models = self.get_values(\"COMP_CLASSES\")\n    for model in models:\n        dirname = model.lower()\n        dirs_to_make.append(os.path.join(exeroot, dirname, \"obj\"))\n\n    dirs_to_make.extend([exeroot, libroot, incroot, rundir, docdir])\n\n    for dir_to_make in dirs_to_make:\n        if (not os.path.isdir(dir_to_make) and not os.path.islink(dir_to_make)):\n            try:\n                logger.debug(\"Making dir '{}'\".format(dir_to_make))\n                os.makedirs(dir_to_make)\n            except OSError as e:\n                # In a multithreaded situation, we may have lost a race to create this dir.\n                # We do not want to crash if that's the case.\n                if not os.path.isdir(dir_to_make):\n                    expect(False, \"Could not make directory '{}', error: {}\".format(dir_to_make, e))\n\n    # As a convenience write the location of the case directory in the bld and run directories\n    for dir_ in (exeroot, rundir):\n        with open(os.path.join(dir_,\"CASEROOT\"),\"w+\") as fd:\n            fd.write(caseroot+\"\\n\")\n\ndef create_namelists(self, component=None):\n    \"\"\"\n    Create component namelists\n    \"\"\"\n    self.flush()\n\n    create_dirs(self)\n\n    casebuild = self.get_value(\"CASEBUILD\")\n    caseroot = self.get_value(\"CASEROOT\")\n    rundir = self.get_value(\"RUNDIR\")\n\n    docdir = os.path.join(caseroot, \"CaseDocs\")\n\n    # Load modules\n    self.load_env()\n\n    self.stage_refcase()\n\n    # Create namelists - must have cpl last in the list below\n    # Note - cpl must be last in the loop below so that in generating its namelist,\n    # it can use xml vars potentially set by other component's buildnml scripts\n    models = self.get_values(\"COMP_CLASSES\")\n    models += [models.pop(0)]\n    for model in models:\n        model_str = model.lower()\n        logger.info(\"  {} {} \".format(time.strftime(\"%Y-%m-%d %H:%M:%S\"),model_str))\n        config_file = self.get_value(\"CONFIG_{}_FILE\".format(model_str.upper()))\n        config_dir = os.path.dirname(config_file)\n        if model_str == \"cpl\":\n            compname = \"drv\"\n        else:\n            compname = self.get_value(\"COMP_{}\".format(model_str.upper()))\n        if component is None or component == model_str or compname==\"ufsatm\":\n            # first look in the case SourceMods directory\n            cmd = os.path.join(caseroot, \"SourceMods\", \"src.\"+compname, \"buildnml\")\n            if os.path.isfile(cmd):\n                logger.warning(\"\\nWARNING: Using local buildnml file {}\\n\".format(cmd))\n            else:\n                # otherwise look in the component config_dir\n                cmd = os.path.join(config_dir, \"buildnml\")\n            expect(os.path.isfile(cmd), \"Could not find buildnml file for component {}\".format(compname))\n            logger.info(\"Create namelist for component {}\".format(compname))\n            run_sub_or_cmd(cmd, (caseroot), \"buildnml\",\n                           (self, caseroot, compname), case=self)\n\n        logger.debug(\"Finished creating component namelists, component {} models = {}\".format(component, models))\n\n    # Save namelists to docdir\n    if (not os.path.isdir(docdir)):\n        os.makedirs(docdir)\n        try:\n            with open(os.path.join(docdir, \"README\"), \"w\") as fd:\n                fd.write(\" CESM Resolved Namelist Files\\n   For documentation only DO NOT MODIFY\\n\")\n        except (OSError, IOError) as e:\n            expect(False, \"Failed to write {}/README: {}\".format(docdir, e))\n\n    for cpglob in [\"*_in_[0-9]*\", \"*modelio*\", \"*_in\", \"nuopc.runconfig\",\n                   \"*streams*txt*\", \"*streams.xml\", \"*stxt\", \"*maps.rc\", \"*cism.config*\", \"nuopc.runseq\"]:\n        for file_to_copy in glob.glob(os.path.join(rundir, cpglob)):\n            logger.debug(\"Copy file from '{}' to '{}'\".format(file_to_copy, docdir))\n            safe_copy(file_to_copy, docdir)\n\n    # Copy over chemistry mechanism docs if they exist\n    atmconf = self.get_value(\"COMP_ATM\") + \"conf\"\n    if (os.path.isdir(os.path.join(casebuild, atmconf))):\n        for file_to_copy in glob.glob(os.path.join(casebuild, atmconf, \"*chem_mech*\")):\n            safe_copy(file_to_copy, docdir)\n", "path": "scripts/lib/CIME/case/preview_namelists.py"}]}