We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
GET_REFCASE wont change from TRUE to FALSE
If the user has GET_REFCASE=TRUE a file Buildconf/refcase.input_data_list is created, if in a subsequent run the user sets GET_REFCASE=FALSE this file must be deleted or the model will still attempt to find a refcase.

--- 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/check_input_data.py`
Content:
```
1 """
2 API for checking input for testcase
3 """
4 from CIME.XML.standard_module_setup import *
5 from CIME.utils import SharedArea, find_files, safe_copy, expect
6 from CIME.XML.inputdata import Inputdata
7 import CIME.Servers
8 
9 import glob
10 
11 logger = logging.getLogger(__name__)
12 
13 def _download_if_in_repo(server, input_data_root, rel_path, isdirectory=False):
14     """
15     Return True if successfully downloaded
16     """
17     if not server.fileexists(rel_path):
18         return False
19 
20     full_path = os.path.join(input_data_root, rel_path)
21     logging.info("Trying to download file: '{}' to path '{}'".format(rel_path, full_path))
22     # Make sure local path exists, create if it does not
23     if isdirectory or full_path.endswith(os.sep):
24         if not os.path.exists(full_path):
25             logger.info("Creating directory {}".format(full_path))
26             os.makedirs(full_path)
27         isdirectory = True
28     elif not os.path.exists(os.path.dirname(full_path)):
29         os.makedirs(os.path.dirname(full_path))
30 
31     # Use umask to make sure files are group read/writable. As long as parent directories
32     # have +s, then everything should work.
33     with SharedArea():
34         if isdirectory:
35             return server.getdirectory(rel_path, full_path)
36         else:
37             return server.getfile(rel_path, full_path)
38 
39 ###############################################################################
40 def check_all_input_data(self, protocol=None, address=None, input_data_root=None, data_list_dir="Buildconf", download=True):
41 ###############################################################################
42     success = False
43     if protocol is not None and address is not None:
44         success = self.check_input_data(protocol=protocol, address=address, download=download,
45                                    input_data_root=input_data_root, data_list_dir=data_list_dir)
46     else:
47         success = self.check_input_data(protocol=protocol, address=address, download=False,
48                                    input_data_root=input_data_root, data_list_dir=data_list_dir)
49         if download and not success:
50             success = _downloadfromserver(self, input_data_root, data_list_dir)
51     
52     expect(not download or (download and success), "Could not find all inputdata on any server")
53     self.stage_refcase(input_data_root=input_data_root, data_list_dir=data_list_dir)
54     return success
55 
56 def _downloadfromserver(case, input_data_root, data_list_dir):
57     # needs to be downloaded
58     success = False
59     protocol = 'svn'
60     inputdata = Inputdata()
61     while not success and protocol is not None:
62         protocol, address = inputdata.get_next_server()
63         logger.info("Checking server {} with protocol {}".format(address, protocol))
64         success = case.check_input_data(protocol=protocol, address=address, download=True,
65                                         input_data_root=input_data_root, data_list_dir=data_list_dir)
66     return success
67 
68 def stage_refcase(self, input_data_root=None, data_list_dir=None):
69     get_refcase  = self.get_value("GET_REFCASE")
70     run_type     = self.get_value("RUN_TYPE")
71     continue_run = self.get_value("CONTINUE_RUN")
72 
73     # We do not fully populate the inputdata directory on every
74     # machine and do not expect every user to download the 3TB+ of
75     # data in our inputdata repository. This code checks for the
76     # existence of inputdata in the local inputdata directory and
77     # attempts to download data from the server if it's needed and
78     # missing.
79     if get_refcase and run_type != "startup" and not continue_run:
80         din_loc_root = self.get_value("DIN_LOC_ROOT")
81         run_refdate  = self.get_value("RUN_REFDATE")
82         run_refcase  = self.get_value("RUN_REFCASE")
83         run_refdir   = self.get_value("RUN_REFDIR")
84         rundir       = self.get_value("RUNDIR")
85 
86         if os.path.isabs(run_refdir):
87             refdir = run_refdir
88         else:
89             refdir = os.path.join(din_loc_root, run_refdir, run_refcase, run_refdate)
90             if not os.path.isdir(refdir):
91                 logger.warning("Refcase not found in {}, will attempt to download from inputdata".format(refdir))
92                 with open(os.path.join("Buildconf","refcase.input_data_list"),"w") as fd:
93                     fd.write("refdir = {}{}".format(refdir, os.sep))
94                 if input_data_root is None:
95                     input_data_root = din_loc_root
96                 if data_list_dir is None:
97                     data_list_dir = "Buildconf"
98                 success = _downloadfromserver(self, input_data_root=input_data_root, data_list_dir=data_list_dir)
99                 expect(success, "Could not download refcase from any server")
100 
101         logger.info(" - Prestaging REFCASE ({}) to {}".format(refdir, rundir))
102 
103         # prestage the reference case's files.
104 
105         if (not os.path.exists(rundir)):
106             logger.debug("Creating run directory: {}".format(rundir))
107             os.makedirs(rundir)
108 
109         # copy the refcases' rpointer files to the run directory
110         for rpointerfile in glob.iglob(os.path.join("{}","*rpointer*").format(refdir)):
111             logger.info("Copy rpointer {}".format(rpointerfile))
112             safe_copy(rpointerfile, rundir)
113 
114         # link everything else
115 
116         for rcfile in glob.iglob(os.path.join(refdir,"*")):
117             rcbaseline = os.path.basename(rcfile)
118             if not os.path.exists("{}/{}".format(rundir, rcbaseline)):
119                 logger.info("Staging file {}".format(rcfile))
120                 os.symlink(rcfile, "{}/{}".format(rundir, rcbaseline))
121         # Backward compatibility, some old refcases have cam2 in the name
122         # link to local cam file.
123         for cam2file in  glob.iglob(os.path.join("{}","*.cam2.*").format(rundir)):
124             camfile = cam2file.replace("cam2", "cam")
125             os.symlink(cam2file, camfile)
126 
127     return True
128 
129 def check_input_data(case, protocol="svn", address=None, input_data_root=None, data_list_dir="Buildconf", download=False):
130     """
131     Return True if no files missing
132     """
133     case.load_env(reset=True)
134     # Fill in defaults as needed
135     input_data_root = case.get_value("DIN_LOC_ROOT") if input_data_root is None else input_data_root
136 
137     expect(os.path.isdir(input_data_root), "Invalid input_data_root directory: '{}'".format(input_data_root))
138     expect(os.path.isdir(data_list_dir), "Invalid data_list_dir directory: '{}'".format(data_list_dir))
139 
140     data_list_files = find_files(data_list_dir, "*.input_data_list")
141     expect(data_list_files, "No .input_data_list files found in dir '{}'".format(data_list_dir))
142 
143     no_files_missing = True
144 
145     if download:
146         if protocol not in vars(CIME.Servers):
147             logger.warning("Client protocol {} not enabled".format(protocol))
148             return False
149 
150         if protocol == "svn":
151             server = CIME.Servers.SVN(address)
152         elif protocol == "gftp":
153             server = CIME.Servers.GridFTP(address)
154         elif protocol == "ftp":
155             server = CIME.Servers.FTP(address)
156         elif protocol == "wget":
157             server = CIME.Servers.WGET(address)
158         else:
159             expect(False, "Unsupported inputdata protocol: {}".format(protocol))
160 
161 
162 
163     for data_list_file in data_list_files:
164         logging.info("Loading input file list: '{}'".format(data_list_file))
165         with open(data_list_file, "r") as fd:
166             lines = fd.readlines()
167 
168         for line in lines:
169             line = line.strip()
170             if (line and not line.startswith("#")):
171                 tokens = line.split('=')
172                 description, full_path = tokens[0].strip(), tokens[1].strip()
173                 if description.endswith('datapath'):
174                     continue
175                 if(full_path):
176                     # expand xml variables
177                     full_path = case.get_resolved_value(full_path)
178                     rel_path  = full_path.replace(input_data_root, "")
179                     model = os.path.basename(data_list_file).split('.')[0]
180 
181                     if ("/" in rel_path and rel_path == full_path):
182                         # User pointing to a file outside of input_data_root, we cannot determine
183                         # rel_path, and so cannot download the file. If it already exists, we can
184                         # proceed
185                         if not os.path.exists(full_path):
186                             logging.warning("Model {} missing file {} = '{}'".format(model, description, full_path))
187                             if download:
188                                 logging.warning("    Cannot download file since it lives outside of the input_data_root '{}'".format(input_data_root))
189                             no_files_missing = False
190                         else:
191                             logging.debug("  Found input file: '{}'".format(full_path))
192 
193                     else:
194                         # There are some special values of rel_path that
195                         # we need to ignore - some of the component models
196                         # set things like 'NULL' or 'same_as_TS' -
197                         # basically if rel_path does not contain '/' (a
198                         # directory tree) you can assume it's a special
199                         # value and ignore it (perhaps with a warning)
200                         if ("/" in rel_path and not os.path.exists(full_path)):
201                             logging.warning("  Model {} missing file {} = '{}'".format(model, description, full_path))
202                             no_files_missing = False
203 
204                             if (download):
205                                 no_files_missing = _download_if_in_repo(server, input_data_root, rel_path.strip(os.sep),
206                                                                isdirectory=rel_path.endswith(os.sep))
207                         else:
208                             logging.debug("  Already had input file: '{}'".format(full_path))
209 
210                 else:
211                     model = os.path.basename(data_list_file).split('.')[0]
212                     logging.warning("Model {} no file specified for {}".format(model, description))
213 
214     return no_files_missing
215
```
--- 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/scripts/lib/CIME/case/check_input_data.py b/scripts/lib/CIME/case/check_input_data.py
--- a/scripts/lib/CIME/case/check_input_data.py
+++ b/scripts/lib/CIME/case/check_input_data.py
@@ -48,7 +48,7 @@
                                    input_data_root=input_data_root, data_list_dir=data_list_dir)
         if download and not success:
             success = _downloadfromserver(self, input_data_root, data_list_dir)
-    
+
     expect(not download or (download and success), "Could not find all inputdata on any server")
     self.stage_refcase(input_data_root=input_data_root, data_list_dir=data_list_dir)
     return success
@@ -123,7 +123,10 @@
         for cam2file in  glob.iglob(os.path.join("{}","*.cam2.*").format(rundir)):
             camfile = cam2file.replace("cam2", "cam")
             os.symlink(cam2file, camfile)
-
+    elif not get_refcase and run_type != "startup":
+        logger.info("GET_REFCASE is false, the user is expected to stage the refcase to the run directory.")
+        if os.path.exists(os.path.join("Buildconf","refcase.input_data_list")):
+            os.remove(os.path.join("Buildconf","refcase.input_data_list"))
     return True
 
 def check_input_data(case, protocol="svn", address=None, input_data_root=None, data_list_dir="Buildconf", download=False):

{"golden_diff": "diff --git a/scripts/lib/CIME/case/check_input_data.py b/scripts/lib/CIME/case/check_input_data.py\n--- a/scripts/lib/CIME/case/check_input_data.py\n+++ b/scripts/lib/CIME/case/check_input_data.py\n@@ -48,7 +48,7 @@\n                                    input_data_root=input_data_root, data_list_dir=data_list_dir)\n         if download and not success:\n             success = _downloadfromserver(self, input_data_root, data_list_dir)\n-    \n+\n     expect(not download or (download and success), \"Could not find all inputdata on any server\")\n     self.stage_refcase(input_data_root=input_data_root, data_list_dir=data_list_dir)\n     return success\n@@ -123,7 +123,10 @@\n         for cam2file in  glob.iglob(os.path.join(\"{}\",\"*.cam2.*\").format(rundir)):\n             camfile = cam2file.replace(\"cam2\", \"cam\")\n             os.symlink(cam2file, camfile)\n-\n+    elif not get_refcase and run_type != \"startup\":\n+        logger.info(\"GET_REFCASE is false, the user is expected to stage the refcase to the run directory.\")\n+        if os.path.exists(os.path.join(\"Buildconf\",\"refcase.input_data_list\")):\n+            os.remove(os.path.join(\"Buildconf\",\"refcase.input_data_list\"))\n     return True\n \n def check_input_data(case, protocol=\"svn\", address=None, input_data_root=None, data_list_dir=\"Buildconf\", download=False):\n", "issue": "GET_REFCASE wont change from TRUE to FALSE\nIf the user has GET_REFCASE=TRUE a file Buildconf/refcase.input_data_list is created, if in a subsequent run the user sets GET_REFCASE=FALSE this file must be deleted or the model will still attempt to find a refcase.\n", "before_files": [{"content": "\"\"\"\nAPI for checking input for testcase\n\"\"\"\nfrom CIME.XML.standard_module_setup import *\nfrom CIME.utils import SharedArea, find_files, safe_copy, expect\nfrom CIME.XML.inputdata import Inputdata\nimport CIME.Servers\n\nimport glob\n\nlogger = logging.getLogger(__name__)\n\ndef _download_if_in_repo(server, input_data_root, rel_path, isdirectory=False):\n    \"\"\"\n    Return True if successfully downloaded\n    \"\"\"\n    if not server.fileexists(rel_path):\n        return False\n\n    full_path = os.path.join(input_data_root, rel_path)\n    logging.info(\"Trying to download file: '{}' to path '{}'\".format(rel_path, full_path))\n    # Make sure local path exists, create if it does not\n    if isdirectory or full_path.endswith(os.sep):\n        if not os.path.exists(full_path):\n            logger.info(\"Creating directory {}\".format(full_path))\n            os.makedirs(full_path)\n        isdirectory = True\n    elif not os.path.exists(os.path.dirname(full_path)):\n        os.makedirs(os.path.dirname(full_path))\n\n    # Use umask to make sure files are group read/writable. As long as parent directories\n    # have +s, then everything should work.\n    with SharedArea():\n        if isdirectory:\n            return server.getdirectory(rel_path, full_path)\n        else:\n            return server.getfile(rel_path, full_path)\n\n###############################################################################\ndef check_all_input_data(self, protocol=None, address=None, input_data_root=None, data_list_dir=\"Buildconf\", download=True):\n###############################################################################\n    success = False\n    if protocol is not None and address is not None:\n        success = self.check_input_data(protocol=protocol, address=address, download=download,\n                                   input_data_root=input_data_root, data_list_dir=data_list_dir)\n    else:\n        success = self.check_input_data(protocol=protocol, address=address, download=False,\n                                   input_data_root=input_data_root, data_list_dir=data_list_dir)\n        if download and not success:\n            success = _downloadfromserver(self, input_data_root, data_list_dir)\n    \n    expect(not download or (download and success), \"Could not find all inputdata on any server\")\n    self.stage_refcase(input_data_root=input_data_root, data_list_dir=data_list_dir)\n    return success\n\ndef _downloadfromserver(case, input_data_root, data_list_dir):\n    # needs to be downloaded\n    success = False\n    protocol = 'svn'\n    inputdata = Inputdata()\n    while not success and protocol is not None:\n        protocol, address = inputdata.get_next_server()\n        logger.info(\"Checking server {} with protocol {}\".format(address, protocol))\n        success = case.check_input_data(protocol=protocol, address=address, download=True,\n                                        input_data_root=input_data_root, data_list_dir=data_list_dir)\n    return success\n\ndef stage_refcase(self, input_data_root=None, data_list_dir=None):\n    get_refcase  = self.get_value(\"GET_REFCASE\")\n    run_type     = self.get_value(\"RUN_TYPE\")\n    continue_run = self.get_value(\"CONTINUE_RUN\")\n\n    # We do not fully populate the inputdata directory on every\n    # machine and do not expect every user to download the 3TB+ of\n    # data in our inputdata repository. This code checks for the\n    # existence of inputdata in the local inputdata directory and\n    # attempts to download data from the server if it's needed and\n    # missing.\n    if get_refcase and run_type != \"startup\" and not continue_run:\n        din_loc_root = self.get_value(\"DIN_LOC_ROOT\")\n        run_refdate  = self.get_value(\"RUN_REFDATE\")\n        run_refcase  = self.get_value(\"RUN_REFCASE\")\n        run_refdir   = self.get_value(\"RUN_REFDIR\")\n        rundir       = self.get_value(\"RUNDIR\")\n\n        if os.path.isabs(run_refdir):\n            refdir = run_refdir\n        else:\n            refdir = os.path.join(din_loc_root, run_refdir, run_refcase, run_refdate)\n            if not os.path.isdir(refdir):\n                logger.warning(\"Refcase not found in {}, will attempt to download from inputdata\".format(refdir))\n                with open(os.path.join(\"Buildconf\",\"refcase.input_data_list\"),\"w\") as fd:\n                    fd.write(\"refdir = {}{}\".format(refdir, os.sep))\n                if input_data_root is None:\n                    input_data_root = din_loc_root\n                if data_list_dir is None:\n                    data_list_dir = \"Buildconf\"\n                success = _downloadfromserver(self, input_data_root=input_data_root, data_list_dir=data_list_dir)\n                expect(success, \"Could not download refcase from any server\")\n\n        logger.info(\" - Prestaging REFCASE ({}) to {}\".format(refdir, rundir))\n\n        # prestage the reference case's files.\n\n        if (not os.path.exists(rundir)):\n            logger.debug(\"Creating run directory: {}\".format(rundir))\n            os.makedirs(rundir)\n\n        # copy the refcases' rpointer files to the run directory\n        for rpointerfile in glob.iglob(os.path.join(\"{}\",\"*rpointer*\").format(refdir)):\n            logger.info(\"Copy rpointer {}\".format(rpointerfile))\n            safe_copy(rpointerfile, rundir)\n\n        # link everything else\n\n        for rcfile in glob.iglob(os.path.join(refdir,\"*\")):\n            rcbaseline = os.path.basename(rcfile)\n            if not os.path.exists(\"{}/{}\".format(rundir, rcbaseline)):\n                logger.info(\"Staging file {}\".format(rcfile))\n                os.symlink(rcfile, \"{}/{}\".format(rundir, rcbaseline))\n        # Backward compatibility, some old refcases have cam2 in the name\n        # link to local cam file.\n        for cam2file in  glob.iglob(os.path.join(\"{}\",\"*.cam2.*\").format(rundir)):\n            camfile = cam2file.replace(\"cam2\", \"cam\")\n            os.symlink(cam2file, camfile)\n\n    return True\n\ndef check_input_data(case, protocol=\"svn\", address=None, input_data_root=None, data_list_dir=\"Buildconf\", download=False):\n    \"\"\"\n    Return True if no files missing\n    \"\"\"\n    case.load_env(reset=True)\n    # Fill in defaults as needed\n    input_data_root = case.get_value(\"DIN_LOC_ROOT\") if input_data_root is None else input_data_root\n\n    expect(os.path.isdir(input_data_root), \"Invalid input_data_root directory: '{}'\".format(input_data_root))\n    expect(os.path.isdir(data_list_dir), \"Invalid data_list_dir directory: '{}'\".format(data_list_dir))\n\n    data_list_files = find_files(data_list_dir, \"*.input_data_list\")\n    expect(data_list_files, \"No .input_data_list files found in dir '{}'\".format(data_list_dir))\n\n    no_files_missing = True\n\n    if download:\n        if protocol not in vars(CIME.Servers):\n            logger.warning(\"Client protocol {} not enabled\".format(protocol))\n            return False\n\n        if protocol == \"svn\":\n            server = CIME.Servers.SVN(address)\n        elif protocol == \"gftp\":\n            server = CIME.Servers.GridFTP(address)\n        elif protocol == \"ftp\":\n            server = CIME.Servers.FTP(address)\n        elif protocol == \"wget\":\n            server = CIME.Servers.WGET(address)\n        else:\n            expect(False, \"Unsupported inputdata protocol: {}\".format(protocol))\n\n\n\n    for data_list_file in data_list_files:\n        logging.info(\"Loading input file list: '{}'\".format(data_list_file))\n        with open(data_list_file, \"r\") as fd:\n            lines = fd.readlines()\n\n        for line in lines:\n            line = line.strip()\n            if (line and not line.startswith(\"#\")):\n                tokens = line.split('=')\n                description, full_path = tokens[0].strip(), tokens[1].strip()\n                if description.endswith('datapath'):\n                    continue\n                if(full_path):\n                    # expand xml variables\n                    full_path = case.get_resolved_value(full_path)\n                    rel_path  = full_path.replace(input_data_root, \"\")\n                    model = os.path.basename(data_list_file).split('.')[0]\n\n                    if (\"/\" in rel_path and rel_path == full_path):\n                        # User pointing to a file outside of input_data_root, we cannot determine\n                        # rel_path, and so cannot download the file. If it already exists, we can\n                        # proceed\n                        if not os.path.exists(full_path):\n                            logging.warning(\"Model {} missing file {} = '{}'\".format(model, description, full_path))\n                            if download:\n                                logging.warning(\"    Cannot download file since it lives outside of the input_data_root '{}'\".format(input_data_root))\n                            no_files_missing = False\n                        else:\n                            logging.debug(\"  Found input file: '{}'\".format(full_path))\n\n                    else:\n                        # There are some special values of rel_path that\n                        # we need to ignore - some of the component models\n                        # set things like 'NULL' or 'same_as_TS' -\n                        # basically if rel_path does not contain '/' (a\n                        # directory tree) you can assume it's a special\n                        # value and ignore it (perhaps with a warning)\n                        if (\"/\" in rel_path and not os.path.exists(full_path)):\n                            logging.warning(\"  Model {} missing file {} = '{}'\".format(model, description, full_path))\n                            no_files_missing = False\n\n                            if (download):\n                                no_files_missing = _download_if_in_repo(server, input_data_root, rel_path.strip(os.sep),\n                                                               isdirectory=rel_path.endswith(os.sep))\n                        else:\n                            logging.debug(\"  Already had input file: '{}'\".format(full_path))\n\n                else:\n                    model = os.path.basename(data_list_file).split('.')[0]\n                    logging.warning(\"Model {} no file specified for {}\".format(model, description))\n\n    return no_files_missing\n", "path": "scripts/lib/CIME/case/check_input_data.py"}], "after_files": [{"content": "\"\"\"\nAPI for checking input for testcase\n\"\"\"\nfrom CIME.XML.standard_module_setup import *\nfrom CIME.utils import SharedArea, find_files, safe_copy, expect\nfrom CIME.XML.inputdata import Inputdata\nimport CIME.Servers\n\nimport glob\n\nlogger = logging.getLogger(__name__)\n\ndef _download_if_in_repo(server, input_data_root, rel_path, isdirectory=False):\n    \"\"\"\n    Return True if successfully downloaded\n    \"\"\"\n    if not server.fileexists(rel_path):\n        return False\n\n    full_path = os.path.join(input_data_root, rel_path)\n    logging.info(\"Trying to download file: '{}' to path '{}'\".format(rel_path, full_path))\n    # Make sure local path exists, create if it does not\n    if isdirectory or full_path.endswith(os.sep):\n        if not os.path.exists(full_path):\n            logger.info(\"Creating directory {}\".format(full_path))\n            os.makedirs(full_path)\n        isdirectory = True\n    elif not os.path.exists(os.path.dirname(full_path)):\n        os.makedirs(os.path.dirname(full_path))\n\n    # Use umask to make sure files are group read/writable. As long as parent directories\n    # have +s, then everything should work.\n    with SharedArea():\n        if isdirectory:\n            return server.getdirectory(rel_path, full_path)\n        else:\n            return server.getfile(rel_path, full_path)\n\n###############################################################################\ndef check_all_input_data(self, protocol=None, address=None, input_data_root=None, data_list_dir=\"Buildconf\", download=True):\n###############################################################################\n    success = False\n    if protocol is not None and address is not None:\n        success = self.check_input_data(protocol=protocol, address=address, download=download,\n                                   input_data_root=input_data_root, data_list_dir=data_list_dir)\n    else:\n        success = self.check_input_data(protocol=protocol, address=address, download=False,\n                                   input_data_root=input_data_root, data_list_dir=data_list_dir)\n        if download and not success:\n            success = _downloadfromserver(self, input_data_root, data_list_dir)\n\n    expect(not download or (download and success), \"Could not find all inputdata on any server\")\n    self.stage_refcase(input_data_root=input_data_root, data_list_dir=data_list_dir)\n    return success\n\ndef _downloadfromserver(case, input_data_root, data_list_dir):\n    # needs to be downloaded\n    success = False\n    protocol = 'svn'\n    inputdata = Inputdata()\n    while not success and protocol is not None:\n        protocol, address = inputdata.get_next_server()\n        logger.info(\"Checking server {} with protocol {}\".format(address, protocol))\n        success = case.check_input_data(protocol=protocol, address=address, download=True,\n                                        input_data_root=input_data_root, data_list_dir=data_list_dir)\n    return success\n\ndef stage_refcase(self, input_data_root=None, data_list_dir=None):\n    get_refcase  = self.get_value(\"GET_REFCASE\")\n    run_type     = self.get_value(\"RUN_TYPE\")\n    continue_run = self.get_value(\"CONTINUE_RUN\")\n\n    # We do not fully populate the inputdata directory on every\n    # machine and do not expect every user to download the 3TB+ of\n    # data in our inputdata repository. This code checks for the\n    # existence of inputdata in the local inputdata directory and\n    # attempts to download data from the server if it's needed and\n    # missing.\n    if get_refcase and run_type != \"startup\" and not continue_run:\n        din_loc_root = self.get_value(\"DIN_LOC_ROOT\")\n        run_refdate  = self.get_value(\"RUN_REFDATE\")\n        run_refcase  = self.get_value(\"RUN_REFCASE\")\n        run_refdir   = self.get_value(\"RUN_REFDIR\")\n        rundir       = self.get_value(\"RUNDIR\")\n\n        if os.path.isabs(run_refdir):\n            refdir = run_refdir\n        else:\n            refdir = os.path.join(din_loc_root, run_refdir, run_refcase, run_refdate)\n            if not os.path.isdir(refdir):\n                logger.warning(\"Refcase not found in {}, will attempt to download from inputdata\".format(refdir))\n                with open(os.path.join(\"Buildconf\",\"refcase.input_data_list\"),\"w\") as fd:\n                    fd.write(\"refdir = {}{}\".format(refdir, os.sep))\n                if input_data_root is None:\n                    input_data_root = din_loc_root\n                if data_list_dir is None:\n                    data_list_dir = \"Buildconf\"\n                success = _downloadfromserver(self, input_data_root=input_data_root, data_list_dir=data_list_dir)\n                expect(success, \"Could not download refcase from any server\")\n\n        logger.info(\" - Prestaging REFCASE ({}) to {}\".format(refdir, rundir))\n\n        # prestage the reference case's files.\n\n        if (not os.path.exists(rundir)):\n            logger.debug(\"Creating run directory: {}\".format(rundir))\n            os.makedirs(rundir)\n\n        # copy the refcases' rpointer files to the run directory\n        for rpointerfile in glob.iglob(os.path.join(\"{}\",\"*rpointer*\").format(refdir)):\n            logger.info(\"Copy rpointer {}\".format(rpointerfile))\n            safe_copy(rpointerfile, rundir)\n\n        # link everything else\n\n        for rcfile in glob.iglob(os.path.join(refdir,\"*\")):\n            rcbaseline = os.path.basename(rcfile)\n            if not os.path.exists(\"{}/{}\".format(rundir, rcbaseline)):\n                logger.info(\"Staging file {}\".format(rcfile))\n                os.symlink(rcfile, \"{}/{}\".format(rundir, rcbaseline))\n        # Backward compatibility, some old refcases have cam2 in the name\n        # link to local cam file.\n        for cam2file in  glob.iglob(os.path.join(\"{}\",\"*.cam2.*\").format(rundir)):\n            camfile = cam2file.replace(\"cam2\", \"cam\")\n            os.symlink(cam2file, camfile)\n    elif not get_refcase and run_type != \"startup\":\n        logger.info(\"GET_REFCASE is false, the user is expected to stage the refcase to the run directory.\")\n        if os.path.exists(os.path.join(\"Buildconf\",\"refcase.input_data_list\")):\n            os.remove(os.path.join(\"Buildconf\",\"refcase.input_data_list\"))\n    return True\n\ndef check_input_data(case, protocol=\"svn\", address=None, input_data_root=None, data_list_dir=\"Buildconf\", download=False):\n    \"\"\"\n    Return True if no files missing\n    \"\"\"\n    case.load_env(reset=True)\n    # Fill in defaults as needed\n    input_data_root = case.get_value(\"DIN_LOC_ROOT\") if input_data_root is None else input_data_root\n\n    expect(os.path.isdir(input_data_root), \"Invalid input_data_root directory: '{}'\".format(input_data_root))\n    expect(os.path.isdir(data_list_dir), \"Invalid data_list_dir directory: '{}'\".format(data_list_dir))\n\n    data_list_files = find_files(data_list_dir, \"*.input_data_list\")\n    expect(data_list_files, \"No .input_data_list files found in dir '{}'\".format(data_list_dir))\n\n    no_files_missing = True\n\n    if download:\n        if protocol not in vars(CIME.Servers):\n            logger.warning(\"Client protocol {} not enabled\".format(protocol))\n            return False\n\n        if protocol == \"svn\":\n            server = CIME.Servers.SVN(address)\n        elif protocol == \"gftp\":\n            server = CIME.Servers.GridFTP(address)\n        elif protocol == \"ftp\":\n            server = CIME.Servers.FTP(address)\n        elif protocol == \"wget\":\n            server = CIME.Servers.WGET(address)\n        else:\n            expect(False, \"Unsupported inputdata protocol: {}\".format(protocol))\n\n\n\n    for data_list_file in data_list_files:\n        logging.info(\"Loading input file list: '{}'\".format(data_list_file))\n        with open(data_list_file, \"r\") as fd:\n            lines = fd.readlines()\n\n        for line in lines:\n            line = line.strip()\n            if (line and not line.startswith(\"#\")):\n                tokens = line.split('=')\n                description, full_path = tokens[0].strip(), tokens[1].strip()\n                if description.endswith('datapath'):\n                    continue\n                if(full_path):\n                    # expand xml variables\n                    full_path = case.get_resolved_value(full_path)\n                    rel_path  = full_path.replace(input_data_root, \"\")\n                    model = os.path.basename(data_list_file).split('.')[0]\n\n                    if (\"/\" in rel_path and rel_path == full_path):\n                        # User pointing to a file outside of input_data_root, we cannot determine\n                        # rel_path, and so cannot download the file. If it already exists, we can\n                        # proceed\n                        if not os.path.exists(full_path):\n                            logging.warning(\"Model {} missing file {} = '{}'\".format(model, description, full_path))\n                            if download:\n                                logging.warning(\"    Cannot download file since it lives outside of the input_data_root '{}'\".format(input_data_root))\n                            no_files_missing = False\n                        else:\n                            logging.debug(\"  Found input file: '{}'\".format(full_path))\n\n                    else:\n                        # There are some special values of rel_path that\n                        # we need to ignore - some of the component models\n                        # set things like 'NULL' or 'same_as_TS' -\n                        # basically if rel_path does not contain '/' (a\n                        # directory tree) you can assume it's a special\n                        # value and ignore it (perhaps with a warning)\n                        if (\"/\" in rel_path and not os.path.exists(full_path)):\n                            logging.warning(\"  Model {} missing file {} = '{}'\".format(model, description, full_path))\n                            no_files_missing = False\n\n                            if (download):\n                                no_files_missing = _download_if_in_repo(server, input_data_root, rel_path.strip(os.sep),\n                                                               isdirectory=rel_path.endswith(os.sep))\n                        else:\n                            logging.debug(\"  Already had input file: '{}'\".format(full_path))\n\n                else:\n                    model = os.path.basename(data_list_file).split('.')[0]\n                    logging.warning(\"Model {} no file specified for {}\".format(model, description))\n\n    return no_files_missing\n", "path": "scripts/lib/CIME/case/check_input_data.py"}]}