def __init__(self): icon_path = os.path.join(os.path.dirname(__file__), "web_ui", "favicon.ico") self.systray = SysTrayIcon(icon_path, "XX-Net", self.make_menu(), self.on_quit, left_click=self.on_show, right_click=self.on_right_click) reg_path = r'Software\Microsoft\Windows\CurrentVersion\Internet Settings' self.INTERNET_SETTINGS = winreg.OpenKey(winreg.HKEY_CURRENT_USER, reg_path, 0, winreg.KEY_ALL_ACCESS) proxy_setting = config.get(["modules", "launcher", "proxy"], "pac") if proxy_setting == "pac": self.on_enable_pac() elif proxy_setting == "gae": self.on_enable_gae_proxy() elif proxy_setting == "disable": # Don't disable proxy setting, just do nothing. pass else: xlog.warn("proxy_setting:%r", proxy_setting)
def download_overwrite_new_version(xxnet_version): global update_progress xxnet_url = 'https://codeload.github.com/XX-net/XX-Net/zip/%s' % xxnet_version xxnet_zip_file = os.path.join(download_path, "XX-Net-%s.zip" % xxnet_version) xxnet_unzip_path = os.path.join(download_path, "XX-Net-%s" % xxnet_version) progress["update_status"] = "Downloading %s" % xxnet_url if not download_file(xxnet_url, xxnet_zip_file): progress["update_status"] = "Download Fail." raise Exception("download xxnet zip fail:%s" % download_path) xlog.info("update download %s finished.", download_path) xlog.info("update start unzip") progress["update_status"] = "Unziping" try: with zipfile.ZipFile(xxnet_zip_file, "r") as dz: dz.extractall(download_path) dz.close() except Exception as e: xlog.warn("unzip %s fail:%r", xxnet_zip_file, e) progress["update_status"] = "Unzip Fail:%s" % e raise e xlog.info("update finished unzip") overwrite(xxnet_version, xxnet_unzip_path) os.remove(xxnet_zip_file) shutil.rmtree(xxnet_unzip_path, ignore_errors=True)
def overwrite(xxnet_version, xxnet_unzip_path): progress["update_status"] = "Over writing" try: for root, subdirs, files in os.walk(xxnet_unzip_path): relate_path = root[len(xxnet_unzip_path)+1:] target_relate_path = relate_path if target_relate_path.startswith("code/default"): target_relate_path = "code/" + xxnet_version + relate_path[12:] for subdir in subdirs: if relate_path == "code" and subdir == "default": subdir = xxnet_version target_path = os.path.join(top_path, target_relate_path, subdir) if not os.path.isdir(target_path): xlog.info("mkdir %s", target_path) os.mkdir(target_path) for filename in files: src_file = os.path.join(root, filename) dst_file = os.path.join(top_path, target_relate_path, filename) if not os.path.isfile(dst_file) or sha1_file(src_file) != sha1_file(dst_file): xlog.info("copy %s => %s", src_file, dst_file) shutil.copy(src_file, dst_file) st = os.stat(src_file) if st.st_mode & stat.S_IEXEC: os.chmod(dst_file, st.st_mode) except Exception as e: xlog.warn("update over write fail:%r", e) progress["update_status"] = "Over write Fail:%r" % e raise e xlog.info("update file finished.")
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 do_POST(self): refer = self.headers.getheader('Referer') if refer: refer_loc = urlparse.urlparse(refer).netloc host = self.headers.getheader('host') if refer_loc != host: xlog.warn("web control ref:%s host:%s", refer_loc, host) return #url_path = urlparse.urlparse(self.path).path url_path_list = self.path.split('/') if len(url_path_list) >= 3 and url_path_list[1] == "module": module = url_path_list[2] if len(url_path_list) >= 4 and url_path_list[3] == "control": if module not in module_init.proc_handler: xlog.warn("request %s no module in path", self.path) self.send_not_found() return path = '/' + '/'.join(url_path_list[4:]) controler = module_init.proc_handler[module][ "imp"].local.web_control.ControlHandler( self.client_address, self.headers, self.command, path, self.rfile, self.wfile) controler.do_POST() return
def download_overwrite_new_version(xxnet_version): global update_progress xxnet_url = 'https://codeload.github.com/XX-net/XX-Net/zip/%s' % xxnet_version xxnet_zip_file = os.path.join(download_path, "XX-Net-%s.zip" % xxnet_version) xxnet_unzip_path = os.path.join(download_path, "XX-Net-%s" % xxnet_version) progress["update_status"] = "Downloading %s" % xxnet_url if not download_file(xxnet_url, xxnet_zip_file): progress["update_status"] = "Download Fail." raise Exception("download xxnet zip fail:%s" % xxnet_zip_file) hash_sum = get_hash_sum(xxnet_version) if len(hash_sum) and hash_file_sum(xxnet_zip_file) != hash_sum: progress["update_status"] = "Download Checksum Fail." raise Exception("download xxnet zip checksum fail:%s" % xxnet_zip_file) xlog.info("update download %s finished.", download_path) xlog.info("update start unzip") progress["update_status"] = "Unziping" try: with zipfile.ZipFile(xxnet_zip_file, "r") as dz: dz.extractall(download_path) dz.close() except Exception as e: xlog.warn("unzip %s fail:%r", xxnet_zip_file, e) progress["update_status"] = "Unzip Fail:%s" % e raise e xlog.info("update finished unzip") overwrite(xxnet_version, xxnet_unzip_path) os.remove(xxnet_zip_file) shutil.rmtree(xxnet_unzip_path, ignore_errors=True)
def do_POST(self): refer = self.headers.getheader("Referer") if refer: refer_loc = urlparse.urlparse(refer).netloc host = self.headers.getheader("host") if refer_loc != host: xlog.warn("web control ref:%s host:%s", refer_loc, host) return # url_path = urlparse.urlparse(self.path).path url_path_list = self.path.split("/") if len(url_path_list) >= 3 and url_path_list[1] == "module": module = url_path_list[2] if len(url_path_list) >= 4 and url_path_list[3] == "control": if module not in module_init.proc_handler: xlog.warn("request %s no module in path", self.path) self.send_not_found() return path = "/" + "/".join(url_path_list[4:]) controler = module_init.proc_handler[module]["imp"].local.web_control.ControlHandler( self.client_address, self.headers, self.command, path, self.rfile, self.wfile ) controler.do_POST() return
def download_overwrite_new_version(xxnet_version): global update_progress xxnet_url = 'https://codeload.github.com/XX-net/XX-Net/zip/%s' % xxnet_version xxnet_zip_file = os.path.join(download_path, "XX-Net-%s.zip" % xxnet_version) xxnet_unzip_path = os.path.join(download_path, "XX-Net-%s" % xxnet_version) progress["update_status"] = "Downloading %s" % xxnet_url if not download_file(xxnet_url, xxnet_zip_file): progress["update_status"] = "Download Fail." raise Exception("download xxnet zip fail:%s" % download_path) xlog.info("update download %s finished.", download_path) xlog.info("update start unzip") progress["update_status"] = "Unziping" try: with zipfile.ZipFile(xxnet_zip_file, "r") as dz: dz.extractall(download_path) dz.close() except Exception as e: xlog.warn("unzip %s fail:%r", xxnet_zip_file, e) progress["update_status"] = "Unzip Fail:%s" % e raise xlog.info("update finished unzip") progress["update_status"] = "Over writing" try: for root, subdirs, files in os.walk(xxnet_unzip_path): relate_path = root[len(xxnet_unzip_path) + 1:] for subdir in subdirs: target_path = os.path.join(root_path, relate_path, subdir) if not os.path.isdir(target_path): xlog.info("mkdir %s", target_path) os.mkdir(target_path) if config.get(["update", "uuid"], '') == 'test' and "launcher" in relate_path: # for debug # don't over write launcher dir continue for filename in files: src_file = os.path.join(root, filename) dst_file = os.path.join(root_path, relate_path, filename) if not os.path.isfile(dst_file) or sha1_file( src_file) != sha1_file(dst_file): xlog.info("copy %s => %s", src_file, dst_file) shutil.copy(src_file, dst_file) except Exception as e: xlog.warn("update over write fail:%r", e) progress["update_status"] = "Over write Fail:%r" % e raise xlog.info("update file finished.") os.remove(xxnet_zip_file) shutil.rmtree(xxnet_unzip_path, ignore_errors=True)
def download_overwrite_new_version(xxnet_version): global update_progress xxnet_url = 'https://codeload.github.com/XX-net/XX-Net/zip/%s' % xxnet_version xxnet_zip_file = os.path.join(download_path, "XX-Net-%s.zip" % xxnet_version) xxnet_unzip_path = os.path.join(download_path, "XX-Net-%s" % xxnet_version) progress["update_status"] = "Downloading %s" % xxnet_url if not download_file(xxnet_url, xxnet_zip_file): progress["update_status"] = "Download Fail." raise Exception("download xxnet zip fail:%s" % download_path) xlog.info("update download %s finished.", download_path) xlog.info("update start unzip") progress["update_status"] = "Unziping" try: with zipfile.ZipFile(xxnet_zip_file, "r") as dz: dz.extractall(download_path) dz.close() except Exception as e: xlog.warn("unzip %s fail:%r", xxnet_zip_file, e) progress["update_status"] = "Unzip Fail:%s" % e raise xlog.info("update finished unzip") progress["update_status"] = "Over writing" try: for root, subdirs, files in os.walk(xxnet_unzip_path): relate_path = root[len(xxnet_unzip_path)+1:] for subdir in subdirs: target_path = os.path.join(root_path, relate_path, subdir) if not os.path.isdir(target_path): xlog.info("mkdir %s", target_path) os.mkdir(target_path) if config.get(["update", "uuid"], '') == 'test' and "launcher" in relate_path: # for debug # don't over write launcher dir continue for filename in files: src_file = os.path.join(root, filename) dst_file = os.path.join(root_path, relate_path, filename) if not os.path.isfile(dst_file) or sha1_file(src_file) != sha1_file(dst_file): xlog.info("copy %s => %s", src_file, dst_file) shutil.copy(src_file, dst_file) except Exception as e: xlog.warn("update over write fail:%r", e) progress["update_status"] = "Over write Fail:%r" % e raise xlog.info("update file finished.") os.remove(xxnet_zip_file) shutil.rmtree(xxnet_unzip_path, ignore_errors=True)
def update_version(version): global update_progress try: download_overwrite_new_version(version) progress["update_status"] = "Restarting" xlog.info("update try restart xxnet") restart_xxnet() except Exception as e: xlog.warn("update version %s fail:%r", version, e)
def older_or_equal(version, reference_version): try: p = re.compile(r'([0-9]+)\.([0-9]+)\.([0-9]+)') m1 = p.match(version) m2 = p.match(reference_version) v1 = map(int, map(m1.group, [1,2,3])) v2 = map(int, map(m2.group, [1,2,3])) return v1 <= v2 except: xlog.warn("older_or_equal fail: %s, %s" % (version, reference_version)) # e.g. "get_version_fail" when post_update.run(last_run_version), "last_run_version" in \data\launcher\config.yaml return False # is not older
def loadConfig(self): proxySetting = config.get(["modules", "launcher", "proxy"], "pac") if proxySetting == "pac": self.on_enable_pac() elif proxySetting == "gae": self.on_enable_gae_proxy() elif proxySetting == "disable": # Don't disable proxy setting, just do nothing. pass else: xlog.warn("proxy_setting:%r", proxySetting)
def older_or_equal(version, reference_version): try: p = re.compile(r'([0-9]+)\.([0-9]+)\.([0-9]+)') m1 = p.match(version) m2 = p.match(reference_version) v1 = map(int, map(m1.group, [1, 2, 3])) v2 = map(int, map(m2.group, [1, 2, 3])) return v1 <= v2 except: xlog.warn( "older_or_equal fail: %s, %s" % (version, reference_version) ) # e.g. "get_version_fail" when post_update.run(last_run_version), "last_run_version" in \data\launcher\config.yaml return False # is not older
def add(name, application): if not os.path.isdir(os.path.expanduser(_xdg_config_home)): xlog.warn("autorun linux config path not found:%s", os.path.expanduser(_xdg_config_home)) return if not os.path.isdir(_xdg_user_autostart): os.mkdir(_xdg_user_autostart) """add a new autostart entry""" desktop_entry = ( "[Desktop Entry]\n" "Name=%s\n" "Exec=%s\n" "Type=Application\n" "Terminal=false\n" % (name, application) ) with open(getfilename(name), "w") as f: f.write(desktop_entry)
def current_version(): readme_file = os.path.join(root_path, "version.txt") try: with open(readme_file) as fd: content = fd.read() p = re.compile(r'([0-9]+)\.([0-9]+)\.([0-9]+)') m = p.match(content) if m: version = m.group(1) + "." + m.group(2) + "." + m.group(3) return version except: xlog.warn("get_version_fail in update_from_github") return "get_version_fail"
def overwrite(xxnet_version, xxnet_unzip_path): progress["update_status"] = "Over writing" try: for root, subdirs, files in os.walk(xxnet_unzip_path): relate_path = root[len(xxnet_unzip_path) + 1:] target_relate_path = relate_path if sys.platform == 'win32': if target_relate_path.startswith("code\\default"): target_relate_path = "code\\" + xxnet_version + relate_path[ 12:] else: if target_relate_path.startswith("code/default"): target_relate_path = "code/" + xxnet_version + relate_path[ 12:] for subdir in subdirs: if relate_path == "code" and subdir == "default": subdir = xxnet_version target_path = os.path.join(top_path, target_relate_path, subdir) if not os.path.isdir(target_path): xlog.info("mkdir %s", target_path) os.mkdir(target_path) for filename in files: src_file = os.path.join(root, filename) dst_file = os.path.join(top_path, target_relate_path, filename) if not os.path.isfile(dst_file) or hash_file_sum( src_file) != hash_file_sum(dst_file): xlog.info("copy %s => %s", src_file, dst_file) #modify by outofmemo, files in '/sdcard' are not allowed to chmod for Android #and shutil.copy() will call shutil.copymode() if sys.platform != 'win32' and os.path.isfile( "/system/bin/dalvikvm" ) == False and os.path.isfile( "/system/bin/dalvikvm64" ) == False and os.path.isfile(dst_file): st = os.stat(dst_file) shutil.copy(src_file, dst_file) if st.st_mode & stat.S_IEXEC: os.chmod(dst_file, st.st_mode) else: shutil.copyfile(src_file, dst_file) except Exception as e: xlog.warn("update over write fail:%r", e) progress["update_status"] = "Over write Fail:%r" % e raise e xlog.info("update file finished.")
def add(name, application): if not os.path.isdir(os.path.expanduser(_xdg_config_home)): xlog.warn("autorun linux config path not found:%s", os.path.expanduser(_xdg_config_home)) return if not os.path.isdir(_xdg_user_autostart): os.mkdir(_xdg_user_autostart) """add a new autostart entry""" desktop_entry = "[Desktop Entry]\n"\ "Name=%s\n"\ "Exec=%s\n"\ "Type=Application\n"\ "Terminal=false\n" % (name, application) with open(getfilename(name), "w") as f: f.write(desktop_entry)
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 overwrite(xxnet_version, xxnet_unzip_path): progress["update_status"] = "Over writing" try: for root, subdirs, files in os.walk(xxnet_unzip_path): relate_path = root[len(xxnet_unzip_path)+1:] target_relate_path = relate_path if sys.platform == 'win32': if target_relate_path.startswith("code\\default"): target_relate_path = "code\\" + xxnet_version + relate_path[12:] else: if target_relate_path.startswith("code/default"): target_relate_path = "code/" + xxnet_version + relate_path[12:] for subdir in subdirs: if relate_path == "code" and subdir == "default": subdir = xxnet_version target_path = os.path.join(top_path, target_relate_path, subdir) if not os.path.isdir(target_path): xlog.info("mkdir %s", target_path) os.mkdir(target_path) for filename in files: src_file = os.path.join(root, filename) dst_file = os.path.join(top_path, target_relate_path, filename) if not os.path.isfile(dst_file) or hash_file_sum(src_file) != hash_file_sum(dst_file): xlog.info("copy %s => %s", src_file, dst_file) #modify by outofmemo, files in '/sdcard' are not allowed to chmod for Android #and shutil.copy() will call shutil.copymode() if sys.platform != 'win32' and os.path.isfile("/system/bin/dalvikvm")==False and os.path.isfile("/system/bin/dalvikvm64")==False and os.path.isfile(dst_file): st = os.stat(dst_file) shutil.copy(src_file, dst_file) if st.st_mode & stat.S_IEXEC: os.chmod(dst_file, st.st_mode) else: shutil.copyfile(src_file, dst_file) except Exception as e: xlog.warn("update over write fail:%r", e) progress["update_status"] = "Over write Fail:%r" % e raise e xlog.info("update file finished.")
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 loadConfig(): if not currentService: return proxy_setting = config.get(["modules", "launcher", "proxy"], "pac") if getProxyState(currentService) == proxy_setting: return try: if proxy_setting == "pac": helperDisableGlobalProxy(currentService) helperEnableAutoProxy(currentService) elif proxy_setting == "gae": helperDisableAutoProxy(currentService) helperEnableGlobalProxy(currentService) elif proxy_setting == "disable": helperDisableAutoProxy(currentService) helperDisableGlobalProxy(currentService) else: xlog.warn("proxy_setting:%r", proxy_setting) except: xlog.warn("helper failed, please manually reset proxy settings after switching connection")
def overwrite(xxnet_version, xxnet_unzip_path): progress["update_status"] = "Over writing" try: for root, subdirs, files in os.walk(xxnet_unzip_path): relate_path = root[len(xxnet_unzip_path) + 1:] target_relate_path = relate_path path_pattern = "code" + os.sep + "default" if target_relate_path.startswith(path_pattern): target_relate_path = "code" + os.sep + xxnet_version + relate_path[ len(path_pattern):] for subdir in subdirs: if relate_path == "code" and subdir == "default": subdir = xxnet_version target_path = os.path.join(top_path, target_relate_path, subdir) if not os.path.isdir(target_path): xlog.info("mkdir %s", target_path) os.mkdir(target_path) for filename in files: src_file = os.path.join(root, filename) dst_file = os.path.join(top_path, target_relate_path, filename) if not os.path.isfile(dst_file) or hash_file_sum( src_file) != hash_file_sum(dst_file): xlog.info("copy %s => %s", src_file, dst_file) if sys.platform != 'win32' and os.path.isfile(dst_file): st = os.stat(dst_file) shutil.copy(src_file, dst_file) if st.st_mode & stat.S_IEXEC: os.chmod(dst_file, st.st_mode) else: shutil.copy(src_file, dst_file) except Exception as e: xlog.warn("update over write fail:%r", e) progress["update_status"] = "Over write Fail:%r" % e raise e xlog.info("update file finished.")
def add(name, application): if not os.path.isdir(home_config_path): xlog.warn("autorun linux config path not found:%s", home_config_path) return if not os.path.isdir(_xdg_user_autostart): try: os.mkdir(_xdg_user_autostart) except Exception as e: xlog.warn("Enable auto start, create path:%s fail:%r", _xdg_user_autostart, e) return """add a new autostart entry""" desktop_entry = "[Desktop Entry]\n"\ "Name=%s\n"\ "Exec=%s\n"\ "Type=Application\n"\ "Terminal=false\n"\ "X-GNOME-Autostart-enabled=true" % (name, application) with open(getfilename(name), "w") as f: f.write(desktop_entry)
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 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 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
if __name__ == "__main__": current_path = os.path.dirname(os.path.abspath(__file__)) python_path = os.path.abspath( os.path.join(current_path, os.pardir, 'python27', '1.0')) noarch_lib = os.path.abspath(os.path.join(python_path, 'lib', 'noarch')) sys.path.append(noarch_lib) pygtk.require('2.0') import gtk gtk.gdk.threads_init() try: import pynotify pynotify.init('XX-Net Notify') except: xlog.warn( "import pynotify fail, please install python-notify if possiable.") pynotify = None import module_init try: import platform import appindicator except: platform = None appindicator = None class Gtk_tray(): notify_list = []
import config if __name__ == "__main__": current_path = os.path.dirname(os.path.abspath(__file__)) python_path = os.path.abspath( os.path.join(current_path, os.pardir, 'python27', '1.0')) noarch_lib = os.path.abspath( os.path.join(python_path, 'lib', 'noarch')) sys.path.append(noarch_lib) pygtk.require('2.0') import gtk gtk.gdk.threads_init() try: import pynotify pynotify.init('XX-Net Notify') except: xlog.warn("import pynotify fail, please install python-notify if possiable.") pynotify = None import module_init try: import platform import appindicator except: platform = None appindicator = None class Gtk_tray(): notify_list = [] def __init__(self):
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 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
def do_GET(self): refer = self.headers.getheader("Referer") if refer: refer_loc = urlparse.urlparse(refer).netloc host = self.headers.getheader("host") if refer_loc != host: xlog.warn("web control ref:%s host:%s", refer_loc, host) return # check for '..', which will leak file if re.search(r"(\.{2})", self.path) is not None: self.wfile.write(b"HTTP/1.1 404\r\n\r\n") xlog.warn("%s %s %s haking", self.address_string(), self.command, self.path) return url_path = urlparse.urlparse(self.path).path if url_path == "/": return self.req_index_handler() url_path_list = self.path.split("/") if len(url_path_list) >= 3 and url_path_list[1] == "module": module = url_path_list[2] if len(url_path_list) >= 4 and url_path_list[3] == "control": if module not in module_init.proc_handler: xlog.warn("request %s no module in path", url_path) self.send_not_found() return if "imp" not in module_init.proc_handler[module]: xlog.warn("request module:%s start fail", module) self.send_not_found() return path = "/" + "/".join(url_path_list[4:]) controler = module_init.proc_handler[module]["imp"].local.web_control.ControlHandler( self.client_address, self.headers, self.command, path, self.rfile, self.wfile ) controler.do_GET() return else: file_path = os.path.join(root_path, module, url_path_list[3:].join("/")) else: file_path = os.path.join(current_path, "web_ui" + url_path) xlog.debug("launcher web_control %s %s %s ", self.address_string(), self.command, self.path) if os.path.isfile(file_path): if file_path.endswith(".js"): mimetype = "application/javascript" elif file_path.endswith(".css"): mimetype = "text/css" elif file_path.endswith(".html"): mimetype = "text/html" elif file_path.endswith(".jpg"): mimetype = "image/jpeg" elif file_path.endswith(".png"): mimetype = "image/png" else: mimetype = "text/plain" self.send_file(file_path, mimetype) elif url_path == "/config": self.req_config_handler() elif url_path == "/download": self.req_download_handler() elif url_path == "/init_module": self.req_init_module_handler() elif url_path == "/quit": self.send_response("text/html", '{"status":"success"}') module_init.stop_all() os._exit(0) elif url_path == "/restart": self.send_response("text/html", '{"status":"success"}') update_from_github.restart_xxnet() else: self.send_not_found() xlog.info('%s "%s %s HTTP/1.1" 404 -', self.address_string(), self.command, self.path)
def do_GET(self): refer = self.headers.getheader('Referer') if refer: refer_loc = urlparse.urlparse(refer).netloc host = self.headers.getheader('host') if refer_loc != host: xlog.warn("web control ref:%s host:%s", refer_loc, host) return # check for '..', which will leak file if re.search(r'(\.{2})', self.path) is not None: self.wfile.write(b'HTTP/1.1 404\r\n\r\n') xlog.warn('%s %s %s haking', self.address_string(), self.command, self.path ) return url_path = urlparse.urlparse(self.path).path if url_path == '/': return self.req_index_handler() url_path_list = self.path.split('/') if len(url_path_list) >= 3 and url_path_list[1] == "module": module = url_path_list[2] if len(url_path_list) >= 4 and url_path_list[3] == "control": if module not in module_init.proc_handler: xlog.warn("request %s no module in path", url_path) self.send_not_found() return if "imp" not in module_init.proc_handler[module]: xlog.warn("request module:%s start fail", module) self.send_not_found() return path = '/' + '/'.join(url_path_list[4:]) controler = module_init.proc_handler[module]["imp"].local.web_control.ControlHandler(self.client_address, self.headers, self.command, path, self.rfile, self.wfile) controler.do_GET() return else: relate_path = '/'.join(url_path_list[3:]) file_path = os.path.join(root_path, module, "web_ui", relate_path) if not os.path.isfile(file_path): return self.send_not_found() # i18n code lines (Both the locale dir & the template dir are module-dependent) locale_dir = os.path.abspath(os.path.join(root_path, module, 'lang')) content = i18n_translator.render(locale_dir, file_path) return self.send_response('text/html', content) else: file_path = os.path.join(current_path, 'web_ui' + url_path) xlog.debug ('launcher web_control %s %s %s ', self.address_string(), self.command, self.path) if os.path.isfile(file_path): if file_path.endswith('.js'): mimetype = 'application/javascript' elif file_path.endswith('.css'): mimetype = 'text/css' elif file_path.endswith('.html'): mimetype = 'text/html' elif file_path.endswith('.jpg'): mimetype = 'image/jpeg' elif file_path.endswith('.png'): mimetype = 'image/png' else: mimetype = 'text/plain' self.send_file(file_path, mimetype) elif url_path == '/config': self.req_config_handler() elif url_path == '/update': self.req_update_handler() elif url_path == '/init_module': self.req_init_module_handler() elif url_path == '/quit': self.send_response('text/html', '{"status":"success"}') module_init.stop_all() os._exit(0) elif url_path == '/restart': self.send_response('text/html', '{"status":"success"}') update_from_github.restart_xxnet() else: self.send_not_found() xlog.info('%s "%s %s HTTP/1.1" 404 -', self.address_string(), self.command, self.path)
def save(): global config, config_path try: yaml.dump(config, open(config_path, "w")) except Exception as e: xlog.warn("save config %s fail %s", config_path, e)
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
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 save(): global config, config_path try: yaml.dump(config, file(config_path, "w")) except Exception as e: xlog.warn("save config %s fail %s", config_path, e)
def do_GET(self): refer = self.headers.getheader('Referer') if refer: refer_loc = urlparse.urlparse(refer).netloc host = self.headers.getheader('host') if refer_loc != host: xlog.warn("web control ref:%s host:%s", refer_loc, host) return # check for '..', which will leak file if re.search(r'(\.{2})', self.path) is not None: self.wfile.write(b'HTTP/1.1 404\r\n\r\n') xlog.warn('%s %s %s haking', self.address_string(), self.command, self.path) return url_path = urlparse.urlparse(self.path).path if url_path == '/': return self.req_index_handler() url_path_list = self.path.split('/') if len(url_path_list) >= 3 and url_path_list[1] == "module": module = url_path_list[2] if len(url_path_list) >= 4 and url_path_list[3] == "control": if module not in module_init.proc_handler: xlog.warn("request %s no module in path", url_path) self.send_not_found() return if "imp" not in module_init.proc_handler[module]: xlog.warn("request module:%s start fail", module) self.send_not_found() return path = '/' + '/'.join(url_path_list[4:]) controler = module_init.proc_handler[module][ "imp"].local.web_control.ControlHandler( self.client_address, self.headers, self.command, path, self.rfile, self.wfile) controler.do_GET() return else: relate_path = '/'.join(url_path_list[3:]) file_path = os.path.join(root_path, module, "web_ui", relate_path) if not os.path.isfile(file_path): return self.send_not_found() # i18n code lines (Both the locale dir & the template dir are module-dependent) locale_dir = os.path.abspath( os.path.join(root_path, module, 'lang')) content = i18n_translator.render(locale_dir, file_path) return self.send_response('text/html', content) else: file_path = os.path.join(current_path, 'web_ui' + url_path) xlog.debug('launcher web_control %s %s %s ', self.address_string(), self.command, self.path) if os.path.isfile(file_path): if file_path.endswith('.js'): mimetype = 'application/javascript' elif file_path.endswith('.css'): mimetype = 'text/css' elif file_path.endswith('.html'): mimetype = 'text/html' elif file_path.endswith('.jpg'): mimetype = 'image/jpeg' elif file_path.endswith('.png'): mimetype = 'image/png' else: mimetype = 'text/plain' self.send_file(file_path, mimetype) elif url_path == '/config': self.req_config_handler() elif url_path == '/update': self.req_update_handler() elif url_path == '/init_module': self.req_init_module_handler() elif url_path == '/quit': self.send_response('text/html', '{"status":"success"}') module_init.stop_all() os._exit(0) elif url_path == '/restart': self.send_response('text/html', '{"status":"success"}') update_from_github.restart_xxnet() else: self.send_not_found() xlog.info('%s "%s %s HTTP/1.1" 404 -', self.address_string(), self.command, self.path)