def stop(module): try: if module not in proc_handler: xlog.error("module %s not running", module) return if os.path.isfile(os.path.join(root_path, module, "__init__.py")): _start = proc_handler[module]["imp"].start xlog.debug("start to terminate %s module", module) _start.client.terminate() xlog.debug("module %s stopping", module) while _start.client.ready: time.sleep(0.1) else: proc_handler[module]["proc"].terminate() # Sends SIGTERM proc_handler[module]["proc"].wait() del proc_handler[module] xlog.info("module %s stopped", module) except Exception as e: xlog.exception("stop module %s fail:%s", module, e) return "Except:%s" % e return "stop success."
def uncaughtExceptionHandler(type_, value, traceback): print("uncaught Exception:", type_, value, traceback) with open(os.path.join(data_launcher_path, "error.log"), "a") as fd: now = datetime.now() time_str = now.strftime("%b %d %H:%M:%S.%f")[:19] fd.write("%s type:%s value=%s traceback:%s" % (time_str, type_, value, traceback)) xlog.error("uncaught Exception, type=%s value=%s traceback:%s", type_, value, traceback)
def general_gtk_callback(widget=None, data=None): args = data.split('|') if len(args) != 3: xlog.error("general_gtk_callback data:%s", data) return module = args[0] new_version = args[1] action = args[2] if action == "download": download_module(module, new_version) elif action == "install": install_module(module, new_version) elif action == "ignore": ignore_module(module, new_version)
def install_module(module, new_version): import module_init import os, subprocess, sys current_path = os.path.dirname(os.path.abspath(__file__)) new_module_version_path = os.path.abspath( os.path.join(current_path, os.pardir, os.pardir, module, new_version)) #check path exist if not os.path.isdir(new_module_version_path): xlog.error("install module %s dir %s not exist", module, new_module_version_path) return #call setup.py setup_script = os.path.join(new_module_version_path, "setup.py") if not os.path.isfile(setup_script): xlog.warn("update %s fail. setup script %s not exist", module, setup_script) return config.set(["modules", module, "current_version"], str(new_version)) config.save() if module == "launcher": module_init.stop_all() import web_control web_control.stop() subprocess.Popen([sys.executable, setup_script], shell=False) os._exit(0) else: xlog.info("Setup %s version %s ...", module, new_version) try: module_init.stop(module) subprocess.call([sys.executable, setup_script], shell=False) xlog.info("Finished new version setup.") xlog.info("Restarting new version ...") module_init.start(module) except Exception as e: xlog.error("install module %s %s fail:%s", module, new_version, e)
def start(module): if not os.path.isdir(os.path.join(root_path, module)): return try: if module not in config.config["modules"]: xlog.error("module not exist %s", module) raise if module in proc_handler: xlog.error("module %s is running", module) return "module is running" if module not in proc_handler: proc_handler[module] = {} if os.path.isfile(os.path.join(root_path, module, "__init__.py")): if "imp" not in proc_handler[module]: proc_handler[module]["imp"] = __import__( module, globals(), locals(), ['local', 'start'], -1) _start = proc_handler[module]["imp"].start p = threading.Thread(target=_start.main) p.daemon = True p.start() proc_handler[module]["proc"] = p while not _start.client.ready: time.sleep(0.1) else: script_path = os.path.join(root_path, module, 'start.py') if not os.path.isfile(script_path): xlog.warn("start module script not exist:%s", script_path) return "fail" proc_handler[module]["proc"] = subprocess.Popen( [sys.executable, script_path], shell=False) xlog.info("module %s started", module) except Exception as e: xlog.exception("start module %s fail:%s", module, e) raise return "start success."
def start(module): if not os.path.isdir(os.path.join(root_path, module)): return try: if module not in config.config["modules"]: xlog.error("module not exist %s", module) raise if module in proc_handler: xlog.error("module %s is running", module) return "module is running" if module not in proc_handler: proc_handler[module] = {} if os.path.isfile(os.path.join(root_path, module, "__init__.py")): if "imp" not in proc_handler[module]: proc_handler[module]["imp"] = __import__(module, globals(), locals(), ['local', 'start'], -1) _start = proc_handler[module]["imp"].start p = threading.Thread(target=_start.main) p.daemon = True p.start() proc_handler[module]["proc"] = p while not _start.client.ready: time.sleep(0.1) else: script_path = os.path.join(root_path, module, 'start.py') if not os.path.isfile(script_path): xlog.warn("start module script not exist:%s", script_path) return "fail" proc_handler[module]["proc"] = subprocess.Popen([sys.executable, script_path], shell=False) xlog.info("module %s started", module) except Exception as e: xlog.exception("start module %s fail:%s", module, e) raise return "start success."
def confirm_module_ready(port): if port == 0: xlog.error("confirm_module_ready with port 0") time.sleep(1) return False for i in range(200): req = http_request("http://127.0.0.1:%d/is_ready" % port) if req == False: time.sleep(1) continue content = req.read(1024) req.close() #logging.debug("cert_import_ready return:%s", content) if content == "True": return True else: time.sleep(1) return False
def confirm_module_ready(port): if port == 0: xlog.error("confirm_module_ready with port 0") time.sleep(1) return False for i in range(200): req = http_request("http://127.0.0.1:%d/is_ready" % port) if req == False: time.sleep(1) continue content = req.read(1024) req.close() #xlog.debug("cert_import_ready return:%s", content) if content == "True": return True else: time.sleep(1) return False
def notify_general(self, msg="msg", title="Title", buttons={}, timeout=3600): xlog.error("Mac notify_general not implemented.")
def download_module(module, new_version): import os global update_content, update_dict current_path = os.path.dirname(os.path.abspath(__file__)) download_path = os.path.abspath( os.path.join(current_path, os.pardir, os.pardir, 'data', 'downloads')) if not os.path.isdir(download_path): os.mkdir(download_path) try: for source in update_dict["modules"][module]["versions"][new_version]["sources"]: url = source["url"] filename = module + "-" + new_version + ".zip" file_path = os.path.join(download_path, filename) if os.path.isfile(file_path) and sha1_file(file_path) == update_dict["modules"][module]["versions"][new_version]["sha1"]: pass elif not download_file(url, file_path): xlog.warn("download %s fail", url) continue sha1 = sha1_file(file_path) if update_dict["modules"][module]["versions"][new_version]["sha1"] != sha1: xlog.warn("download %s sha1 wrong", url) continue module_path = os.path.abspath( os.path.join(current_path, os.pardir, os.pardir, module)) if not os.path.isdir(module_path): os.path.mkdir(module_path, "755") version_path = os.path.join(module_path, new_version) if os.path.isdir(version_path): xlog.error("module dir exist:%s, download exist.", version_path) return with zipfile.ZipFile(file_path, "r") as dz: dz.extractall(module_path) dz.close() import shutil unzip_path = os.path.abspath(os.path.join(module_path, module + "-" + new_version)) tag_path = os.path.abspath(os.path.join(module_path, new_version)) shutil.move(unzip_path, tag_path) msg = "Module %s new version %s downloaded, Install?" % (module, new_version) if sys.platform == "linux" or sys.platform == "linux2": from gtk_tray import sys_tray data_install = "%s|%s|install" % (module, new_version) data_ignore = "%s|%s|ignore" % (module, new_version) buttons = {1: {"data":data_install, "label":"Install", 'callback':general_gtk_callback}, 2: {"data":data_ignore, "label":"Ignore", 'callback':general_gtk_callback}} sys_tray.notify_general(msg=msg, title="Install", buttons=buttons) elif sys.platform == "win32": from win_tray import sys_tray if sys_tray.dialog_yes_no(msg, u"Install", None, None) == 1: install_module(module, new_version) else: ignore_module(module, new_version) elif sys.platform == "darwin": from mac_tray import sys_tray if sys_tray.dialog_yes_no(msg, u"Install", None, None) == 1: install_module(module, new_version) else: ignore_module(module, new_version) else: install_module(module, new_version) break except Exception as e: xlog.warn("get gae_proxy source fail, content:%s err:%s", update_content, e)
def download_module(module, new_version): import os global update_content, update_dict current_path = os.path.dirname(os.path.abspath(__file__)) download_path = os.path.abspath( os.path.join(current_path, os.pardir, os.pardir, 'data', 'downloads')) if not os.path.isdir(download_path): os.mkdir(download_path) try: for source in update_dict["modules"][module]["versions"][new_version][ "sources"]: url = source["url"] filename = module + "-" + new_version + ".zip" file_path = os.path.join(download_path, filename) if os.path.isfile(file_path) and sha1_file( file_path) == update_dict["modules"][module]["versions"][ new_version]["sha1"]: pass elif not download_file(url, file_path): xlog.warn("download %s fail", url) continue sha1 = sha1_file(file_path) if update_dict["modules"][module]["versions"][new_version][ "sha1"] != sha1: xlog.warn("download %s sha1 wrong", url) continue module_path = os.path.abspath( os.path.join(current_path, os.pardir, os.pardir, module)) if not os.path.isdir(module_path): os.path.mkdir(module_path, "755") version_path = os.path.join(module_path, new_version) if os.path.isdir(version_path): xlog.error("module dir exist:%s, download exist.", version_path) return with zipfile.ZipFile(file_path, "r") as dz: dz.extractall(module_path) dz.close() import shutil unzip_path = os.path.abspath( os.path.join(module_path, module + "-" + new_version)) tag_path = os.path.abspath(os.path.join(module_path, new_version)) shutil.move(unzip_path, tag_path) msg = "Module %s new version %s downloaded, Install?" % ( module, new_version) if sys.platform == "linux" or sys.platform == "linux2": from gtk_tray import sys_tray data_install = "%s|%s|install" % (module, new_version) data_ignore = "%s|%s|ignore" % (module, new_version) buttons = { 1: { "data": data_install, "label": "Install", 'callback': general_gtk_callback }, 2: { "data": data_ignore, "label": "Ignore", 'callback': general_gtk_callback } } sys_tray.notify_general(msg=msg, title="Install", buttons=buttons) elif sys.platform == "win32": from win_tray import sys_tray if sys_tray.dialog_yes_no(msg, u"Install", None, None) == 1: install_module(module, new_version) else: ignore_module(module, new_version) elif sys.platform == "darwin": from mac_tray import sys_tray if sys_tray.dialog_yes_no(msg, u"Install", None, None) == 1: install_module(module, new_version) else: ignore_module(module, new_version) else: install_module(module, new_version) break except Exception as e: xlog.warn("get gae_proxy source fail, content:%s err:%s", update_content, e)