prompt: You will be provided with a partial code base and an issue statement explaining a problem to resolve.
  <issue>
  Add --component option to preview_namelists
  It would be useful if users could specify a single component when running `preview_namelists` rather than building namelists for all components when the script is run from the command line in a case directory.
  
  </issue>
  <code>
  [start of scripts/lib/CIME/preview_namelists.py]
  1 """
  2 API for preview namelist
  3 """
  4 
  5 from CIME.XML.standard_module_setup import *
  6 
  7 import glob, shutil, imp
  8 logger = logging.getLogger(__name__)
  9 
  10 def create_dirs(case):
  11     """
  12     Make necessary directories for case
  13     """
  14     # Get data from XML
  15     exeroot  = case.get_value("EXEROOT")
  16     libroot  = case.get_value("LIBROOT")
  17     incroot  = case.get_value("INCROOT")
  18     rundir   = case.get_value("RUNDIR")
  19     caseroot = case.get_value("CASEROOT")
  20 
  21     docdir = os.path.join(caseroot, "CaseDocs")
  22     dirs_to_make = []
  23     models = case.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)):
  32             try:
  33                 logger.debug("Making dir '%s'" % dir_to_make)
  34                 os.makedirs(dir_to_make)
  35             except OSError as e:
  36                 expect(False, "Could not make directory '%s', error: %s" % (dir_to_make, e))
  37 
  38     # As a convenience write the location of the case directory in the bld and run directories
  39     for dir_ in (exeroot, rundir):
  40         with open(os.path.join(dir_,"CASEROOT"),"w+") as fd:
  41             fd.write(caseroot+"\n")
  42 
  43 def create_namelists(case):
  44     """
  45     Create component namelists
  46     """
  47     case.flush()
  48 
  49     create_dirs(case)
  50 
  51     casebuild = case.get_value("CASEBUILD")
  52     caseroot = case.get_value("CASEROOT")
  53     rundir = case.get_value("RUNDIR")
  54 
  55     docdir = os.path.join(caseroot, "CaseDocs")
  56 
  57     # Load modules
  58     case.load_env()
  59 
  60     logger.info("Creating component namelists")
  61 
  62     # Create namelists - must have cpl last in the list below
  63     # Note - cpl must be last in the loop below so that in generating its namelist,
  64     # it can use xml vars potentially set by other component's buildnml scripts
  65     models = case.get_values("COMP_CLASSES")
  66     models += [models.pop(0)]
  67     for model in models:
  68         model_str = model.lower()
  69         config_file = case.get_value("CONFIG_%s_FILE" % model_str.upper())
  70         config_dir = os.path.dirname(config_file)
  71         if model_str == "cpl":
  72             compname = "drv"
  73         else:
  74             compname = case.get_value("COMP_%s" % model_str.upper())
  75 
  76         cmd = os.path.join(config_dir, "buildnml")
  77         do_run_cmd = False
  78         # This code will try to import and run each buildnml as a subroutine
  79         # if that fails it will run it as a program in a seperate shell
  80         try:
  81             with open(cmd, 'r') as f:
  82                 first_line = f.readline()
  83             if "python" in first_line:
  84                 mod = imp.load_source("buildnml", cmd)
  85                 logger.info("   Calling %s buildnml"%compname)
  86                 mod.buildnml(case, caseroot, compname)
  87             else:
  88                 raise SyntaxError
  89         except SyntaxError as detail:
  90             if 'python' in first_line:
  91                 expect(False, detail)
  92             else:
  93                 do_run_cmd = True
  94         except AttributeError:
  95             do_run_cmd = True
  96         except:
  97             raise
  98 
  99         if do_run_cmd:
  100             logger.info("   Running %s buildnml"%compname)
  101             case.flush()
  102             output = run_cmd_no_fail("%s %s" % (cmd, caseroot), verbose=False)
  103             logger.info(output)
  104             # refresh case xml object from file
  105             case.read_xml()
  106 
  107     logger.info("Finished creating component namelists")
  108 
  109     # Save namelists to docdir
  110     if (not os.path.isdir(docdir)):
  111         os.makedirs(docdir)
  112         try:
  113             with open(os.path.join(docdir, "README"), "w") as fd:
  114                 fd.write(" CESM Resolved Namelist Files\n   For documentation only DO NOT MODIFY\n")
  115         except (OSError, IOError) as e:
  116             expect(False, "Failed to write %s/README: %s" % (docdir, e))
  117 
  118     for cpglob in ["*_in_[0-9]*", "*modelio*", "*_in",
  119                    "*streams*txt*", "*stxt", "*maps.rc", "*cism.config*"]:
  120         for file_to_copy in glob.glob(os.path.join(rundir, cpglob)):
  121             logger.debug("Copy file from '%s' to '%s'" % (file_to_copy, docdir))
  122             shutil.copy2(file_to_copy, docdir)
  123 
  124     # Copy over chemistry mechanism docs if they exist
  125     if (os.path.isdir(os.path.join(casebuild, "camconf"))):
  126         for file_to_copy in glob.glob(os.path.join(casebuild, "camconf", "*chem_mech*")):
  127             shutil.copy2(file_to_copy, docdir)
  128
  [end of scripts/lib/CIME/preview_namelists.py]
  </code>
  
  I need you to solve this issue by generating a single patch file that I can apply directly to this repository using git apply. Please respond with a single patch file in the following format.
  <patch>
  diff --git a/file.py b/file.py
  --- a/file.py
  +++ b/file.py
  @@ -1,27 +1,35 @@
   def euclidean(a, b):
  -    while b:
  -        a, b = b, a % b
  -    return a
  +    if b == 0:
  +        return a
  +    return euclidean(b, a % b)
   
   
   def bresenham(x0, y0, x1, y1):
       points = []
       dx = abs(x1 - x0)
       dy = abs(y1 - y0)
  -    sx = 1 if x0 < x1 else -1
  -    sy = 1 if y0 < y1 else -1
  -    err = dx - dy
  +    x, y = x0, y0
  +    sx = -1 if x0 > x1 else 1
  +    sy = -1 if y0 > y1 else 1
   
  -    while True:
  -        points.append((x0, y0))
  -        if x0 == x1 and y0 == y1:
  -            break
  -        e2 = 2 * err
  -        if e2 > -dy:
  -            err -= dy
  -            x0 += sx
  -        if e2 < dx:
  -            err += dx
  -            y0 += sy
  +    if dx > dy:
  +        err = dx / 2.0
  +        while x != x1:
  +            points.append((x, y))
  +            err -= dy
  +            if err < 0:
  +                y += sy
  +                err += dx
  +            x += sx
  +    else:
  +        err = dy / 2.0
  +        while y != y1:
  +            points.append((x, y))
  +            err -= dx
  +            if err < 0:
  +                x += sx
  +                err += dy
  +            y += sy
  +
  +    points.append((x, y))
       return points
  
  </patch>
