| 813 | |
| 814 | @classmethod |
| 815 | def get_env_info(cls, path: str) -> Optional[Tuple[str, str, str]]: |
| 816 | cfg_file = Path(path) / "pyvenv.cfg" |
| 817 | atv_file = Path(path) / "bin" / "activate" |
| 818 | if not cfg_file.exists(): |
| 819 | return None |
| 820 | |
| 821 | if not atv_file.exists(): |
| 822 | return None |
| 823 | |
| 824 | version_list = [] |
| 825 | name = "" |
| 826 | try: |
| 827 | with open(cfg_file, "r") as fp: |
| 828 | for line in fp: |
| 829 | if line.startswith("home ="): |
| 830 | # home_dir = line.split("=")[1].strip() # 改为从虚拟环境的bin目录中获取 |
| 831 | continue |
| 832 | elif line.startswith("implementation ="): |
| 833 | version_list.insert(0, line.split("=")[1].strip()) |
| 834 | elif line.startswith("version_info ="): |
| 835 | v = line.split("=")[1].strip() |
| 836 | if v.count(".") > 2: |
| 837 | v = ".".join(v.split(".")[:3]) |
| 838 | version_list.append(v) |
| 839 | elif line.startswith("version ="): |
| 840 | version_list.append(line.split("=")[1].strip()) |
| 841 | with open(atv_file, "r") as fp: |
| 842 | atv_data = fp.read() |
| 843 | for regexp in cls.NAME_REGEXPS: |
| 844 | res = regexp.search(atv_data) |
| 845 | if res: |
| 846 | name = res.group("name").strip().strip('"\'') |
| 847 | break |
| 848 | except Exception as e: |
| 849 | print("获取虚拟环境信息失败:{}".format(str(e))) |
| 850 | return None |
| 851 | |
| 852 | if not version_list: |
| 853 | return None |
| 854 | |
| 855 | bin_path = "{}/bin/python".format(path) |
| 856 | if not os.path.exists(bin_path) or not os.access(bin_path, os.X_OK): |
| 857 | bin_path = "{}/bin/python3".format(path) |
| 858 | if not os.path.exists(bin_path) or not os.access(bin_path, os.X_OK): |
| 859 | return None |
| 860 | |
| 861 | real_bin = os.path.realpath(bin_path) |
| 862 | if len(version_list) == 1: |
| 863 | if os.path.basename(real_bin).startswith("pypy"): |
| 864 | version_list.insert(0, "Python") |
| 865 | version_list.append("(PyPy)") |
| 866 | else: |
| 867 | version_list.insert(0, "Python") |
| 868 | |
| 869 | version = " ".join(version_list) |
| 870 | if not name: |
| 871 | name = os.path.basename(path) |
| 872 | |