1# http://pyrocko.org - GPLv3 

2# 

3# The Pyrocko Developers, 21st Century 

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

5 

6from __future__ import absolute_import, print_function 

7 

8import logging 

9 

10from pyrocko import squirrel as sq 

11from ..common import SquirrelCommand 

12 

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

14 

15headline = 'Database inspection and maintenance.' 

16 

17description = '''%s 

18 

19Get information about Squirrel's meta-data cache and database. Where it is, 

20what files it knows about and what index entries are available. It also allows 

21to do some basic cleanup actions. 

22''' % headline 

23 

24 

25class Env(SquirrelCommand): 

26 

27 def make_subparser(self, subparsers): 

28 return subparsers.add_parser( 

29 'env', 

30 help='Show current Squirrel environment.', 

31 description='Show current Squirrel environment.') 

32 

33 def run(self, parser, args): 

34 env = sq.get_environment() 

35 env.path_prefix = env.get_basepath() 

36 print(env) 

37 

38 

39class Stats(SquirrelCommand): 

40 

41 def make_subparser(self, subparsers): 

42 return subparsers.add_parser( 

43 'stats', 

44 help='Show information about cached meta-data.', 

45 description='Show information about cached meta-data.') 

46 

47 def setup(self, parser): 

48 parser.add_argument( 

49 '--full', 

50 help='Show details.', 

51 action='store_true') 

52 

53 def run(self, parser, args): 

54 s = sq.Squirrel() 

55 db = s.get_database() 

56 if args.full: 

57 print(db.get_stats().dump()) 

58 else: 

59 print(db.get_stats()) 

60 

61 

62class Files(SquirrelCommand): 

63 

64 def make_subparser(self, subparsers): 

65 headline = \ 

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

67 

68 return subparsers.add_parser( 

69 'files', 

70 help=headline, 

71 description=headline) 

72 

73 def setup(self, parser): 

74 parser.add_argument( 

75 '--nnuts', 

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

77 action='store_true') 

78 

79 def run(self, parser, args): 

80 s = sq.Squirrel() 

81 db = s.get_database() 

82 

83 if args.nnuts: 

84 for path, nnuts in db.iter_nnuts_by_file(): 

85 print(path, nnuts) 

86 else: 

87 for path in db.iter_paths(): 

88 print(path) 

89 

90 

91class Nuts(SquirrelCommand): 

92 

93 def make_subparser(self, subparsers): 

94 headline = \ 

95 'Dump index entry summaries.' 

96 

97 return subparsers.add_parser( 

98 'nuts', 

99 help=headline, 

100 description=headline) 

101 

102 def run(self, parser, args): 

103 s = sq.Squirrel() 

104 db = s.get_database() 

105 for path, nuts in db.undig_all(): 

106 print(path) 

107 for nut in nuts: 

108 print(' ' + nut.summary) 

109 

110 

111class Cleanup(SquirrelCommand): 

112 

113 def make_subparser(self, subparsers): 

114 headline = \ 

115 'Remove leftover volatile data entries.' 

116 

117 return subparsers.add_parser( 

118 'cleanup', 

119 help=headline, 

120 description=headline) 

121 

122 def run(self, parser, args): 

123 s = sq.Squirrel() 

124 db = s.get_database() 

125 n_removed = db._remove_volatile() 

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

127 

128 

129class Remove(SquirrelCommand): 

130 

131 def make_subparser(self, subparsers): 

132 headline = \ 

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

134 

135 return subparsers.add_parser( 

136 'remove', 

137 help=headline, 

138 description=headline) 

139 

140 def setup(self, parser): 

141 parser.add_argument( 

142 'paths', 

143 nargs='+', 

144 metavar='PATHS', 

145 help='Glob patterns of paths to be removed (should be quoted to ' 

146 'prevent the shell from expanding them).') 

147 

148 def run(self, parser, args): 

149 s = sq.Squirrel() 

150 db = s.get_database() 

151 

152 n_removed = 0 

153 for path in args.paths: 

154 n_removed += db.remove_glob(path) 

155 

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

157 

158 

159def make_subparser(subparsers): 

160 return subparsers.add_parser( 

161 'database', 

162 help=headline, 

163 subcommands=[Env(), Stats(), Files(), Nuts(), Cleanup(), Remove()], 

164 description=description) 

165 

166 

167def setup(parser): 

168 pass 

169 

170 

171def run(parser, args): 

172 parser.print_help()