|
| 1 | +"""Workaround for the Yandex.Cloud SDK dependencies BS 💩.""" |
| 2 | + |
| 3 | +# This Source Code Form is subject to the terms of the Mozilla Public |
| 4 | +# License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 | +# file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 6 | + |
| 7 | +from homeassistant.util import package as pkg_util |
| 8 | +from .const import LOGGER |
| 9 | + |
| 10 | +# Details: |
| 11 | +# https://github.com/black-roland/homeassistant-yandexgpt/issues/32 |
| 12 | +# |
| 13 | +# The history of "we don't give a 💩": |
| 14 | +# https://github.com/yandex-cloud/python-sdk/issues?q=is%3Aissue%20dependencies%20OR%20requirements%20OR%20constraints |
| 15 | +REQUIRED_PACKAGES = [ |
| 16 | + "yandex-cloud-ml-sdk==0.13.1", |
| 17 | + "yandexcloud==0.353.0" |
| 18 | +] |
| 19 | + |
| 20 | + |
| 21 | +def is_latest_sdk_installed() -> bool: |
| 22 | + """Check if the required versions of SDKs are installed.""" |
| 23 | + try: |
| 24 | + for package in REQUIRED_PACKAGES: |
| 25 | + if not pkg_util.is_installed(package): |
| 26 | + LOGGER.debug("Package %s is not installed or not in the required version.", package) |
| 27 | + return False |
| 28 | + |
| 29 | + LOGGER.debug("All required Yandex Cloud SDK packages are installed in correct versions.") |
| 30 | + return True |
| 31 | + except Exception as e: |
| 32 | + LOGGER.warning("Error checking SDK package installation status: %s", e) |
| 33 | + return False |
| 34 | + |
| 35 | + |
| 36 | +def upgrade_sdk() -> bool: |
| 37 | + """Use pkg_util to install the required versions of the SDKs.""" |
| 38 | + |
| 39 | + # Empty args to avoid setting any package constraints |
| 40 | + _pip_kwargs = {} |
| 41 | + |
| 42 | + success = True |
| 43 | + for package in REQUIRED_PACKAGES: |
| 44 | + try: |
| 45 | + LOGGER.info("Attempting to install/upgrade Yandex Cloud SDK package: %s", package) |
| 46 | + |
| 47 | + install_success = pkg_util.install_package(package, **_pip_kwargs) |
| 48 | + if not install_success: |
| 49 | + LOGGER.error("Failed to install/upgrade Yandex Cloud SDK package: %s", package) |
| 50 | + success = False |
| 51 | + continue |
| 52 | + |
| 53 | + LOGGER.info("Successfully installed/upgraded Yandex Cloud SDK package: %s", package) |
| 54 | + except Exception as e: |
| 55 | + LOGGER.exception("Exception occurred while installing/upgrading package %s: %s", package, e) |
| 56 | + success = False |
| 57 | + return success |
0 commit comments