Path: blob/main/scripts/reports/fill_inventory.py
1128 views
#! /usr/bin/env sage1import sys2import os3import argparse4import importlib56debug = False7selected_dbs = None8selected_collections = None9action = False10file_output = None11jdbs = None #This is not a global, this fixes pyflakes problem in start_lmfdb_connection12iud = None1314def start_lmfdb_connection():1516scriptdir = os.path.join('.',os.path.dirname(__file__))17up2 = os.path.abspath(os.path.join(scriptdir,os.pardir,os.pardir))18sys.path.insert(0, up2)19save_stderr = None20if not debug:21try:22save_stderr = sys.stderr23sys.stderr = open('/dev/null','w')24except Exception:25#This is only for neatness, so no worries if it fails26pass2728#Trick pyflakes because it lacks name ignoring and can't detect the global29global jdbs30global iud31del globals()['jdbs']32del globals()['iud']3334globals()['jdbs'] = importlib.import_module('jsonify_db_structure')35globals()['iud'] = importlib.import_module('inventory_upload_data')36if not debug:37try:38sys.stderr = save_stderr39except Exception:40pass4142def show_databases():4344global action45action = True46if not file_output:47fh = sys.stdout48else:49fh = open(file_output, 'w')50if selected_dbs:51fh.write('Single database ' + selected_dbs[0] + ' specified\n')52return5354start_lmfdb_connection()55dbs = jdbs.get_lmfdb_databases()56fh.write('\n')57fh.write('Known good databases\n')58fh.write('--------------------\n')59for db in dbs:60fh.write(db+'\n')6162if file_output: fh.close()6364def show_collections():6566global action67action = True68if not file_output:69fh = sys.stdout70else:71fh = open(file_output, 'w')72if selected_dbs and selected_collections:73fh.write('Single collection ' + selected_collections[0] + ' specified\n')74return7576start_lmfdb_connection()77dbs = selected_dbs78if not dbs: dbs = jdbs.get_lmfdb_databases()79colls = jdbs.get_lmfdb_collections(databases = dbs)80fh.write('Known good collections\n')81fh.write('--------------------\n')82for db in colls:83first = True84for scol in colls[db]:85pt = False86if not selected_collections:87pt = True88else:89if scol in selected_collections:90pt = True91if pt and first:92first = False93fh.write('In database ' + db +':\n')94if pt: fh.write(' ' * 4 + scol + '\n')95if file_output: fh.close()9697def generate_inventory():98raise NotImplementedError99# global action100# action = True101#102# start_lmfdb_connection()103# result = jdbs.parse_lmfdb_to_json(collections = selected_collections,104# databases = selected_dbs)105#106# if file_output:107# fh = open(file_output, 'w')108# fh.write(json.dumps(result, indent=4, sort_keys = True))109# fh.close()110# else:111# invdb = connection['inventory']112# for db in result:113# for coll in result[db]:114# iud.upload_table_structure(invdb, db, coll, result)115# iud.upload_collection_indices(invdb, db, coll, result)116117if __name__ == '__main__':118parser = argparse.ArgumentParser()119parser.add_argument('--list_databases', action='store_true', help = 'List available databases')120parser.add_argument('--list_collections', action='store_true', help = 'List available collections')121parser.add_argument('--file_output', action ='store', help = 'Redirect output to file')122parser.add_argument('--generate_inventory', action='store_true', help = 'Generate the inventory data for the specified database and/or collections')123parser.add_argument('--database', action='store', help='Set working database')124parser.add_argument('--collection', action='store', help='Set working collection')125args = parser.parse_args()126127print('LMFDB report tool')128print('------------------------')129if (len(sys.argv) == 1):parser.print_help()130sys.argv = sys.argv[0]131if args.database: selected_dbs = [args.database]132if args.collection: selected_collections = [args.collection]133if args.file_output: file_output = args.file_output134if args.list_databases: show_databases()135if args.list_collections: show_collections()136if args.generate_inventory: generate_inventory()137138if not action:139print('No action specified!')140else:141if file_output: print('Run complete. All primary output written to file ' + file_output)142143144