Skip to content

Commit

Permalink
Make DBHelpAction accept the driver dict as argument
Browse files Browse the repository at this point in the history
  • Loading branch information
shivam committed Mar 30, 2023
1 parent b4e6b5c commit eb37cec
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 75 deletions.
8 changes: 5 additions & 3 deletions kcidb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from kcidb.misc import LIGHT_ASSERTS
# Silence flake8 "imported but unused" warning
from kcidb import io, db, mq, orm, oo, monitor, tests, unittest, misc, argparse # noqa

from kcidb.db import DRIVER_TYPES

# Module's logger
LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -216,7 +216,8 @@ def query_main():
sys.excepthook = misc.log_and_print_excepthook
description = \
"kcidb-query - Query Kernel CI reports"
parser = kcidb.db.argparse.QueryArgumentParser(description=description)
parser = kcidb.db.argparse.QueryArgumentParser(driver_types=DRIVER_TYPES,
description=description)
args = parser.parse_args()
client = Client(database=args.database)
query_iter = client.query_iter(
Expand Down Expand Up @@ -334,7 +335,8 @@ def ingest_main():
sys.excepthook = misc.log_and_print_excepthook
description = 'kcidb-ingest - Load data into a (new) database and ' \
'generate notifications for new and modified objects'
parser = kcidb.db.argparse.ArgumentParser(database="sqlite::memory:",
parser = kcidb.db.argparse.ArgumentParser(driver_types=DRIVER_TYPES,
database="sqlite::memory:",
description=description)
args = parser.parse_args()

Expand Down
24 changes: 16 additions & 8 deletions kcidb/db/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,8 @@ def dump_main():
sys.excepthook = kcidb.misc.log_and_print_excepthook
description = \
'kcidb-db-dump - Dump all data from Kernel CI report database'
parser = argparse.SplitOutputArgumentParser(description=description)
parser = argparse.SplitOutputArgumentParser(driver_types=DRIVER_TYPES,
description=description)
args = parser.parse_args()
client = Client(args.database)
if not client.is_initialized():
Expand All @@ -387,7 +388,8 @@ def query_main():
sys.excepthook = kcidb.misc.log_and_print_excepthook
description = \
"kcidb-db-query - Query objects from Kernel CI report database"
parser = argparse.QueryArgumentParser(description=description)
parser = argparse.QueryArgumentParser(driver_types=DRIVER_TYPES,
description=description)
args = parser.parse_args()
client = Client(args.database)
if not client.is_initialized():
Expand All @@ -412,7 +414,8 @@ def load_main():
sys.excepthook = kcidb.misc.log_and_print_excepthook
description = \
'kcidb-db-load - Load reports into Kernel CI report database'
parser = argparse.ArgumentParser(description=description)
parser = argparse.ArgumentParser(driver_types=DRIVER_TYPES,
description=description)
args = parser.parse_args()
client = Client(args.database)
if not client.is_initialized():
Expand All @@ -428,7 +431,8 @@ def schemas_main():
sys.excepthook = kcidb.misc.log_and_print_excepthook
description = 'kcidb-db-schemas - List available database schemas ' \
'(as <DB>: <I/O>)'
parser = argparse.ArgumentParser(description=description)
parser = argparse.ArgumentParser(driver_types=DRIVER_TYPES,
description=description)
args = parser.parse_args()
client = Client(args.database)
curr_version = client.get_schema()[0] if client.is_initialized() else None
Expand All @@ -450,7 +454,8 @@ def init_main():
"""Execute the kcidb-db-init command-line tool"""
sys.excepthook = kcidb.misc.log_and_print_excepthook
description = 'kcidb-db-init - Initialize a Kernel CI report database'
parser = argparse.ArgumentParser(description=description)
parser = argparse.ArgumentParser(driver_types=DRIVER_TYPES,
description=description)
parser.add_argument(
'--ignore-initialized',
help='Do not fail if the database is already initialized.',
Expand Down Expand Up @@ -482,7 +487,8 @@ def upgrade_main():
"""Execute the kcidb-db-upgrade command-line tool"""
sys.excepthook = kcidb.misc.log_and_print_excepthook
description = 'kcidb-db-upgrade - Upgrade database schema'
parser = argparse.ArgumentParser(description=description)
parser = argparse.ArgumentParser(driver_types=DRIVER_TYPES,
description=description)
parser.add_argument(
'-s', '--schema',
metavar="VERSION",
Expand Down Expand Up @@ -518,7 +524,8 @@ def cleanup_main():
"""Execute the kcidb-db-cleanup command-line tool"""
sys.excepthook = kcidb.misc.log_and_print_excepthook
description = 'kcidb-db-cleanup - Cleanup a Kernel CI report database'
parser = kcidb.db.argparse.ArgumentParser(description=description)
parser = kcidb.db.argparse.ArgumentParser(driver_types=DRIVER_TYPES,
description=description)
parser.add_argument(
'--ignore-not-initialized',
help='Do not fail if the database is not initialized.',
Expand Down Expand Up @@ -547,7 +554,8 @@ def empty_main():
sys.excepthook = kcidb.misc.log_and_print_excepthook
description = 'kcidb-db-empty - Remove all data from a ' \
'Kernel CI report database'
parser = argparse.ArgumentParser(description=description)
parser = argparse.ArgumentParser(driver_types=DRIVER_TYPES,
description=description)
args = parser.parse_args()
client = Client(args.database)
if client.is_initialized():
Expand Down
100 changes: 36 additions & 64 deletions kcidb/db/argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,6 @@
import kcidb.orm
import kcidb.misc
import kcidb.argparse
from kcidb.db import mux, \
bigquery, postgresql, sqlite, json, null # noqa: F401


class MuxDriver(mux.Driver):
"""Kernel CI multiplexing database driver"""

@classmethod
def get_doc(cls):
"""
Get driver documentation.
Returns:
The driver documentation string.
"""
return super().get_doc() + \
"\n Example: postgresql bigquery:kcidb_01"

@classmethod
def get_drivers(cls):
"""
Retrieve a dictionary of driver names and types available for driver's
control.
Returns:
A driver dictionary.
"""
return DRIVER_TYPES


# A dictionary of known driver names and types
DRIVER_TYPES = dict(
bigquery=bigquery.Driver,
postgresql=postgresql.Driver,
sqlite=sqlite.Driver,
json=json.Driver,
null=null.Driver,
mux=MuxDriver,
)


class DBHelpAction(argparse.Action):
Expand Down Expand Up @@ -82,7 +43,7 @@ def __call__(self, parser, namespace, values, option_string=None):
"\n"
"Available drivers and format of their parameter strings "
"follow.\n")
for name, driver in DRIVER_TYPES.items():
for name, driver in parser.driver_types.items():
print(f"\n{name!r} driver\n" +
"-" * (len(name) + 9) + "\n" +
driver.get_doc())
Expand Down Expand Up @@ -119,17 +80,20 @@ class ArgumentParser(kcidb.argparse.ArgumentParser):
Command-line argument parser with common database arguments added.
"""

def __init__(self, *args, database=None, **kwargs):
def __init__(self, driver_types, *args, database=None, **kwargs):
"""
Initialize the parser, adding common database arguments.
Args:
args: Positional arguments to initialize ArgumentParser
with.
database: The default database specification to use, or None to
make database specification required.
kwargs: Keyword arguments to initialize ArgumentParser with.
driver_types: A dictionary of known driver names and types
args: Positional arguments to initialize
ArgumentParser with.
database: The default database specification to use,
or None to make database specification required.
kwargs: Keyword arguments to initialize ArgumentParser
with.
"""
self.driver_types = driver_types
super().__init__(*args, **kwargs)
add_args(self, database=database)

Expand All @@ -140,17 +104,20 @@ class OutputArgumentParser(kcidb.argparse.OutputArgumentParser):
with common database arguments added.
"""

def __init__(self, *args, database=None, **kwargs):
def __init__(self, driver_types, *args, database=None, **kwargs):
"""
Initialize the parser, adding JSON output arguments.
Args:
args: Positional arguments to initialize ArgumentParser
with.
database: The default database specification to use, or None to
make database specification required.
kwargs: Keyword arguments to initialize ArgumentParser with.
driver_types: A dictionary of known driver names and types
args: Positional arguments to initialize
ArgumentParser with.
database: The default database specification to use,
or None to make database specification required.
kwargs: Keyword arguments to initialize ArgumentParser
with.
"""
self.driver_types = driver_types
super().__init__(*args, **kwargs)
add_args(self, database=database)

Expand All @@ -161,17 +128,20 @@ class SplitOutputArgumentParser(kcidb.argparse.SplitOutputArgumentParser):
with common database arguments added.
"""

def __init__(self, *args, database=None, **kwargs):
def __init__(self, driver_types, *args, database=None, **kwargs):
"""
Initialize the parser, adding split-report output arguments.
Args:
args: Positional arguments to initialize ArgumentParser
with.
database: The default database specification to use, or None to
make database specification required.
kwargs: Keyword arguments to initialize ArgumentParser with.
driver_types: A dictionary of known driver names and types
args: Positional arguments to initialize
ArgumentParser with.
database: The default database specification to use,
or None to make database specification required.
kwargs: Keyword arguments to initialize ArgumentParser
with.
"""
self.driver_types = driver_types
super().__init__(*args, **kwargs)
add_args(self, database=database)

Expand All @@ -182,17 +152,19 @@ class QueryArgumentParser(SplitOutputArgumentParser):
Command-line argument parser with common database query arguments added.
"""

def __init__(self, *args, **kwargs):
def __init__(self, driver_types, *args, **kwargs):
"""
Initialize the parser, adding common database query arguments.
Args:
args: Positional arguments to initialize the parent
SplitOutputArgumentParser with.
kwargs: Keyword arguments to initialize the parent
SplitOutputArgumentParser with.
driver_types: A dictionary of known driver names and types
args: Positional arguments to initialize
ArgumentParser with.
kwargs: Keyword arguments to initialize ArgumentParser
with.
"""
super().__init__(*args, **kwargs)
self.driver_types = driver_types
super().__init__(driver_types=driver_types, *args, **kwargs)

self.add_argument(
'-c', '--checkout-id',
Expand Down

0 comments on commit eb37cec

Please sign in to comment.