Skip to content

Commit

Permalink
Merge pull request ESMCI#4642 from ESMCI/fix-list_e3sm_tests
Browse files Browse the repository at this point in the history
Fix list_e3sm_tests tool

Fixes list_e3sm_tests tool.

Adds description with examples
Adds option to print compset long names
Fixes listing test suites by default
Removes use of parse_args_and_handle_standard_logging_options
Removes custom usage
Test suite: n/a
Test baseline: n/a
Test namelist changes: n/a
Test status: n/a

Fixes ESMCI#4628

User interface changes?:
Update gh-pages html (Y/N)?:
  • Loading branch information
jgfouca authored Jul 11, 2024
2 parents fcb9c6e + 38bca92 commit ca6c0cd
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 61 deletions.
154 changes: 94 additions & 60 deletions CIME/Tools/list_e3sm_tests
Original file line number Diff line number Diff line change
Expand Up @@ -6,90 +6,124 @@ list tested grids, compsets, etc.
"""

from standard_script_setup import *
from CIME.utils import expect
from CIME import utils
from CIME import get_tests
from CIME.XML.files import Files
from CIME.XML.compsets import Compsets

import sys, argparse, os
import argparse
import logging


logger = logging.getLogger(__name__)


def parse_command_line():
description = """This tool will print all test suite names.
If any test suite names are provided, then all `term` values for the tests in the suites will be listed.
Examples
--------
>>> %(prog)s
e3sm_developer
cime_tiny
>>> %(prog)s e3sm_developer
ERS.f19_g16_rx1.A.docker_gnu
NCK.f19_g16_rx1.A.docker_gnu
>>> %(prog)s -t compsets e3sm_developer
A
F2010
I1850ELM
>>> %(prog)s -t grids e3sm_developer
f19_g16_rx1"""

###############################################################################
def parse_command_line(args, description):
###############################################################################
parser = argparse.ArgumentParser(
usage="""\n{0} <thing-to-list> [<test category> <test category> ...] [--verbose]
OR
{0} --help
\033[1mEXAMPLES:\033[0m
\033[1;32m# List all tested compsets \033[0m
> {0} compsets
\033[1;32m# List all compsets tested by e3sm_developer \033[0m
> {0} compsets e3sm_developer
\033[1;32m# List all grids tested by e3sm_developer \033[0m
> {0} grid e3sm_developer
""".format(
os.path.basename(args[0])
),
description=description,
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
description=description, formatter_class=argparse.RawDescriptionHelpFormatter
)

CIME.utils.setup_standard_logging_options(parser)

parser.add_argument(
"suites",
nargs="+",
help="The tests suites to list. Test suites: {}".format(
", ".join(get_tests.get_test_suites())
),
nargs="*",
help="The test suites to list.",
)

parser.add_argument(
"-t",
"--thing-to-list",
"--term",
choices=("compsets", "grids", "testcases", "tests"),
default="tests",
help="The thing you want to list",
help="Term of the test name to print.",
)

args = CIME.utils.parse_args_and_handle_standard_logging_options(args, parser)
compsets_group = parser.add_argument_group("Compset options")

compsets_group.add_argument(
"-l", "--long", action="store_true", help="Prints the longname of compsets."
)

utils.setup_standard_logging_options(parser)

kwargs = vars(parser.parse_args())

utils.configure_logging(**kwargs)

return kwargs


def list_tests(term, suites, long, **_):
values = [x for s in suites for x in get_tests.get_test_suite(s)]

if term != "tests":
terms = [utils.parse_test_name(x) for x in values]

index_map = {"compsets": 3, "grids": 2, "testcases": 0}

index = index_map[term]

values = set(x[index] for x in terms)

if long and term == "compsets":
compset_longnames = get_compset_longnames()

values = set(compset_longnames[x] for x in values)

print("\n".join(sorted(values)))

logger.info(f"Found {len(values)!r} {term}")


def get_compset_longnames():
files = Files()

names = files.get_components("COMPSETS_SPEC_FILE")

values = {}

if not args.suites:
args.suites = get_tests.get_test_suites()
for n in names:
comp_file = files.get_value("COMPSETS_SPEC_FILE", attribute={"component": n})

return args.thing_to_list, args.suites
values.update({x for x in Compsets(comp_file)})

return values

###############################################################################
def list_tests(thing_to_list, suites):
###############################################################################
things = set()
for suite in suites:
tests = get_tests.get_test_suite(suite)
for test in tests:
testcase, _, grid, compset = CIME.utils.parse_test_name(test)[:4]
if thing_to_list == "compsets":
things.add(compset)
elif thing_to_list == "grids":
things.add(grid)
elif thing_to_list == "testcases":
things.add(testcase)
elif thing_to_list == "tests":
things.add(test)
else:
expect(False, "Unrecognized thing to list '{}'".format(thing_to_list))

print("Tested {} for test suites: {}".format(thing_to_list, ", ".join(suites)))
for item in sorted(things):
print(" {}".format(item))
def _main_func():
args = parse_command_line()

if len(args["suites"]) == 0:
test_suites = sorted(get_tests.get_test_suites())

###############################################################################
def _main_func(description):
###############################################################################
thing_to_list, suites = parse_command_line(sys.argv, description)
for suite in test_suites:
print(suite)

list_tests(thing_to_list, suites)
logger.info(f"Found {len(test_suites)!r} test suites")
else:
list_tests(**args)


if __name__ == "__main__":
_main_func(__doc__)
_main_func()
22 changes: 22 additions & 0 deletions CIME/XML/compsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ def __init__(self, infile=None, files=None):
files = Files()
schema = files.get_schema("COMPSETS_SPEC_FILE")
GenericXML.__init__(self, infile, schema=schema)
self._index = 0
self._compsets = None

def get_compset_match(self, name):
"""
Expand Down Expand Up @@ -108,3 +110,23 @@ def get_compset_longnames(self):
for comp in compset_nodes:
longnames.append(self.text(self.get_child("lname", root=comp)))
return longnames

def __iter__(self):
self._index = 0
self._compsets = self.get_children("compset")

return self

def __next__(self):
if self._index >= len(self._compsets):
raise StopIteration()

value = self._compsets[self._index]

alias = self.text(self.get_child("alias", root=value))

lname = self.text(self.get_child("lname", root=value))

self._index += 1

return alias, lname
3 changes: 2 additions & 1 deletion CIME/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Common functions used by cime python scripts
Warning: you cannot use CIME Classes in this module as it causes circular dependencies
"""

import shlex
import configparser
import io, logging, gzip, sys, os, time, re, shutil, glob, string, random, importlib, fnmatch
Expand Down Expand Up @@ -1666,7 +1667,7 @@ def filter(self, record):
return 1 if record.levelno < self.max_level else 0


def configure_logging(verbose, debug, silent):
def configure_logging(verbose, debug, silent, **_):
root_logger = logging.getLogger()

verbose_formatter = logging.Formatter(
Expand Down

0 comments on commit ca6c0cd

Please sign in to comment.