通过包名查询在site-packages的*-info/RECORD可执行文件的位置 (可用于 pip, gunicorn, uwsgi)
(self, pkg_name: str)
| 243 | return error_msg |
| 244 | |
| 245 | def _site_pkg_name2bin(self, pkg_name: str) -> Optional[str]: |
| 246 | """通过包名查询在site-packages的*-info/RECORD可执行文件的位置 (可用于 pip, gunicorn, uwsgi)""" |
| 247 | record_file = "" |
| 248 | name_lower = pkg_name.lower() |
| 249 | if not self.site_packages or not os.path.exists(self.site_packages): |
| 250 | self.site_packages = _EnvironmentDetector.get_site_packages(self.bin_path) |
| 251 | if not self.site_packages or not os.path.exists(self.site_packages): |
| 252 | return None |
| 253 | for i in os.listdir(self.site_packages): |
| 254 | i_lower = i.lower() |
| 255 | if i.endswith(".dist-info") and i_lower.startswith(name_lower + "-"): |
| 256 | record_file = os.path.join(self.site_packages, i, "RECORD") |
| 257 | break |
| 258 | if not record_file: |
| 259 | return None |
| 260 | with open(record_file, "r") as f: |
| 261 | for line in f: |
| 262 | tmp = line.split(",") |
| 263 | if len(tmp) != 3: |
| 264 | continue |
| 265 | # 可执行文件的位置不会site-packages在文件夹下 |
| 266 | if name_lower in os.path.basename(tmp[0].lower()) and tmp[0].startswith("../"): |
| 267 | bin_p = Path(self.site_packages) / tmp[0] |
| 268 | if bin_p.exists(): |
| 269 | return str(bin_p.resolve()) |
| 270 | return None |
| 271 | |
| 272 | def uwsgi_bin(self) -> Optional[str]: |
| 273 | return self._site_pkg_name2bin("uwsgi") |