We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Remove PESetupHist from cime.setup
When running cime.setup --clean, we don't need to save files to PESetupHist


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `utils/python/CIME/case_setup.py`
Content:
```
1 """
2 Library for case.setup.
3 """
4 
5 from CIME.XML.standard_module_setup import *
6 
7 from CIME.check_lockedfiles import check_lockedfiles
8 from CIME.preview_namelists import create_dirs, create_namelists
9 from CIME.XML.env_mach_pes  import EnvMachPes
10 from CIME.XML.machines      import Machines
11 from CIME.BuildTools.configure import configure
12 from CIME.utils             import append_status, get_cime_root
13 from CIME.test_status       import *
14 
15 import shutil, time, glob
16 
17 logger = logging.getLogger(__name__)
18 
19 ###############################################################################
20 def _check_pelayouts_require_rebuild(case, models):
21 ###############################################################################
22     """
23     Create if we require a rebuild, expects cwd is caseroot
24     """
25     locked_pes = "LockedFiles/env_mach_pes.xml"
26     if os.path.exists(locked_pes):
27         # Look to see if $comp_PE_CHANGE_REQUIRES_REBUILD is defined
28         # for any component
29         env_mach_pes_locked = EnvMachPes(infile=locked_pes)
30         for comp in models:
31             if case.get_value("%s_PE_CHANGE_REQUIRES_REBUILD" % comp):
32                 # Changing these values in env_mach_pes.xml will force
33                 # you to clean the corresponding component
34                 old_tasks   = env_mach_pes_locked.get_value("NTASKS_%s" % comp)
35                 old_threads = env_mach_pes_locked.get_value("NTHRDS_%s" % comp)
36                 old_inst    = env_mach_pes_locked.get_value("NINST_%s" % comp)
37 
38                 new_tasks   = case.get_value("NTASKS_%s" % comp)
39                 new_threads = case.get_value("NTHRDS_%s" % comp)
40                 new_inst    = case.get_value("NINST_%s" % comp)
41 
42                 if old_tasks != new_tasks or old_threads != new_threads or old_inst != new_inst:
43                     logger.warn("%s pe change requires clean build" % comp)
44                     cleanflag = comp.lower()
45                     run_cmd_no_fail("./case.build --clean %s" % cleanflag)
46 
47         os.remove(locked_pes)
48 
49 ###############################################################################
50 def _build_usernl_files(case, model, comp):
51 ###############################################################################
52     """
53     Create user_nl_xxx files, expects cwd is caseroot
54     """
55     model = model.upper()
56     model_file = case.get_value("CONFIG_%s_FILE" % model)
57     model_dir = os.path.dirname(model_file)
58 
59     expect(os.path.isdir(model_dir),
60            "cannot find cime_config directory %s for component %s" % (model_dir, comp))
61 
62     if comp == "cpl":
63         if not os.path.exists("user_nl_cpl"):
64             shutil.copy(os.path.join(model_dir, "user_nl_cpl"), ".")
65     else:
66         ninst = case.get_value("NINST_%s" % model)
67         nlfile = "user_nl_%s" % comp
68         model_nl = os.path.join(model_dir, nlfile)
69         if ninst > 1:
70             for inst_counter in xrange(1, ninst+1):
71                 inst_nlfile = "%s_%04d" % (nlfile, inst_counter)
72                 if not os.path.exists(inst_nlfile):
73                     # If there is a user_nl_foo in the case directory, copy it
74                     # to user_nl_foo_INST; otherwise, copy the original
75                     # user_nl_foo from model_dir
76                     if os.path.exists(nlfile):
77                         shutil.copy(nlfile, inst_nlfile)
78                     elif os.path.exists(model_nl):
79                         shutil.copy(model_nl, inst_nlfile)
80         else:
81             # ninst = 1
82             if not os.path.exists(nlfile):
83                 if os.path.exists(model_nl):
84                     shutil.copy(model_nl, nlfile)
85 
86 ###############################################################################
87 def _case_setup_impl(case, caseroot, clean=False, test_mode=False, reset=False):
88 ###############################################################################
89     os.chdir(caseroot)
90     msg = "case.setup starting"
91     append_status(msg, caseroot=caseroot, sfile="CaseStatus")
92 
93     cimeroot = get_cime_root(case)
94 
95     # Check that $DIN_LOC_ROOT exists - and abort if not a namelist compare tests
96     din_loc_root = case.get_value("DIN_LOC_ROOT")
97     testcase     = case.get_value("TESTCASE")
98     expect(not (not os.path.isdir(din_loc_root) and testcase != "SBN"),
99            "inputdata root is not a directory: \"$din_loc_root\" ")
100 
101     # Check that userdefine settings are specified before expanding variable
102     for vid, value in case:
103         expect(not (type(value) is str and "USERDEFINED_required_build" in value),
104                "Parameter '%s' must be defined" % vid)
105 
106     # Create batch script
107     if reset or clean:
108         # Clean batch script
109 
110         backup_dir = "PESetupHist/b.%s" % time.strftime("%y%m%d-%H%M%S")
111         if not os.path.isdir(backup_dir):
112             os.makedirs(backup_dir)
113 
114         # back up relevant files
115         for fileglob in ["case.run", "env_build.xml", "env_mach_pes.xml", "Macros*"]:
116             for filename in glob.glob(fileglob):
117                 shutil.copy(filename, backup_dir)
118         if os.path.exists("case.run"):
119             os.remove("case.run")
120 
121         # only do the following if are NOT in testmode
122         if not test_mode:
123             # rebuild the models (even on restart)
124             case.set_value("BUILD_COMPLETE", False)
125 
126             # backup and then clean test script
127             if os.path.exists("case.test"):
128                 shutil.copy("case.test", backup_dir)
129                 os.remove("case.test")
130                 logger.info("Successfully cleaned test script case.test")
131 
132             if os.path.exists("case.testdriver"):
133                 shutil.copy("case.testdriver", backup_dir)
134                 os.remove("case.testdriver")
135                 logger.info("Successfully cleaned test script case.testdriver")
136 
137         logger.info("Successfully cleaned batch script case.run")
138 
139         logger.info("Successfully cleaned batch script case.run")
140         logger.info("Some files have been saved to %s" % backup_dir)
141 
142         msg = "case.setup clean complete"
143         append_status(msg, caseroot=caseroot, sfile="CaseStatus")
144 
145     if not clean:
146         case.load_env()
147 
148         models = case.get_values("COMP_CLASSES")
149         mach = case.get_value("MACH")
150         compiler = case.get_value("COMPILER")
151         debug = case.get_value("DEBUG")
152         mpilib = case.get_value("MPILIB")
153         sysos = case.get_value("OS")
154         expect(mach is not None, "xml variable MACH is not set")
155 
156         # creates the Macros.make, Depends.compiler, Depends.machine, Depends.machine.compiler
157         # and env_mach_specific.xml if they don't already exist.
158         if not os.path.isfile("Macros.make") or not os.path.isfile("env_mach_specific.xml"):
159             configure(Machines(machine=mach), caseroot, ["Makefile"], compiler, mpilib, debug, sysos)
160 
161         # Set tasks to 1 if mpi-serial library
162         if mpilib == "mpi-serial":
163             for vid, value in case:
164                 if vid.startswith("NTASKS_") and value != 1:
165                     case.set_value(vid, 1)
166 
167         # Check ninst.
168         # In CIME there can be multiple instances of each component model (an ensemble) NINST is the instance of that component.
169         for comp in models:
170             if comp == "DRV":
171                 continue
172             ninst  = case.get_value("NINST_%s" % comp)
173             ntasks = case.get_value("NTASKS_%s" % comp)
174             if ninst > ntasks:
175                 if ntasks == 1:
176                     case.set_value("NTASKS_%s" % comp, ninst)
177                 else:
178                     expect(False, "NINST_%s value %d greater than NTASKS_%s %d" % (comp, ninst, comp, ntasks))
179 
180         if os.path.exists("case.run"):
181             logger.info("Machine/Decomp/Pes configuration has already been done ...skipping")
182         else:
183             _check_pelayouts_require_rebuild(case, models)
184 
185             if os.path.exists("LockedFiles/env_build.xml"):
186                 os.remove("LockedFiles/env_build.xml")
187 
188             case.flush()
189             check_lockedfiles()
190             env_mach_pes = case.get_env("mach_pes")
191             pestot = env_mach_pes.get_total_tasks(models)
192             logger.debug("at update TOTALPES = %s"%pestot)
193             case.set_value("TOTALPES", pestot)
194             thread_count = env_mach_pes.get_max_thread_count(models)
195             if thread_count > 1:
196                 case.set_value("BUILD_THREADED", True)
197 
198             expect(not (case.get_value("BUILD_THREADED")  and compiler == "nag"),
199                    "it is not possible to run with OpenMP if using the NAG Fortran compiler")
200 
201 
202             cost_pes = env_mach_pes.get_cost_pes(pestot, thread_count, machine=case.get_value("MACH"))
203             case.set_value("COST_PES", cost_pes)
204 
205             # create batch file
206             logger.info("Creating batch script case.run")
207 
208             # Use BatchFactory to get the appropriate instance of a BatchMaker,
209             # use it to create our batch scripts
210             env_batch = case.get_env("batch")
211             num_nodes = env_mach_pes.get_total_nodes(pestot, thread_count)
212             tasks_per_node = env_mach_pes.get_tasks_per_node(pestot, thread_count)
213             for job in env_batch.get_jobs():
214                 input_batch_script  = os.path.join(case.get_value("MACHDIR"), env_batch.get_value('template', subgroup=job))
215                 if job == "case.test" and testcase is not None and not test_mode:
216                     logger.info("Writing %s script" % job)
217                     testscript = os.path.join(cimeroot, "scripts", "Testing", "Testcases", "%s_script" % testcase)
218                     # Short term fix to be removed when csh tests are removed
219                     if not os.path.exists(testscript):
220                         env_batch.make_batch_script(input_batch_script, job, case, pestot, tasks_per_node, num_nodes, thread_count)
221                 elif job != "case.test":
222                     logger.info("Writing %s script from input template %s" % (job, input_batch_script))
223                     env_batch.make_batch_script(input_batch_script, job, case, pestot, tasks_per_node, num_nodes, thread_count)
224 
225             # Make a copy of env_mach_pes.xml in order to be able
226             # to check that it does not change once case.setup is invoked
227             logger.info("Locking file env_mach_pes.xml")
228             case.flush()
229             logger.debug("at copy TOTALPES = %s"%case.get_value("TOTALPES"))
230             shutil.copy("env_mach_pes.xml", "LockedFiles")
231 
232         # Create user_nl files for the required number of instances
233         if not os.path.exists("user_nl_cpl"):
234             logger.info("Creating user_nl_xxx files for components and cpl")
235         # loop over models
236         for model in models:
237             comp = case.get_value("COMP_%s" % model)
238             logger.info("Building %s usernl files"%model)
239             _build_usernl_files(case, model, comp)
240             if comp == "cism":
241                 run_cmd_no_fail("%s/../components/cism/cime_config/cism.template %s" % (cimeroot, caseroot))
242 
243         _build_usernl_files(case, "drv", "cpl")
244 
245         # Create needed directories for case
246         create_dirs(case)
247 
248         logger.info("If an old case build already exists, might want to run \'case.build --clean\' before building")
249 
250         # Create test script if appropriate
251         # Short term fix to be removed when csh tests are removed
252         if os.path.exists("env_test.xml"):
253             if not os.path.exists("case.test"):
254                 logger.info("Starting testcase.setup")
255                 run_cmd_no_fail("./testcase.setup -caseroot %s" % caseroot)
256                 logger.info("Finished testcase.setup")
257 
258         # some tests need namelists created here (ERP)
259         if test_mode:
260             create_namelists(case)
261 
262         msg = "case.setup complete"
263         append_status(msg, caseroot=caseroot, sfile="CaseStatus")
264 
265         # Record env information
266         env_module = case.get_env("mach_specific")
267         env_module.make_env_mach_specific_file(compiler, debug, mpilib, "sh")
268         env_module.make_env_mach_specific_file(compiler, debug, mpilib, "csh")
269         with open("software_environment.txt", "w") as f:
270             f.write(env_module.list_modules())
271         run_cmd_no_fail("echo -e '\n' >> software_environment.txt && \
272                          env >> software_environment.txt")
273 
274 ###############################################################################
275 def case_setup(case, clean=False, test_mode=False, reset=False):
276 ###############################################################################
277     caseroot, casebaseid = case.get_value("CASEROOT"), case.get_value("CASEBASEID")
278     if case.get_value("TEST"):
279         test_name = casebaseid if casebaseid is not None else case.get_value("CASE")
280         with TestStatus(test_dir=caseroot, test_name=test_name) as ts:
281             try:
282                 _case_setup_impl(case, caseroot, clean=clean, test_mode=test_mode, reset=reset)
283             except:
284                 ts.set_status(SETUP_PHASE, TEST_FAIL_STATUS)
285                 raise
286             else:
287                 ts.set_status(SETUP_PHASE, TEST_PASS_STATUS)
288     else:
289         _case_setup_impl(case, caseroot, clean=clean, test_mode=test_mode, reset=reset)
290
```
--- 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()

