Coverage for /usr/local/lib/python3.11/dist-packages/pyrocko/squirrel/tool/commands/database.py: 40%
78 statements
« prev ^ index » next coverage.py v6.5.0, created at 2023-10-04 09:52 +0000
« prev ^ index » next coverage.py v6.5.0, created at 2023-10-04 09:52 +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 Remove(SquirrelCommand):
133 def make_subparser(self, subparsers):
134 headline = \
135 'Remove cached meta-data of files matching given patterns.'
137 return subparsers.add_parser(
138 'remove',
139 help=headline,
140 description=headline)
142 def setup(self, parser):
143 parser.add_argument(
144 'paths',
145 nargs='+',
146 metavar='PATHS',
147 help='Glob patterns of paths to be removed (should be quoted to '
148 'prevent the shell from expanding them).')
150 def run(self, parser, args):
151 s = sq.Squirrel()
152 db = s.get_database()
154 n_removed = 0
155 for path in args.paths:
156 n_removed += db.remove_glob(path)
158 logger.info('Number of entries removed: %i' % n_removed)
161def make_subparser(subparsers):
162 return subparsers.add_parser(
163 'database',
164 help=headline,
165 subcommands=[Env(), Stats(), Files(), Nuts(), Cleanup(), Remove()],
166 description=description)
169def setup(parser):
170 pass
173def run(parser, args):
174 parser.print_help()