From 5adbae33557a48164c110a2ca4037b6529d98f0b Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Apr 26 2016 09:25:42 +0000 Subject: Add a script to remove the cache of one or more widgets as needed --- diff --git a/smart_cache_invalidator.py b/smart_cache_invalidator.py new file mode 100755 index 0000000..b1b32c5 --- /dev/null +++ b/smart_cache_invalidator.py @@ -0,0 +1,119 @@ +#!/usr/bin/env python + +""" +Small script to remove the cache of one or more widgets while keeping the +other ones un-touched. + +This is really useful for development purposes as it allow seeing changes +made to a widget without dropping the entire cache database. +""" + + +import argparse +import os + +import anydbm +import fedmsg.config +import fedmsg.meta + +import hubs.models + +# get the DB session +fedmsg_config = fedmsg.config.load_config() +fedmsg.meta.make_processors(**fedmsg_config) + +session = hubs.models.init(fedmsg_config['hubs.sqlalchemy.uri']) + + +def do_list(args): + ''' List the different widget for which there is data cached. ''' + db = anydbm.open(args.cache_db) + for key in db.keys(): + widget_id = key.split('|')[0] + widget = hubs.models.Widget.get(session, widget_id) + if widget: + print '- Widget cached %s (#%s) in %s' % ( + widget.hub_id, widget_id, widget.plugin) + db.close() + + +def do_clean(args): + ''' Clean the widget for which there is data cached. ''' + for ext in ['.dogpile.lock', '.rw.lock']: + filename = args.cache_db + ext + if os.path.exists(filename): + os.unlink(filename) + + db = anydbm.open(args.cache_db, 'w') + for key in db.keys(): + widget_id = key.split('|')[0] + widget = hubs.models.Widget.get(session, widget_id) + if args.widget in [widget.plugin if widget else '', widget_id]: + del(db[key]) + break + db.close() + + +def setup_parser(): + ''' + Set the main arguments. + ''' + parser = argparse.ArgumentParser(prog="smart_cache_invalidator") + # General connection options + parser.add_argument('--cache-db', dest="cache_db", + default='/var/tmp/fedora-hubs-cache.db', + help="The Fedora-hubs cache database") + + subparsers = parser.add_subparsers(title='actions') + + # List + parser_list = subparsers.add_parser( + 'list', + help='List the different widgets for which there is cached data') + parser_list.set_defaults(func=do_list) + + # Clean + parser_clean = subparsers.add_parser( + 'clean', help='Clean the specified widget') + parser_clean.add_argument( + 'widget', help="Identifier or name of the widget to clean") + parser_clean.set_defaults(func=do_clean) + + return parser + + +def main(): + ''' Main function ''' + return_code = 0 + + # Set up parser for global args + parser = setup_parser() + + # Parse the commandline + try: + arg = parser.parse_args() + except argparse.ArgumentTypeError as err: + print("\nError: {0}".format(err)) + return 1 + + if not os.path.exists(arg.cache_db): + print('No cache DB found') + return 2 + + try: + arg.func(arg) + except KeyboardInterrupt: + print("\nInterrupted by user.") + return_code = 3 + except argparse.ArgumentError as err: + print('{0}'.format(err.message)) + return_code = 4 + except Exception as err: + print('Error: {0}'.format(err)) + return_code = 5 + + return return_code + + +if __name__ == '__main__': + main()