-
-
Notifications
You must be signed in to change notification settings - Fork 37.4k
Extract requirements #12051
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
Extract requirements #12051
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| """Module to handle installing requirements.""" | ||
| import asyncio | ||
| from functools import partial | ||
| import logging | ||
| import os | ||
|
|
||
| import homeassistant.util.package as pkg_util | ||
|
|
||
| DATA_PIP_LOCK = 'pip_lock' | ||
| CONSTRAINT_FILE = 'package_constraints.txt' | ||
| _LOGGER = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| @asyncio.coroutine | ||
| def async_process_requirements(hass, name, requirements): | ||
| """Install the requirements for a component or platform. | ||
|
|
||
| This method is a coroutine. | ||
| """ | ||
| pip_lock = hass.data.get(DATA_PIP_LOCK) | ||
| if pip_lock is None: | ||
| pip_lock = hass.data[DATA_PIP_LOCK] = asyncio.Lock(loop=hass.loop) | ||
|
|
||
| pip_install = partial(pkg_util.install_package, | ||
| **pip_kwargs(hass.config.config_dir)) | ||
|
|
||
| with (yield from pip_lock): | ||
| for req in requirements: | ||
| ret = yield from hass.async_add_job(pip_install, req) | ||
| if not ret: | ||
| _LOGGER.error("Not initializing %s because could not install " | ||
| "requirement %s", name, req) | ||
| return False | ||
|
|
||
| return True | ||
|
|
||
|
|
||
| def pip_kwargs(config_dir): | ||
| """Return keyword arguments for PIP install.""" | ||
| kwargs = { | ||
| 'constraints': os.path.join(os.path.dirname(__file__), CONSTRAINT_FILE) | ||
| } | ||
| if not pkg_util.running_under_virtualenv(): | ||
| kwargs['target'] = os.path.join(config_dir, 'deps') | ||
| return kwargs | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| """Test requirements module.""" | ||
| import os | ||
| from unittest import mock | ||
|
|
||
| from homeassistant import loader, setup | ||
| from homeassistant.requirements import CONSTRAINT_FILE | ||
|
|
||
| from tests.common import get_test_home_assistant, MockModule | ||
|
|
||
|
|
||
| class TestRequirements: | ||
| """Test the requirements module.""" | ||
|
|
||
| hass = None | ||
| backup_cache = None | ||
|
|
||
| # pylint: disable=invalid-name, no-self-use | ||
| def setup_method(self, method): | ||
| """Setup the test.""" | ||
| self.hass = get_test_home_assistant() | ||
|
|
||
| def teardown_method(self, method): | ||
| """Clean up.""" | ||
| self.hass.stop() | ||
|
|
||
| @mock.patch('os.path.dirname') | ||
| @mock.patch('homeassistant.util.package.running_under_virtualenv', | ||
| return_value=True) | ||
| @mock.patch('homeassistant.util.package.install_package', | ||
| return_value=True) | ||
| def test_requirement_installed_in_venv( | ||
| self, mock_install, mock_venv, mock_dirname): | ||
| """Test requirement installed in virtual environment.""" | ||
| mock_venv.return_value = True | ||
| mock_dirname.return_value = 'ha_package_path' | ||
| self.hass.config.skip_pip = False | ||
| loader.set_component( | ||
| 'comp', MockModule('comp', requirements=['package==0.0.1'])) | ||
| assert setup.setup_component(self.hass, 'comp') | ||
| assert 'comp' in self.hass.config.components | ||
| assert mock_install.call_args == mock.call( | ||
| 'package==0.0.1', | ||
| constraints=os.path.join('ha_package_path', CONSTRAINT_FILE)) | ||
|
|
||
| @mock.patch('os.path.dirname') | ||
| @mock.patch('homeassistant.util.package.running_under_virtualenv', | ||
| return_value=False) | ||
| @mock.patch('homeassistant.util.package.install_package', | ||
| return_value=True) | ||
| def test_requirement_installed_in_deps( | ||
| self, mock_install, mock_venv, mock_dirname): | ||
| """Test requirement installed in deps directory.""" | ||
| mock_dirname.return_value = 'ha_package_path' | ||
| self.hass.config.skip_pip = False | ||
| loader.set_component( | ||
| 'comp', MockModule('comp', requirements=['package==0.0.1'])) | ||
| assert setup.setup_component(self.hass, 'comp') | ||
| assert 'comp' in self.hass.config.components | ||
| assert mock_install.call_args == mock.call( | ||
| 'package==0.0.1', target=self.hass.config.path('deps'), | ||
| constraints=os.path.join('ha_package_path', CONSTRAINT_FILE)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is changing the target path. Target should be the library folder.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nevermind, I'm wrong and remembered incorrectly.