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(subparsers): 

31 p = common.add_parser( 

32 subparsers, 'database', 

33 help='Database inspection and maintenance.', 

34 description=description) 

35 

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

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

38 'cleanup', 'remove']) 

39 

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

41 return p 

42 

43 

44def call(parser, args): 

45 action = args.action 

46 if action == 'env': 

47 print(sq.get_environment()) 

48 

49 else: 

50 s = sq.Squirrel() 

51 db = s.get_database() 

52 if action == 'stats': 

53 print(db.get_stats()) 

54 elif action == 'stats-full': 

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

56 

57 elif action == 'files': 

58 for path in db.iter_paths(): 

59 print(path) 

60 

61 elif action == 'files-nnuts': 

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

63 print(path, nnuts) 

64 

65 elif action == 'nuts': 

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

67 print(path) 

68 for nut in nuts: 

69 print(' ' + nut.summary) 

70 

71 elif action == 'cleanup': 

72 n_removed = db._remove_volatile() 

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

74 

75 elif action == 'remove': 

76 if not args.path_patterns: 

77 raise sq.SquirrelError( 

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

79 

80 n_removed = 0 

81 for path in args.path_patterns: 

82 n_removed += db.remove_glob(path) 

83 

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