From 3910112f7d7d8ee10c71f99faf8ffa5e19a1fe98 Mon Sep 17 00:00:00 2001 From: Vu Hong Quang Date: Sat, 20 Apr 2019 11:47:01 +0700 Subject: [PATCH] Upgrade to v0.2.0 (Final) Misc fixes for process path and exiting --- app/hub.py | 16 +++++---- manager/ProcessManager.py | 71 +++++++++++++-------------------------- 2 files changed, 33 insertions(+), 54 deletions(-) diff --git a/app/hub.py b/app/hub.py index 86a7963..3ad7e5e 100644 --- a/app/hub.py +++ b/app/hub.py @@ -20,7 +20,7 @@ QInputDialog, QLineEdit from PySide.QtCore import QObject, Slot, Signal -from utils.common import print_money, print_money2, readFile +from utils.common import print_money, print_money2 from settings import APP_NAME, VERSION, DATA_DIR, COIN, makeDir, seed_languages from utils.logger import log, LEVEL_ERROR, LEVEL_INFO @@ -586,7 +586,6 @@ def new_wallet_ui(self): QMessageBox.warning(self.ui, "Incorrect Wallet Password", "Wallet password is not correct!") return - ret = self.ui.wallet_rpc_manager.rpc_request.close_wallet() if ret['status'] == "ERROR": error_message = ret['message'] @@ -632,7 +631,14 @@ def open_existing_wallet(self): if os.path.normpath(os.path.dirname(wallet_filename)) != os.path.normpath(wallet_dir_path): QMessageBox.warning(self.ui, \ 'Open Wallet File',\ - """Error: Only wallet files at default location are available for opening""") + """Error: Only wallet files at default location are available for opening.
+ You can import wallet via 'New... > Import' feature instead.""") + return + + if os.path.basename(wallet_filename) == os.path.basename(self.ui.wallet_info.wallet_filepath): + QMessageBox.warning(self.ui, \ + 'Open Wallet File',\ + """Error: Cannot open the same wallet!""") return while True: @@ -682,10 +688,6 @@ def open_existing_wallet(self): while not self.ui.wallet_rpc_manager.is_ready(): self.app_process_events(0.1) -# self.app_process_events(10) -# self.on_open_existing_wallet_complete_event.emit() -# QMessageBox.information(self.ui, "Wallet Loaded", "Wallet [" -# + os.path.basename(wallet_filename) + "] successfully loaded") except Exception, err: log(str(err), LEVEL_ERROR) ret = self.ui.wallet_rpc_manager.rpc_request.open_wallet( diff --git a/manager/ProcessManager.py b/manager/ProcessManager.py index 783120a..bd87ebb 100644 --- a/manager/ProcessManager.py +++ b/manager/ProcessManager.py @@ -13,7 +13,6 @@ from subprocess import Popen, PIPE, STDOUT import signal from threading import Thread -from multiprocessing import Event from time import sleep from uuid import uuid4 @@ -21,25 +20,16 @@ from rpc import WalletRPCRequest -CREATE_NO_WINDOW = 0x08000000 if sys.platform == "win32" else 0 # disable creating the window -DETACHED_PROCESS = 0x00000008 # forcing the child to have no console at all -BELOW_NORMAL_PRIORITY_CLASS = 0x00004000 - -def signal_handler(signal, frame): - sleep(1) - print('Ctrl+C received') - -signal.signal(signal.SIGINT, signal_handler) - class ProcessManager(Thread): - def __init__(self, proc_args, proc_name=""): + def __init__(self, bin_path, proc_args, proc_name=""): super(ProcessManager, self).__init__() - args_array = proc_args.encode( sys.getfilesystemencoding() ).split(u' ') + args_array = proc_args.encode(sys.getfilesystemencoding()).split(u' ') + args_array.insert(0, bin_path.encode(sys.getfilesystemencoding())) self.proc = Popen(args_array, shell=True, stdout=PIPE, stderr=STDOUT, stdin=PIPE) self.proc_name = proc_name - self.daemon = False + self.daemon = True log("[%s] started" % proc_name, LEVEL_INFO, self.proc_name) def run(self): @@ -57,7 +47,6 @@ def send_command(self, cmd): def get_pid(self): return self.proc.pid - def is_proc_running(self): return (self.proc.poll() is None) @@ -65,10 +54,9 @@ def is_proc_running(self): class SumokoindManager(ProcessManager): def __init__(self, resources_path, log_level=0, block_sync_size=20, limit_rate_up=2048, limit_rate_down=8192): - proc_args = u'%s/bin/sumokoind --log-level %d --block-sync-size %d --limit-rate-up %d --limit-rate-down %d' % (resources_path, log_level, block_sync_size, limit_rate_up, limit_rate_down) - super(SumokoindManager, self).__init__(proc_args, "sumokoind") - self.synced = Event() - self.stopped = Event() + bin_path = u'%s/bin/sumokoind' % resources_path + proc_args = u'--log-level %d --block-sync-size %d --limit-rate-up %d --limit-rate-down %d' % (log_level, block_sync_size, limit_rate_up, limit_rate_down) + super(SumokoindManager, self).__init__(bin_path, proc_args, "sumokoind") def run(self): err_str = "ERROR" @@ -82,8 +70,6 @@ def run(self): if not self.proc.stdout.closed: self.proc.stdout.close() - self.stopped.set() - def stop(self): try: @@ -110,24 +96,21 @@ def __init__(self, resources_path, wallet_dir_path, app, log_level=1): self.rpc_user_name = str(uuid4().hex)[:12] self.rpc_password = str(uuid4().hex)[:12] wallet_log_dir_path = os.path.abspath(os.path.join(wallet_dir_path, ".." , "logs", "sumo-wallet-rpc-bin.log")) - wallet_rpc_args = u'%s/bin/sumo-wallet-rpc --wallet-dir %s --log-file %s --rpc-bind-port 19738 --rpc-login %s:%s --log-level %d' \ - % (resources_path, wallet_dir_path, wallet_log_dir_path, self.rpc_user_name, self.rpc_password, log_level) - - super(WalletRPCManager, self).__init__(wallet_rpc_args, "sumo-wallet-rpc") + bin_path = u'%s/bin/sumo-wallet-rpc' % resources_path + wallet_rpc_args = u'--wallet-dir %s --log-file %s --rpc-bind-port 19738 --rpc-login %s:%s --log-level %d' \ + % (wallet_dir_path, wallet_log_dir_path, self.rpc_user_name, self.rpc_password, log_level) + super(WalletRPCManager, self).__init__(bin_path, wallet_rpc_args, "sumo-wallet-rpc") self.rpc_request = WalletRPCRequest(app, self.rpc_user_name, self.rpc_password) self._ready = False self._block_height = 0 self._block_hash = None -# self.last_log_lines = [] self._stopped = False def run(self): rpc_ready_strs = ["Binding on 127.0.0.1", "Starting wallet rpc server", "Run net_service loop", "Refresh done"] err_str = "ERROR" height_regex = re.compile(r"Processed block: \<([a-z0-9]+)\>, height (\d+)") -# height_regex2 = re.compile(r"Skipped block by height: (\d+)") -# height_regex3 = re.compile(r"Skipped block by timestamp, height: (\d+)") for line in iter(self.proc.stdout.readline, b''): m_height = height_regex.search(line) @@ -135,12 +118,6 @@ def run(self): self._block_hash = m_height.group(1) self._block_height = m_height.group(2) self._ready = False -# if not m_height: -# m_height = height_regex2.search(line) -# if m_height: self._block_height = m_height.group(1) -# if not m_height: -# m_height = height_regex3.search(line) -# if m_height: self._block_height = m_height.group(1) if not self._ready and any(s in line for s in rpc_ready_strs): self._ready = True @@ -156,8 +133,8 @@ def run(self): if self._stopped: break - if not self.proc.stdout.closed: - self.proc.stdout.close() +# if not self.proc.stdout.closed: +# self.proc.stdout.close() def is_ready(self): return self._ready @@ -177,18 +154,19 @@ def reset_block_height(self): def stop(self): self._stopped = True self.rpc_request.stop_wallet(no_wait=True) + sleep(2) counter = 1 - while True: + while self.is_proc_running(): try: - if counter < 30: - if counter % 10 == 0: - try: - os.kill(self.proc.pid, signal.CTRL_C_EVENT) - except KeyboardInterrupt: - pass - sleep(1) - if not self.is_proc_running(): - break + if counter < 20: + if counter % 5 == 0: + if hasattr(signal, 'CTRL_C_EVENT'): + try: + os.kill(self.proc.pid, signal.CTRL_C_EVENT) + except KeyboardInterrupt: + pass + else: + os.kill(self.proc.pid, signal.SIGINT) sleep(1) counter += 1 else: @@ -197,6 +175,5 @@ def stop(self): break except: break - self._ready = Event() log("[%s] stopped" % self.proc_name, LEVEL_INFO, self.proc_name)