```

diff --git a/utils/python/CIME/case_setup.py b/utils/python/CIME/case_setup.py
--- a/utils/python/CIME/case_setup.py
+++ b/utils/python/CIME/case_setup.py
@@ -105,16 +105,7 @@
 
     # Create batch script
     if reset or clean:
-        # Clean batch script
-
-        backup_dir = "PESetupHist/b.%s" % time.strftime("%y%m%d-%H%M%S")
-        if not os.path.isdir(backup_dir):
-            os.makedirs(backup_dir)
-
         # back up relevant files
-        for fileglob in ["case.run", "env_build.xml", "env_mach_pes.xml", "Macros*"]:
-            for filename in glob.glob(fileglob):
-                shutil.copy(filename, backup_dir)
         if os.path.exists("case.run"):
             os.remove("case.run")
 
@@ -125,20 +116,15 @@
 
             # backup and then clean test script
             if os.path.exists("case.test"):
-                shutil.copy("case.test", backup_dir)
                 os.remove("case.test")
                 logger.info("Successfully cleaned test script case.test")
 
             if os.path.exists("case.testdriver"):
-                shutil.copy("case.testdriver", backup_dir)
                 os.remove("case.testdriver")
                 logger.info("Successfully cleaned test script case.testdriver")
 
         logger.info("Successfully cleaned batch script case.run")
 
-        logger.info("Successfully cleaned batch script case.run")
-        logger.info("Some files have been saved to %s" % backup_dir)
-
         msg = "case.setup clean complete"
         append_status(msg, caseroot=caseroot, sfile="CaseStatus")

{"golden_diff": "diff --git a/utils/python/CIME/case_setup.py b/utils/python/CIME/case_setup.py\n--- a/utils/python/CIME/case_setup.py\n+++ b/utils/python/CIME/case_setup.py\n@@ -105,16 +105,7 @@\n \n     # Create batch script\n     if reset or clean:\n-        # Clean batch script\n-\n-        backup_dir = \"PESetupHist/b.%s\" % time.strftime(\"%y%m%d-%H%M%S\")\n-        if not os.path.isdir(backup_dir):\n-            os.makedirs(backup_dir)\n-\n         # back up relevant files\n-        for fileglob in [\"case.run\", \"env_build.xml\", \"env_mach_pes.xml\", \"Macros*\"]:\n-            for filename in glob.glob(fileglob):\n-                shutil.copy(filename, backup_dir)\n         if os.path.exists(\"case.run\"):\n             os.remove(\"case.run\")\n \n@@ -125,20 +116,15 @@\n \n             # backup and then clean test script\n             if os.path.exists(\"case.test\"):\n-                shutil.copy(\"case.test\", backup_dir)\n                 os.remove(\"case.test\")\n                 logger.info(\"Successfully cleaned test script case.test\")\n \n             if os.path.exists(\"case.testdriver\"):\n-                shutil.copy(\"case.testdriver\", backup_dir)\n                 os.remove(\"case.testdriver\")\n                 logger.info(\"Successfully cleaned test script case.testdriver\")\n \n         logger.info(\"Successfully cleaned batch script case.run\")\n \n-        logger.info(\"Successfully cleaned batch script case.run\")\n-        logger.info(\"Some files have been saved to %s\" % backup_dir)\n-\n         msg = \"case.setup clean complete\"\n         append_status(msg, caseroot=caseroot, sfile=\"CaseStatus\")\n", "issue": "Remove PESetupHist from cime.setup\nWhen running cime.setup --clean, we don't need to save files to PESetupHist\n\n", "before_files": [{"content": "\"\"\"\nLibrary for case.setup.\n\"\"\"\n\nfrom CIME.XML.standard_module_setup import *\n\nfrom CIME.check_lockedfiles import check_lockedfiles\nfrom CIME.preview_namelists import create_dirs, create_namelists\nfrom CIME.XML.env_mach_pes  import EnvMachPes\nfrom CIME.XML.machines      import Machines\nfrom CIME.BuildTools.configure import configure\nfrom CIME.utils             import append_status, get_cime_root\nfrom CIME.test_status       import *\n\nimport shutil, time, glob\n\nlogger = logging.getLogger(__name__)\n\n###############################################################################\ndef _check_pelayouts_require_rebuild(case, models):\n###############################################################################\n    \"\"\"\n    Create if we require a rebuild, expects cwd is caseroot\n    \"\"\"\n    locked_pes = \"LockedFiles/env_mach_pes.xml\"\n    if os.path.exists(locked_pes):\n        # Look to see if $comp_PE_CHANGE_REQUIRES_REBUILD is defined\n        # for any component\n        env_mach_pes_locked = EnvMachPes(infile=locked_pes)\n        for comp in models:\n            if case.get_value(\"%s_PE_CHANGE_REQUIRES_REBUILD\" % comp):\n                # Changing these values in env_mach_pes.xml will force\n                # you to clean the corresponding component\n                old_tasks   = env_mach_pes_locked.get_value(\"NTASKS_%s\" % comp)\n                old_threads = env_mach_pes_locked.get_value(\"NTHRDS_%s\" % comp)\n                old_inst    = env_mach_pes_locked.get_value(\"NINST_%s\" % comp)\n\n                new_tasks   = case.get_value(\"NTASKS_%s\" % comp)\n                new_threads = case.get_value(\"NTHRDS_%s\" % comp)\n                new_inst    = case.get_value(\"NINST_%s\" % comp)\n\n                if old_tasks != new_tasks or old_threads != new_threads or old_inst != new_inst:\n                    logger.warn(\"%s pe change requires clean build\" % comp)\n                    cleanflag = comp.lower()\n                    run_cmd_no_fail(\"./case.build --clean %s\" % cleanflag)\n\n        os.remove(locked_pes)\n\n###############################################################################\ndef _build_usernl_files(case, model, comp):\n###############################################################################\n    \"\"\"\n    Create user_nl_xxx files, expects cwd is caseroot\n    \"\"\"\n    model = model.upper()\n    model_file = case.get_value(\"CONFIG_%s_FILE\" % model)\n    model_dir = os.path.dirname(model_file)\n\n    expect(os.path.isdir(model_dir),\n           \"cannot find cime_config directory %s for component %s\" % (model_dir, comp))\n\n    if comp == \"cpl\":\n        if not os.path.exists(\"user_nl_cpl\"):\n            shutil.copy(os.path.join(model_dir, \"user_nl_cpl\"), \".\")\n    else:\n        ninst = case.get_value(\"NINST_%s\" % model)\n        nlfile = \"user_nl_%s\" % comp\n        model_nl = os.path.join(model_dir, nlfile)\n        if ninst > 1:\n            for inst_counter in xrange(1, ninst+1):\n                inst_nlfile = \"%s_%04d\" % (nlfile, inst_counter)\n                if not os.path.exists(inst_nlfile):\n                    # If there is a user_nl_foo in the case directory, copy it\n                    # to user_nl_foo_INST; otherwise, copy the original\n                    # user_nl_foo from model_dir\n                    if os.path.exists(nlfile):\n                        shutil.copy(nlfile, inst_nlfile)\n                    elif os.path.exists(model_nl):\n                        shutil.copy(model_nl, inst_nlfile)\n        else:\n            # ninst = 1\n            if not os.path.exists(nlfile):\n                if os.path.exists(model_nl):\n                    shutil.copy(model_nl, nlfile)\n\n###############################################################################\ndef _case_setup_impl(case, caseroot, clean=False, test_mode=False, reset=False):\n###############################################################################\n    os.chdir(caseroot)\n    msg = \"case.setup starting\"\n    append_status(msg, caseroot=caseroot, sfile=\"CaseStatus\")\n\n    cimeroot = get_cime_root(case)\n\n    # Check that $DIN_LOC_ROOT exists - and abort if not a namelist compare tests\n    din_loc_root = case.get_value(\"DIN_LOC_ROOT\")\n    testcase     = case.get_value(\"TESTCASE\")\n    expect(not (not os.path.isdir(din_loc_root) and testcase != \"SBN\"),\n           \"inputdata root is not a directory: \\\"$din_loc_root\\\" \")\n\n    # Check that userdefine settings are specified before expanding variable\n    for vid, value in case:\n        expect(not (type(value) is str and \"USERDEFINED_required_build\" in value),\n               \"Parameter '%s' must be defined\" % vid)\n\n    # Create batch script\n    if reset or clean:\n        # Clean batch script\n\n        backup_dir = \"PESetupHist/b.%s\" % time.strftime(\"%y%m%d-%H%M%S\")\n        if not os.path.isdir(backup_dir):\n            os.makedirs(backup_dir)\n\n        # back up relevant files\n        for fileglob in [\"case.run\", \"env_build.xml\", \"env_mach_pes.xml\", \"Macros*\"]:\n            for filename in glob.glob(fileglob):\n                shutil.copy(filename, backup_dir)\n        if os.path.exists(\"case.run\"):\n            os.remove(\"case.run\")\n\n        # only do the following if are NOT in testmode\n        if not test_mode:\n            # rebuild the models (even on restart)\n            case.set_value(\"BUILD_COMPLETE\", False)\n\n            # backup and then clean test script\n            if os.path.exists(\"case.test\"):\n                shutil.copy(\"case.test\", backup_dir)\n                os.remove(\"case.test\")\n                logger.info(\"Successfully cleaned test script case.test\")\n\n            if os.path.exists(\"case.testdriver\"):\n                shutil.copy(\"case.testdriver\", backup_dir)\n                os.remove(\"case.testdriver\")\n                logger.info(\"Successfully cleaned test script case.testdriver\")\n\n        logger.info(\"Successfully cleaned batch script case.run\")\n\n        logger.info(\"Successfully cleaned batch script case.run\")\n        logger.info(\"Some files have been saved to %s\" % backup_dir)\n\n        msg = \"case.setup clean complete\"\n        append_status(msg, caseroot=caseroot, sfile=\"CaseStatus\")\n\n    if not clean:\n        case.load_env()\n\n        models = case.get_values(\"COMP_CLASSES\")\n        mach = case.get_value(\"MACH\")\n        compiler = case.get_value(\"COMPILER\")\n        debug = case.get_value(\"DEBUG\")\n        mpilib = case.get_value(\"MPILIB\")\n        sysos = case.get_value(\"OS\")\n        expect(mach is not None, \"xml variable MACH is not set\")\n\n        # creates the Macros.make, Depends.compiler, Depends.machine, Depends.machine.compiler\n        # and env_mach_specific.xml if they don't already exist.\n        if not os.path.isfile(\"Macros.make\") or not os.path.isfile(\"env_mach_specific.xml\"):\n            configure(Machines(machine=mach), caseroot, [\"Makefile\"], compiler, mpilib, debug, sysos)\n\n        # Set tasks to 1 if mpi-serial library\n        if mpilib == \"mpi-serial\":\n            for vid, value in case:\n                if vid.startswith(\"NTASKS_\") and value != 1:\n                    case.set_value(vid, 1)\n\n        # Check ninst.\n        # In CIME there can be multiple instances of each component model (an ensemble) NINST is the instance of that component.\n        for comp in models:\n            if comp == \"DRV\":\n                continue\n            ninst  = case.get_value(\"NINST_%s\" % comp)\n            ntasks = case.get_value(\"NTASKS_%s\" % comp)\n            if ninst > ntasks:\n                if ntasks == 1:\n                    case.set_value(\"NTASKS_%s\" % comp, ninst)\n                else:\n                    expect(False, \"NINST_%s value %d greater than NTASKS_%s %d\" % (comp, ninst, comp, ntasks))\n\n        if os.path.exists(\"case.run\"):\n            logger.info(\"Machine/Decomp/Pes configuration has already been done ...skipping\")\n        else:\n            _check_pelayouts_require_rebuild(case, models)\n\n            if os.path.exists(\"LockedFiles/env_build.xml\"):\n                os.remove(\"LockedFiles/env_build.xml\")\n\n            case.flush()\n            check_lockedfiles()\n            env_mach_pes = case.get_env(\"mach_pes\")\n            pestot = env_mach_pes.get_total_tasks(models)\n            logger.debug(\"at update TOTALPES = %s\"%pestot)\n            case.set_value(\"TOTALPES\", pestot)\n            thread_count = env_mach_pes.get_max_thread_count(models)\n            if thread_count > 1:\n                case.set_value(\"BUILD_THREADED\", True)\n\n            expect(not (case.get_value(\"BUILD_THREADED\")  and compiler == \"nag\"),\n                   \"it is not possible to run with OpenMP if using the NAG Fortran compiler\")\n\n\n            cost_pes = env_mach_pes.get_cost_pes(pestot, thread_count, machine=case.get_value(\"MACH\"))\n            case.set_value(\"COST_PES\", cost_pes)\n\n            # create batch file\n            logger.info(\"Creating batch script case.run\")\n\n            # Use BatchFactory to get the appropriate instance of a BatchMaker,\n            # use it to create our batch scripts\n            env_batch = case.get_env(\"batch\")\n            num_nodes = env_mach_pes.get_total_nodes(pestot, thread_count)\n            tasks_per_node = env_mach_pes.get_tasks_per_node(pestot, thread_count)\n            for job in env_batch.get_jobs():\n                input_batch_script  = os.path.join(case.get_value(\"MACHDIR\"), env_batch.get_value('template', subgroup=job))\n                if job == \"case.test\" and testcase is not None and not test_mode:\n                    logger.info(\"Writing %s script\" % job)\n                    testscript = os.path.join(cimeroot, \"scripts\", \"Testing\", \"Testcases\", \"%s_script\" % testcase)\n                    # Short term fix to be removed when csh tests are removed\n                    if not os.path.exists(testscript):\n                        env_batch.make_batch_script(input_batch_script, job, case, pestot, tasks_per_node, num_nodes, thread_count)\n                elif job != \"case.test\":\n                    logger.info(\"Writing %s script from input template %s\" % (job, input_batch_script))\n                    env_batch.make_batch_script(input_batch_script, job, case, pestot, tasks_per_node, num_nodes, thread_count)\n\n            # Make a copy of env_mach_pes.xml in order to be able\n            # to check that it does not change once case.setup is invoked\n            logger.info(\"Locking file env_mach_pes.xml\")\n            case.flush()\n            logger.debug(\"at copy TOTALPES = %s\"%case.get_value(\"TOTALPES\"))\n            shutil.copy(\"env_mach_pes.xml\", \"LockedFiles\")\n\n        # Create user_nl files for the required number of instances\n        if not os.path.exists(\"user_nl_cpl\"):\n            logger.info(\"Creating user_nl_xxx files for components and cpl\")\n        # loop over models\n        for model in models:\n            comp = case.get_value(\"COMP_%s\" % model)\n            logger.info(\"Building %s usernl files\"%model)\n            _build_usernl_files(case, model, comp)\n            if comp == \"cism\":\n                run_cmd_no_fail(\"%s/../components/cism/cime_config/cism.template %s\" % (cimeroot, caseroot))\n\n        _build_usernl_files(case, \"drv\", \"cpl\")\n\n        # Create needed directories for case\n        create_dirs(case)\n\n        logger.info(\"If an old case build already exists, might want to run \\'case.build --clean\\' before building\")\n\n        # Create test script if appropriate\n        # Short term fix to be removed when csh tests are removed\n        if os.path.exists(\"env_test.xml\"):\n            if not os.path.exists(\"case.test\"):\n                logger.info(\"Starting testcase.setup\")\n                run_cmd_no_fail(\"./testcase.setup -caseroot %s\" % caseroot)\n                logger.info(\"Finished testcase.setup\")\n\n        # some tests need namelists created here (ERP)\n        if test_mode:\n            create_namelists(case)\n\n        msg = \"case.setup complete\"\n        append_status(msg, caseroot=caseroot, sfile=\"CaseStatus\")\n\n        # Record env information\n        env_module = case.get_env(\"mach_specific\")\n        env_module.make_env_mach_specific_file(compiler, debug, mpilib, \"sh\")\n        env_module.make_env_mach_specific_file(compiler, debug, mpilib, \"csh\")\n        with open(\"software_environment.txt\", \"w\") as f:\n            f.write(env_module.list_modules())\n        run_cmd_no_fail(\"echo -e '\\n' >> software_environment.txt && \\\n                         env >> software_environment.txt\")\n\n###############################################################################\ndef case_setup(case, clean=False, test_mode=False, reset=False):\n###############################################################################\n    caseroot, casebaseid = case.get_value(\"CASEROOT\"), case.get_value(\"CASEBASEID\")\n    if case.get_value(\"TEST\"):\n        test_name = casebaseid if casebaseid is not None else case.get_value(\"CASE\")\n        with TestStatus(test_dir=caseroot, test_name=test_name) as ts:\n            try:\n                _case_setup_impl(case, caseroot, clean=clean, test_mode=test_mode, reset=reset)\n            except:\n                ts.set_status(SETUP_PHASE, TEST_FAIL_STATUS)\n                raise\n            else:\n                ts.set_status(SETUP_PHASE, TEST_PASS_STATUS)\n    else:\n        _case_setup_impl(case, caseroot, clean=clean, test_mode=test_mode, reset=reset)\n", "path": "utils/python/CIME/case_setup.py"}], "after_files": [{"content": "\"\"\"\nLibrary for case.setup.\n\"\"\"\n\nfrom CIME.XML.standard_module_setup import *\n\nfrom CIME.check_lockedfiles import check_lockedfiles\nfrom CIME.preview_namelists import create_dirs, create_namelists\nfrom CIME.XML.env_mach_pes  import EnvMachPes\nfrom CIME.XML.machines      import Machines\nfrom CIME.BuildTools.configure import configure\nfrom CIME.utils             import append_status, get_cime_root\nfrom CIME.test_status       import *\n\nimport shutil, time, glob\n\nlogger = logging.getLogger(__name__)\n\n###############################################################################\ndef _check_pelayouts_require_rebuild(case, models):\n###############################################################################\n    \"\"\"\n    Create if we require a rebuild, expects cwd is caseroot\n    \"\"\"\n    locked_pes = \"LockedFiles/env_mach_pes.xml\"\n    if os.path.exists(locked_pes):\n        # Look to see if $comp_PE_CHANGE_REQUIRES_REBUILD is defined\n        # for any component\n        env_mach_pes_locked = EnvMachPes(infile=locked_pes)\n        for comp in models:\n            if case.get_value(\"%s_PE_CHANGE_REQUIRES_REBUILD\" % comp):\n                # Changing these values in env_mach_pes.xml will force\n                # you to clean the corresponding component\n                old_tasks   = env_mach_pes_locked.get_value(\"NTASKS_%s\" % comp)\n                old_threads = env_mach_pes_locked.get_value(\"NTHRDS_%s\" % comp)\n                old_inst    = env_mach_pes_locked.get_value(\"NINST_%s\" % comp)\n\n                new_tasks   = case.get_value(\"NTASKS_%s\" % comp)\n                new_threads = case.get_value(\"NTHRDS_%s\" % comp)\n                new_inst    = case.get_value(\"NINST_%s\" % comp)\n\n                if old_tasks != new_tasks or old_threads != new_threads or old_inst != new_inst:\n                    logger.warn(\"%s pe change requires clean build\" % comp)\n                    cleanflag = comp.lower()\n                    run_cmd_no_fail(\"./case.build --clean %s\" % cleanflag)\n\n        os.remove(locked_pes)\n\n###############################################################################\ndef _build_usernl_files(case, model, comp):\n###############################################################################\n    \"\"\"\n    Create user_nl_xxx files, expects cwd is caseroot\n    \"\"\"\n    model = model.upper()\n    model_file = case.get_value(\"CONFIG_%s_FILE\" % model)\n    model_dir = os.path.dirname(model_file)\n\n    expect(os.path.isdir(model_dir),\n           \"cannot find cime_config directory %s for component %s\" % (model_dir, comp))\n\n    if comp == \"cpl\":\n        if not os.path.exists(\"user_nl_cpl\"):\n            shutil.copy(os.path.join(model_dir, \"user_nl_cpl\"), \".\")\n    else:\n        ninst = case.get_value(\"NINST_%s\" % model)\n        nlfile = \"user_nl_%s\" % comp\n        model_nl = os.path.join(model_dir, nlfile)\n        if ninst > 1:\n            for inst_counter in xrange(1, ninst+1):\n                inst_nlfile = \"%s_%04d\" % (nlfile, inst_counter)\n                if not os.path.exists(inst_nlfile):\n                    # If there is a user_nl_foo in the case directory, copy it\n                    # to user_nl_foo_INST; otherwise, copy the original\n                    # user_nl_foo from model_dir\n                    if os.path.exists(nlfile):\n                        shutil.copy(nlfile, inst_nlfile)\n                    elif os.path.exists(model_nl):\n                        shutil.copy(model_nl, inst_nlfile)\n        else:\n            # ninst = 1\n            if not os.path.exists(nlfile):\n                if os.path.exists(model_nl):\n                    shutil.copy(model_nl, nlfile)\n\n###############################################################################\ndef _case_setup_impl(case, caseroot, clean=False, test_mode=False, reset=False):\n###############################################################################\n    os.chdir(caseroot)\n    msg = \"case.setup starting\"\n    append_status(msg, caseroot=caseroot, sfile=\"CaseStatus\")\n\n    cimeroot = get_cime_root(case)\n\n    # Check that $DIN_LOC_ROOT exists - and abort if not a namelist compare tests\n    din_loc_root = case.get_value(\"DIN_LOC_ROOT\")\n    testcase     = case.get_value(\"TESTCASE\")\n    expect(not (not os.path.isdir(din_loc_root) and testcase != \"SBN\"),\n           \"inputdata root is not a directory: \\\"$din_loc_root\\\" \")\n\n    # Check that userdefine settings are specified before expanding variable\n    for vid, value in case:\n        expect(not (type(value) is str and \"USERDEFINED_required_build\" in value),\n               \"Parameter '%s' must be defined\" % vid)\n\n    # Create batch script\n    if reset or clean:\n        # back up relevant files\n        if os.path.exists(\"case.run\"):\n            os.remove(\"case.run\")\n\n        # only do the following if are NOT in testmode\n        if not test_mode:\n            # rebuild the models (even on restart)\n            case.set_value(\"BUILD_COMPLETE\", False)\n\n            # backup and then clean test script\n            if os.path.exists(\"case.test\"):\n                os.remove(\"case.test\")\n                logger.info(\"Successfully cleaned test script case.test\")\n\n            if os.path.exists(\"case.testdriver\"):\n                os.remove(\"case.testdriver\")\n                logger.info(\"Successfully cleaned test script case.testdriver\")\n\n        logger.info(\"Successfully cleaned batch script case.run\")\n\n        msg = \"case.setup clean complete\"\n        append_status(msg, caseroot=caseroot, sfile=\"CaseStatus\")\n\n    if not clean:\n        case.load_env()\n\n        models = case.get_values(\"COMP_CLASSES\")\n        mach = case.get_value(\"MACH\")\n        compiler = case.get_value(\"COMPILER\")\n        debug = case.get_value(\"DEBUG\")\n        mpilib = case.get_value(\"MPILIB\")\n        sysos = case.get_value(\"OS\")\n        expect(mach is not None, \"xml variable MACH is not set\")\n\n        # creates the Macros.make, Depends.compiler, Depends.machine, Depends.machine.compiler\n        # and env_mach_specific.xml if they don't already exist.\n        if not os.path.isfile(\"Macros.make\") or not os.path.isfile(\"env_mach_specific.xml\"):\n            configure(Machines(machine=mach), caseroot, [\"Makefile\"], compiler, mpilib, debug, sysos)\n\n        # Set tasks to 1 if mpi-serial library\n        if mpilib == \"mpi-serial\":\n            for vid, value in case:\n                if vid.startswith(\"NTASKS_\") and value != 1:\n                    case.set_value(vid, 1)\n\n        # Check ninst.\n        # In CIME there can be multiple instances of each component model (an ensemble) NINST is the instance of that component.\n        for comp in models:\n            if comp == \"DRV\":\n                continue\n            ninst  = case.get_value(\"NINST_%s\" % comp)\n            ntasks = case.get_value(\"NTASKS_%s\" % comp)\n            if ninst > ntasks:\n                if ntasks == 1:\n                    case.set_value(\"NTASKS_%s\" % comp, ninst)\n                else:\n                    expect(False, \"NINST_%s value %d greater than NTASKS_%s %d\" % (comp, ninst, comp, ntasks))\n\n        if os.path.exists(\"case.run\"):\n            logger.info(\"Machine/Decomp/Pes configuration has already been done ...skipping\")\n        else:\n            _check_pelayouts_require_rebuild(case, models)\n\n            if os.path.exists(\"LockedFiles/env_build.xml\"):\n                os.remove(\"LockedFiles/env_build.xml\")\n\n            case.flush()\n            check_lockedfiles()\n            env_mach_pes = case.get_env(\"mach_pes\")\n            pestot = env_mach_pes.get_total_tasks(models)\n            logger.debug(\"at update TOTALPES = %s\"%pestot)\n            case.set_value(\"TOTALPES\", pestot)\n            thread_count = env_mach_pes.get_max_thread_count(models)\n            if thread_count > 1:\n                case.set_value(\"BUILD_THREADED\", True)\n\n            expect(not (case.get_value(\"BUILD_THREADED\")  and compiler == \"nag\"),\n                   \"it is not possible to run with OpenMP if using the NAG Fortran compiler\")\n\n\n            cost_pes = env_mach_pes.get_cost_pes(pestot, thread_count, machine=case.get_value(\"MACH\"))\n            case.set_value(\"COST_PES\", cost_pes)\n\n            # create batch file\n            logger.info(\"Creating batch script case.run\")\n\n            # Use BatchFactory to get the appropriate instance of a BatchMaker,\n            # use it to create our batch scripts\n            env_batch = case.get_env(\"batch\")\n            num_nodes = env_mach_pes.get_total_nodes(pestot, thread_count)\n            tasks_per_node = env_mach_pes.get_tasks_per_node(pestot, thread_count)\n            for job in env_batch.get_jobs():\n                input_batch_script  = os.path.join(case.get_value(\"MACHDIR\"), env_batch.get_value('template', subgroup=job))\n                if job == \"case.test\" and testcase is not None and not test_mode:\n                    logger.info(\"Writing %s script\" % job)\n                    testscript = os.path.join(cimeroot, \"scripts\", \"Testing\", \"Testcases\", \"%s_script\" % testcase)\n                    # Short term fix to be removed when csh tests are removed\n                    if not os.path.exists(testscript):\n                        env_batch.make_batch_script(input_batch_script, job, case, pestot, tasks_per_node, num_nodes, thread_count)\n                elif job != \"case.test\":\n                    logger.info(\"Writing %s script from input template %s\" % (job, input_batch_script))\n                    env_batch.make_batch_script(input_batch_script, job, case, pestot, tasks_per_node, num_nodes, thread_count)\n\n            # Make a copy of env_mach_pes.xml in order to be able\n            # to check that it does not change once case.setup is invoked\n            logger.info(\"Locking file env_mach_pes.xml\")\n            case.flush()\n            logger.debug(\"at copy TOTALPES = %s\"%case.get_value(\"TOTALPES\"))\n            shutil.copy(\"env_mach_pes.xml\", \"LockedFiles\")\n\n        # Create user_nl files for the required number of instances\n        if not os.path.exists(\"user_nl_cpl\"):\n            logger.info(\"Creating user_nl_xxx files for components and cpl\")\n        # loop over models\n        for model in models:\n            comp = case.get_value(\"COMP_%s\" % model)\n            logger.info(\"Building %s usernl files\"%model)\n            _build_usernl_files(case, model, comp)\n            if comp == \"cism\":\n                run_cmd_no_fail(\"%s/../components/cism/cime_config/cism.template %s\" % (cimeroot, caseroot))\n\n        _build_usernl_files(case, \"drv\", \"cpl\")\n\n        # Create needed directories for case\n        create_dirs(case)\n\n        logger.info(\"If an old case build already exists, might want to run \\'case.build --clean\\' before building\")\n\n        # Create test script if appropriate\n        # Short term fix to be removed when csh tests are removed\n        if os.path.exists(\"env_test.xml\"):\n            if not os.path.exists(\"case.test\"):\n                logger.info(\"Starting testcase.setup\")\n                run_cmd_no_fail(\"./testcase.setup -caseroot %s\" % caseroot)\n                logger.info(\"Finished testcase.setup\")\n\n        # some tests need namelists created here (ERP)\n        if test_mode:\n            create_namelists(case)\n\n        msg = \"case.setup complete\"\n        append_status(msg, caseroot=caseroot, sfile=\"CaseStatus\")\n\n        # Record env information\n        env_module = case.get_env(\"mach_specific\")\n        env_module.make_env_mach_specific_file(compiler, debug, mpilib, \"sh\")\n        env_module.make_env_mach_specific_file(compiler, debug, mpilib, \"csh\")\n        with open(\"software_environment.txt\", \"w\") as f:\n            f.write(env_module.list_modules())\n        run_cmd_no_fail(\"echo -e '\\n' >> software_environment.txt && \\\n                         env >> software_environment.txt\")\n\n###############################################################################\ndef case_setup(case, clean=False, test_mode=False, reset=False):\n###############################################################################\n    caseroot, casebaseid = case.get_value(\"CASEROOT\"), case.get_value(\"CASEBASEID\")\n    if case.get_value(\"TEST\"):\n        test_name = casebaseid if casebaseid is not None else case.get_value(\"CASE\")\n        with TestStatus(test_dir=caseroot, test_name=test_name) as ts:\n            try:\n                _case_setup_impl(case, caseroot, clean=clean, test_mode=test_mode, reset=reset)\n            except:\n                ts.set_status(SETUP_PHASE, TEST_FAIL_STATUS)\n                raise\n            else:\n                ts.set_status(SETUP_PHASE, TEST_PASS_STATUS)\n    else:\n        _case_setup_impl(case, caseroot, clean=clean, test_mode=test_mode, reset=reset)\n", "path": "utils/python/CIME/case_setup.py"}]}