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-06 06:59 +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 Remove(SquirrelCommand): 

132 

133 def make_subparser(self, subparsers): 

134 headline = \ 

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

136 

137 return subparsers.add_parser( 

138 'remove', 

139 help=headline, 

140 description=headline) 

141 

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

149 

150 def run(self, parser, args): 

151 s = sq.Squirrel() 

152 db = s.get_database() 

153 

154 n_removed = 0 

155 for path in args.paths: 

156 n_removed += db.remove_glob(path) 

157 

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

159 

160 

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) 

167 

168 

169def setup(parser): 

170 pass 

171 

172 

173def run(parser, args): 

174 parser.print_help()