-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_repository_indexer.py
93 lines (68 loc) · 2.68 KB
/
run_repository_indexer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
"""Class for indexing repository statistics"""
import argparse
import logging
import sys
from lib.util import Utilities
from dspace_reports.repository_indexer import RepositoryIndexer
class RunRepositoryIndexer():
"""Class for indexing repository statistics"""
def __init__(self, config=None, logger=None):
if config is None:
print("ERROR: A configuration file required to create the stats indexer.")
sys.exit(1)
self.config = config
self.solr_server = config['solr_server']
# Set up logging
if logger is not None:
self.logger = logger
else:
self.logger = logging.getLogger('dspace-reports')
def run(self):
"""Function to run repository indexer"""
# Create items stats indexer
repository_indexer = RepositoryIndexer(config=self.config)
# Index repository stats from Solr
repository_indexer.index()
def main():
"""Main function"""
parser = argparse.ArgumentParser(
prog='Database Manager',
description='Commands to manage statistics database tables')
parser.add_argument("-c", "--config", dest="config_file", action='store', type=str,
default="config/application.yml", help="Configuration file")
parser.add_argument("-o", "--output_dir", dest="output_dir", action='store', type=str,
help="Directory for results files.")
args = parser.parse_args()
# Create utilities object
utilities = Utilities()
# Check required options fields
if args.output_dir is None:
parser.print_help()
parser.error("Must specify an output directory.")
# Load config
print("Loading configuration from file: %s", args.config_file)
config = utilities.load_config(args.config_file)
if not config:
print("ERROR: Unable to load configuration.")
sys.exit(1)
# Set up logging
logger = utilities.load_logger(config=config)
# Ensure work_dir has trailing slash
work_dir = config['work_dir']
if work_dir[len(work_dir)-1] != '/':
work_dir = work_dir + '/'
# Ensure output_dir has trailing slash
output_dir = args.output_dir
if output_dir[len(output_dir)-1] != '/':
output_dir = output_dir + '/'
# Ensure output_dir exists
output_dir_exists = utilities.ensure_directory_exists(output_dir)
if output_dir_exists is False:
logger.error("Output directory does not exist.")
sys.exit(1)
# Create stats indexer
indexer = RunRepositoryIndexer(config=config, logger=logger)
# Get item statistics from Solr
indexer.run()
if __name__ == "__main__":
main()