From d47ae47709be899e1cd99043cb0bf966e1398dd5 Mon Sep 17 00:00:00 2001 From: Nikolai Kondrashov Date: Wed, 6 Nov 2024 20:31:19 +0200 Subject: [PATCH] db: Add kcidb-db-time command-line tool --- kcidb/db/__init__.py | 29 +++++++++++++++++++++++++++++ setup.py | 1 + 2 files changed, 30 insertions(+) diff --git a/kcidb/db/__init__.py b/kcidb/db/__init__.py index 67ff2a3c..5cd62791 100644 --- a/kcidb/db/__init__.py +++ b/kcidb/db/__init__.py @@ -1070,3 +1070,32 @@ def purge_main(): if not client.is_initialized(): raise Exception(f"Database {args.database!r} is not initialized") return 0 if client.purge(before=args.before) else 2 + + +def time_main(): + """Execute the kcidb-db-time command-line tool""" + sys.excepthook = kcidb.misc.log_and_print_excepthook + description = 'kcidb-db-time - Fetch various timestamps from a KCIDB DB' + parser = ArgumentParser(description=description) + parser.add_argument( + 'type', + choices=['first_modified', 'last_modified', 'current'], + help="The type of timestamp to retrieve." + ) + args = parser.parse_args() + client = Client(args.database) + if not client.is_initialized(): + raise Exception(f"Database {args.database!r} is not initialized") + ts = None + if args.type == 'first_modified': + ts = client.get_first_modified() + elif args.type == 'last_modified': + ts = client.get_last_modified() + elif args.type == 'current': + ts = client.get_current_time() + if ts is None: + print("The database is empty, cannot retrieve modification time", + file=sys.stderr) + return 1 + print(ts.isoformat(timespec='microseconds')) + return 0 diff --git a/setup.py b/setup.py index eff4c7cf..44295dd1 100644 --- a/setup.py +++ b/setup.py @@ -89,6 +89,7 @@ "kcidb-db-load = kcidb.db:load_main", "kcidb-db-dump = kcidb.db:dump_main", "kcidb-db-query = kcidb.db:query_main", + "kcidb-db-time = kcidb.db:time_main", "kcidb-mq-email-publisher = kcidb.mq:email_publisher_main", "kcidb-mq-email-subscriber = kcidb.mq:email_subscriber_main", "kcidb-mq-io-publisher = kcidb.mq:io_publisher_main",