1# http://pyrocko.org - GPLv3
2#
3# The Pyrocko Developers, 21st Century
4# ---|P------/S----------~Lg----------
6from __future__ import absolute_import, print_function
8import logging
10from pyrocko import squirrel as sq
12from .. import common
14logger = logging.getLogger('psq.cli.database')
16description = '''
17Actions:
19 env Show current Squirrel environment.
20 stats Show brief information about cached meta-data.
21 stats-full Show detailed information about cached meta-data.
22 files Show paths of files for which cached meta-data is available.
23 files-nnuts Show number of index entries for each known file.
24 nuts Dump index entry summaries.
25 cleanup Remove leftover volatile data entries.
26 remove paths Remove cached meta-data of files matching given patterns.
27'''.strip()
30def setup_subcommand(subparsers):
31 return common.add_parser(
32 subparsers, 'database',
33 help='Database inspection and maintenance.',
34 description=description)
37def setup(parser):
38 parser.add_argument('action', nargs='?', default='env', choices=[
39 'env', 'stats', 'stats-full', 'files', 'files-nnuts', 'nuts',
40 'cleanup', 'remove'])
42 parser.add_argument('path_patterns', nargs='*')
45def call(parser, args):
46 action = args.action
47 if action == 'env':
48 print(sq.get_environment())
50 else:
51 s = sq.Squirrel()
52 db = s.get_database()
53 if action == 'stats':
54 print(db.get_stats())
55 elif action == 'stats-full':
56 print(db.get_stats().dump())
58 elif action == 'files':
59 for path in db.iter_paths():
60 print(path)
62 elif action == 'files-nnuts':
63 for path, nnuts in db.iter_nnuts_by_file():
64 print(path, nnuts)
66 elif action == 'nuts':
67 for path, nuts in db.undig_all():
68 print(path)
69 for nut in nuts:
70 print(' ' + nut.summary)
72 elif action == 'cleanup':
73 n_removed = db._remove_volatile()
74 logger.info('Number of entries removed: %i' % n_removed)
76 elif action == 'remove':
77 if not args.path_patterns:
78 raise sq.SquirrelError(
79 'No path patterns to remove have been specified.')
81 n_removed = 0
82 for path in args.path_patterns:
83 n_removed += db.remove_glob(path)
85 logger.info('Number of entries removed: %i' % n_removed)