def req_init_module_handler(self): req = urlparse.urlparse(self.path).query reqs = urlparse.parse_qs(req, keep_blank_values=True) data = '' try: module = reqs['module'][0] config.load() if reqs['cmd'] == ['start']: result = module_init.start(module) data = '{ "module": "%s", "cmd": "start", "result": "%s" }' % ( module, result) elif reqs['cmd'] == ['stop']: result = module_init.stop(module) data = '{ "module": "%s", "cmd": "stop", "result": "%s" }' % ( module, result) elif reqs['cmd'] == ['restart']: result_stop = module_init.stop(module) result_start = module_init.start(module) data = '{ "module": "%s", "cmd": "restart", "stop_result": "%s", "start_result": "%s" }' % ( module, result_stop, result_start) except Exception as e: xlog.exception("init_module except:%s", e) self.send_response("text/html", data)
def req_init_module_handler(self): req = urlparse.urlparse(self.path).query reqs = urlparse.parse_qs(req, keep_blank_values=True) data = "" try: module = reqs["module"][0] config.load() if reqs["cmd"] == ["start"]: result = module_init.start(module) data = '{ "module": "%s", "cmd": "start", "result": "%s" }' % (module, result) elif reqs["cmd"] == ["stop"]: result = module_init.stop(module) data = '{ "module": "%s", "cmd": "stop", "result": "%s" }' % (module, result) elif reqs["cmd"] == ["restart"]: result_stop = module_init.stop(module) result_start = module_init.start(module) data = '{ "module": "%s", "cmd": "restart", "stop_result": "%s", "start_result": "%s" }' % ( module, result_stop, result_start, ) except Exception as e: xlog.exception("init_module except:%s", e) self.send_response("text/html", data)
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 update_version(version): try: download_overwrite_new_version(version) update_config(version) restart_xxnet() except Exception as e: xlog.exception("update version %s fail:%r", version, e)
def check_update(): try: update_rule = config.get(["update", "check_update"], "stable") if update_rule == "dont-check": return check_push_update() if update_rule != "stable" and update_rule != "test": return versions = update_from_github.get_github_versions() current_version = update_from_github.current_version() if update_rule == "test": if LooseVersion(current_version) < LooseVersion(versions[0][1]): xlog.info("update to test version %s", versions[0][1]) update_from_github.update_version(versions[0][1]) elif update_rule == "stable": if LooseVersion(current_version) < LooseVersion(versions[1][1]): xlog.info("update to stable version %s", versions[1][1]) update_from_github.update_version(versions[1][1]) except IOError as e: xlog.warn("check update fail:%r", e) except Exception as e: xlog.exception("check_update fail:%r", e)
def get_xxnet_url_version(readme_file): try: fd = open(readme_file, "r") lines = fd.readlines() p = re.compile(r'https://codeload.github.com/XX-net/XX-Net/zip/([0-9]+)\.([0-9]+)\.([0-9]+)') for line in lines: m = p.match(line) if m: version = m.group(1) + "." + m.group(2) + "." + m.group(3) return m.group(0), version except Exception as e: xlog.exception("xxnet_version fail:%s", e) raise "get_version_fail:" % readme_file
def parse_readme_versions(readme_file): versions = [] try: fd = open(readme_file, "r") lines = fd.readlines() p = re.compile(r'https://codeload.github.com/XX-net/XX-Net/zip/([0-9]+)\.([0-9]+)\.([0-9]+)') for line in lines: m = p.match(line) if m: version = m.group(1) + "." + m.group(2) + "." + m.group(3) versions.append([m.group(0), version]) if len(versions) == 2: return versions except Exception as e: xlog.exception("xxnet_version fail:%r", e) raise "get_version_fail:" % readme_file
def current_version(): readme_file = os.path.join(root_path, "README.md") try: fd = open(readme_file, "r") lines = fd.readlines() import re p = re.compile(r'https://codeload.github.com/XX-net/XX-Net/zip/([0-9]+)\.([0-9]+)\.([0-9]+)') #zip/([0-9]+).([0-9]+).([0-9]+) #m = p.match(content) for line in lines: m = p.match(line) if m: version = m.group(1) + "." + m.group(2) + "." + m.group(3) return version except Exception as e: xlog.exception("xxnet_version fail") return "get_version_fail"
def download_file(url, filename): if url not in progress: progress[url] = {} progress[url]["status"] = "downloading" progress[url]["size"] = 1 progress[url]["downloaded"] = 0 else: if progress[url]["status"] == "downloading": xlog.warn("url in downloading, %s", url) return False for i in range(0, 2): try: xlog.info("download %s to %s, retry:%d", url, filename, i) opener = get_opener(i) req = opener.open(url, timeout=30) progress[url]["size"] = int(req.headers.get('content-length') or 0) chunk_len = 65536 downloaded = 0 with open(filename, 'wb') as fp: while True: chunk = req.read(chunk_len) if not chunk: break fp.write(chunk) downloaded += len(chunk) progress[url]["downloaded"] = downloaded if downloaded != progress[url]["size"]: xlog.warn("download size:%d, need size:%d, download fail.", downloaded, progress[url]["size"]) continue else: progress[url]["status"] = "finished" return True except (urllib2.URLError, ssl.SSLError) as e: xlog.warn("download %s to %s URL fail:%r", url, filename, e) continue except Exception as e: xlog.exception("download %s to %s fail:%r", url, filename, e) continue progress[url]["status"] = "failed" return False
def current_version(): readme_file = os.path.join(root_path, "README.md") try: fd = open(readme_file, "r") lines = fd.readlines() import re p = re.compile( r'https://codeload.github.com/XX-net/XX-Net/zip/([0-9]+)\.([0-9]+)\.([0-9]+)' ) #zip/([0-9]+).([0-9]+).([0-9]+) #m = p.match(content) for line in lines: m = p.match(line) if m: version = m.group(1) + "." + m.group(2) + "." + m.group(3) return version except Exception as e: xlog.exception("xxnet_version fail") return "get_version_fail"
def parse_readme_versions(readme_file): versions = [] try: fd = open(readme_file, "r") lines = fd.readlines() p = re.compile( r'https://codeload.github.com/XX-net/XX-Net/zip/([0-9]+)\.([0-9]+)\.([0-9]+)' ) for line in lines: m = p.match(line) if m: version = m.group(1) + "." + m.group(2) + "." + m.group(3) versions.append([m.group(0), version]) if len(versions) == 2: return versions except Exception as e: xlog.exception("xxnet_version fail:%r", e) raise "get_version_fail:" % readme_file
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 download_file(url, file): if url not in download_progress: download_progress[url] = {} download_progress[url]["status"] = "downloading" else: if download_progress[url]["status"] == "downloading": xlog.warn("url in downloading, %s", url) return False for i in range(0, 2): try: xlog.info("download %s to %s, retry:%d", url, file, i) opener = get_opener(i) req = opener.open(url) download_progress[url]["size"] = int( req.headers.get('content-length') or 0) CHUNK = 16 * 1024 downloaded = 0 with open(file, 'wb') as fp: while True: chunk = req.read(CHUNK) if not chunk: break fp.write(chunk) downloaded += len(chunk) download_progress[url]["downloaded"] = downloaded if downloaded != download_progress[url]["size"]: xlog.warn("download size:%d, need size:%d, download fail.", downloaded, download_progress[url]["size"]) continue else: download_progress[url]["status"] = "finished" return True except urllib2.URLError as e: xlog.warn("download %s to %s URL fail:%r", url, file, e) continue except Exception as e: xlog.exception("download %s to %s fail:%r", url, file, e) continue download_progress[url]["status"] = "failed" return False
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 check_update(): try: if update_from_github.update_info == "dont-check": return check_push_update() update_rule = config.get(["update", "check_update"], "notice-stable") if update_rule not in ("stable", "notice-stable", "test", "notice-test"): return versions = update_from_github.get_github_versions() current_version = update_from_github.current_version() test_version, stable_version = versions[0][1], versions[1][1] if test_version != config.get(["update", "skip_test_version"]): if update_rule == "notice-test": if LooseVersion(current_version) < LooseVersion(test_version): xlog.info("checked new test version %s", test_version) update_from_github.update_info = '{"type":"test", "version":"%s"}' % test_version elif update_rule == "test": if LooseVersion(current_version) < LooseVersion(test_version): xlog.info("update to test version %s", test_version) update_from_github.update_version(test_version) if stable_version != config.get(["update", "skip_stable_version"]): if update_rule == "notice-stable": if LooseVersion(current_version) < LooseVersion( stable_version): xlog.info("checked new stable version %s", stable_version) update_from_github.update_info = '{"type":"stable", "version":"%s"}' % stable_version elif update_rule == "stable": if LooseVersion(current_version) < LooseVersion( stable_version): xlog.info("update to stable version %s", stable_version) update_from_github.update_version(stable_version) except IOError as e: xlog.warn("check update fail:%r", e) except Exception as e: xlog.exception("check_update fail:%r", e) finally: if update_from_github.update_info == "init": update_from_github.update_info = ""
def download_file(url, file): if url not in download_progress: download_progress[url] = {} download_progress[url]["status"] = "downloading" else: if download_progress[url]["status"] == "downloading": xlog.warn("url in downloading, %s", url) return False for i in range(0, 2): try: xlog.info("download %s to %s, retry:%d", url, file, i) opener = get_opener(i) req = opener.open(url) download_progress[url]["size"] = int(req.headers.get('content-length') or 0) CHUNK = 16 * 1024 downloaded = 0 with open(file, 'wb') as fp: while True: chunk = req.read(CHUNK) if not chunk: break fp.write(chunk) downloaded += len(chunk) download_progress[url]["downloaded"] = downloaded if downloaded != download_progress[url]["size"]: xlog.warn("download size:%d, need size:%d, download fail.", downloaded, download_progress[url]["size"]) continue else: download_progress[url]["status"] = "finished" return True except urllib2.URLError as e: xlog.warn("download %s to %s URL fail:%r", url, file, e) continue except Exception as e: xlog.exception("download %s to %s fail:%r", url, file, e) continue download_progress[url]["status"] = "failed" return False
platform_lib = "" try: sys.path.insert(0, noarch_lib) sys.path.insert(0, platform_lib) import OpenSSL xlog.info("use build-in openssl lib") except Exception as e1: xlog.info("import build-in openssl fail:%r", e1) sys.path.pop(0) sys.path.pop(0) try: import OpenSSL except Exception as e2: xlog.exception("import system python-OpenSSL fail:%r", e2) print("Try install python-openssl\r\n") raw_input("Press Enter to continue...") os._exit(0) import config import web_control import module_init import update import setup_win_python import update_from_github def exit_handler(): print 'Stopping all modules before exit!'
sys.path.append(extra_lib) try: import mac_tray as sys_tray except: from non_tray import sys_tray else: print("detect platform fail:%s" % sys.platform) from non_tray import sys_tray has_desktop = False try: import OpenSSL except Exception as e: xlog.exception("import openssl fail:%r", e) print("Try install python-openssl or cffi\r\n") raw_input("Press Enter to continue...") os._exit(0) import config import web_control import module_init import update import setup_win_python import update_from_github def exit_handler(): print 'Stopping all modules before exit!'
def check_push_update(): global update_content, update_dict try: opener = get_opener() req_url = update_url + "?uuid=" + get_uuid() + "&version=" + update_from_github.current_version() try: update_content = opener.open(req_url).read() except Exception as e: xlog.warn("check_update fail:%r", e) return False update_dict = json.loads(update_content) return True for module in update_dict["modules"]: new_version = str(update_dict["modules"][module]["last_version"]) describe = update_dict["modules"][module]["versions"][new_version]["describe"] if update_dict["modules"][module]["versions"][new_version]["notify"] != "true": continue if not module in config.config["modules"]: ignore_version = 0 current_version = 0 config.config["modules"][module] = {} config.config["modules"][module]["current_version"] = '0.0.0' else: current_version = config.get(["modules", module, "current_version"]) if "ignore_version" in config.config["modules"][module]: ignore_version = config.config["modules"][module]["ignore_version"] else: ignore_version = current_version if version_to_bin(new_version) <= version_to_bin(ignore_version): continue if version_to_bin(new_version) > version_to_bin(current_version): xlog.info("new %s version:%s", module, new_version) if sys.platform == "linux" or sys.platform == "linux2": from gtk_tray import sys_tray msg = "Module %s new version: %s, Download?\nNew:%s" % (module, new_version, describe) data_download = "%s|%s|download" % (module, new_version) data_ignore = "%s|%s|ignore" % (module, new_version) buttons = {1: {"data":data_download, "label":"Download", 'callback':general_gtk_callback}, 2: {"data":data_ignore, "label":"Ignore", 'callback':general_gtk_callback}} sys_tray.notify_general(msg=msg, title="New Version", buttons=buttons) elif sys.platform == "win32": from win_tray import sys_tray msg = "Module %s new version: %s, Download?" % (module, new_version) if sys_tray.dialog_yes_no(msg, u"Download", None, None) == 1: download_module(module, new_version) else: ignore_module(module, new_version) elif sys.platform == "darwin": from mac_tray import sys_tray msg = "Module %s new version: %s, Download?" % (module, new_version) if sys_tray.dialog_yes_no(msg, u"Download", None, None) == 1: download_module(module, new_version) else: ignore_module(module, new_version) else: download_module(module, new_version) except Exception as e: xlog.exception("check_update except:%s", e) return
try: sys.path.insert(0, noarch_lib) import OpenSSL as oss_test xlog.info("use build-in openssl lib") except Exception as e1: xlog.info("import build-in openssl fail:%r", e1) sys.path.pop(0) del sys.path_importer_cache[noarch_lib] unload("OpenSSL") unload("cryptography") unload("cffi") try: import OpenSSL except Exception as e2: xlog.exception("import system python-OpenSSL fail:%r", e2) print("Try install python-openssl\r\n") raw_input("Press Enter to continue...") os._exit(0) import config import web_control import module_init import update import setup_win_python import update_from_github def exit_handler(): print 'Stopping all modules before exit!'
def check_setup(): #40ms if is_winxp(): try: copy_VCR_files() except Exception as e: xlog.exception("setup win python except:%s", e)
module_init.start_all_auto() web_control.start() if has_desktop and config.get(["modules", "launcher", "popup_webui"], 1) == 1: host_port = config.get(["modules", "launcher", "control_port"], 8085) import webbrowser webbrowser.open("http://127.0.0.1:%s/" % host_port) update.start() if config.get(["modules", "launcher", "show_systray"], 1): sys_tray.serve_forever() else: while True: time.sleep(100) module_init.stop_all() sys.exit() if __name__ == '__main__': try: main() except KeyboardInterrupt: # Ctrl + C on console module_init.stop_all() os._exit(0) except Exception as e: xlog.exception("launcher except:%r", e) input('Press any to exit...')
web_control.start() if has_desktop and config.get(["modules", "launcher", "popup_webui"], 1) == 1: host_port = config.get(["modules", "launcher", "control_port"], 8085) import webbrowser webbrowser.open("http://127.0.0.1:%s/" % host_port) update.start() if config.get(["modules", "launcher", "show_systray"], 1): sys_tray.serve_forever() else: while True: time.sleep(100) module_init.stop_all() sys.exit() if __name__ == '__main__': try: main() except KeyboardInterrupt: # Ctrl + C on console module_init.stop_all() os._exit(0) except Exception as e: xlog.exception("launcher except:%r", e) input('Press any to exit...')
extra_lib = "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjc" sys.path.append(extra_lib) try: import mac_tray as sys_tray except: from non_tray import sys_tray else: print("detect platform fail:%s" % sys.platform) from non_tray import sys_tray has_desktop = False try: import OpenSSL except Exception as e: xlog.exception("import openssl fail:%r", e) print("Try install python-openssl or cffi\r\n") raw_input("Press Enter to continue...") os._exit(0) import config import web_control import module_init import update import setup_win_python import update_from_github def exit_handler(): print 'Stopping all modules before exit!' module_init.stop_all()
def check_push_update(): global update_content, update_dict try: opener = get_opener() req_url = update_url + "?uuid=" + get_uuid( ) + "&version=" + update_from_github.current_version() try: update_content = opener.open(req_url).read() except Exception as e: xlog.warn("check_update fail:%r", e) return False update_dict = json.loads(update_content) return True for module in update_dict["modules"]: new_version = str(update_dict["modules"][module]["last_version"]) describe = update_dict["modules"][module]["versions"][new_version][ "describe"] if update_dict["modules"][module]["versions"][new_version][ "notify"] != "true": continue if not module in config.config["modules"]: ignore_version = 0 current_version = 0 config.config["modules"][module] = {} config.config["modules"][module]["current_version"] = '0.0.0' else: current_version = config.get( ["modules", module, "current_version"]) if "ignore_version" in config.config["modules"][module]: ignore_version = config.config["modules"][module][ "ignore_version"] else: ignore_version = current_version if version_to_bin(new_version) <= version_to_bin(ignore_version): continue if version_to_bin(new_version) > version_to_bin(current_version): xlog.info("new %s version:%s", module, new_version) if sys.platform == "linux" or sys.platform == "linux2": from gtk_tray import sys_tray msg = "Module %s new version: %s, Download?\nNew:%s" % ( module, new_version, describe) data_download = "%s|%s|download" % (module, new_version) data_ignore = "%s|%s|ignore" % (module, new_version) buttons = { 1: { "data": data_download, "label": "Download", 'callback': general_gtk_callback }, 2: { "data": data_ignore, "label": "Ignore", 'callback': general_gtk_callback } } sys_tray.notify_general(msg=msg, title="New Version", buttons=buttons) elif sys.platform == "win32": from win_tray import sys_tray msg = "Module %s new version: %s, Download?" % ( module, new_version) if sys_tray.dialog_yes_no(msg, u"Download", None, None) == 1: download_module(module, new_version) else: ignore_module(module, new_version) elif sys.platform == "darwin": from mac_tray import sys_tray msg = "Module %s new version: %s, Download?" % ( module, new_version) if sys_tray.dialog_yes_no(msg, u"Download", None, None) == 1: download_module(module, new_version) else: ignore_module(module, new_version) else: download_module(module, new_version) except Exception as e: xlog.exception("check_update except:%s", e) return