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

1# http://pyrocko.org - GPLv3 

2# 

3# The Pyrocko Developers, 21st Century 

4# ---|P------/S----------~Lg---------- 

5 

6''' 

7Implementation of :app:`squirrel database`. 

8''' 

9 

10import logging 

11 

12from pyrocko import squirrel as sq 

13from ..common import SquirrelCommand 

14 

15logger = logging.getLogger('psq.cli.database') 

16 

17headline = 'Database inspection and maintenance.' 

18 

19description = '''%s 

20 

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 

25 

26 

27class Env(SquirrelCommand): 

28 

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.') 

34 

35 def run(self, parser, args): 

36 env = sq.get_environment() 

37 env.path_prefix = env.get_basepath() 

38 print(env) 

39 

40 

41class Stats(SquirrelCommand): 

42 

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.') 

48 

49 def setup(self, parser): 

50 parser.add_argument( 

51 '--full', 

52 help='Show details.', 

53 action='store_true') 

54 

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()) 

62 

63 

64class Files(SquirrelCommand): 

65 

66 def make_subparser(self, subparsers): 

67 headline = \ 

68 'Show paths of files for which cached meta-data is available.' 

69 

70 return subparsers.add_parser( 

71 'files', 

72 help=headline, 

73 description=headline) 

74 

75 def setup(self, parser): 

76 parser.add_argument( 

77 '--nnuts', 

78 help='Show nut count for each file.', 

79 action='store_true') 

80 

81 def run(self, parser, args): 

82 s = sq.Squirrel() 

83 db = s.get_database() 

84 

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) 

91 

92 

93class Nuts(SquirrelCommand): 

94 

95 def make_subparser(self, subparsers): 

96 headline = \ 

97 'Dump index entry summaries.' 

98 

99 return subparsers.add_parser( 

100 'nuts', 

101 help=headline, 

102 description=headline) 

103 

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) 

111 

112 

113class Cleanup(SquirrelCommand): 

114 

115 def make_subparser(self, subparsers): 

116 headline = \ 

117 'Remove leftover volatile data entries.' 

118 

119 return subparsers.add_parser( 

120 'cleanup', 

121 help=headline, 

122 description=headline) 

123 

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) 

129 

130 

131class Vacuum(SquirrelCommand): 

132 

133 def make_subparser(self, subparsers): 

134 headline = \ 

135 'Run SQLite `VACUUM` command.' 

136 

137 return subparsers.add_parser( 

138 'vacuum', 

139 help=headline, 

140 description=headline) 

141 

142 def run(self, parser, args): 

143 s = sq.Squirrel() 

144 db = s.get_database() 

145 db.vacuum() 

146 

147 

148class Optimize(SquirrelCommand): 

149 

150 def make_subparser(self, subparsers): 

151 headline = \ 

152 'Run SQLite `PRAGMA optimize` command.' 

153 

154 return subparsers.add_parser( 

155 'optimize', 

156 help=headline, 

157 description=headline) 

158 

159 def run(self, parser, args): 

160 s = sq.Squirrel() 

161 db = s.get_database() 

162 db.optimize() 

163 

164 

165class ActivateWAL(SquirrelCommand): 

166 

167 def make_subparser(self, subparsers): 

168 headline = \ 

169 'Activate SQLite\'s WAL mode (Write-Ahead Log mode).' 

170 

171 return subparsers.add_parser( 

172 'wal', 

173 help=headline, 

174 description=headline) 

175 

176 def run(self, parser, args): 

177 s = sq.Squirrel() 

178 db = s.get_database() 

179 db.activate_wal_mode() 

180 

181 

182class Remove(SquirrelCommand): 

183 

184 def make_subparser(self, subparsers): 

185 headline = \ 

186 'Remove cached meta-data of files matching given patterns.' 

187 

188 return subparsers.add_parser( 

189 'remove', 

190 help=headline, 

191 description=headline) 

192 

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).') 

200 

201 def run(self, parser, args): 

202 s = sq.Squirrel() 

203 db = s.get_database() 

204 

205 n_removed = 0 

206 for path in args.paths: 

207 n_removed += db.remove_glob(path) 

208 

209 logger.info('Number of entries removed: %i' % n_removed) 

210 

211 

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) 

220 

221 

222def setup(parser): 

223 pass 

224 

225 

226def run(parser, args): 

227 parser.print_help()