golden_diff: diff --git a/scripts/lib/CIME/preview_namelists.py b/scripts/lib/CIME/preview_namelists.py
  --- a/scripts/lib/CIME/preview_namelists.py
  +++ b/scripts/lib/CIME/preview_namelists.py
  @@ -40,7 +40,7 @@
           with open(os.path.join(dir_,"CASEROOT"),"w+") as fd:
               fd.write(caseroot+"\n")
   
  -def create_namelists(case):
  +def create_namelists(case, component=None):
       """
       Create component namelists
       """
  @@ -73,36 +73,37 @@
           else:
               compname = case.get_value("COMP_%s" % model_str.upper())
   
  -        cmd = os.path.join(config_dir, "buildnml")
  -        do_run_cmd = False
  -        # This code will try to import and run each buildnml as a subroutine
  -        # if that fails it will run it as a program in a seperate shell
  -        try:
  -            with open(cmd, 'r') as f:
  -                first_line = f.readline()
  -            if "python" in first_line:
  -                mod = imp.load_source("buildnml", cmd)
  -                logger.info("   Calling %s buildnml"%compname)
  -                mod.buildnml(case, caseroot, compname)
  -            else:
  -                raise SyntaxError
  -        except SyntaxError as detail:
  -            if 'python' in first_line:
  -                expect(False, detail)
  -            else:
  +        if component is None or component == model_str:
  +            cmd = os.path.join(config_dir, "buildnml")
  +            do_run_cmd = False
  +            # This code will try to import and run each buildnml as a subroutine
  +            # if that fails it will run it as a program in a seperate shell
  +            try:
  +                with open(cmd, 'r') as f:
  +                    first_line = f.readline()
  +                if "python" in first_line:
  +                    mod = imp.load_source("buildnml", cmd)
  +                    logger.info("   Calling %s buildnml"%compname)
  +                    mod.buildnml(case, caseroot, compname)
  +                else:
  +                    raise SyntaxError
  +            except SyntaxError as detail:
  +                if 'python' in first_line:
  +                    expect(False, detail)
  +                else:
  +                    do_run_cmd = True
  +            except AttributeError:
                   do_run_cmd = True
  -        except AttributeError:
  -            do_run_cmd = True
  -        except:
  -            raise
  -
  -        if do_run_cmd:
  -            logger.info("   Running %s buildnml"%compname)
  -            case.flush()
  -            output = run_cmd_no_fail("%s %s" % (cmd, caseroot), verbose=False)
  -            logger.info(output)
  -            # refresh case xml object from file
  -            case.read_xml()
  +            except:
  +                raise
  +
  +            if do_run_cmd:
  +                logger.info("   Running %s buildnml"%compname)
  +                case.flush()
  +                output = run_cmd_no_fail("%s %s" % (cmd, caseroot), verbose=False)
  +                logger.info(output)
  +                # refresh case xml object from file
  +                case.read_xml()
   
       logger.info("Finished creating component namelists")
  
