From 43158fdce2c1564936d7b1dafbfa384f19a89b5a Mon Sep 17 00:00:00 2001 From: Kelsey Hawley Date: Wed, 18 Dec 2013 21:16:52 -0800 Subject: [PATCH] reconfigured dump-anydbm to check the output of whichdb instead of checking for file existence first. This helps bypass the issue of an ndbm database only be recognized without its extension. If which returns None, then the file does not exist, and if it returns '' then it cannot process it --- bin/dump-anydbm | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/bin/dump-anydbm b/bin/dump-anydbm index f1f8a42..dce0dc8 100755 --- a/bin/dump-anydbm +++ b/bin/dump-anydbm @@ -24,15 +24,31 @@ if __name__ == "__main__": sys.stderr.write("usage: {} DBM_FILE\n".format(sys.argv[0])) exit(1) - if not os.path.exists(sys.argv[1]): + filename = sys.argv[1] + file_location = sys.argv[1] + which = whichdb(filename) + + # if which returns none and the file does not exist, print usage line + if which == None and not os.path.exists(sys.argv[1]): sys.stderr.write('No such file {}\n\n'.format(sys.argv[1])) sys.stderr.write("usage: {} DBM_FILE\n".format(sys.argv[0])) exit(1) - which = whichdb(sys.argv[1]) - print('{} is a {} db'.format(sys.argv[1], which)) + # covers case where an ndbm is checked with its extension & identified incorrectly + elif 'bsd' in which: + correct_file = filename.split(".db")[0] + correct_which = whichdb(correct_file) + if correct_which == ('dbm' or 'dbm.ndbm'): + filename = correct_file + which = correct_which - db = dbm.open(sys.argv[1], 'r') + elif which == '': + sys.stderr.write("{} is an unrecognized database type\n".format(sys.argv[1])) + exit(1) + + print('{} is a {} db'.format(filename, which)) + + db = dbm.open(filename, 'r') for key in db.keys(): print("{}:{}".format(key, db[key]))