update daemon

-notify user when connected to lbrynet
-check for updates from status bar app
This commit is contained in:
Jack 2016-03-02 18:32:48 -05:00
parent 2fc3ec7224
commit 02d8dc22e9
2 changed files with 128 additions and 49 deletions

View file

@ -1,3 +1,4 @@
import locale
import os import os
import sys import sys
import json import json
@ -8,10 +9,10 @@ import subprocess
import logging import logging
import argparse import argparse
import pwd import pwd
import requests
from twisted.web import xmlrpc, server from twisted.web import xmlrpc, server
from twisted.internet import defer, threads, reactor, error from twisted.internet import defer, threads, reactor, error
from datetime import datetime from datetime import datetime
from decimal import Decimal from decimal import Decimal
from StringIO import StringIO from StringIO import StringIO
@ -39,8 +40,9 @@ from lbrynet.core.LBRYcrdWallet import LBRYcrdWallet, LBRYumWallet
from lbrynet.lbryfilemanager.LBRYFileManager import LBRYFileManager from lbrynet.lbryfilemanager.LBRYFileManager import LBRYFileManager
from lbrynet.lbryfile.LBRYFileMetadataManager import DBLBRYFileMetadataManager, TempLBRYFileMetadataManager from lbrynet.lbryfile.LBRYFileMetadataManager import DBLBRYFileMetadataManager, TempLBRYFileMetadataManager
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
# logging.basicConfig(level=logging.DEBUG) # logging.basicConfig(level=logging.DEBUG)
@ -118,6 +120,8 @@ class LBRYDaemon(xmlrpc.XMLRPC):
self.max_key_fee = DEFAULT_MAX_KEY_FEE self.max_key_fee = DEFAULT_MAX_KEY_FEE
self.max_search_results = DEFAULT_MAX_SEARCH_RESULTS self.max_search_results = DEFAULT_MAX_SEARCH_RESULTS
self.restart_message = "" self.restart_message = ""
self.startup_message = ""
self.announced_startup = False
self.search_timeout = 3.0 self.search_timeout = 3.0
self.query_handlers = {} self.query_handlers = {}
@ -229,7 +233,7 @@ class LBRYDaemon(xmlrpc.XMLRPC):
d.addCallback(lambda _: _update_lbrynet()) d.addCallback(lambda _: _update_lbrynet())
d.addCallback(lambda _: _update_lbrycrdd() if self.wallet_type == 'lbrycrd' else _update_lbryum()) d.addCallback(lambda _: _update_lbrycrdd() if self.wallet_type == 'lbrycrd' else _update_lbryum())
d.addCallback(lambda _: os.system("open /Applications/LBRY\ Updater.app &>/dev/null") if self.restart_message d.addCallback(lambda _: os.system("open /Applications/LBRY\ Updater.app &>/dev/null") if self.restart_message
else defer.succeed(None)) else defer.succeed(None))
d.addCallbacks(lambda _: self._restart() if self.restart_message else defer.succeed(None)) d.addCallbacks(lambda _: self._restart() if self.restart_message else defer.succeed(None))
return defer.succeed(None) return defer.succeed(None)
@ -323,8 +327,6 @@ class LBRYDaemon(xmlrpc.XMLRPC):
d = self._stop_server() d = self._stop_server()
if self.session is not None: if self.session is not None:
d.addCallback(lambda _: self.session.shut_down()) d.addCallback(lambda _: self.session.shut_down())
# if self.status_app:
# d.addCallback(lambda _: self.status_app.stop())
return d return d
def _update_settings(self): def _update_settings(self):
@ -453,8 +455,45 @@ class LBRYDaemon(xmlrpc.XMLRPC):
dl.addCallback(combine_results) dl.addCallback(combine_results)
dl.addCallback(create_session) dl.addCallback(create_session)
dl.addCallback(lambda _: self.session.setup()) dl.addCallback(lambda _: self.session.setup())
dl.addCallback(lambda _: self._check_first_run())
dl.addCallback(self._show_first_run_result)
return dl return dl
def _check_first_run(self):
d = self.session.wallet.check_first_run()
d.addCallback(lambda is_first_run: self._do_first_run() if is_first_run else 0.0)
return d
def _do_first_run(self):
d = self.session.wallet.get_new_address()
def send_request(url, data):
r = requests.post(url, json=data)
if r.status_code == 200:
return r.json()['credits_sent']
return 0.0
def log_error(err):
log.warning("unable to request free credits. %s", err.getErrorMessage())
return 0.0
def request_credits(address):
url = "http://credreq.lbry.io/requestcredits"
data = {"address": address}
d = threads.deferToThread(send_request, url, data)
d.addErrback(log_error)
return d
d.addCallback(request_credits)
return d
def _show_first_run_result(self, credits_received):
if credits_received != 0.0:
points_string = locale.format_string("%.2f LBC", (round(credits_received, 2),), grouping=True)
self.startup_message = "Thank you for testing the alpha version of LBRY! You have been given %s for free because we love you. Please give them a few minutes to show up while you catch up with our blockchain." % points_string
else:
self.startup_message = "Connected to LBRYnet"
def _get_lbrycrdd_path(self): def _get_lbrycrdd_path(self):
def get_lbrycrdd_path_conf_file(): def get_lbrycrdd_path_conf_file():
lbrycrdd_path_conf_path = os.path.join(os.path.expanduser("~"), ".lbrycrddpath.conf") lbrycrdd_path_conf_path = os.path.join(os.path.expanduser("~"), ".lbrycrddpath.conf")
@ -629,7 +668,14 @@ class LBRYDaemon(xmlrpc.XMLRPC):
return d return d
def xmlrpc_is_running(self): def xmlrpc_is_running(self):
return True if self.startup_message != "" and self.announced_startup == False:
print "Startup message:", self.startup_message
self.announced_startup = True
return self.startup_message
elif self.announced_startup:
return True
else:
return False
def xmlrpc_get_settings(self): def xmlrpc_get_settings(self):
""" """
@ -973,7 +1019,7 @@ class LBRYDaemon(xmlrpc.XMLRPC):
content_license = None content_license = None
log.info('[' + str(datetime.now()) + '] Publish: ', name, file_path, bid, title, description, thumbnail, log.info('[' + str(datetime.now()) + '] Publish: ', name, file_path, bid, title, description, thumbnail,
key_fee, key_fee_address, content_license) key_fee, key_fee_address, content_license)
p = Publisher(self.session, self.lbry_file_manager, self.session.wallet) p = Publisher(self.session, self.lbry_file_manager, self.session.wallet)
d = p.start(name, file_path, bid, title, description, thumbnail, key_fee, key_fee_address, content_license) d = p.start(name, file_path, bid, title, description, thumbnail, key_fee, key_fee_address, content_license)
@ -1050,40 +1096,52 @@ class LBRYDaemon(xmlrpc.XMLRPC):
return self.fetcher.verbose return self.fetcher.verbose
def xmlrpc_check_for_new_version(self): def xmlrpc_check_for_new_version(self):
message = "" def _check_for_updates(package):
git_version = subprocess.check_output("git ls-remote " + package['git'] + " | grep HEAD | cut -f 1", shell=True)
up_to_date = False
if os.path.isfile(package['version_file']):
f = open(package['version_file'], 'r')
current_version = f.read()
f.close()
git_version = subprocess.check_output("git ls-remote https://github.com/lbryio/lbry.git | grep HEAD | cut -f 1", shell=True) if git_version == current_version:
if os.path.isfile(os.path.join(self.db_dir, "lbrynet_version.txt")): r = package['name'] + " is up to date"
f = open(os.path.join(self.db_dir, "lbrynet_version.txt"), 'r') up_to_date = True
current_version = f.read() else:
f.close() r = package['name'] + " version is out of date"
if git_version == current_version:
message += "LBRYnet is up to date\n"
else: else:
message += "LBRYnet version is out of date, restart the daemon to update\n" r = "Unknown version of " + package['name']
else:
message += "Unknown version of LBRYnet, try running installer again\n"
git_version = subprocess.check_output("git ls-remote https://github.com/jackrobison/lbrynet-app.git | grep HEAD | cut -f 1", shell=True) return (up_to_date, r)
if os.path.isfile(os.path.join(self.wallet_dir, "lbry_app_version.txt")):
f = open(os.path.join(self.wallet_dir, "lbry_app_version.txt"), 'r')
current_version = f.read()
f.close()
if git_version == current_version: package_infos = {
message += "LBRY is up to date" "lbrynet": {"name": "LBRYnet",
else: "git": "https://github.com/lbryio/lbry.git",
message += "LBRY version is out of date, restart the daemon to update" "version_file": os.path.join(self.db_dir, ".lbrynet_version"),
else: "clone": ".lbrygit",
message += "Unknown version of LBRYnet, try running installer again\n" },
"lbryum": {"name": "lbryum",
"git": "https://github.com/lbryio/lbryum.git",
"version_file": os.path.join(self.db_dir, ".lbryum_version"),
"clone": ".lbryumgit",
},
"lbry": {"name": "LBRY",
"git": "https://github.com/jackrobison/lbrynet-app.git",
"version_file": os.path.join(self.db_dir, ".lbry_app_version"),
"clone": None,
},
}
return message return [_check_for_updates(package_infos[p]) for p in package_infos.keys()]
def xmlrpc_start_status_bar_app(self): def xmlrpc_start_status_bar_app(self):
if sys.platform == 'darwin': if sys.platform == 'darwin':
subprocess.Popen("screen -dmS lbry-status bash -c 'lbrynet-daemon-status --startdaemon=False'", shell=True) if os.path.isdir("/Applications/LBRY.app"):
return "Started" # subprocess.Popen("screen -dmS lbry-status bash -c 'lbrynet-daemon-status --startdaemon=False'", shell=True)
subprocess.Popen("screen -dmS lbry-status bash -c 'open /Applications/LBRY.app'")
return "Started"
else:
return "Couldn't find LBRY.app, try running the installer"
else: else:
return "Status bar not implemented on non OS X" return "Status bar not implemented on non OS X"