verification_info: {"golden_diff": "diff --git a/scripts/lib/CIME/preview_namelists.py b/scripts/lib/CIME/preview_namelists.py\n--- a/scripts/lib/CIME/preview_namelists.py\n+++ b/scripts/lib/CIME/preview_namelists.py\n@@ -40,7 +40,7 @@\n         with open(os.path.join(dir_,\"CASEROOT\"),\"w+\") as fd:\n             fd.write(caseroot+\"\\n\")\n \n-def create_namelists(case):\n+def create_namelists(case, component=None):\n     \"\"\"\n     Create component namelists\n     \"\"\"\n@@ -73,36 +73,37 @@\n         else:\n             compname = case.get_value(\"COMP_%s\" % model_str.upper())\n \n-        cmd = os.path.join(config_dir, \"buildnml\")\n-        do_run_cmd = False\n-        # This code will try to import and run each buildnml as a subroutine\n-        # if that fails it will run it as a program in a seperate shell\n-        try:\n-            with open(cmd, 'r') as f:\n-                first_line = f.readline()\n-            if \"python\" in first_line:\n-                mod = imp.load_source(\"buildnml\", cmd)\n-                logger.info(\"   Calling %s buildnml\"%compname)\n-                mod.buildnml(case, caseroot, compname)\n-            else:\n-                raise SyntaxError\n-        except SyntaxError as detail:\n-            if 'python' in first_line:\n-                expect(False, detail)\n-            else:\n+        if component is None or component == model_str:\n+            cmd = os.path.join(config_dir, \"buildnml\")\n+            do_run_cmd = False\n+            # This code will try to import and run each buildnml as a subroutine\n+            # if that fails it will run it as a program in a seperate shell\n+            try:\n+                with open(cmd, 'r') as f:\n+                    first_line = f.readline()\n+                if \"python\" in first_line:\n+                    mod = imp.load_source(\"buildnml\", cmd)\n+                    logger.info(\"   Calling %s buildnml\"%compname)\n+                    mod.buildnml(case, caseroot, compname)\n+                else:\n+                    raise SyntaxError\n+            except SyntaxError as detail:\n+                if 'python' in first_line:\n+                    expect(False, detail)\n+                else:\n+                    do_run_cmd = True\n+            except AttributeError:\n                 do_run_cmd = True\n-        except AttributeError:\n-            do_run_cmd = True\n-        except:\n-            raise\n-\n-        if do_run_cmd:\n-            logger.info(\"   Running %s buildnml\"%compname)\n-            case.flush()\n-            output = run_cmd_no_fail(\"%s %s\" % (cmd, caseroot), verbose=False)\n-            logger.info(output)\n-            # refresh case xml object from file\n-            case.read_xml()\n+            except:\n+                raise\n+\n+            if do_run_cmd:\n+                logger.info(\"   Running %s buildnml\"%compname)\n+                case.flush()\n+                output = run_cmd_no_fail(\"%s %s\" % (cmd, caseroot), verbose=False)\n+                logger.info(output)\n+                # refresh case xml object from file\n+                case.read_xml()\n \n     logger.info(\"Finished creating component namelists\")\n", "issue": "Add --component option to preview_namelists\nIt would be useful if users could specify a single component when running `preview_namelists` rather than building namelists for all components when the script is run from the command line in a case directory.\n", "before_files": [{"content": "\"\"\"\nAPI for preview namelist\n\"\"\"\n\nfrom CIME.XML.standard_module_setup import *\n\nimport glob, shutil, imp\nlogger = logging.getLogger(__name__)\n\ndef create_dirs(case):\n    \"\"\"\n    Make necessary directories for case\n    \"\"\"\n    # Get data from XML\n    exeroot  = case.get_value(\"EXEROOT\")\n    libroot  = case.get_value(\"LIBROOT\")\n    incroot  = case.get_value(\"INCROOT\")\n    rundir   = case.get_value(\"RUNDIR\")\n    caseroot = case.get_value(\"CASEROOT\")\n\n    docdir = os.path.join(caseroot, \"CaseDocs\")\n    dirs_to_make = []\n    models = case.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)):\n            try:\n                logger.debug(\"Making dir '%s'\" % dir_to_make)\n                os.makedirs(dir_to_make)\n            except OSError as e:\n                expect(False, \"Could not make directory '%s', error: %s\" % (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(case):\n    \"\"\"\n    Create component namelists\n    \"\"\"\n    case.flush()\n\n    create_dirs(case)\n\n    casebuild = case.get_value(\"CASEBUILD\")\n    caseroot = case.get_value(\"CASEROOT\")\n    rundir = case.get_value(\"RUNDIR\")\n\n    docdir = os.path.join(caseroot, \"CaseDocs\")\n\n    # Load modules\n    case.load_env()\n\n    logger.info(\"Creating component namelists\")\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 = case.get_values(\"COMP_CLASSES\")\n    models += [models.pop(0)]\n    for model in models:\n        model_str = model.lower()\n        config_file = case.get_value(\"CONFIG_%s_FILE\" % model_str.upper())\n        config_dir = os.path.dirname(config_file)\n        if model_str == \"cpl\":\n            compname = \"drv\"\n        else:\n            compname = case.get_value(\"COMP_%s\" % model_str.upper())\n\n        cmd = os.path.join(config_dir, \"buildnml\")\n        do_run_cmd = False\n        # This code will try to import and run each buildnml as a subroutine\n        # if that fails it will run it as a program in a seperate shell\n        try:\n            with open(cmd, 'r') as f:\n                first_line = f.readline()\n            if \"python\" in first_line:\n                mod = imp.load_source(\"buildnml\", cmd)\n                logger.info(\"   Calling %s buildnml\"%compname)\n                mod.buildnml(case, caseroot, compname)\n            else:\n                raise SyntaxError\n        except SyntaxError as detail:\n            if 'python' in first_line:\n                expect(False, detail)\n            else:\n                do_run_cmd = True\n        except AttributeError:\n            do_run_cmd = True\n        except:\n            raise\n\n        if do_run_cmd:\n            logger.info(\"   Running %s buildnml\"%compname)\n            case.flush()\n            output = run_cmd_no_fail(\"%s %s\" % (cmd, caseroot), verbose=False)\n            logger.info(output)\n            # refresh case xml object from file\n            case.read_xml()\n\n    logger.info(\"Finished creating component namelists\")\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 %s/README: %s\" % (docdir, e))\n\n    for cpglob in [\"*_in_[0-9]*\", \"*modelio*\", \"*_in\",\n                   \"*streams*txt*\", \"*stxt\", \"*maps.rc\", \"*cism.config*\"]:\n        for file_to_copy in glob.glob(os.path.join(rundir, cpglob)):\n            logger.debug(\"Copy file from '%s' to '%s'\" % (file_to_copy, docdir))\n            shutil.copy2(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            shutil.copy2(file_to_copy, docdir)\n", "path": "scripts/lib/CIME/preview_namelists.py"}]}