2016-07-20 19:00:34 +02:00
|
|
|
import base64
|
2016-10-18 03:00:24 +02:00
|
|
|
import datetime
|
2016-07-25 23:09:13 +02:00
|
|
|
import distutils.version
|
2016-10-18 03:00:24 +02:00
|
|
|
import logging
|
|
|
|
import json
|
2015-08-20 17:27:15 +02:00
|
|
|
import random
|
2016-09-16 06:14:25 +02:00
|
|
|
import os
|
2016-10-18 03:00:24 +02:00
|
|
|
import socket
|
2016-09-16 06:14:25 +02:00
|
|
|
import yaml
|
2015-08-20 17:27:15 +02:00
|
|
|
|
2016-10-27 21:18:25 +02:00
|
|
|
from lbrynet.conf import settings
|
2016-10-26 09:16:33 +02:00
|
|
|
from lbrynet.conf import AdjustableSettings
|
2016-07-25 23:09:13 +02:00
|
|
|
from lbrynet.core.cryptoutils import get_lbry_hash_obj
|
|
|
|
|
2016-10-18 03:00:24 +02:00
|
|
|
|
2016-09-30 06:06:07 +02:00
|
|
|
# digest_size is in bytes, and blob hashes are hex encoded
|
|
|
|
blobhash_length = get_lbry_hash_obj().digest_size * 2
|
2015-08-20 17:27:15 +02:00
|
|
|
|
|
|
|
|
2016-10-18 03:00:24 +02:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2016-10-05 21:16:20 +02:00
|
|
|
# defining these time functions here allows for easier overriding in testing
|
2016-09-30 06:06:07 +02:00
|
|
|
def now():
|
|
|
|
return datetime.datetime.now()
|
|
|
|
|
2016-10-05 21:16:20 +02:00
|
|
|
|
2016-09-30 06:06:07 +02:00
|
|
|
def utcnow():
|
|
|
|
return datetime.datetime.utcnow()
|
|
|
|
|
2016-10-05 21:16:20 +02:00
|
|
|
|
2016-09-30 06:06:07 +02:00
|
|
|
def isonow():
|
|
|
|
"""Return utc now in isoformat with timezone"""
|
|
|
|
return utcnow().isoformat() + 'Z'
|
|
|
|
|
|
|
|
def today():
|
|
|
|
return datetime.datetime.today()
|
|
|
|
|
|
|
|
|
2015-08-20 17:27:15 +02:00
|
|
|
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()
|
|
|
|
|
|
|
|
|
2016-10-05 21:16:20 +02:00
|
|
|
def is_valid_hashcharacter(char):
|
|
|
|
return char in "0123456789abcdef"
|
|
|
|
|
|
|
|
|
2015-08-20 17:27:15 +02:00
|
|
|
def is_valid_blobhash(blobhash):
|
2016-10-19 00:25:16 +02:00
|
|
|
"""Checks whether the blobhash is the correct length and contains only
|
|
|
|
valid characters (0-9, a-f)
|
|
|
|
|
2015-08-20 17:27:15 +02:00
|
|
|
@param blobhash: string, the blobhash to check
|
|
|
|
|
2016-10-19 00:25:16 +02:00
|
|
|
@return: True/False
|
2015-08-20 17:27:15 +02:00
|
|
|
"""
|
2016-10-19 00:25:16 +02:00
|
|
|
return len(blobhash) == blobhash_length and all(is_valid_hashcharacter(l) for l in blobhash)
|
2016-07-25 23:09:13 +02:00
|
|
|
|
|
|
|
|
|
|
|
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)
|
2016-07-20 19:00:34 +02:00
|
|
|
|
|
|
|
|
|
|
|
def deobfuscate(obfustacated):
|
|
|
|
return base64.b64decode(obfustacated.decode('rot13'))
|
|
|
|
|
|
|
|
|
|
|
|
def obfuscate(plain):
|
|
|
|
return base64.b64encode(plain).encode('rot13')
|
2016-09-16 06:14:25 +02:00
|
|
|
|
|
|
|
|
|
|
|
settings_decoders = {
|
|
|
|
'.json': json.loads,
|
|
|
|
'.yml': yaml.load
|
|
|
|
}
|
|
|
|
|
|
|
|
settings_encoders = {
|
|
|
|
'.json': json.dumps,
|
|
|
|
'.yml': yaml.safe_dump
|
|
|
|
}
|
|
|
|
|
2016-10-26 09:16:33 +02:00
|
|
|
ADJUSTABLE_SETTINGS = AdjustableSettings().get_dict()
|
|
|
|
|
2016-09-16 06:14:25 +02:00
|
|
|
|
|
|
|
def load_settings(path):
|
|
|
|
ext = os.path.splitext(path)[1]
|
2016-10-19 06:12:44 +02:00
|
|
|
with open(path, 'r') as settings_file:
|
|
|
|
data = settings_file.read()
|
2016-09-16 06:14:25 +02:00
|
|
|
decoder = settings_decoders.get(ext, False)
|
|
|
|
assert decoder is not False, "Unknown settings format .%s" % ext
|
|
|
|
return decoder(data)
|
|
|
|
|
|
|
|
|
2016-10-19 06:12:44 +02:00
|
|
|
def save_settings(path):
|
|
|
|
to_save = {k: v for k, v in settings.__dict__.iteritems() if k in ADJUSTABLE_SETTINGS}
|
2016-09-16 06:14:25 +02:00
|
|
|
ext = os.path.splitext(path)[1]
|
|
|
|
encoder = settings_encoders.get(ext, False)
|
|
|
|
assert encoder is not False, "Unknown settings format .%s" % ext
|
2016-10-19 06:12:44 +02:00
|
|
|
with open(path, 'w') as settings_file:
|
|
|
|
settings_file.write(encoder(to_save))
|
2016-10-13 19:35:55 +02:00
|
|
|
|
|
|
|
|
2016-10-18 03:00:24 +02:00
|
|
|
def check_connection(server="www.lbry.io", port=80):
|
|
|
|
"""Attempts to open a socket to server:port and returns True if successful."""
|
|
|
|
try:
|
|
|
|
host = socket.gethostbyname(server)
|
|
|
|
s = socket.create_connection((host, port), 2)
|
|
|
|
return True
|
|
|
|
except Exception as ex:
|
|
|
|
log.info(
|
|
|
|
"Failed to connect to %s:%s. Maybe the internet connection is not working",
|
|
|
|
server, port, exc_info=True)
|
|
|
|
return False
|