马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
代码如下 求大佬帮忙看看from json import load
from os import listdir, path
from subprocess import run
from sys import argv
from typing import List, Tuple
emt = Tuple[str, str, str, str, str]
def func(root_dir: str) -> List[emt]:
data = []
proc_list = [_ for _ in listdir(path=root_dir) if path.isdir(path.join(root_dir, _))]
def get_proc_type(proc_path):
with open(file=proc_path) as f:
proc_type = load(f)["Processes"][0]["Configs"][0]["ProcType"][0]["name"]
return proc_type
def get_pid(proc):
result = run(args=f"pidof {proc}", capture_output=True, shell=True, text=True)
return result.stdout.strip() if result.returncode == 0 else "Null"
for row_prefix, proc in enumerate(iterable=proc_list, start=1):
proc_dir = path.join(root_dir, proc)
if path.exists(path=(path_json := path.join(proc_dir, "etc", "exec_config.json"))):
pid = get_pid(proc=proc)
proc_type = get_proc_type(proc_path=path_json)
proc_status = "Running" if pid != "Null" else "Failed"
if path.exists(f"{proc_dir}/bin"):
data.append([str(row_prefix), proc, f"{proc_status:<12}", pid, proc_type])
elif path.exists(f"{proc_dir}/bin_disable"):
data.append([str(row_prefix), proc, "Disable", "", proc_type])
return data
def tabulated_printing(headers: emt, data: List[emt]) -> None:
format_tabulation = []
max_length = lambda cell: len(cell.encode("gb2312"))
col_widths = [max(max_length(str(cell)) for cell in column) for column in zip(headers, *data)]
def make_line_separator() -> str:
return f"+{'+'.join('-' * (col_width + 2) for col_width in col_widths)}+"
def format_row(row: List[str], color: str = "") -> str:
formatted_row = " | ".join(
f"{item:<{col_width - max_length(item) + len(item)}}"
for item, col_width in zip(row, col_widths)
)
return f"| \033[{color}m{formatted_row}\033[0m |" if color else f"| {formatted_row} |"
format_tabulation.extend([make_line_separator(), format_row(row=headers), make_line_separator()])
format_tabulation.extend([
format_row(row=row, color="9" if row[-2] == "Null" else "") for row in data
])
format_tabulation.append(make_line_separator())
print("\n".join(format_tabulation))
if __name__ == '__main__':
root_dir = None
try:
root_dir = argv[1]
except Exception as err:
print(f"input dir error:{err}")
headers = ["No.", "Proc", "Status", "Pid", "Type"]
tabulated_printing(headers=headers, data=func(root_dir=root_dir))
|