环境报告生成器
| 1020 | |
| 1021 | |
| 1022 | class EnvironmentReporter: |
| 1023 | """环境报告生成器""" |
| 1024 | REPORT_FILE = "/www/server/panel/data/python_project_env.json" |
| 1025 | |
| 1026 | def __init__(self): |
| 1027 | _python_manager_path = python_manager_path() |
| 1028 | _pyenv_path = pyenv_path() |
| 1029 | self.detectors = [ |
| 1030 | CondaDetector(), |
| 1031 | VirtualEnvDetector([ |
| 1032 | _pyenv_path, |
| 1033 | _python_manager_path, |
| 1034 | ]), |
| 1035 | SystemPythonDetector([ |
| 1036 | _pyenv_path, |
| 1037 | "{}/versions".format(_pyenv_path), |
| 1038 | "{}/pypy_versions".format(_pyenv_path), |
| 1039 | "{}/versions".format(_python_manager_path), |
| 1040 | ]) |
| 1041 | ] |
| 1042 | |
| 1043 | def generate_report(self) -> Dict: |
| 1044 | report = {"environments": [], "update_time": int(time.time())} |
| 1045 | for detector in self.detectors: |
| 1046 | try: |
| 1047 | envs = detector.detect() |
| 1048 | report["environments"].extend([e.to_dict() for e in envs]) |
| 1049 | except Exception as e: |
| 1050 | print("环境检测失败:{}".format(str(e))) |
| 1051 | traceback.print_exc() |
| 1052 | continue |
| 1053 | return report |
| 1054 | |
| 1055 | @classmethod |
| 1056 | def update_report(cls, *now_report: Dict) -> Optional[str]: |
| 1057 | try: |
| 1058 | with open(cls.REPORT_FILE, "r") as fs: |
| 1059 | old_data = json.loads(fs.read()) |
| 1060 | except: |
| 1061 | old_data = {"environments": [], "update_time": int(time.time())} |
| 1062 | |
| 1063 | path_map = OrderedDict() |
| 1064 | for e in old_data["environments"]: |
| 1065 | path_map[e["bin_path"]] = e |
| 1066 | |
| 1067 | add_list = [] |
| 1068 | for e in now_report: |
| 1069 | if e["bin_path"] in path_map: |
| 1070 | tmp_ps = path_map[e["bin_path"]]["ps"] |
| 1071 | path_map[e["bin_path"]].update(e) |
| 1072 | if tmp_ps and not path_map[e["bin_path"]]["ps"]: |
| 1073 | path_map[e["bin_path"]]["ps"] = tmp_ps |
| 1074 | else: |
| 1075 | path_map[e["bin_path"]] = e |
| 1076 | add_list.append(e) |
| 1077 | |
| 1078 | now_data = {"environments": add_list + old_data["environments"], "update_time": int(time.time())} |
| 1079 | try: |
no outgoing calls
no test coverage detected