系统Python检测器
| 906 | |
| 907 | |
| 908 | class SystemPythonDetector(_EnvironmentDetector): |
| 909 | """系统Python检测器""" |
| 910 | |
| 911 | def __init__(self, search_dirs: List[str]): |
| 912 | self.search_dirs = search_dirs |
| 913 | self.python_bin_map = {} |
| 914 | self.regexp_name = re.compile("^(python|pypy)([23](\.\d+)?)?$") |
| 915 | |
| 916 | @staticmethod |
| 917 | def get_conda_path() -> List[str]: |
| 918 | conda_path = [] |
| 919 | for i in CondaDetector().find_conda_executable(): |
| 920 | base_path = i.split("/bin/")[0] |
| 921 | envs_path = base_path + "/envs" |
| 922 | conda_path.extend([envs_path, base_path]) |
| 923 | return conda_path |
| 924 | |
| 925 | def is_conda_python(self, python_bin: str) -> bool: |
| 926 | if hasattr(self, "_conda_path"): |
| 927 | _conda_path = self._conda_path |
| 928 | else: |
| 929 | _conda_path = self.get_conda_path() |
| 930 | setattr(self, "_conda_path", _conda_path) |
| 931 | for i in _conda_path: |
| 932 | if python_bin.startswith(i): |
| 933 | return True |
| 934 | return False |
| 935 | |
| 936 | def detect(self) -> List[PythonEnvironment]: |
| 937 | # 新增:从系统PATH查找 |
| 938 | self.python_bin_map = {} |
| 939 | for dir_path in self.now_env_path_list(): |
| 940 | p = Path(dir_path) |
| 941 | if not p.is_dir(): |
| 942 | continue |
| 943 | |
| 944 | self.detect_by_path(p) |
| 945 | |
| 946 | max_depth = 2 |
| 947 | for base_dir in self.search_dirs: |
| 948 | expanded_dir = os.path.expanduser(base_dir) |
| 949 | if not os.path.exists(expanded_dir): |
| 950 | continue |
| 951 | |
| 952 | for root, dirs, _ in os.walk(expanded_dir, topdown=True): |
| 953 | # 避免过多搜索子目录 |
| 954 | if root.replace(expanded_dir, "").count(os.sep) >= max_depth: |
| 955 | dirs.clear() |
| 956 | continue |
| 957 | |
| 958 | for tmp_dir in dirs: |
| 959 | self.detect_by_path(Path(root) / tmp_dir) |
| 960 | |
| 961 | res_list = [] |
| 962 | for python_bin in self.python_bin_map.values(): |
| 963 | if self.is_conda_python(str(python_bin)): |
| 964 | continue |
| 965 | version = self.get_py_version(str(python_bin)) |