1# http://pyrocko.org - GPLv3 

2# 

3# The Pyrocko Developers, 21st Century 

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

5 

6import logging 

7 

8from pyrocko import squirrel as sq 

9from ..common import SquirrelCommand 

10 

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

12 

13headline = 'Database inspection and maintenance.' 

14 

15description = '''%s 

16 

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

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

19to do some basic cleanup actions. 

20''' % headline 

21 

22 

23class Env(SquirrelCommand): 

24 

25 def make_subparser(self, subparsers): 

26 return subparsers.add_parser( 

27 'env', 

28 help='Show current Squirrel environment.', 

29 description='Show current Squirrel environment.') 

30 

31 def run(self, parser, args): 

32 env = sq.get_environment() 

33 env.path_prefix = env.get_basepath() 

34 print(env) 

35 

36 

37class Stats(SquirrelCommand): 

38 

39 def make_subparser(self, subparsers): 

40 return subparsers.add_parser( 

41 'stats', 

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

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

44 

45 def setup(self, parser): 

46 parser.add_argument( 

47 '--full', 

48 help='Show details.', 

49 action='store_true') 

50 

51 def run(self, parser, args): 

52 s = sq.Squirrel() 

53 db = s.get_database() 

54 if args.full: 

55 print(db.get_stats().dump()) 

56 else: 

57 print(db.get_stats()) 

58 

59 

60class Files(SquirrelCommand): 

61 

62 def make_subparser(self, subparsers): 

63 headline = \ 

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

65 

66 return subparsers.add_parser( 

67 'files', 

68 help=headline, 

69 description=headline) 

70 

71 def setup(self, parser): 

72 parser.add_argument( 

73 '--nnuts', 

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

75 action='store_true') 

76 

77 def run(self, parser, args): 

78 s = sq.Squirrel() 

79 db = s.get_database() 

80 

81 if args.nnuts: 

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

83 print(path, nnuts) 

84 else: 

85 for path in db.iter_paths(): 

86 print(path) 

87 

88 

89class Nuts(SquirrelCommand): 

90 

91 def make_subparser(self, subparsers): 

92 headline = \ 

93 'Dump index entry summaries.' 

94 

95 return subparsers.add_parser( 

96 'nuts', 

97 help=headline, 

98 description=headline) 

99 

100 def run(self, parser, args): 

101 s = sq.Squirrel() 

102 db = s.get_database() 

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

104 print(path) 

105 for nut in nuts: 

106 print(' ' + nut.summary) 

107 

108 

109class Cleanup(SquirrelCommand): 

110 

111 def make_subparser(self, subparsers): 

112 headline = \ 

113 'Remove leftover volatile data entries.' 

114 

115 return subparsers.add_parser( 

116 'cleanup', 

117 help=headline, 

118 description=headline) 

119 

120 def run(self, parser, args): 

121 s = sq.Squirrel() 

122 db = s.get_database() 

123 n_removed = db._remove_volatile() 

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

125 

126 

127class Remove(SquirrelCommand): 

128 

129 def make_subparser(self, subparsers): 

130 headline = \ 

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

132 

133 return subparsers.add_parser( 

134 'remove', 

135 help=headline, 

136 description=headline) 

137 

138 def setup(self, parser): 

139 parser.add_argument( 

140 'paths', 

141 nargs='+', 

142 metavar='PATHS', 

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

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

145 

146 def run(self, parser, args): 

147 s = sq.Squirrel() 

148 db = s.get_database() 

149 

150 n_removed = 0 

151 for path in args.paths: 

152 n_removed += db.remove_glob(path) 

153 

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

155 

156 

157def make_subparser(subparsers): 

158 return subparsers.add_parser( 

159 'database', 

160 help=headline, 

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

162 description=description) 

163 

164 

165def setup(parser): 

166 pass 

167 

168 

169def run(parser, args): 

170 parser.print_help()