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
11from ..common import SquirrelCommand
13logger = logging.getLogger('psq.cli.database')
15headline = 'Database inspection and maintenance.'
17description = '''%s
19Get information about Squirrel's meta-data cache and database. Where it is,
20what files it knows about and what index entries are available. It also allows
21to do some basic cleanup actions.
22''' % headline
25class Env(SquirrelCommand):
27 def make_subparser(self, subparsers):
28 return subparsers.add_parser(
29 'env',
30 help='Show current Squirrel environment.',
31 description='Show current Squirrel environment.')
33 def run(self, parser, args):
34 env = sq.get_environment()
35 env.path_prefix = env.get_basepath()
36 print(env)
39class Stats(SquirrelCommand):
41 def make_subparser(self, subparsers):
42 return subparsers.add_parser(
43 'stats',
44 help='Show information about cached meta-data.',
45 description='Show information about cached meta-data.')
47 def setup(self, parser):
48 parser.add_argument(
49 '--full',
50 help='Show details.',
51 action='store_true')
53 def run(self, parser, args):
54 s = sq.Squirrel()
55 db = s.get_database()
56 if args.full:
57 print(db.get_stats().dump())
58 else:
59 print(db.get_stats())
62class Files(SquirrelCommand):
64 def make_subparser(self, subparsers):
65 headline = \
66 'Show paths of files for which cached meta-data is available.'
68 return subparsers.add_parser(
69 'files',
70 help=headline,
71 description=headline)
73 def setup(self, parser):
74 parser.add_argument(
75 '--nnuts',
76 help='Show nut count for each file.',
77 action='store_true')
79 def run(self, parser, args):
80 s = sq.Squirrel()
81 db = s.get_database()
83 if args.nnuts:
84 for path, nnuts in db.iter_nnuts_by_file():
85 print(path, nnuts)
86 else:
87 for path in db.iter_paths():
88 print(path)
91class Nuts(SquirrelCommand):
93 def make_subparser(self, subparsers):
94 headline = \
95 'Dump index entry summaries.'
97 return subparsers.add_parser(
98 'nuts',
99 help=headline,
100 description=headline)
102 def run(self, parser, args):
103 s = sq.Squirrel()
104 db = s.get_database()
105 for path, nuts in db.undig_all():
106 print(path)
107 for nut in nuts:
108 print(' ' + nut.summary)
111class Cleanup(SquirrelCommand):
113 def make_subparser(self, subparsers):
114 headline = \
115 'Remove leftover volatile data entries.'
117 return subparsers.add_parser(
118 'cleanup',
119 help=headline,
120 description=headline)
122 def run(self, parser, args):
123 s = sq.Squirrel()
124 db = s.get_database()
125 n_removed = db._remove_volatile()
126 logger.info('Number of entries removed: %i' % n_removed)
129class Remove(SquirrelCommand):
131 def make_subparser(self, subparsers):
132 headline = \
133 'Remove cached meta-data of files matching given patterns.'
135 return subparsers.add_parser(
136 'remove',
137 help=headline,
138 description=headline)
140 def setup(self, parser):
141 parser.add_argument(
142 'paths',
143 nargs='+',
144 metavar='PATHS',
145 help='Glob patterns of paths to be removed (should be quoted to '
146 'prevent the shell from expanding them).')
148 def run(self, parser, args):
149 s = sq.Squirrel()
150 db = s.get_database()
152 n_removed = 0
153 for path in args.paths:
154 n_removed += db.remove_glob(path)
156 logger.info('Number of entries removed: %i' % n_removed)
159def make_subparser(subparsers):
160 return subparsers.add_parser(
161 'database',
162 help=headline,
163 subcommands=[Env(), Stats(), Files(), Nuts(), Cleanup(), Remove()],
164 description=description)
167def setup(parser):
168 pass
171def run(parser, args):
172 parser.print_help()