Skip to content

Commit fed7cfd

Browse files
committed
support requirements.txt and apps and modules directories
1 parent 8587150 commit fed7cfd

File tree

2 files changed

+49
-52
lines changed

2 files changed

+49
-52
lines changed

custom_components/pyscript/__init__.py

Lines changed: 46 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -269,53 +269,52 @@ async def unload_scripts(global_ctx_only=None, unload_all=False):
269269
@bind_hass
270270
async def install_requirements(hass):
271271
"""Install missing requirements from requirements.txt."""
272-
requirements_path = os.path.join(hass.config.path(FOLDER), "requirements.txt")
273-
274-
if os.path.exists(requirements_path):
275-
with open(requirements_path, "r") as requirements_file:
276-
requirements_to_install = []
277-
for pkg in requirements_file.readlines():
278-
# Remove inline comments which are accepted by pip but not by Home
279-
# Assistant's installation method.
280-
# https://rosettacode.org/wiki/Strip_comments_from_a_string#Python
281-
i = pkg.find("#")
282-
if i >= 0:
283-
pkg = pkg[:i]
284-
pkg = pkg.strip()
285-
286-
try:
287-
# Attempt to get version of package. Do nothing if it's found since
288-
# we want to use the version that's already installed to be safe
289-
requirement = pkg_resources.Requirement.parse(pkg)
290-
requirement_installed_version = installed_version(requirement.project_name)
291-
292-
if requirement_installed_version in requirement:
293-
_LOGGER.debug("`%s` already found", requirement.project_name)
294-
else:
295-
_LOGGER.warning(
296-
(
297-
"`%s` already found but found version `%s` does not"
298-
" match requirement. Keeping found version."
299-
),
300-
requirement.project_name,
301-
requirement_installed_version,
302-
)
303-
except PackageNotFoundError:
304-
# Since package wasn't found, add it to installation list
305-
_LOGGER.debug("%s not found, adding it to package installation list", pkg)
306-
requirements_to_install.append(pkg)
307-
except ValueError:
308-
# Not valid requirements line so it can be skipped
309-
_LOGGER.debug("Ignoring `%s` because it is not a valid package", pkg)
310-
if requirements_to_install:
311-
_LOGGER.info(
312-
"Installing the following packages from %s: %s",
313-
requirements_path,
314-
", ".join(requirements_to_install),
315-
)
316-
await async_process_requirements(hass, DOMAIN, requirements_to_install)
317-
else:
318-
_LOGGER.debug("All packages in %s are already available", requirements_path)
272+
for root in ("", "apps/*", "modules/*"):
273+
for requirements_path in glob.glob(os.path.join(hass.config.path(FOLDER), root, "requirements.txt")):
274+
with open(requirements_path, "r") as requirements_file:
275+
requirements_to_install = []
276+
for pkg in requirements_file.readlines():
277+
# Remove inline comments which are accepted by pip but not by Home
278+
# Assistant's installation method.
279+
# https://rosettacode.org/wiki/Strip_comments_from_a_string#Python
280+
i = pkg.find("#")
281+
if i >= 0:
282+
pkg = pkg[:i]
283+
pkg = pkg.strip()
284+
285+
try:
286+
# Attempt to get version of package. Do nothing if it's found since
287+
# we want to use the version that's already installed to be safe
288+
requirement = pkg_resources.Requirement.parse(pkg)
289+
requirement_installed_version = installed_version(requirement.project_name)
290+
291+
if requirement_installed_version in requirement:
292+
_LOGGER.debug("`%s` already found", requirement.project_name)
293+
else:
294+
_LOGGER.warning(
295+
(
296+
"`%s` already found but found version `%s` does not"
297+
" match requirement. Keeping found version."
298+
),
299+
requirement.project_name,
300+
requirement_installed_version,
301+
)
302+
except PackageNotFoundError:
303+
# Since package wasn't found, add it to installation list
304+
_LOGGER.debug("%s not found, adding it to package installation list", pkg)
305+
requirements_to_install.append(pkg)
306+
except ValueError:
307+
# Not valid requirements line so it can be skipped
308+
_LOGGER.debug("Ignoring `%s` because it is not a valid package", pkg)
309+
if requirements_to_install:
310+
_LOGGER.info(
311+
"Installing the following packages from %s: %s",
312+
requirements_path,
313+
", ".join(requirements_to_install),
314+
)
315+
await async_process_requirements(hass, DOMAIN, requirements_to_install)
316+
else:
317+
_LOGGER.debug("All packages in %s are already available", requirements_path)
319318

320319

321320
@bind_hass

tests/test_init.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -493,11 +493,9 @@ async def test_install_requirements(hass):
493493
494494
"""
495495

496-
with patch("os.path.exists", return_value=True), patch(
497-
"custom_components.pyscript.async_hass_config_yaml", return_value={}
498-
), patch("custom_components.pyscript.open", mock_open(read_data=requirements), create=True,), patch(
499-
"custom_components.pyscript.async_process_requirements"
500-
) as install_requirements:
496+
with patch("custom_components.pyscript.async_hass_config_yaml", return_value={}), patch(
497+
"custom_components.pyscript.open", mock_open(read_data=requirements), create=True,
498+
), patch("custom_components.pyscript.async_process_requirements") as install_requirements:
501499
await setup_script(hass, None, dt(2020, 7, 1, 11, 59, 59, 999999), "")
502500
assert install_requirements.call_args[0][2] == ["pytube==9.7.0", "pykakasi==2.0.1"]
503501
install_requirements.reset_mock()

0 commit comments

Comments
 (0)