lbry-sdk/lbrynet/core/utils.py
2016-07-25 16:09:13 -05:00

39 lines
1.1 KiB
Python

import distutils.version
import random
from lbrynet.core.cryptoutils import get_lbry_hash_obj
blobhash_length = get_lbry_hash_obj().digest_size * 2 # digest_size is in bytes, and blob hashes are hex encoded
def generate_id(num=None):
h = get_lbry_hash_obj()
if num is not None:
h.update(str(num))
else:
h.update(str(random.getrandbits(512)))
return h.digest()
def is_valid_blobhash(blobhash):
"""
@param blobhash: string, the blobhash to check
@return: Whether the blobhash is the correct length and contains only valid characters (0-9, a-f)
"""
if len(blobhash) != blobhash_length:
return False
for l in blobhash:
if l not in "0123456789abcdef":
return False
return True
def version_is_greater_than(a, b):
"""Returns True if version a is more recent than version b"""
try:
return distutils.version.StrictVersion(a) > distutils.version.StrictVersion(b)
except ValueError:
return distutils.version.LooseVersion(a) > distutils.version.LooseVersion(b)