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 

11 

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

13 

14description = ''' 

15Actions: 

16 

17 env Show current Squirrel environment. 

18 stats Show brief information about cached meta-data. 

19 stats-full Show detailed information about cached meta-data. 

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

21 files-nnuts Show number of index entries for each known file. 

22 nuts Dump index entry summaries. 

23 cleanup Remove leftover volatile data entries. 

24 remove paths Remove cached meta-data of files matching given patterns. 

25'''.strip() 

26 

27 

28def make_subparser(subparsers): 

29 return subparsers.add_parser( 

30 'database', 

31 help='Database inspection and maintenance.', 

32 description=description) 

33 

34 

35def setup(parser): 

36 parser.add_argument('action', nargs='?', default='env', choices=[ 

37 'env', 'stats', 'stats-full', 'files', 'files-nnuts', 'nuts', 

38 'cleanup', 'remove']) 

39 

40 parser.add_argument('path_patterns', nargs='*') 

41 

42 

43def run(parser, args): 

44 action = args.action 

45 if action == 'env': 

46 env = sq.get_environment() 

47 env.path_prefix = env.get_basepath() 

48 print(env) 

49 

50 else: 

51 s = sq.Squirrel() 

52 db = s.get_database() 

53 if action == 'stats': 

54 print(db.get_stats()) 

55 elif action == 'stats-full': 

56 print(db.get_stats().dump()) 

57 

58 elif action == 'files': 

59 for path in db.iter_paths(): 

60 print(path) 

61 

62 elif action == 'files-nnuts': 

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

64 print(path, nnuts) 

65 

66 elif action == 'nuts': 

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

68 print(path) 

69 for nut in nuts: 

70 print(' ' + nut.summary) 

71 

72 elif action == 'cleanup': 

73 n_removed = db._remove_volatile() 

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

75 

76 elif action == 'remove': 

77 if not args.path_patterns: 

78 raise sq.SquirrelError( 

79 'No path patterns to remove have been specified.') 

80 

81 n_removed = 0 

82 for path in args.path_patterns: 

83 n_removed += db.remove_glob(path) 

84 

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