-
-
Save webmaster128/9d5a5a7c50119f45ed37ee75c296c5b9 to your computer and use it in GitHub Desktop.
Python LevelDB debug dumper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import argparse | |
import binascii | |
import sys | |
import leveldb | |
def b2x(b): | |
"""Convert bytes to a hex string""" | |
return binascii.hexlify(b).decode('utf8') | |
def db_count(filepath): | |
"""Prints number of records in LevelDB database""" | |
db = leveldb.LevelDB(filepath) | |
count = 1 | |
for k in db.RangeIter(None, None, False): | |
count += 1 | |
print(count) | |
def db_dump(filepath, key_format, value_format): | |
"""Prints records of LevelDB database""" | |
db = leveldb.LevelDB(filepath) | |
for k, v in db.RangeIter(None, None): | |
if key_format == "hex": | |
k = b2x(k) | |
if key_format == "omit": | |
k = None | |
if value_format == "hex": | |
v = b2x(v) | |
if value_format == "omit": | |
v = None | |
print('%s %s' % (k, v,)) | |
if __name__ == '__main__': | |
if len(sys.argv) == 1: | |
print('Dumps or counts content of one or more LevelDB databases.') | |
print('Binary entries can be converted to hex.\n') | |
print('Usage:') | |
print(' python %s [--count] [--binary] db_path\n' % (sys.argv[0],)) | |
print('Examples:') | |
print(' python %s ~/.bitcoin/testnet3/MP_spinfo/' % (sys.argv[0],)) | |
print(' python %s --count ~/.bitcoin/testnet3/MP_txlist/' % (sys.argv[0],)) | |
print(' python %s --key_format=hex --value_format=hex ~/.bitcoin/testnet3/chainstate/' % (sys.argv[0],)) | |
parser = argparse.ArgumentParser(description='Process some integers.') | |
parser.add_argument('--count', help='Count entries', action='store_true') | |
parser.add_argument('--key_format', default="native", choices=["hex", "native", "omit"], | |
help='Format in which the key is shown') | |
parser.add_argument('--value_format', default="native", choices=["hex", "native", "omit"], | |
help='Format in which the value is shown') | |
parser.add_argument('path', | |
help='Path to the database') | |
args = parser.parse_args() | |
if args.count: | |
db_count(args.path) | |
else: | |
db_dump(args.path, args.key_format, args.value_format) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment