Python环境元数据载体类
| 97 | |
| 98 | |
| 99 | class PythonEnvironment: |
| 100 | """Python环境元数据载体类""" |
| 101 | _BT_PROJECT_ENV_SHELL = "/www/server/python_project/btpyprojectenv.sh" |
| 102 | _bt_etc_pyenv = "/www/server/panel/data/bt_etc_pyenv.sh" |
| 103 | project_to_pyenv_map_file = "/www/server/panel/data/python_project_name2env.txt" |
| 104 | default_pip_source = "https://mirrors.aliyun.com/pypi/simple/" |
| 105 | _ONLY_BINARY_NAMES = {"numpy", "scipy", "pandas", "cryptography", "pyOpenSSL", "Pillow", "opencv-python","psycopg2-binary", "opencv", "mysqlclient", "lxml", "pyarrow"} |
| 106 | _PYENV_FORMAT_DATA = """ |
| 107 | # Start-Python-Env 命令行环境设置 |
| 108 | if [ -f '/www/server/panel/data/bt_etc_pyenv.sh' ]; then source /www/server/panel/data/bt_etc_pyenv.sh; fi |
| 109 | # End-Python-Env |
| 110 | """ |
| 111 | |
| 112 | @staticmethod |
| 113 | def return_profile(): |
| 114 | if os.path.exists('/root/.bash_profile'): |
| 115 | return '/root/.bash_profile' |
| 116 | if os.path.exists('/root/.bashrc'): |
| 117 | return '/root/.bashrc' |
| 118 | fd = open('/root/.bashrc', mode="w", encoding="utf-8") |
| 119 | fd.close() |
| 120 | return '/root/.bashrc' |
| 121 | |
| 122 | # ptah python安装路径 |
| 123 | def __init__(self, bin_path: str, version: str, env_type: str, **kwargs): |
| 124 | self.bin_path = bin_path |
| 125 | self.version = version |
| 126 | self.env_type = env_type # conda venv virtualenv system |
| 127 | self.conda_path = kwargs.get("conda_path", "") |
| 128 | self.venv_name = kwargs.get("venv_name", "") |
| 129 | self.activate_sh = kwargs.get("activate_sh", "") |
| 130 | self.system_path = kwargs.get("system_path", "") |
| 131 | self.ps = kwargs.get("ps", "") |
| 132 | self.site_packages = kwargs.get("site_packages", "") # 应当支持site-packages 为空的情况 |
| 133 | self._pip_source = "" |
| 134 | if not os.path.isfile(self.project_to_pyenv_map_file): |
| 135 | public.writeFile(self.project_to_pyenv_map_file, "") |
| 136 | |
| 137 | def set_pip_source(self, pip_source: str): |
| 138 | self._pip_source = pip_source |
| 139 | |
| 140 | def to_dict(self, **kwargs) -> Dict: |
| 141 | data = { |
| 142 | "bin_path": self.bin_path, |
| 143 | "version": self.version, |
| 144 | "type": self.env_type, |
| 145 | "conda_path": self.conda_path, |
| 146 | "venv_name": self.venv_name, |
| 147 | "activate_sh": self.activate_sh, |
| 148 | "system_path" : self.system_path, |
| 149 | "ps": self.ps, |
| 150 | "site_packages": self.site_packages |
| 151 | } |
| 152 | for k, v in kwargs.items(): |
| 153 | if k in data: |
| 154 | continue |
| 155 | data[k] = v |
| 156 | return data |
no outgoing calls
no test coverage detected