xfstestsdb/xfstestsdb/list.py

88 lines
3.6 KiB
Python

# Copyright 2023 (c) Anna Schumaker.
"""The `xfstestsdb list` command."""
import argparse
from . import colors
from . import commands
from . import sqlite
from . import table
class Command(commands.Command):
"""The `xfstestsdb list` command."""
def __init__(self, subparser: argparse.Action,
sql: sqlite.Connection) -> None:
"""Set up the list command."""
super().__init__(subparser, sql, "list", help="list the xfstest runs")
self.parser.add_argument("--color", metavar="color", nargs="?",
choices=["light", "dark", "none"],
const=colors.get_default_colors(),
default=colors.get_default_colors(),
help="show runs with color output "
f"[default={colors.get_default_colors()}]")
self.parser.add_argument("--device", metavar="device", nargs=1,
help="show runs with a device matching "
"the given pattern")
self.parser.add_argument("--tags", metavar="tags", nargs="?",
const=True, default=False,
help="show runs with a tag matching "
"the given pattern")
self.parser.add_argument("--timestamp", metavar="timestamp", nargs=1,
help="show runs with a timestamp matching "
"the given pattern")
self.parser.add_argument("--xunits", metavar="xunits", nargs="?",
const=True, default=False,
help="show runs with an xunit matching "
"the given pattern")
def do_command(self, args: argparse.Namespace) -> None:
"""Create a new row in the xfstestsdb_runs table."""
tbl = table.Table(["run", "timestamp", "device"],
["r", "c", "l"], args.color)
sql_where = ""
sql_args = []
filter = []
if args.device:
filter.append("device GLOB ?")
sql_args.append(args.device[0])
if args.tags:
tbl.add_column("tags", "l")
if isinstance(args.tags, str):
filter.append("tag GLOB ?")
sql_args.append(args.tags)
else:
filter.append("tag IS NOT NULL")
if args.timestamp:
filter.append("timestamp GLOB ?")
sql_args.append(args.timestamp[0])
if args.xunits:
tbl.add_column("xunits", "l")
if isinstance(args.xunits, str):
filter.append("xunit GLOB ?")
sql_args.append(args.xunits)
else:
filter.append("xunit IS NOT NULL")
if len(filter) > 0:
sql_where = " WHERE " + " AND ".join(filter) + " "
cur = self.sql("""SELECT runid, timestamp, device,
GROUP_CONCAT(DISTINCT tag) as taglist,
GROUP_CONCAT(DISTINCT xunit) as xunitlist
FROM tagged_runs""" + sql_where + """
GROUP BY runid ORDER BY runid, tag, xunit""", *sql_args)
for row in cur.fetchall():
data = [row["runid"], row["timestamp"], row["device"]]
if args.tags:
data.append(row["taglist"])
if args.xunits:
data.append(row["xunitlist"])
tbl.add_row(*data)
if len(tbl.rows) > 0:
print(tbl)