Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor codebase of 'buildtest inspect' #1466

Merged
merged 3 commits into from
May 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 68 additions & 60 deletions buildtest/cli/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import re
import sys

from buildtest.cli.report import Report
from buildtest.defaults import console
from buildtest.utils.file import read_file, resolve_path
from buildtest.utils.tools import checkColor
Expand All @@ -12,46 +11,6 @@
from rich.table import Table


def inspect_cmd(args, configuration, report_file=None):
"""Entry point for ``buildtest inspect`` command

Args:
args (dict): Parsed arguments from `ArgumentParser.parse_args <https://docs.python.org/3/library/argparse.html#argparse.ArgumentParser.parse_args>`_
"""

report = Report(configuration=configuration, report_file=report_file)

# implements command 'buildtest inspect list'
if args.inspect in ["list", "l"]:
inspect_list(
report,
terse=args.terse,
no_header=args.no_header,
builder=args.builder,
color=args.color,
pager=args.pager,
row_count=args.row_count,
)
return

# implements command 'buildtest inspect name'
if args.inspect in ["name", "n"]:
inspect_by_name(report, args.name, args.pager)
return

if args.inspect in ["query", "q"]:
inspect_query(report, args, args.pager)
return

if args.inspect in ["buildspec", "b"]:
inspect_buildspec(
report,
input_buildspecs=args.buildspec,
all_records=args.all,
pager=args.pager,
)


def fetch_test_names(report, names):
"""Return a list of builders given input test names by search the report file for valid records. If test is found it will be returned as a builder name. If names
are specified without test ID then we retrieve latest record for test name. If names are specified with ID we find the first matching test record.
Expand Down Expand Up @@ -209,16 +168,31 @@ def inspect_list(
console.print(inspect_table)


def print_by_query(report, args):
def print_by_query(
report,
name,
theme=None,
output=None,
error=None,
testpath=None,
buildscript=None,
buildenv=None,
):
"""This method prints the test records when they are queried using ``buildtest inspect query`` command.

Args:
report (str): Path to report file
args (dict): Parsed arguments from `ArgumentParser.parse_args <https://docs.python.org/3/library/argparse.html#argparse.ArgumentParser.parse_args>`_
report (buildtest.cli.report.Report): An instance of Report class
name (list): List of test names to query
theme (str, optional): Specify pygments theme for syntax highlighting
output (bool, optional): Print output file when set to True
error (bool, optional): Print error file when set to True
testpath (bool, optional): Print testpath when set to True
buildscript (bool, optional): Print buildscript when set to True
buildenv (bool, optional): Print buildenv when set to True
"""

records = {}
query_builders = fetch_test_names(report, args.name)
query_builders = fetch_test_names(report, name)

for builder in query_builders:
tid = builder.split("/")[1]
Expand All @@ -231,7 +205,7 @@ def print_by_query(report, args):
for name, test_record in records.items():
for tests in test_record:
for full_id, test in tests.items():
theme = args.theme or "monokai"
theme = theme or "monokai"

console.rule(f"[cyan]{name}/{full_id}")

Expand All @@ -257,67 +231,101 @@ def print_by_query(report, args):
console.print(table)

# print content of output file when 'buildtest inspect query --output' is set
if args.output:
if output:
content = read_file(test["outfile"])
console.rule(f"Output File: {test['outfile']}")

syntax = Syntax(content, "text")
console.print(syntax)

# print content of error file when 'buildtest inspect query --error' is set
if args.error:
if error:
content = read_file(test["errfile"])
console.rule(f"Error File: {test['errfile']}")

syntax = Syntax(content, "text", theme=theme)
console.print(syntax)

# print content of testpath when 'buildtest inspect query --testpath' is set
if args.testpath:
if testpath:
content = read_file(test["testpath"])
console.rule(f"Test File: {test['testpath']}")

syntax = Syntax(content, "shell", theme=theme)
console.print(syntax)

# print content of build script when 'buildtest inspect query --buildscript' is set
if args.buildscript:
if buildscript:
content = read_file(test["build_script"])
console.rule(f"Test File: {test['build_script']}")

syntax = Syntax(content, lexer="shell", theme=theme)
console.print(syntax)

if args.buildenv:
if buildenv:
content = read_file(test["buildenv"])
console.rule(f"Test File: {test['buildenv']}")

syntax = Syntax(content, lexer="text", theme=theme)
console.print(syntax)


def inspect_query(report, args, pager=None):
def inspect_query(
report,
name,
theme=None,
output=None,
error=None,
testpath=None,
buildscript=None,
buildenv=None,
pager=None,
):
"""Entry point for ``buildtest inspect query`` command.

