| 1107 | |
| 1108 | |
| 1109 | class EnvironmentManager: |
| 1110 | _REPORT_FILE = "/www/server/panel/data/python_project_env.json" |
| 1111 | |
| 1112 | def __init__(self): |
| 1113 | self._all_env = None |
| 1114 | |
| 1115 | @property |
| 1116 | def all_env(self) -> List[PythonEnvironment]: |
| 1117 | venv_src_map = {} |
| 1118 | if self._all_env is None: |
| 1119 | self._all_env = [] |
| 1120 | for e in self.load_report()["environments"]: |
| 1121 | if not os.path.isfile(e["bin_path"]): # 文件已不存在的,就不在再展示 |
| 1122 | continue |
| 1123 | tmp_p = PythonEnvironment.from_dict(e) |
| 1124 | self._all_env.append(tmp_p) |
| 1125 | if tmp_p.env_type == "venv": |
| 1126 | venv_src_map[tmp_p.system_path] = tmp_p |
| 1127 | |
| 1128 | for tmp_p in self._all_env: |
| 1129 | if tmp_p.env_type == "system" and tmp_p.bin_path in venv_src_map: |
| 1130 | venv_src_map.pop(tmp_p.bin_path) |
| 1131 | |
| 1132 | for tmp_p in venv_src_map.values(): |
| 1133 | self._all_env.remove(tmp_p) |
| 1134 | |
| 1135 | return self._all_env |
| 1136 | |
| 1137 | def get_env_py_path(self, python_path: str) -> Optional[PythonEnvironment]: |
| 1138 | python_path = python_path.rstrip("/") |
| 1139 | for tmp_p in self.all_env: |
| 1140 | if tmp_p.bin_path == python_path: |
| 1141 | return tmp_p |
| 1142 | # 兼容旧版本 |
| 1143 | if python_path.startswith("/www/server/pyporject_evn/"): |
| 1144 | for tmp_p in self.all_env: |
| 1145 | if tmp_p.bin_path.startswith(python_path) and tmp_p.env_type == "system": |
| 1146 | return tmp_p |
| 1147 | return None |
| 1148 | |
| 1149 | @staticmethod |
| 1150 | def load_report() -> Dict: |
| 1151 | with open(EnvironmentReporter.REPORT_FILE, "r") as fs: |
| 1152 | return json.loads(fs.read()) |
| 1153 | |
| 1154 | @staticmethod |
| 1155 | def add_python_env(add_type: str, path: str) -> Optional[str]: |
| 1156 | p = Path(path) |
| 1157 | if not p.exists(): |
| 1158 | return "Python环境不存在" |
| 1159 | if path.find("conda") and add_type == "system": |
| 1160 | tmp_path = path.rsplit("/bin")[0] |
| 1161 | if os.path.isfile(tmp_path + "/condabin/conda"): |
| 1162 | add_type = "conda" |
| 1163 | path = tmp_path + "/condabin/conda" |
| 1164 | elif os.path.isfile(tmp_path + "/../../condabin/conda"): |
| 1165 | add_type = "conda" |
| 1166 | path = os.path.realpath(tmp_path + "/../../condabin/conda") |
no outgoing calls
no test coverage detected