1# http://pyrocko.org - GPLv3
2#
3# The Pyrocko Developers, 21st Century
4# ---|P------/S----------~Lg----------
6import logging
8from pyrocko import squirrel as sq
9from ..common import SquirrelCommand
11logger = logging.getLogger('psq.cli.database')
13headline = 'Database inspection and maintenance.'
15description = '''%s
17Get information about Squirrel's meta-data cache and database. Where it is,
18what files it knows about and what index entries are available. It also allows
19to do some basic cleanup actions.
20''' % headline
23class Env(SquirrelCommand):
25 def make_subparser(self, subparsers):
26 return subparsers.add_parser(
27 'env',
28 help='Show current Squirrel environment.',
29 description='Show current Squirrel environment.')
31 def run(self, parser, args):
32 env = sq.get_environment()
33 env.path_prefix = env.get_basepath()
34 print(env)
37class Stats(SquirrelCommand):
39 def make_subparser(self, subparsers):
40 return subparsers.add_parser(
41 'stats',
42 help='Show information about cached meta-data.',
43 description='Show information about cached meta-data.')
45 def setup(self, parser):
46 parser.add_argument(
47 '--full',
48 help='Show details.',
49 action='store_true')
51 def run(self, parser, args):
52 s = sq.Squirrel()
53 db = s.get_database()
54 if args.full:
55 print(db.get_stats().dump())
56 else:
57 print(db.get_stats())
60class Files(SquirrelCommand):
62 def make_subparser(self, subparsers):
63 headline = \
64 'Show paths of files for which cached meta-data is available.'
66 return subparsers.add_parser(
67 'files',
68 help=headline,
69 description=headline)
71 def setup(self, parser):
72 parser.add_argument(
73 '--nnuts',
74 help='Show nut count for each file.',
75 action='store_true')
77 def run(self, parser, args):
78 s = sq.Squirrel()
79 db = s.get_database()
81 if args.nnuts:
82 for path, nnuts in db.iter_nnuts_by_file():
83 print(path, nnuts)
84 else:
85 for path in db.iter_paths():
86 print(path)
89class Nuts(SquirrelCommand):
91 def make_subparser(self, subparsers):
92 headline = \
93 'Dump index entry summaries.'
95 return subparsers.add_parser(
96 'nuts',
97 help=headline,
98 description=headline)
100 def run(self, parser, args):
101 s = sq.Squirrel()
102 db = s.get_database()
103 for path, nuts in db.undig_all():
104 print(path)
105 for nut in nuts:
106 print(' ' + nut.summary)
109class Cleanup(SquirrelCommand):
111 def make_subparser(self, subparsers):
112 headline = \
113 'Remove leftover volatile data entries.'
115 return subparsers.add_parser(
116 'cleanup',
117 help=headline,
118 description=headline)
120 def run(self, parser, args):
121 s = sq.Squirrel()
122 db = s.get_database()
123 n_removed = db._remove_volatile()
124 logger.info('Number of entries removed: %i' % n_removed)
127class Remove(SquirrelCommand):
129 def make_subparser(self, subparsers):
130 headline = \
131 'Remove cached meta-data of files matching given patterns.'
133 return subparsers.add_parser(
134 'remove',
135 help=headline,
136 description=headline)
138 def setup(self, parser):
139 parser.add_argument(
140 'paths',
141 nargs='+',
142 metavar='PATHS',
143 help='Glob patterns of paths to be removed (should be quoted to '
144 'prevent the shell from expanding them).')
146 def run(self, parser, args):
147 s = sq.Squirrel()
148 db = s.get_database()
150 n_removed = 0
151 for path in args.paths:
152 n_removed += db.remove_glob(path)
154 logger.info('Number of entries removed: %i' % n_removed)
157def make_subparser(subparsers):
158 return subparsers.add_parser(
159 'database',
160 help=headline,
161 subcommands=[Env(), Stats(), Files(), Nuts(), Cleanup(), Remove()],
162 description=description)
165def setup(parser):
166 pass
169def run(parser, args):
170 parser.print_help()