View file

@ -9,7 +9,6 @@ import argparse
class DaemonStatusBarApp(rumps.App): class DaemonStatusBarApp(rumps.App):
def __init__(self): def __init__(self):
icon_path = 'app.icns' icon_path = 'app.icns'
if os.path.isfile(icon_path): if os.path.isfile(icon_path):
rumps.App.__init__(self, name="LBRY", icon=icon_path, quit_button=None, rumps.App.__init__(self, name="LBRY", icon=icon_path, quit_button=None,
menu=["Open", "Preferences", "View balance", "Quit"]) menu=["Open", "Preferences", "View balance", "Quit"])
@ -17,51 +16,69 @@ class DaemonStatusBarApp(rumps.App):
rumps.App.__init__(self, name="LBRY", title="LBRY", quit_button=None, rumps.App.__init__(self, name="LBRY", title="LBRY", quit_button=None,
menu=["Open", "Preferences", "View balance", "Quit"]) menu=["Open", "Preferences", "View balance", "Quit"])
@rumps.timer(1)
def alert_daemon_start(self):
daemon = xmlrpclib.ServerProxy("http://localhost:7080/")
try:
start_msg = daemon.is_running()
if isinstance(start_msg, str):
rumps.notification(title='LBRY', subtitle='', message=str(start_msg), sound=True)
update_info = daemon.check_for_new_version()
update_msg = ""
for p in update_info:
if not p[0]:
update_msg += p[1] + "\n"
if update_msg:
update_msg += "\n Try running the installer again to fix this"
rumps.notification(title='LBRY', subtitle='', message=update_msg, sound=True)
except:
pass
@rumps.clicked('Open') @rumps.clicked('Open')
def get_ui(self): def get_ui(self):
daemon = xmlrpclib.ServerProxy("http://localhost:7080/")
try: try:
daemon = xmlrpclib.ServerProxy('http://localhost:7080')
daemon.is_running() daemon.is_running()
webbrowser.open("lbry://lbry") webbrowser.open("lbry://lbry")
except: except:
try: try:
rumps.notification(title='LBRY', subtitle='status', message="Couldn't connect to lbrynet daemon", sound=True) rumps.notification(title='LBRY', subtitle='', message="Couldn't connect to lbrynet daemon", sound=True)
except: except:
rumps.alert(title='LBRY', message="Couldn't connect to lbrynet daemon") rumps.alert(title='LBRY', message="Couldn't connect to lbrynet daemon")
@rumps.clicked("Preferences") @rumps.clicked("Preferences")
def prefs(self, _): def prefs(self):
daemon = xmlrpclib.ServerProxy("http://localhost:7080/")
try: try:
daemon = xmlrpclib.ServerProxy('http://localhost:7080')
daemon.is_running() daemon.is_running()
webbrowser.open("lbry://settings") webbrowser.open("lbry://settings")
except: except:
rumps.notification(title='LBRY', subtitle='status', message="Couldn't connect to lbrynet daemon", sound=True) rumps.notification(title='LBRY', subtitle='', message="Couldn't connect to lbrynet daemon", sound=True)
@rumps.clicked("View balance") @rumps.clicked("View balance")
def disp_balance(self): def disp_balance(self):
daemon = xmlrpclib.ServerProxy('http://localhost:7080') daemon = xmlrpclib.ServerProxy("http://localhost:7080/")
try: try:
balance = daemon.get_balance() balance = daemon.get_balance()
r = round(float(balance), 2)
try: try:
rumps.notification(title='LBRY', subtitle='status', message="Your balance is " + str(balance), sound=False) rumps.notification(title='LBRY', subtitle='', message=str("Your balance is %.2f LBC" % r), sound=False)
except: except:
rumps.alert(title='LBRY', message="Your balance is " + str(balance)) rumps.alert(title='LBRY', message=str("Your balance is %.2f LBC" % r))
except: except:
try: try:
rumps.notification(title='LBRY', subtitle='status', message="Couldn't connect to lbrynet daemon", sound=True) rumps.notification(title='LBRY', subtitle='', message="Couldn't connect to lbrynet daemon", sound=True)
except: except:
rumps.alert(title='LBRY', message="Couldn't connect to lbrynet daemon") rumps.alert(title='LBRY', message="Couldn't connect to lbrynet daemon")
@rumps.clicked('Quit') @rumps.clicked('Quit')
def clean_quit(self): def clean_quit(self):
daemon = xmlrpclib.ServerProxy('http://localhost:7080') daemon = xmlrpclib.ServerProxy("http://localhost:7080/")
try: try:
daemon.stop() daemon.stop()
except: except:
pass pass
rumps.quit_application() rumps.quit_application()
@ -74,10 +91,14 @@ def main():
args = parser.parse_args() args = parser.parse_args()
if str(args.startdaemon).lower() == "true": if str(args.startdaemon).lower() == "true":
subprocess.Popen("screen -dmS lbrynet bash -c " daemon = xmlrpclib.ServerProxy('http://localhost:7080')
"'PYTHONPATH=$PYTHONPATH:`cat /Users/${USER}/Library/Application\ Support/lbrynet/.python_path`; " try:
"PATH=$PATH:`cat /Users/${USER}/Library/Application\ Support/lbrynet/.lbry_bin_path`; " daemon.is_running()
"lbrynet-daemon --update=False'", shell=True) except:
subprocess.Popen("screen -dmS lbrynet bash -c "
"'PYTHONPATH=$PYTHONPATH:`cat /Users/${USER}/Library/Application\ Support/lbrynet/.python_path`; "
"PATH=$PATH:`cat /Users/${USER}/Library/Application\ Support/lbrynet/.lbry_bin_path`; "
"lbrynet-daemon --update=False'", shell=True)
status_app = DaemonStatusBarApp() status_app = DaemonStatusBarApp()
status_app.run() status_app.run()