Coverage for /usr/local/lib/python3.13/dist-packages/pyrocko/squirrel/tool/commands/database.py: 39%
101 statements
« prev ^ index » next coverage.py v7.6.0, created at 2025-12-04 10:41 +0000
« prev ^ index » next coverage.py v7.6.0, created at 2025-12-04 10:41 +0000
1# http://pyrocko.org - GPLv3
2#
3# The Pyrocko Developers, 21st Century
4# ---|P------/S----------~Lg----------
6'''
7Implementation of :app:`squirrel database`.
8'''
10import logging
12from pyrocko import squirrel as sq
13from ..common import SquirrelCommand
15logger = logging.getLogger('psq.cli.database')
17headline = 'Database inspection and maintenance.'
19description = '''%s
21Get information about Squirrel's meta-data cache and database. Where it is,
22what files it knows about and what index entries are available. It also allows
23to do some basic cleanup actions.
24''' % headline
27class Env(SquirrelCommand):
29 def make_subparser(self, subparsers):
30 return subparsers.add_parser(
31 'env',
32 help='Show current Squirrel environment.',
33 description='Show current Squirrel environment.')
35 def run(self, parser, args):
36 env = sq.get_environment()
37 env.path_prefix = env.get_basepath()
38 print(env)
41class Stats(SquirrelCommand):
43 def make_subparser(self, subparsers):
44 return subparsers.add_parser(
45 'stats',
46 help='Show information about cached meta-data.',
47 description='Show information about cached meta-data.')
49 def setup(self, parser):
50 parser.add_argument(
51 '--full',
52 help='Show details.',
53 action='store_true')
55 def run(self, parser, args):
56 s = sq.Squirrel()
57 db = s.get_database()
58 if args.full:
59 print(db.get_stats().dump())
60 else:
61 print(db.get_stats())
64class Files(SquirrelCommand):
66 def make_subparser(self, subparsers):
67 headline = \
68 'Show paths of files for which cached meta-data is available.'
70 return subparsers.add_parser(
71 'files',
72 help=headline,
73 description=headline)
75 def setup(self, parser):
76 parser.add_argument(
77 '--nnuts',
78 help='Show nut count for each file.',
79 action='store_true')
81 def run(self, parser, args):
82 s = sq.Squirrel()
83 db = s.get_database()
85 if args.nnuts:
86 for path, nnuts in db.iter_nnuts_by_file():
87 print(path, nnuts)
88 else:
89 for path in db.iter_paths():
90 print(path)
93class Nuts(SquirrelCommand):
95 def make_subparser(self, subparsers):
96 headline = \
97 'Dump index entry summaries.'
99 return subparsers.add_parser(
100 'nuts',
101 help=headline,
102 description=headline)
104 def run(self, parser, args):
105 s = sq.Squirrel()
106 db = s.get_database()
107 for path, nuts in db.undig_all():
108 print(path)
109 for nut in nuts:
110 print(' ' + nut.summary)
113class Cleanup(SquirrelCommand):
115 def make_subparser(self, subparsers):
116 headline = \
117 'Remove leftover volatile data entries.'
119 return subparsers.add_parser(
120 'cleanup',
121 help=headline,
122 description=headline)
124 def run(self, parser, args):
125 s = sq.Squirrel()
126 db = s.get_database()
127 n_removed = db._remove_volatile()
128 logger.info('Number of entries removed: %i' % n_removed)
131class Vacuum(SquirrelCommand):
133 def make_subparser(self, subparsers):
134 headline = \
135 'Run SQLite `VACUUM` command.'
137 return subparsers.add_parser(
138 'vacuum',
139 help=headline,
140 description=headline)
142 def run(self, parser, args):
143 s = sq.Squirrel()
144 db = s.get_database()
145 db.vacuum()
148class Optimize(SquirrelCommand):
150 def make_subparser(self, subparsers):
151 headline = \
152 'Run SQLite `PRAGMA optimize` command.'
154 return subparsers.add_parser(
155 'optimize',
156 help=headline,
157 description=headline)
159 def run(self, parser, args):
160 s = sq.Squirrel()
161 db = s.get_database()
162 db.optimize()
165class ActivateWAL(SquirrelCommand):
167 def make_subparser(self, subparsers):
168 headline = \
169 'Activate SQLite\'s WAL mode (Write-Ahead Log mode).'
171 return subparsers.add_parser(
172 'wal',
173 help=headline,
174 description=headline)
176 def run(self, parser, args):
177 s = sq.Squirrel()
178 db = s.get_database()
179 db.activate_wal_mode()
182class Remove(SquirrelCommand):
184 def make_subparser(self, subparsers):
185 headline = \
186 'Remove cached meta-data of files matching given patterns.'
188 return subparsers.add_parser(
189 'remove',
190 help=headline,
191 description=headline)
193 def setup(self, parser):
194 parser.add_argument(
195 'paths',
196 nargs='+',
197 metavar='PATHS',
198 help='Glob patterns of paths to be removed (should be quoted to '
199 'prevent the shell from expanding them).')
201 def run(self, parser, args):
202 s = sq.Squirrel()
203 db = s.get_database()
205 n_removed = 0
206 for path in args.paths:
207 n_removed += db.remove_glob(path)
209 logger.info('Number of entries removed: %i' % n_removed)
212def make_subparser(subparsers):
213 return subparsers.add_parser(
214 'database',
215 help=headline,
216 subcommands=[
217 Env(), Stats(), Files(), Nuts(), Cleanup(), Remove(), Vacuum(),
218 Optimize(), ActivateWAL()],
219 description=description)
222def setup(parser):
223 pass
226def run(parser, args):
227 parser.print_help()