Skip to content

Install packaging module if not found #91

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 17, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion custom_components/pyscript/requirements.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import os
import sys

from packaging.version import Version
from pkg_resources import Requirement

from homeassistant.loader import bind_hass
Expand Down Expand Up @@ -70,6 +69,10 @@ def process_all_requirements(pyscript_folder, requirements_paths, requirements_f

Returns files and a list of packages, if any, that need to be installed.
"""

# Re-import Version to avoid dealing with multiple flake and pylint errors
from packaging.version import Version # pylint: disable=import-outside-toplevel

all_requirements_to_process = {}
for root in requirements_paths:
for requirements_path in glob.glob(os.path.join(pyscript_folder, root, requirements_file)):
Expand Down Expand Up @@ -203,6 +206,14 @@ async def install_requirements(hass, config_entry, pyscript_folder):

pyscript_installed_packages = config_entry.data.get(CONF_INSTALLED_PACKAGES, {}).copy()

# Import packaging inside install_requirements so that we can use Home Assistant to install it
# if it can't been found
try:
from packaging.version import Version # pylint: disable=import-outside-toplevel
except ModuleNotFoundError:
await async_process_requirements(hass, DOMAIN, ["packaging"])
from packaging.version import Version # pylint: disable=import-outside-toplevel

all_requirements = await hass.async_add_executor_job(
process_all_requirements, pyscript_folder, REQUIREMENTS_PATHS, REQUIREMENTS_FILE
)
Expand Down