Args:
report (str): Path to report file
args (dict): Parsed arguments from `ArgumentParser.parse_args <https://docs.python.org/3/library/argparse.html#argparse.ArgumentParser.parse_args>`_
report (buildtest.cli.report.Report): An instance of Report class
name (list): List of test names to query
theme (str, optional): Specify pygments theme for syntax highlighting
output (bool, optional): Print output file when set to True
error (bool, optional): Print error file when set to True
testpath (bool, optional): Print testpath when set to True
buildscript (bool, optional): Print buildscript when set to True
buildenv (bool, optional): Print buildenv when set to True
pager (bool, optional): Print output in paging format
"""

if pager:
with console.pager():
print_by_query(report, args)
print_by_query(
report,
name=name,
theme=theme,
output=output,
error=error,
testpath=testpath,
buildscript=buildscript,
buildenv=buildenv,
)
return

print_by_query(report, args)
print_by_query(
report,
name=name,
theme=theme,
output=output,
error=error,
testpath=testpath,
buildscript=buildscript,
buildenv=buildenv,
)


def inspect_buildspec(report, input_buildspecs, all_records, pager=None):
def inspect_buildspec(report, input_buildspecs, all_records=None, pager=None):
"""This method implements command ``buildtest inspect buildspec``

Args:
report (str): Path to report file
report (buildtest.cli.report.Report): An instance of Report class
input_buildspecs (list): List of buildspecs to search in report file. This is specified as positional arguments to ``buildtest inspect buildspec``
all_records (bool): Determine whether to display all records for every test that matches the buildspec. By default we retrieve the latest record.
pager (bool, optional): Print output in paging format
Expand Down Expand Up @@ -386,7 +394,7 @@ def print_by_name(report, names):
"""This method prints test records by given name in JSON format.

Args:
report (str): Path to report file
report (buildtest.cli.report.Report): An instance of Report class
names (list): List of test names to search in report file. This is specified as positional arguments to ``buildtest inspect name``
"""

Expand Down Expand Up @@ -422,7 +430,7 @@ def inspect_by_name(report, names, pager=None):
buildtest inspect name exit1_fail/123

Args:
report (str): Path to report file
report (buildtest.cli.report.Report): An instance of Report class
names (list): List of test names to search in report file. This is specified as positional arguments to ``buildtest inspect name``
pager (bool, optional): Print output in paging format
"""
Expand Down
49 changes: 45 additions & 4 deletions buildtest/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,14 @@
from buildtest.cli.helpcolor import print_available_colors
from buildtest.cli.history import build_history
from buildtest.cli.info import buildtest_info
from buildtest.cli.inspect import inspect_cmd
from buildtest.cli.inspect import (
inspect_buildspec,
inspect_by_name,
inspect_list,
inspect_query,
)
from buildtest.cli.path import path_cmd
from buildtest.cli.report import report_cmd
from buildtest.cli.report import Report, report_cmd
from buildtest.cli.schema import schema_cmd
from buildtest.cli.stats import stats_cmd
from buildtest.config import SiteConfiguration
Expand Down Expand Up @@ -177,10 +182,11 @@ def main():

if cmd.build_success():
build_history_dir = cmd.get_build_history_dir()

shutil.move(fname, os.path.join(build_history_dir, "output.txt"))

# buildtest build history
elif args.subcommands in ["history", "hy"]:
if args.subcommands in ["history", "hy"]:
build_history(args)

# implementation for 'buildtest buildspec find'
Expand Down Expand Up @@ -240,7 +246,42 @@ def main():

# running buildtest inspect
elif args.subcommands in ["inspect", "it"]:
inspect_cmd(args, configuration=configuration, report_file=report_file)
report = Report(configuration=configuration, report_file=report_file)
if args.inspect in ["list", "l"]:
inspect_list(
report,
terse=args.terse,
no_header=args.no_header,
builder=args.builder,
color=args.color,
pager=args.pager,
row_count=args.row_count,
)
# implements command 'buildtest inspect name'
if args.inspect in ["name", "n"]:
inspect_by_name(report, names=args.name, pager=args.pager)

if args.inspect in ["query", "q"]:
inspect_query(
report,
name=args.name,
theme=args.theme,
output=args.output,
error=args.error,
testpath=args.testpath,
buildscript=args.buildscript,
buildenv=args.buildenv,
pager=args.pager,
)

if args.inspect in ["buildspec", "b"]:
inspect_buildspec(
report,
input_buildspecs=args.buildspec,
all_records=args.all,
pager=args.pager,
)
return

elif args.subcommands in ["stats"]:
stats_cmd(name=args.name, configuration=configuration, report_file=report_file)
Expand Down
2 changes: 1 addition & 1 deletion docs/buildspec_tutorial.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Tutorials Setup


To get started for this tutorial, you will need `docker <https://docs.docker.com/get-docker/>`_ on your machine to pull the container. At NERSC,
you can use `podman <https://podman.io/>`_ or `shifter <https://github.com/NERSC/shifter>`_ to access the container, you will need to start an interactive shell.
you can use `podman <https://docs.podman.io/en/latest/>`_ or `shifter <https://github.com/NERSC/shifter>`_ to access the container, you will need to start an interactive shell.

.. tab-set::

Expand Down
Loading