2016-07-20 19:00:34 +02:00
|
|
|
import base64
|
2018-07-18 02:35:53 +02:00
|
|
|
import codecs
|
2016-10-18 03:00:24 +02:00
|
|
|
import datetime
|
2015-08-20 17:27:15 +02:00
|
|
|
import random
|
2016-10-18 03:00:24 +02:00
|
|
|
import socket
|
2016-12-30 19:35:17 +01:00
|
|
|
import string
|
2018-07-21 20:12:29 +02:00
|
|
|
import json
|
2018-06-07 18:18:07 +02:00
|
|
|
import traceback
|
|
|
|
import functools
|
|
|
|
import logging
|
2016-11-19 23:58:40 +01:00
|
|
|
import pkg_resources
|
2018-06-07 18:18:07 +02:00
|
|
|
from twisted.python.failure import Failure
|
2018-02-28 20:59:12 +01:00
|
|
|
from twisted.internet import defer
|
2018-09-17 22:31:44 +02:00
|
|
|
from lbrynet.schema.claim import ClaimDict
|
2018-11-07 21:15:05 +01:00
|
|
|
from lbrynet.cryptoutils import get_lbry_hash_obj
|
2016-07-25 23:09:13 +02:00
|
|
|
|
2018-06-07 18:18:07 +02:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
2016-10-18 03:00:24 +02:00
|
|
|
|
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'
|
|
|
|
|
2016-10-31 22:19:19 +01:00
|
|
|
|
2016-09-30 06:06:07 +02:00
|
|
|
def today():
|
|
|
|
return datetime.datetime.today()
|
|
|
|
|
|
|
|
|
2017-01-02 20:52:24 +01:00
|
|
|
def timedelta(**kwargs):
|
|
|
|
return datetime.timedelta(**kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
def datetime_obj(*args, **kwargs):
|
|
|
|
return datetime.datetime(*args, **kwargs)
|
|
|
|
|
|
|
|
|
2017-02-09 17:22:23 +01:00
|
|
|
def call_later(delay, func, *args, **kwargs):
|
2017-04-10 16:51:49 +02:00
|
|
|
# Import here to ensure that it gets called after installing a reactor
|
2017-02-09 17:22:23 +01:00
|
|
|
# see: http://twistedmatrix.com/documents/current/core/howto/choosing-reactor.html
|
|
|
|
from twisted.internet import reactor
|
|
|
|
return reactor.callLater(delay, func, *args, **kwargs)
|
|
|
|
|
2018-11-07 21:15:05 +01:00
|
|
|
|
2017-06-20 21:03:17 +02:00
|
|
|
def safe_start_looping_call(looping_call, interval_sec):
|
|
|
|
if not looping_call.running:
|
|
|
|
looping_call.start(interval_sec)
|
|
|
|
|
|
|
|
def safe_stop_looping_call(looping_call):
|
|
|
|
if looping_call.running:
|
|
|
|
looping_call.stop()
|
2017-02-09 17:22:23 +01:00
|
|
|
|
2015-08-20 17:27:15 +02:00
|
|
|
def generate_id(num=None):
|
|
|
|
h = get_lbry_hash_obj()
|
|
|
|
if num is not None:
|
2018-06-12 17:54:01 +02:00
|
|
|
h.update(str(num).encode())
|
2015-08-20 17:27:15 +02:00
|
|
|
else:
|
2018-06-12 17:54:01 +02:00
|
|
|
h.update(str(random.getrandbits(512)).encode())
|
2015-08-20 17:27:15 +02:00
|
|
|
return h.digest()
|
|
|
|
|
|
|
|
|
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"""
|
2016-11-19 23:58:40 +01:00
|
|
|
return pkg_resources.parse_version(a) > pkg_resources.parse_version(b)
|
2016-07-20 19:00:34 +02:00
|
|
|
|
|
|
|
|
2018-07-18 02:35:53 +02:00
|
|
|
def rot13(some_str):
|
|
|
|
return codecs.encode(some_str, 'rot_13')
|
|
|
|
|
|
|
|
|
2016-07-20 19:00:34 +02:00
|
|
|
def deobfuscate(obfustacated):
|
2018-10-18 21:57:15 +02:00
|
|
|
return base64.b64decode(rot13(obfustacated)).decode()
|
2016-07-20 19:00:34 +02:00
|
|
|
|
|
|
|
|
|
|
|
def obfuscate(plain):
|
2018-07-22 03:12:33 +02:00
|
|
|
return rot13(base64.b64encode(plain).decode())
|
2016-09-16 06:14:25 +02:00
|
|
|
|
|
|
|
|
2018-08-04 23:19:10 +02:00
|
|
|
def check_connection(server="lbry.io", port=80, timeout=5):
|
2016-10-18 03:00:24 +02:00
|
|
|
"""Attempts to open a socket to server:port and returns True if successful."""
|
2017-09-20 22:52:16 +02:00
|
|
|
log.debug('Checking connection to %s:%s', server, port)
|
2016-10-18 03:00:24 +02:00
|
|
|
try:
|
2017-10-31 17:18:47 +01:00
|
|
|
server = socket.gethostbyname(server)
|
2018-10-16 17:03:56 +02:00
|
|
|
conn = socket.create_connection((server, port), timeout)
|
|
|
|
conn.close()
|
2016-11-11 17:10:00 +01:00
|
|
|
log.debug('Connection successful')
|
2016-10-18 03:00:24 +02:00
|
|
|
return True
|
2017-09-20 22:52:16 +02:00
|
|
|
except (socket.gaierror, socket.herror) as ex:
|
2017-10-31 17:18:55 +01:00
|
|
|
log.warning("Failed to connect to %s:%s. Unable to resolve domain. Trying to bypass DNS",
|
|
|
|
server, port)
|
2017-09-20 22:52:16 +02:00
|
|
|
try:
|
|
|
|
server = "8.8.8.8"
|
|
|
|
port = 53
|
|
|
|
socket.create_connection((server, port), timeout)
|
|
|
|
log.debug('Connection successful')
|
|
|
|
return True
|
|
|
|
except Exception as ex:
|
2017-10-31 17:18:55 +01:00
|
|
|
log.error("Failed to connect to %s:%s. Maybe the internet connection is not working",
|
|
|
|
server, port)
|
2017-09-20 22:52:16 +02:00
|
|
|
return False
|
2016-10-18 03:00:24 +02:00
|
|
|
except Exception as ex:
|
2017-10-31 17:18:55 +01:00
|
|
|
log.error("Failed to connect to %s:%s. Maybe the internet connection is not working",
|
|
|
|
server, port)
|
2016-10-18 03:00:24 +02:00
|
|
|
return False
|
2016-10-22 00:26:36 +02:00
|
|
|
|
|
|
|
|
2016-12-30 19:35:17 +01:00
|
|
|
def random_string(length=10, chars=string.ascii_lowercase):
|
|
|
|
return ''.join([random.choice(chars) for _ in range(length)])
|
2017-02-16 15:09:21 +01:00
|
|
|
|
|
|
|
|
|
|
|
def short_hash(hash_str):
|
|
|
|
return hash_str[:6]
|
2017-03-09 16:39:17 +01:00
|
|
|
|
|
|
|
|
|
|
|
def get_sd_hash(stream_info):
|
|
|
|
if not stream_info:
|
|
|
|
return None
|
2017-04-07 02:37:10 +02:00
|
|
|
if isinstance(stream_info, ClaimDict):
|
|
|
|
return stream_info.source_hash
|
2018-02-06 07:16:10 +01:00
|
|
|
result = stream_info.get('claim', {}).\
|
|
|
|
get('value', {}).\
|
|
|
|
get('stream', {}).\
|
|
|
|
get('source', {}).\
|
|
|
|
get('source')
|
2018-02-04 05:08:15 +01:00
|
|
|
if not result:
|
2018-07-22 01:08:28 +02:00
|
|
|
log.warning("Unable to get sd_hash")
|
2018-02-04 05:08:15 +01:00
|
|
|
return result
|
2017-03-15 21:19:11 +01:00
|
|
|
|
|
|
|
|
|
|
|
def json_dumps_pretty(obj, **kwargs):
|
|
|
|
return json.dumps(obj, sort_keys=True, indent=2, separators=(',', ': '), **kwargs)
|
2018-02-28 20:59:12 +01:00
|
|
|
|
|
|
|
|
2018-07-22 00:34:59 +02:00
|
|
|
class DeferredLockContextManager:
|
2017-10-23 21:36:50 +02:00
|
|
|
def __init__(self, lock):
|
|
|
|
self._lock = lock
|
|
|
|
|
|
|
|
def __enter__(self):
|
2018-10-18 13:40:37 +02:00
|
|
|
yield self._lock.acquire()
|
2017-10-23 21:36:50 +02:00
|
|
|
|
|
|
|
def __exit__(self, exc_type, exc_val, exc_tb):
|
|
|
|
yield self._lock.release()
|
|
|
|
|
|
|
|
|
2018-02-28 20:59:12 +01:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def DeferredDict(d, consumeErrors=False):
|
|
|
|
keys = []
|
|
|
|
dl = []
|
|
|
|
response = {}
|
2018-07-06 07:17:20 +02:00
|
|
|
for k, v in d.items():
|
2018-02-28 20:59:12 +01:00
|
|
|
keys.append(k)
|
|
|
|
dl.append(v)
|
|
|
|
results = yield defer.DeferredList(dl, consumeErrors=consumeErrors)
|
|
|
|
for k, (success, result) in zip(keys, results):
|
|
|
|
if success:
|
|
|
|
response[k] = result
|
|
|
|
defer.returnValue(response)
|
2018-06-06 23:21:56 +02:00
|
|
|
|
|
|
|
|
2018-07-22 00:34:59 +02:00
|
|
|
class DeferredProfiler:
|
2018-06-06 23:21:56 +02:00
|
|
|
def __init__(self):
|
|
|
|
self.profile_results = {}
|
|
|
|
|
|
|
|
def add_result(self, fn, start_time, finished_time, stack, success):
|
|
|
|
self.profile_results[fn].append((start_time, finished_time, stack, success))
|
|
|
|
|
|
|
|
def show_profile_results(self, fn):
|
|
|
|
profile_results = list(self.profile_results[fn])
|
|
|
|
call_counts = {
|
|
|
|
caller: [(start, finished, finished - start, success)
|
|
|
|
for (start, finished, _caller, success) in profile_results
|
|
|
|
if _caller == caller]
|
2018-10-18 12:42:45 +02:00
|
|
|
for caller in {result[2] for result in profile_results}
|
2018-06-06 23:21:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
log.info("called %s %i times from %i sources\n", fn.__name__, len(profile_results), len(call_counts))
|
|
|
|
for caller in sorted(list(call_counts.keys()), key=lambda c: len(call_counts[c]), reverse=True):
|
|
|
|
call_info = call_counts[caller]
|
|
|
|
times = [r[2] for r in call_info]
|
|
|
|
own_time = sum(times)
|
|
|
|
times.sort()
|
|
|
|
longest = 0 if not times else times[-1]
|
|
|
|
shortest = 0 if not times else times[0]
|
|
|
|
log.info(
|
|
|
|
"%i successes and %i failures\nlongest %f, shortest %f, avg %f\ncaller:\n%s",
|
|
|
|
len([r for r in call_info if r[3]]),
|
|
|
|
len([r for r in call_info if not r[3]]),
|
|
|
|
longest, shortest, own_time / float(len(call_info)), caller
|
|
|
|
)
|
|
|
|
|
|
|
|
def profiled_deferred(self, reactor=None):
|
|
|
|
if not reactor:
|
|
|
|
from twisted.internet import reactor
|
|
|
|
|
|
|
|
def _cb(result, fn, start, caller_info):
|
2018-11-26 07:02:46 +01:00
|
|
|
got_error = isinstance(result, (Failure, Exception))
|
|
|
|
self.add_result(fn, start, reactor.seconds(), caller_info, not got_error)
|
|
|
|
if got_error:
|
|
|
|
raise result
|
2018-06-06 23:21:56 +02:00
|
|
|
else:
|
|
|
|
return result
|
|
|
|
|
|
|
|
def _profiled_deferred(fn):
|
|
|
|
reactor.addSystemEventTrigger("after", "shutdown", self.show_profile_results, fn)
|
|
|
|
self.profile_results[fn] = []
|
|
|
|
|
|
|
|
@functools.wraps(fn)
|
|
|
|
def _wrapper(*args, **kwargs):
|
|
|
|
caller_info = "".join(traceback.format_list(traceback.extract_stack()[-3:-1]))
|
|
|
|
start = reactor.seconds()
|
|
|
|
d = defer.maybeDeferred(fn, *args, **kwargs)
|
|
|
|
d.addBoth(_cb, fn, start, caller_info)
|
|
|
|
return d
|
|
|
|
|
|
|
|
return _wrapper
|
|
|
|
|
|
|
|
return _profiled_deferred
|
|
|
|
|
|
|
|
|
|
|
|
_profiler = DeferredProfiler()
|
|
|
|
profile_deferred = _profiler.profiled_deferred
|