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 

12from .. import common 

13 

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

15 

16description = ''' 

17Actions: 

18 

19 env Show current Squirrel environment. 

20 stats Show brief information about cached meta-data. 

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

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

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

24 nuts Dump index entry summaries. 

25 cleanup Remove leftover volatile data entries. 

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

27'''.strip() 

28 

29 

30def setup_subcommand(subparsers): 

31 return common.add_parser( 

32 subparsers, 'database', 

33 help='Database inspection and maintenance.', 

34 description=description) 

35 

36 

37def setup(parser): 

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

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

40 'cleanup', 'remove']) 

41 

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

43 

44 

45def call(parser, args): 

46 action = args.action 

47 if action == 'env': 

48 print(sq.get_environment()) 

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)