Created
November 2, 2014 23:46
-
-
Save dexX7/cbd16b05f1f3870b7afc 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 binascii | |
import sys | |
import leveldb | |
def b2x(b): | |
"""Convert bytes to a hex string""" | |
if sys.version > '3': | |
return binascii.hexlify(b).decode('utf8') | |
else: | |
return binascii.hexlify(b) | |
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, to_hex=0): | |
"""Prints records of LevelDB database""" | |
db = leveldb.LevelDB(filepath) | |
for k, v in db.RangeIter(None, None): | |
if to_hex: | |
k = b2x(k) | |
v = b2x(v) | |
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] path1 [path2, path3, ...]\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 --binary ~/.bitcoin/testnet3/chainstate/' % (sys.argv[0],)) | |
count = 0 | |
to_hex = 0 | |
for arg in sys.argv[1:]: | |
if '--count' in arg: | |
count = 1 | |
continue | |
if '--binary' in arg: | |
to_hex = 1 | |
continue | |
if count: | |
db_count(arg) | |
else: | |
db_dump(arg, to_hex) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment