def test_fs_func(self): if not xutils.is_windows(): item0, item1 = xutils.splitpath("/fs/test/") self.assertEqual("/fs/", item0.path) self.assertEqual("/fs/test/", item1.path) if xutils.is_windows(): item0, item1, item2 = xutils.splitpath("C:/data/name/") self.assertEqual("C:/", item0.path) self.assertEqual("C:/data/", item1.path) self.assertEqual("C:/data/name/", item2.path)
def GET(self): mem_used = 0 sys_mem_used = 0 sys_mem_total = 0 thread_cnt = 0 formated_mem_size = 0 if psutil: p = psutil.Process(pid=os.getpid()) mem_info = p.memory_info() mem_used = mem_info.rss sys_mem = psutil.virtual_memory() sys_mem_used = sys_mem.used sys_mem_total = sys_mem.total formated_mem_size = xutils.format_size(mem_used) elif xutils.is_windows(): mem_usage = os.popen("tasklist /FI \"PID eq %s\" /FO csv" % os.getpid()).read() str_list = mem_usage.split(",") pattern = re.compile(r"[0-9,]+ [kK]") mem_list = pattern.findall(mem_usage) formated_mem_size = mem_list[-1] else: formated_mem_size = "" thread_cnt = len(threading.enumerate()) return xtemplate.render( "system/monitor.html", sys_mem_used=formated_mem_size, sys_mem_total=xutils.format_size(sys_mem_total), python_version=sys.version, sys_version=platform.version(), processor=platform.processor(), thread_cnt=thread_cnt, start_time=xconfig.get("start_time"))
def list_directory(self, path): try: if xutils.is_windows() and path == "/": # return self.list_win_drives() filelist = get_win_drives() else: filelist = list_abs_dir(path) except OSError: return "No permission to list directory" # filelist中路径均不带/ # 排序:文件夹优先,按字母顺序排列 # filelist.sort(key=lambda a: a.lower()) # filelist.sort(key=lambda a: not os.path.isdir(os.path.join(path,a))) filelist = [FileItem(item) for item in filelist] filelist.sort() # SAE上遇到中文出错 # Fix bad filenames,修改不生效 # filelist = list(map(lambda x: xutils.decode_bytes(x.encode("utf-8", errors='surrogateescape')), filelist)) # Fix, some `file` in *nix is not file either directory. os.stat方法报错 path = path.replace("\\", "/") kw = get_filesystem_kw() kw["filelist"] = filelist kw["path"] = path kw["fspathlist"] = xutils.splitpath(path) kw["token"] = xauth.get_current_user().token return xtemplate.render("fs/fs.html", **kw)
def get_local_ipv6_address(): """ This function will return your local machine's ipv6 address if it exits. If the local machine doesn't have a ipv6 address,then this function return None. This function use subprocess to execute command "ipconfig", then get the output and use regex to parse it ,trying to find ipv6 address. """ if xutils.is_windows(): getIPV6_process = subprocess.Popen("ipconfig", stdout = subprocess.PIPE) elif xutils.is_mac(): getIPV6_process = subprocess.Popen("ifconfig", stdout = subprocess.PIPE) else: getIPV6_process = None if not getIPV6_process: return None output = (getIPV6_process.stdout.read()) ipv6_pattern='(([a-f0-9]{1,4}:){7}[a-f0-9]{1,4})' m = re.search(ipv6_pattern, str(output)) if m is not None: return m.group() else: return None
def GET(self): path = xutils.get_argument("path") path = '"%s"' % path if xutils.is_windows(): path = path.replace("/", "\\") cmd = "explorer %s" % path elif xutils.is_mac(): cmd = "open %s" % path print(cmd) os.popen(cmd) return "<html><script>window.close()</script></html>"
def init(): global _leveldb print("init leveldb start ...") if leveldb: import xconfig _leveldb = leveldb.LevelDB(xconfig.DB_DIR) if xutils.is_windows(): os.environ["PATH"] += os.pathsep + "lib" import leveldbpy, xconfig _leveldb = LevelDBPy(xconfig.DB_DIR) print("init leveldb done, leveldb =", _leveldb)
def GET(self, name): if name == "home": link_path = "./" if xutils.is_mac(): link_path = os.environ['HOME'] if xutils.is_windows(): link_path = os.path.join(os.environ['HOMEDRIVE'], os.environ['HOMEPATH']) else: link_path = os.path.join(xconfig.DATA_DIR, name) link_path = os.path.abspath(link_path) raise web.seeother("/fs/%s" % link_path)
def POST(self): path = xutils.get_argument("path", "") # command = xutils.readfile(path) # subprocess和os.popen不能执行多条命令(win32) # subprocess在IDLE下会创建新的会话窗口,cmd下也不会创建新窗口 # subprocess执行命令不能换行 # os.popen可以执行系统命令 # os.popen就是subprocess.Popen的封装 if xutils.is_mac(): os.system("open -a Terminal \"%s\"" % path) return "success" if xutils.is_windows(): os.popen("start; cd \"%s\"" % path) return "success" return "failed"
def list_directory(self, path): try: if xutils.is_windows() and path == "/": filelist = list_win_drives() else: filelist = list_abs_dir(path) except OSError: return xtemplate.render("fs/fs.html", show_aside=False, path=path, filelist=[], error="No permission to list directory") # filelist中路径均不带/ # 排序:文件夹优先,按字母顺序排列 # filelist.sort(key=lambda a: a.lower()) # filelist.sort(key=lambda a: not os.path.isdir(os.path.join(path,a))) filelist = process_file_list(filelist) # SAE上遇到中文出错 # Fix bad filenames,修改不生效 # filelist = list(map(lambda x: xutils.decode_bytes(x.encode("utf-8", errors='surrogateescape')), filelist)) # Fix, some `file` in *nix is not file either directory. os.stat方法报错 path = path.replace("\\", "/") kw = get_filesystem_kw() kw["filelist"] = filelist kw["path"] = path kw["token"] = xauth.get_current_user().token kw["parent_path"] = get_parent_path(path) kw["search_action"] = "/fs_find" kw["show_aside"] = False kw["show_hidden_files"] = xutils.get_argument("show_hidden_files", False, type=bool) mode = xutils.get_argument("mode", xconfig.FS_VIEW_MODE) kw["fs_mode"] = mode if mode == "grid": return xtemplate.render("fs/fs_grid.html", **kw) elif mode == "shell": return xtemplate.render("fs/fs_shell.html", **kw) elif mode == "sidebar": kw["show_aside"] = False return xtemplate.render("fs/fs_sidebar.html", **kw) else: return xtemplate.render("fs/fs.html", **kw)
def GET(self): mem_used = 0 sys_mem_used = 0 sys_mem_total = 0 thread_cnt = 0 formated_mem_size = 0 if psutil: p = psutil.Process(pid=os.getpid()) mem_info = p.memory_info() mem_used = mem_info.rss sys_mem = psutil.virtual_memory() sys_mem_used = sys_mem.used sys_mem_total = sys_mem.total formated_mem_size = xutils.format_size(mem_used) elif xutils.is_windows(): mem_usage = os.popen("tasklist /FI \"PID eq %s\" /FO csv" % os.getpid()).read() str_list = mem_usage.split(",") pattern = re.compile(r"[0-9,]+ [kK]") mem_list = pattern.findall(mem_usage) formated_mem_size = mem_list[-1] else: formated_mem_size = "" thread_cnt = len(threading.enumerate()) item_list = [ Item('软件版本', get_xnote_version()), Item('sqlite版本', sqlite3.sqlite_version if sqlite3 != None else '') ] return xtemplate.render( "system/template/settings.html", show_aside=False, html_title="系统设置", item_list=item_list, sys_mem_used=formated_mem_size, sys_mem_total=xutils.format_size(sys_mem_total), python_version=sys.version, sys_version=platform.version(), processor=platform.processor(), thread_cnt=thread_cnt, xconfig=xconfig, xnote_version=get_xnote_version(), start_time=xconfig.get("start_time"), init_script_url=INIT_SCRIPT_URL)
def get_mem_info(): mem_used = 0 mem_total = 0 if psutil: p = psutil.Process(pid=os.getpid()) mem_info = p.memory_info() mem_used = mem_info.rss sys_mem = psutil.virtual_memory() sys_mem_used = sys_mem.used sys_mem_total = sys_mem.total formated_mem_size = xutils.format_size(mem_used) elif xutils.is_windows(): mem_usage = os.popen("tasklist /FI \"PID eq %s\" /FO csv" % os.getpid()).read() str_list = mem_usage.split(",") pattern = re.compile(r"[0-9,]+ [kK]") mem_list = pattern.findall(mem_usage) formated_mem_size = mem_list[-1] else: # ps -C -p 10538 formated_mem_size = "" return xutils.Storage(used = sys_mem_used, total = sys_mem_total)
def GET(self): command = xutils.get_argument("command", "") path = xutils.get_argument("path", "") # command = xutils.readfile(path) # subprocess和os.popen不能执行多条命令(win32) # subprocess在IDLE下会创建新的会话窗口,cmd下也不会创建新窗口 # subprocess执行命令不能换行 # os.popen可以执行系统命令 # os.popen就是subprocess.Popen的封装 print(command, path) if command == "openTerminal": if xutils.is_mac(): # TODO pass if xutils.is_windows(): os.popen("start; cd \"%s\"" % path) return "success" if path.endswith(".bat"): os.popen("start %s" % path) else: os.popen(path) # os.popen(command) return "success"
def GET(self): if xutils.is_windows(): raise web.seeother("/fs-D:/") else: raise web.seeother("/fs-/")
def list_file_objects(fpath): if xutils.is_windows() and fpath == "/": filenames = list_win_drives() else: filenames = list_abs_dir(fpath) return process_file_list(filenames)
def test_splitpath(self): if not xutils.is_windows(): path = "/root/test" pathlist = xutils.splitpath(path) self.assertEqual(2, len(pathlist))
def get_default_shell_ext(): if xutils.is_mac(): return ".command" elif xutils.is_windows(): return ".bat" return ".sh"