|
| 1 | +import os |
| 2 | +import subprocess |
| 3 | +from pathlib import Path |
| 4 | +from typing import Union |
| 5 | + |
| 6 | + |
| 7 | +class FilePathValidator: |
| 8 | + """ |
| 9 | + 根据给定的路径验证文件的有效性 |
| 10 | + """ |
| 11 | + |
| 12 | + @classmethod |
| 13 | + def validate_script(cls, script_path: Union[str, Path]) -> bool: |
| 14 | + """ |
| 15 | + 验证脚本路径是否有效 \n |
| 16 | + :param script_path: 脚本路径 |
| 17 | + """ |
| 18 | + |
| 19 | + path = Path(script_path) |
| 20 | + # TODO 完善验证方法 |
| 21 | + if path.exists() and path.is_file(): |
| 22 | + return True |
| 23 | + else: |
| 24 | + return False |
| 25 | + |
| 26 | + @classmethod |
| 27 | + def validate_icon(cls, icon_path: Union[str, Path]) -> bool: |
| 28 | + """ |
| 29 | + 验证图标路径是否有效 \n |
| 30 | + :param icon_path: 图标路径 |
| 31 | + """ |
| 32 | + |
| 33 | + path = Path(icon_path) |
| 34 | + # TODO 完善验证方法 |
| 35 | + if path.exists() and path.is_file(): |
| 36 | + return True |
| 37 | + else: |
| 38 | + return False |
| 39 | + |
| 40 | + |
| 41 | +class InterpreterValidator: |
| 42 | + """ |
| 43 | + 验证给定的可执行文件是否为有效的Python解释器,并获取该解释器相关信息 |
| 44 | + """ |
| 45 | + |
| 46 | + def __init__(self, path: Union[str, os.PathLike[str]]) -> None: |
| 47 | + """ |
| 48 | + :param path: 可执行文件路径 |
| 49 | + """ |
| 50 | + |
| 51 | + self._itp_path: Path = Path(path) |
| 52 | + self._itp_validated: bool = False |
| 53 | + self.validate_itp() |
| 54 | + |
| 55 | + def validate_itp(self) -> bool: |
| 56 | + """ |
| 57 | + :return: 解释器是否有效 |
| 58 | + """ |
| 59 | + |
| 60 | + if ( |
| 61 | + self._itp_path.exists() # 该路径存在 |
| 62 | + and self._itp_path.is_file() # 为文件 |
| 63 | + and self._itp_path.name.startswith("python") # 文件名以python起始 |
| 64 | + ): |
| 65 | + try: |
| 66 | + # 尝试将该文件作为Python解释器运行 |
| 67 | + subprocess_args = [ |
| 68 | + str(self._itp_path.resolve()), |
| 69 | + "-c", |
| 70 | + "import sys", |
| 71 | + ] |
| 72 | + result = subprocess.run( |
| 73 | + args=subprocess_args, |
| 74 | + timeout=300, |
| 75 | + ) |
| 76 | + if result.returncode == 0: |
| 77 | + self._itp_validated = True |
| 78 | + return True |
| 79 | + else: |
| 80 | + self._itp_validated = False |
| 81 | + return False |
| 82 | + except OSError: |
| 83 | + self._itp_validated = False |
| 84 | + return False |
| 85 | + else: |
| 86 | + self._itp_validated = False |
| 87 | + return False |
| 88 | + |
| 89 | + def itp_info(self): |
| 90 | + """ |
| 91 | + 返回解释器的相关信息 \n |
| 92 | + """ |
| 93 | + |
| 94 | + # FIXME 完善此方法 |
| 95 | + if self._itp_validated: |
| 96 | + pass |
| 97 | + |
| 98 | + def module_installed(self, module: str) -> bool: |
| 99 | + """ |
| 100 | + 验证该解释器环境中是否已安装某个模块 \n |
| 101 | + :param module: 模块名,要求为import语句中使用的名称 |
| 102 | + :return: 未安装该模块或解释器无效时返回False |
| 103 | + """ |
| 104 | + |
| 105 | + if self._itp_validated: |
| 106 | + subprocess_arg_list = [ |
| 107 | + str(self._itp_path.resolve()), |
| 108 | + "-c", |
| 109 | + f"import {module}", |
| 110 | + ] |
| 111 | + result = subprocess.run( |
| 112 | + args=subprocess_arg_list, |
| 113 | + stdout=subprocess.PIPE, |
| 114 | + stderr=subprocess.PIPE, |
| 115 | + timeout=300, |
| 116 | + ) |
| 117 | + if result.returncode != 0 and b"ModuleNotFoundError" in result.stderr: |
| 118 | + return False |
| 119 | + else: |
| 120 | + return True |
| 121 | + else: |
| 122 | + return False |
| 123 | + |
| 124 | + @classmethod |
| 125 | + def validate(cls, path: Union[str, os.PathLike[str]]) -> bool: |
| 126 | + """ |
| 127 | + 验证path是否指向有效的Python解释器 \n |
| 128 | + :return: 是否有效 |
| 129 | + """ |
| 130 | + |
| 131 | + return InterpreterValidator(path).validate_itp() |
| 132 | + |
| 133 | + |
| 134 | +if __name__ == "__main__": |
| 135 | + # print(InterpreterValidator.validate("/usr/bin/python")) |
| 136 | + iv = InterpreterValidator("/usr/bin/python") |
| 137 | + print(iv.module_installed("PySide6")) |
| 138 | + print(iv.module_installed("black")) |
0 commit comments