Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
AndrewVSutherland
GitHub Repository: AndrewVSutherland/lmfdb
Path: blob/main/scripts/reports/fill_inventory.py
1128 views
1
#! /usr/bin/env sage
2
import sys
3
import os
4
import argparse
5
import importlib
6
7
debug = False
8
selected_dbs = None
9
selected_collections = None
10
action = False
11
file_output = None
12
jdbs = None #This is not a global, this fixes pyflakes problem in start_lmfdb_connection
13
iud = None
14
15
def start_lmfdb_connection():
16
17
scriptdir = os.path.join('.',os.path.dirname(__file__))
18
up2 = os.path.abspath(os.path.join(scriptdir,os.pardir,os.pardir))
19
sys.path.insert(0, up2)
20
save_stderr = None
21
if not debug:
22
try:
23
save_stderr = sys.stderr
24
sys.stderr = open('/dev/null','w')
25
except Exception:
26
#This is only for neatness, so no worries if it fails
27
pass
28
29
#Trick pyflakes because it lacks name ignoring and can't detect the global
30
global jdbs
31
global iud
32
del globals()['jdbs']
33
del globals()['iud']
34
35
globals()['jdbs'] = importlib.import_module('jsonify_db_structure')
36
globals()['iud'] = importlib.import_module('inventory_upload_data')
37
if not debug:
38
try:
39
sys.stderr = save_stderr
40
except Exception:
41
pass
42
43
def show_databases():
44
45
global action
46
action = True
47
if not file_output:
48
fh = sys.stdout
49
else:
50
fh = open(file_output, 'w')
51
if selected_dbs:
52
fh.write('Single database ' + selected_dbs[0] + ' specified\n')
53
return
54
55
start_lmfdb_connection()
56
dbs = jdbs.get_lmfdb_databases()
57
fh.write('\n')
58
fh.write('Known good databases\n')
59
fh.write('--------------------\n')
60
for db in dbs:
61
fh.write(db+'\n')
62
63
if file_output: fh.close()
64
65
def show_collections():
66
67
global action
68
action = True
69
if not file_output:
70
fh = sys.stdout
71
else:
72
fh = open(file_output, 'w')
73
if selected_dbs and selected_collections:
74
fh.write('Single collection ' + selected_collections[0] + ' specified\n')
75
return
76
77
start_lmfdb_connection()
78
dbs = selected_dbs
79
if not dbs: dbs = jdbs.get_lmfdb_databases()
80
colls = jdbs.get_lmfdb_collections(databases = dbs)
81
fh.write('Known good collections\n')
82
fh.write('--------------------\n')
83
for db in colls:
84
first = True
85
for scol in colls[db]:
86
pt = False
87
if not selected_collections:
88
pt = True
89
else:
90
if scol in selected_collections:
91
pt = True
92
if pt and first:
93
first = False
94
fh.write('In database ' + db +':\n')
95
if pt: fh.write(' ' * 4 + scol + '\n')
96
if file_output: fh.close()
97
98
def generate_inventory():
99
raise NotImplementedError
100
# global action
101
# action = True
102
#
103
# start_lmfdb_connection()
104
# result = jdbs.parse_lmfdb_to_json(collections = selected_collections,
105
# databases = selected_dbs)
106
#
107
# if file_output:
108
# fh = open(file_output, 'w')
109
# fh.write(json.dumps(result, indent=4, sort_keys = True))
110
# fh.close()
111
# else:
112
# invdb = connection['inventory']
113
# for db in result:
114
# for coll in result[db]:
115
# iud.upload_table_structure(invdb, db, coll, result)
116
# iud.upload_collection_indices(invdb, db, coll, result)
117
118
if __name__ == '__main__':
119
parser = argparse.ArgumentParser()
120
parser.add_argument('--list_databases', action='store_true', help = 'List available databases')
121
parser.add_argument('--list_collections', action='store_true', help = 'List available collections')
122
parser.add_argument('--file_output', action ='store', help = 'Redirect output to file')
123
parser.add_argument('--generate_inventory', action='store_true', help = 'Generate the inventory data for the specified database and/or collections')
124
parser.add_argument('--database', action='store', help='Set working database')
125
parser.add_argument('--collection', action='store', help='Set working collection')
126
args = parser.parse_args()
127
128
print('LMFDB report tool')
129
print('------------------------')
130
if (len(sys.argv) == 1):parser.print_help()
131
sys.argv = sys.argv[0]
132
if args.database: selected_dbs = [args.database]
133
if args.collection: selected_collections = [args.collection]
134
if args.file_output: file_output = args.file_output
135
if args.list_databases: show_databases()
136
if args.list_collections: show_collections()
137
if args.generate_inventory: generate_inventory()
138
139
if not action:
140
print('No action specified!')
141
else:
142
if file_output: print('Run complete. All primary output written to file ' + file_output)
143
144